Module: Cyclid::API::Plugins::ApiExtension::GithubMethods::Helpers
- Included in:
- Cyclid::API::Plugins::ApiExtension::GithubMethods
- Defined in:
- app/cyclid/plugins/api/github/helpers.rb
Overview
Github event handler helper methods
Instance Method Summary collapse
- #find_job_file(tree) ⇒ Object
- #find_oauth_token(config, clone_url) ⇒ Object
-
#humanish(uri) ⇒ Object
Extract the “humanish” part from a Git repository URL.
- #load_job_file(repo, sha, type) ⇒ Object
-
#normalize(url) ⇒ Object
Normalize a Github URL into a Cyclid style source definition.
-
#oauth_state ⇒ Object
Generate a “state” key that can be passed to the OAuth endpoints.
- #pr_base ⇒ Object
- #pr_base_repo ⇒ Object
- #pr_base_url ⇒ Object
- #pr_head ⇒ Object
- #pr_head_repo ⇒ Object
- #pr_head_url ⇒ Object
- #pr_ref ⇒ Object
- #pr_repository ⇒ Object
- #pr_sha ⇒ Object
- #pr_trees_url ⇒ Object
- #pull_request ⇒ Object
- #push_clone_url ⇒ Object
- #push_head_commit ⇒ Object
- #push_ref ⇒ Object
- #push_repository ⇒ Object
- #push_sha ⇒ Object
Instance Method Details
#find_job_file(tree) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 109 def find_job_file(tree) # See if a .cyclid.yml or .cyclid.json file exists in the project # root sha = nil type = nil tree['tree'].each do |file| match = file['path'].match(/\A\.cyclid\.(json|yml)\z/) next unless match sha = file['sha'] type = match[1] break end [sha, type] end |
#find_oauth_token(config, clone_url) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 93 def find_oauth_token(config, clone_url) # Get an OAuth token, if one is set for this repo Cyclid.logger.debug "attempting to find auth token for #{clone_url}" auth_token = nil config['repository_tokens'].each do |entry| entry_url = URI(entry['url']) auth_token = entry['token'] if entry_url.host == clone_url.host && \ entry_url.path == clone_url.path end # If we didn't find a token specifically for this repository, use # the organization OAuth token auth_token = config['oauth_token'] if auth_token.nil? return auth_token end |
#humanish(uri) ⇒ Object
Extract the “humanish” part from a Git repository URL
155 156 157 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 155 def humanish(uri) uri.path.split('/').last.gsub('.git', '') end |
#load_job_file(repo, sha, type) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 125 def load_job_file(repo, sha, type) blob = @client.blob(repo, sha) case type when 'json' Oj.load(Base64.decode64(blob.content)) when 'yml' YAML.load(Base64.decode64(blob.content)) end end |
#normalize(url) ⇒ Object
Normalize a Github URL into a Cyclid style source definition
143 144 145 146 147 148 149 150 151 152 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 143 def normalize(url) uri = URI.parse(url) source = {} source['url'] = "#{uri.scheme}://#{uri.host}#{uri.path.gsub(/.git$/, '')}" source['token'] = uri.user if uri.user source['branch'] = uri.fragment if uri.fragment source end |
#oauth_state ⇒ Object
Generate a “state” key that can be passed to the OAuth endpoints
136 137 138 139 140 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 136 def oauth_state org = retrieve_organization state = "#{org.name}:#{org.salt}:#{org.owner_email}" Base64.urlsafe_encode64(Digest::SHA256.digest(state)) end |
#pr_base ⇒ Object
36 37 38 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 36 def pr_base @pr_base ||= pull_request['base'] end |
#pr_base_repo ⇒ Object
48 49 50 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 48 def pr_base_repo @pr_base_repo ||= pr_base['repo'] end |
#pr_base_url ⇒ Object
52 53 54 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 52 def pr_base_url pr_base_repo['html_url'] end |
#pr_head ⇒ Object
32 33 34 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 32 def pr_head @pr_head ||= pull_request['head'] end |
#pr_head_repo ⇒ Object
56 57 58 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 56 def pr_head_repo @pr_head_repo ||= pr_head['repo'] end |
#pr_head_url ⇒ Object
60 61 62 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 60 def pr_head_url pr_head_repo['html_url'] end |
#pr_ref ⇒ Object
44 45 46 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 44 def pr_ref pr_head['ref'] end |
#pr_repository ⇒ Object
69 70 71 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 69 def pr_repository @repo ||= Octokit::Repository.from_url(pr_base_url) end |
#pr_sha ⇒ Object
40 41 42 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 40 def pr_sha pr_head['sha'] end |
#pr_trees_url ⇒ Object
64 65 66 67 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 64 def pr_trees_url url = pr_head_repo['trees_url'] @pr_trees_url ||= url.gsub('{/sha}', "/#{pr_sha}") end |
#pull_request ⇒ Object
28 29 30 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 28 def pull_request @pr ||= @payload['pull_request'] end |
#push_clone_url ⇒ Object
85 86 87 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 85 def push_clone_url @push_clone_url ||= @payload['repository']['html_url'] end |
#push_head_commit ⇒ Object
73 74 75 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 73 def push_head_commit @head_commit ||= @payload['head_commit'] end |
#push_ref ⇒ Object
77 78 79 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 77 def push_ref @payload['ref'] end |
#push_repository ⇒ Object
89 90 91 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 89 def push_repository @push_repo ||= Octokit::Repository.from_url(push_clone_url) end |
#push_sha ⇒ Object
81 82 83 |
# File 'app/cyclid/plugins/api/github/helpers.rb', line 81 def push_sha @push_sha ||= push_head_commit['id'] end |