Module: Github

Defined in:
lib/github.rb

Constant Summary collapse

@@pulls =
nil

Class Method Summary collapse

Class Method Details

.api(authorization_info = {}) ⇒ Object

Get an instance of the Octokit API class

Authorization info is the structure from here: developer.github.com/v3/oauth/#create-a-new-authorization

something like this:

:scopes => ['repo'],
:note => "git-scripts command line interface",
:note_url => "https://github.com/ifixit/git-scripts"



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/github.rb', line 31

def self.api(authorization_info = {})
   # Let Octokit handle pagination automagically for us.
   Octokit.auto_paginate = true
   # Defaults
   authorization_info = {
      :scopes => ['repo'],
      :note => "ifixit git-scripts command line interface",
      :note_url => "https://github.com/ifixit/git-scripts"
   }.merge(authorization_info)
   OctokitWrapper.new(self::get_authentication(authorization_info))
end

.config(property) ⇒ Object

Get a git config property from the first place that defines it. The ‘git config` command takes properties, by default, from the repository, the user’s config, and the system config, in that order. We used to specify --global here, but that means to read only from the global config, and it also prohibits processing includes by default.



14
15
16
# File 'lib/github.rb', line 14

def self.config(property)
   `git config #{property.to_s.shellescape}`.strip
end

.get_authentication(authorization_info) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/github.rb', line 43

def self.get_authentication(authorization_info)
   username = self::config("github.user")
   token    = self::config("github.token")
   if !username.empty? && !token.empty?
      return {:login => username, :access_token => token}
   else
      return self::request_authorization(authorization_info)
   end
end

.get_commit_status_warning(status) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/github.rb', line 215

def self.get_commit_status_warning(status)
   warning = 'Merge with caution.'
   case status
   when 'failure'
      return 'This pull request has failed to pass continuous integration' +
       " tests. #{warning}"
   when 'pending'
      return "Continuous integration tests have not finished. #{warning}"
   when 'error'
      return "Build tests were not able to complete. #{warning}"
   when 'none'
      return 'Continuous integration has not been set up.'
   when nil
      return 'No pull request found for this branch.'
   else
      return ''
   end
end

.get_github_repoObject

Returns the github repo identifier in the form that the API likes: “someuser/theirrepo”

Requires the “origin” remote to be set to a github url



92
93
94
95
96
97
98
99
100
# File 'lib/github.rb', line 92

def self.get_github_repo()
   url = self::config("remote.origin.url")
   m = /github\.com.(.*?)\/(.*)/.match(url)
   if m
     return [m[1], m[2].sub(/\.git\Z/, "")].join("/")
   else
      raise "remote.origin.url in git config but be a github url"
   end
end

.get_most_recent_commit_status(repo, sha) ⇒ Object

Returns the most recent github commit status for a given commit



125
126
127
# File 'lib/github.rb', line 125

def self.get_most_recent_commit_status(repo, sha)
   api.statuses(repo, sha).sort_by {|status| status['id'] }.last
end

.get_pull_request_description(branch_name = nil) ⇒ Object

Prompts the user (using $EDITOR) to provide a title and body for this pull-request

Returns a hash containing a :title and :body



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/github.rb', line 108

def self.get_pull_request_description(branch_name = nil)
   if branch_name
      initial_message = Git::commit_message(branch_name).gsub("\r","")
   else
      initial_message = "Title of pull-request\n#  Second line is ignored (do no edit)\nBody of pull-request\n      MESSAGE\n   end\n\n   return self::open_title_body_editor(initial_message)\nend\n"

.get_pull_request_info_from_api(branch_name, into_branch) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/github.rb', line 179

def self.get_pull_request_info_from_api(branch_name, into_branch)
   pull = self.pull_for_branch(branch_name)

   if pull
      # This will grab the latest commit and retrieve the state from it.
      sha = pull[:head][:sha]
      state = self.get_most_recent_commit_status(get_github_repo, sha)
      state = state ? state[:state] : 'none'

      desc = "Merge \#{branch_name} (#\#{pull[:number]}) into \#{into_branch}\n\n\#{pull[:title].gsub(\"\\r\", '')}\n\n\#{pull[:body].gsub(\"\\r\", '')}\n   MSG\n\n      return {:status => state, :description => desc}\n   else\n      return {:status => nil, :description => \"Merge \#{branch_name} into \#{into_branch}\"}\n   end\nend\n"

.get_url(branch_name) ⇒ Object

Returns a URL based off the branch name.



174
175
176
177
# File 'lib/github.rb', line 174

def self.get_url(branch_name)
   pull = self.pull_for_branch(branch_name)
   return pull && pull[:html_url]
end

.open_title_body_editor(message) ⇒ Object

Prompts the user (using $EDITOR) to confirm the title and body in the provided message.

Returns a hash containing a :title and :body



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/github.rb', line 135

def self.open_title_body_editor(message)
   require 'tempfile'

   msg = Tempfile.new('pull-message')
   msg.write(message)
   msg.close

   Plugins.invoke :pre_message_edit, msg.path

   editor = Git::editor
   if (editor == 'vim')
      opts = "'+set ft=gitcommit' '+set textwidth=72'" +
       " '+setlocal spell spelllang=en_us'"
   else
      opts = ""
   end

   system("#{editor} #{opts} #{msg.path.shellescape}")
   full_message = File.open(msg.path, "r").read
   lines = full_message.split("\n")
   lines = lines.reject {|line| line =~ /^\s*#/ }
   title = lines.shift
   body  = lines.join("\n").strip

   if title.empty? || body.empty?
      puts "You must provide a title and a body:\n"
      puts title
      puts
      puts body
      exit 1
   end

   return {
      :title => title,
      :body => body
   }
end

.pull_for_branch(branch_name) ⇒ Object



211
212
213
# File 'lib/github.rb', line 211

def self.pull_for_branch(branch_name)
   pull = self.pulls.find {|pull| branch_name == pull[:head][:ref] }
end

.pullsObject



203
204
205
206
207
208
209
# File 'lib/github.rb', line 203

def self.pulls
   if !@@pulls
      repo = get_github_repo
      @@pulls = api.pulls(repo)
   end
   return @@pulls 
end

.request_authorization(authorization_info) ⇒ Object

Returns a hash containing the username and github oauth token

Prompts the user for credentials if the token isn’t stored in git config



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/github.rb', line 58

def self.request_authorization(authorization_info)
   puts "Authorizing..."

   username ||= Readline.readline("github username: ", true)
   print "github password: "
   password = STDIN.noecho(&:gets).chomp
   puts # blank line

   octokit = OctokitWrapper.new(:login => username, :password => password)

   auth = octokit.authorizations.find {|auth|
      note = auth['note']
      note && note.include?(authorization_info[:note])
   }

   auth = auth || octokit.create_authorization(authorization_info)

   success =
      system("git config --global github.user #{username}") &&
      system("git config --global github.token #{auth[:token]}")

   unless success
      die("Couldn't set git config")
   end

   return {:login => username, :access_token => auth[:token]}
end