Module: Thegarage::Gitx::Github
- Included in:
- Cli::IntegrateCommand, Cli::ReleaseCommand, Cli::ReviewCommand
- Defined in:
- lib/thegarage/gitx/github.rb
Constant Summary collapse
- GLOBAL_CONFIG_FILE =
'~/.config/gitx/github.yml'
- REVIEW_CONTEXT =
'peer_review'
- CLIENT_URL =
'https://github.com/thegarage/thegarage-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(commit_sha, 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
170 171 172 173 174 |
# File 'lib/thegarage/gitx/github.rb', line 170 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
90 91 92 93 94 95 96 97 98 |
# File 'lib/thegarage/gitx/github.rb', line 90 def auth_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
47 48 49 50 |
# File 'lib/thegarage/gitx/github.rb', line 47 def branch_status(branch) response = github_client.status(github_slug, branch) response.state end |
#create_authorization ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/thegarage/gitx/github.rb', line 100 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
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/thegarage/gitx/github.rb', line 58 def create_pull_request(branch) say "Creating pull request for " say "#{branch} ", :green say "against " say "#{Thegarage::Gitx::BASE_BRANCH} ", :green say "in " say github_slug, :green title = branch body = pull_request_body(branch) github_client.create_pull_request(github_slug, Thegarage::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 |
# File 'lib/thegarage/gitx/github.rb', line 20 def find_or_create_pull_request(branch) pull_request = find_pull_request(branch) pull_request ||= begin execute_command(Thegarage::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
35 36 37 38 39 40 41 42 43 |
# File 'lib/thegarage/gitx/github.rb', line 35 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
122 123 124 |
# File 'lib/thegarage/gitx/github.rb', line 122 def github_client @client ||= Octokit::Client.new(:access_token => ) end |
#github_client_name ⇒ Object
117 118 119 120 |
# File 'lib/thegarage/gitx/github.rb', line 117 def github_client_name = Time.now.utc.strftime('%FT%R:%S%z') client_name = "The Garage Git eXtensions #{}" end |
#github_organization ⇒ Object
144 145 146 |
# File 'lib/thegarage/gitx/github.rb', line 144 def github_organization github_slug.split('/').first end |
#github_slug ⇒ Object
Returns the github slug for the current repository’s remote origin url.
139 140 141 142 |
# File 'lib/thegarage/gitx/github.rb', line 139 def github_slug remote = repo.config['remote.origin.url'] remote.to_s.gsub(/\.git$/,'').split(/[:\/]/).last(2).join('/') end |
#global_config ⇒ Object
152 153 154 155 156 |
# File 'lib/thegarage/gitx/github.rb', line 152 def global_config @config ||= begin File.exists?(global_config_file) ? YAML.load_file(global_config_file) : {} end end |
#global_config_file ⇒ Object
148 149 150 |
# File 'lib/thegarage/gitx/github.rb', line 148 def global_config_file File.(GLOBAL_CONFIG_FILE) end |
#pull_request_body(branch) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/thegarage/gitx/github.rb', line 71 def pull_request_body(branch) changelog = run_cmd("git log #{Thegarage::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
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/thegarage/gitx/github.rb', line 158 def save_global_config() config_dir = File.dirname(global_config_file) ::FileUtils.mkdir_p(config_dir, mode: 0700) unless File.exists?(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(commit_sha, state, description) ⇒ Object
Update build status with peer review status
53 54 55 |
# File 'lib/thegarage/gitx/github.rb', line 53 def update_review_status(commit_sha, state, description) 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.
128 129 130 131 132 |
# File 'lib/thegarage/gitx/github.rb', line 128 def username username = repo.config['github.user'] fail "Github user not configured. Run: `git config --global github.user '[email protected]'`" unless username username end |