Class: Toolshed::Git::Github

Inherits:
Base
  • Object
show all
Extended by:
Toolshed::Git
Includes:
HTTParty
Defined in:
lib/toolshed/git/github.rb

Constant Summary

Constants included from Toolshed::Git

DEFAULT_BRANCH_FROM, DEFAULT_GIT_TOOL

Instance Attribute Summary collapse

Attributes inherited from Base

#from_remote_branch_name, #from_remote_name, #to_remote_branch_name, #to_remote_name, #validator

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Toolshed::Git

branch_name, branch_name_from_id, branched_from, checkout, clean_branch_name, delete, git_submodule_command, push

Methods inherited from Base

#create_branch

Constructor Details

#initialize(options = {}) ⇒ Github

Returns a new instance of Github.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/toolshed/git/github.rb', line 9

def initialize(options={})
  super(options)

  username  = Toolshed::Client::github_username
  password  = Toolshed::Client::github_password
  token     = Toolshed::Client::github_token

  unless (options[:username].nil?)
    username = options[:username]
  end

  unless (options[:password].nil?)
     password = options[:password]
  end

  unless (token.nil?)
    username = token
    password = nil
  end

  unless (options[:token].nil?)
    username = options[:token]
    password = nil
  end

  @auth = { username: username, password: password }
  self.default_options = {
    :headers => {
        "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17",
    },
    basic_auth: @auth,
  }
end

Instance Attribute Details

#default_optionsObject

Returns the value of attribute default_options.



7
8
9
# File 'lib/toolshed/git/github.rb', line 7

def default_options
  @default_options
end

Class Method Details

.create_instanceObject



94
95
96
# File 'lib/toolshed/git/github.rb', line 94

def self.create_instance
  Toolshed::Git::Github.new({ username: Toolshed::Git::Github.username, password: Toolshed::Git::Github.password })
end

.passwordObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/toolshed/git/github.rb', line 81

def self.password
  password = Toolshed::Client::github_password
  if (password.nil?)
    # prompt to ask for password
    system "stty -echo"
    puts "Github password? "
    password = $stdin.gets.chomp.strip
    system "stty echo"
  end

  return password
end

.usernameObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/toolshed/git/github.rb', line 70

def self.username
  username = Toolshed::Client::github_username
  if (username.nil?)
    # prompt to ask for username
    puts "Github username? "
    username = $stdin.gets.chomp.strip
  end

  return username
end

Instance Method Details

#create_pull_request(title, body, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/toolshed/git/github.rb', line 43

def create_pull_request(title, body, options={})
  options.merge!(self.default_options)
  options.merge!({
    body: {
      title: title,
       body: body,
       head: "#{Toolshed::Client.github_username}:#{Toolshed::Git::Base.branch_name}",
       base: Toolshed::Git::Base.branched_from
    }.to_json
  })

  response = HTTParty.post("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.pull_from_repository_user}/#{Toolshed::Client.pull_from_repository_name}/pulls", options).response
  response = JSON.parse(response.body)
  if (response["errors"].nil?)
    response
  else
    raise "validation errors #{response.inspect}"
  end
end

#list_branches(options = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/toolshed/git/github.rb', line 63

def list_branches(options={})
  options.merge!(self.default_options)

  response = HTTParty.get("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.github_username}/#{Toolshed::Client.pull_from_repository_name}/branches", options).response
  response = JSON.parse(response.body)
end