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 =
" # Pull Request Protips(tm):\n # * Include description of how this change accomplishes the task at hand.\n # * Use GitHub flavored Markdown http://github.github.com/github-flavored-markdown/\n # * Review CONTRIBUTING.md for recommendations of artifacts, links, images, screencasts, etc.\n #\n # This footer will automatically be stripped from the pull request description\n".dedent
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
174 175 176 177 178 |
# File 'lib/gitx/github.rb', line 174 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
94 95 96 97 98 99 100 101 102 |
# File 'lib/gitx/github.rb', line 94 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gitx/github.rb', line 104 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.message}", :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
126 127 128 |
# File 'lib/gitx/github.rb', line 126 def github_client @client ||= Octokit::Client.new(access_token: ) end |
#github_client_name ⇒ Object
121 122 123 124 |
# File 'lib/gitx/github.rb', line 121 def github_client_name = Time.now.utc.strftime('%FT%R:%S%z') client_name = "The Garage Git eXtensions #{timestamp}" end |
#github_organization ⇒ Object
148 149 150 |
# File 'lib/gitx/github.rb', line 148 def github_organization github_slug.split('/').first end |
#github_slug ⇒ Object
Returns the github slug for the current repository's remote origin url.
143 144 145 146 |
# File 'lib/gitx/github.rb', line 143 def github_slug remote = repo.config['remote.origin.url'] remote.to_s.gsub(/\.git$/, '').split(/[:\/]/).last(2).join('/') end |
#global_config ⇒ Object
156 157 158 159 160 |
# File 'lib/gitx/github.rb', line 156 def global_config @config ||= begin File.exist?(global_config_file) ? YAML.load_file(global_config_file) : {} end end |
#global_config_file ⇒ Object
152 153 154 |
# File 'lib/gitx/github.rb', line 152 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 86 87 |
# 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:'* %B'") description = [:description] description_template = [] description_template << "#{description}\n" if description description_template << '### What changed?' description_template << changelog description_template << '### What was fixed?' description_template << '### What needs to be done?' 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
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/gitx/github.rb', line 162 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.
132 133 134 135 136 |
# File 'lib/gitx/github.rb', line 132 def username username = repo.config['github.user'] fail "Github user not configured. Run: `git config --global github.user '[email protected]'`" unless username username end |