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

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_stateObject

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_baseObject



36
37
38
# File 'app/cyclid/plugins/api/github/helpers.rb', line 36

def pr_base
  @pr_base ||= pull_request['base']
end

#pr_base_repoObject



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_urlObject



52
53
54
# File 'app/cyclid/plugins/api/github/helpers.rb', line 52

def pr_base_url
  pr_base_repo['html_url']
end

#pr_headObject



32
33
34
# File 'app/cyclid/plugins/api/github/helpers.rb', line 32

def pr_head
  @pr_head ||= pull_request['head']
end

#pr_head_repoObject



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_urlObject



60
61
62
# File 'app/cyclid/plugins/api/github/helpers.rb', line 60

def pr_head_url
  pr_head_repo['html_url']
end

#pr_refObject



44
45
46
# File 'app/cyclid/plugins/api/github/helpers.rb', line 44

def pr_ref
  pr_head['ref']
end

#pr_repositoryObject



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_shaObject



40
41
42
# File 'app/cyclid/plugins/api/github/helpers.rb', line 40

def pr_sha
  pr_head['sha']
end

#pr_trees_urlObject



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_requestObject



28
29
30
# File 'app/cyclid/plugins/api/github/helpers.rb', line 28

def pull_request
  @pr ||= @payload['pull_request']
end

#push_clone_urlObject



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_commitObject



73
74
75
# File 'app/cyclid/plugins/api/github/helpers.rb', line 73

def push_head_commit
  @head_commit ||= @payload['head_commit']
end

#push_refObject



77
78
79
# File 'app/cyclid/plugins/api/github/helpers.rb', line 77

def push_ref
  @payload['ref']
end

#push_repositoryObject



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_shaObject



81
82
83
# File 'app/cyclid/plugins/api/github/helpers.rb', line 81

def push_sha
  @push_sha ||= push_head_commit['id']
end