Module: Github
- Defined in:
- lib/github.rb
Constant Summary collapse
- @@pulls =
nil
Class Method Summary collapse
-
.api(authorization_info = {}) ⇒ Object
Get an instance of the Octokit API class.
-
.config(property) ⇒ Object
Get a git config property from the first place that defines it.
- .get_authentication(authorization_info) ⇒ Object
- .get_commit_status_warning(status) ⇒ Object
-
.get_github_repo ⇒ Object
Returns the github repo identifier in the form that the API likes: “someuser/theirrepo”.
-
.get_most_recent_commit_status(repo, sha) ⇒ Object
Returns the most recent github commit status for a given commit.
-
.get_pull_request_description(branch_name = nil) ⇒ Object
Prompts the user (using $EDITOR) to provide a title and body for this pull-request.
- .get_pull_request_info_from_api(branch_name, into_branch) ⇒ Object
-
.get_url(branch_name) ⇒ Object
Returns a URL based off the branch name.
-
.open_title_body_editor(message) ⇒ Object
Prompts the user (using $EDITOR) to confirm the title and body in the provided message.
- .pull_for_branch(branch_name) ⇒ Object
- .pulls ⇒ Object
-
.request_authorization(authorization_info) ⇒ Object
Returns a hash containing the username and github oauth token.
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"
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/github.rb', line 33 def self.api( = {}) # Let Octokit handle pagination automagically for us. Octokit.auto_paginate = true # Defaults = { :scopes => ['repo'], :note => "ifixit git-scripts command line interface", :note_url => "https://github.com/ifixit/git-scripts" }.merge() OctokitWrapper.new(self::get_authentication()) 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.
16 17 18 |
# File 'lib/github.rb', line 16 def self.config(property) `git config #{property.to_s.shellescape}`.strip end |
.get_authentication(authorization_info) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/github.rb', line 45 def self.get_authentication() username = self::config("github.user") token = self::config("github.token") if !username.empty? && !token.empty? return {:login => username, :access_token => token} else return self::() end end |
.get_commit_status_warning(status) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/github.rb', line 217 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_repo ⇒ Object
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
94 95 96 97 98 99 100 101 102 |
# File 'lib/github.rb', line 94 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
127 128 129 |
# File 'lib/github.rb', line 127 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
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/github.rb', line 110 def self.get_pull_request_description(branch_name = nil) if branch_name = Git::(branch_name).gsub("\r","") else = <<-MESSAGE Title of pull-request # Second line is ignored (do no edit) Body of pull-request MESSAGE end return self::open_title_body_editor() end |
.get_pull_request_info_from_api(branch_name, into_branch) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/github.rb', line 181 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 = <<-MSG Merge #{branch_name} (##{pull[:number]}) into #{into_branch} #{pull[:title].gsub("\r", '')} #{pull[:body].gsub("\r", '')} MSG return {:status => state, :description => desc} else return {:status => nil, :description => "Merge #{branch_name} into #{into_branch}"} end end |
.get_url(branch_name) ⇒ Object
Returns a URL based off the branch name.
176 177 178 179 |
# File 'lib/github.rb', line 176 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
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 172 173 |
# File 'lib/github.rb', line 137 def self.open_title_body_editor() require 'tempfile' msg = Tempfile.new('pull-message') msg.write() 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}") = File.open(msg.path, "r").read lines = .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
213 214 215 |
# File 'lib/github.rb', line 213 def self.pull_for_branch(branch_name) pull = self.pulls.find {|pull| branch_name == pull[:head][:ref] } end |
.pulls ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/github.rb', line 205 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
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 85 86 |
# File 'lib/github.rb', line 60 def self.() 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..find {|auth| note = auth['note'] note && note.include?([:note]) } auth = auth || octokit.() 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 |