Module: Gitx::Github
- Included in:
- Cli::IntegrateCommand, Cli::ReleaseCommand, Cli::ReviewCommand
- Defined in:
- lib/gitx/github.rb
Constant Summary collapse
- GLOBAL_CONFIG_FILE =
'~/.config/gitx/github.yml'
- REVIEW_CONTEXT =
'peer_review'
- CLIENT_URL =
'https://github.com/wireframe/gitx'
- PULL_REQUEST_FOOTER =
<<-EOS.dedent # Pull Request Protips(tm): # * Include description of how this change accomplishes the task at hand. # * Use GitHub flavored Markdown http://github.github.com/github-flavored-markdown/ # * Review CONTRIBUTING.md for recommendations of artifacts, links, images, screencasts, etc. # # This footer will automatically be stripped from the pull request description EOS
Instance Method Summary collapse
- #ask_without_echo(message) ⇒ Object
-
#authorization_token ⇒ String
authorization token used for github API calls the token is cached on the filesystem for future use.
-
#branch_status(branch) ⇒ Object
Get the current commit status of a branch.
- #create_authorization ⇒ Object
- #create_pull_request(branch) ⇒ Object
- #find_or_create_pull_request(branch) ⇒ Object
- #find_pull_request(branch) ⇒ Sawyer::Resource
- #github_client ⇒ Object
- #github_client_name ⇒ Object
- #github_organization ⇒ Object
-
#github_slug ⇒ Object
The github slug for the current repository’s remote origin url.
- #global_config ⇒ Object
- #global_config_file ⇒ Object
- #pull_request_body(branch) ⇒ Object
- #save_global_config(options) ⇒ Object
-
#update_review_status(pull_request, state, description) ⇒ Object
Update build status with peer review status.
-
#username ⇒ String
Github username (ex: ‘wireframe’) of the current github.user.
Instance Method Details
#ask_without_echo(message) ⇒ Object
172 173 174 175 176 |
# File 'lib/gitx/github.rb', line 172 def ask_without_echo() value = ask(, echo: false) say '' value end |
#authorization_token ⇒ String
authorization token used for github API calls the token is cached on the filesystem for future use
92 93 94 95 96 97 98 99 100 |
# File 'lib/gitx/github.rb', line 92 def auth_token = ENV['GITX_GITHUB_TOKEN'] || global_config['token'] auth_token ||= begin new_token = save_global_config('token' => new_token) new_token end auth_token end |
#branch_status(branch) ⇒ Object
Get the current commit status of a branch
48 49 50 51 |
# File 'lib/gitx/github.rb', line 48 def branch_status(branch) response = github_client.status(github_slug, branch) response.state end |
#create_authorization ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/gitx/github.rb', line 102 def password = ask_without_echo("Github password for #{username}: ") client = Octokit::Client.new(login: username, password: password) = { scopes: ['repo'], note: github_client_name, note_url: CLIENT_URL } two_factor_auth_token = ask_without_echo('Github two factor authorization token (if enabled): ') [:headers] = { 'X-GitHub-OTP' => two_factor_auth_token } if two_factor_auth_token response = client.() response.token rescue Octokit::ClientError => e say "Error creating authorization: #{e.}", :red retry end |
#create_pull_request(branch) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gitx/github.rb', line 60 def create_pull_request(branch) say 'Creating pull request for ' say "#{branch} ", :green say 'against ' say "#{Gitx::BASE_BRANCH} ", :green say 'in ' say github_slug, :green title = branch body = pull_request_body(branch) github_client.create_pull_request(github_slug, Gitx::BASE_BRANCH, branch, title, body) end |
#find_or_create_pull_request(branch) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gitx/github.rb', line 20 def find_or_create_pull_request(branch) pull_request = find_pull_request(branch) pull_request ||= begin checkout_branch(branch) execute_command(Gitx::Cli::UpdateCommand, :update) pull_request = create_pull_request(branch) say 'Created pull request: ' say pull_request.html_url, :green pull_request end pull_request end |
#find_pull_request(branch) ⇒ Sawyer::Resource
36 37 38 39 40 41 42 43 44 |
# File 'lib/gitx/github.rb', line 36 def find_pull_request(branch) head_reference = "#{github_organization}:#{branch}" params = { head: head_reference, state: 'open' } pull_requests = github_client.pull_requests(github_slug, params) pull_requests.first end |
#github_client ⇒ Object
124 125 126 |
# File 'lib/gitx/github.rb', line 124 def github_client @client ||= Octokit::Client.new(access_token: ) end |
#github_client_name ⇒ Object
119 120 121 122 |
# File 'lib/gitx/github.rb', line 119 def github_client_name = Time.now.utc.strftime('%FT%R:%S%z') client_name = "The Garage Git eXtensions #{}" end |
#github_organization ⇒ Object
146 147 148 |
# File 'lib/gitx/github.rb', line 146 def github_organization github_slug.split('/').first end |
#github_slug ⇒ Object
Returns the github slug for the current repository’s remote origin url.
141 142 143 144 |
# File 'lib/gitx/github.rb', line 141 def github_slug remote = repo.config['remote.origin.url'] remote.to_s.gsub(/\.git$/, '').split(/[:\/]/).last(2).join('/') end |
#global_config ⇒ Object
154 155 156 157 158 |
# File 'lib/gitx/github.rb', line 154 def global_config @config ||= begin File.exist?(global_config_file) ? YAML.load_file(global_config_file) : {} end end |
#global_config_file ⇒ Object
150 151 152 |
# File 'lib/gitx/github.rb', line 150 def global_config_file File.(GLOBAL_CONFIG_FILE) end |
#pull_request_body(branch) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gitx/github.rb', line 73 def pull_request_body(branch) changelog = run_cmd("git log #{Gitx::BASE_BRANCH}...#{branch} --reverse --no-merges --pretty=format:'* %s%n%b'") description = [:description] description_template = [] description_template << "#{description}\n" if description description_template << '### Changelog' description_template << changelog description_template << PULL_REQUEST_FOOTER body = ask_editor(description_template.join("\n"), repo.config['core.editor']) body.gsub(PULL_REQUEST_FOOTER, '').chomp.strip end |
#save_global_config(options) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/gitx/github.rb', line 160 def save_global_config() config_dir = File.dirname(global_config_file) ::FileUtils.mkdir_p(config_dir, mode: 0700) unless File.exist?(config_dir) @config = global_config.merge() File.open(global_config_file, 'a+') do |file| file.truncate(0) file.write(@config.to_yaml) end File.chmod(0600, global_config_file) end |
#update_review_status(pull_request, state, description) ⇒ Object
Update build status with peer review status
54 55 56 57 |
# File 'lib/gitx/github.rb', line 54 def update_review_status(pull_request, state, description) commit_sha = pull_request.head.sha github_client.create_status(github_slug, commit_sha, state, context: REVIEW_CONTEXT, description: description) end |
#username ⇒ String
Returns github username (ex: ‘wireframe’) of the current github.user.
130 131 132 133 134 |
# File 'lib/gitx/github.rb', line 130 def username username = repo.config['github.user'] fail "Github user not configured. Run: `git config --global github.user '[email protected]'`" unless username username end |