Class: Toolshed::Github

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/toolshed/github.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Github

Returns a new instance of Github.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/toolshed/github.rb', line 5

def initialize(options={})
  username = Toolshed::Client::github_username
  password = Toolshed::Client::github_password

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

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

  @auth = { username: username, password: password }
  @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

Class Method Details

.passwordObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/toolshed/github.rb', line 76

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



65
66
67
68
69
70
71
72
73
74
# File 'lib/toolshed/github.rb', line 65

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

#branch_nameObject



56
57
58
59
# File 'lib/toolshed/github.rb', line 56

def branch_name
  # branch information
  branch_name = `git rev-parse --abbrev-ref HEAD`.strip
end

#branched_fromObject



61
62
63
# File 'lib/toolshed/github.rb', line 61

def branched_from
  branched_from = `git rev-parse --abbrev-ref --symbolic-full-name @{u}`.split('/')[-1].strip
end

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/toolshed/github.rb', line 26

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

  response = HTTParty.post("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.branched_from_user}/#{Toolshed::Client.branched_from_repo_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



47
48
49
50
51
52
53
54
# File 'lib/toolshed/github.rb', line 47

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

  response = HTTParty.get("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.github_username}/toolshed/branches", options).response
  JSON.parse(response.body).each do |branch|
    puts branch.inspect
  end
end