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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/cyclid/plugins/api/github/helpers.rb', line 113

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|yaml)\z/)
    next unless match

    sha = file['sha']
    type = match[1]
    break
  end
  [sha, type]
end

#find_oauth_token(config, clone_url) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/cyclid/plugins/api/github/helpers.rb', line 97

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



159
160
161
# File 'app/cyclid/plugins/api/github/helpers.rb', line 159

def humanish(uri)
  uri.path.split('/').last.gsub('.git', '')
end

#load_job_file(repo, sha, type) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'app/cyclid/plugins/api/github/helpers.rb', line 129

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'
    YAML.load(Base64.decode64(blob.content))
  end
end

#normalize(url) ⇒ Object

Normalize a Github URL into a Cyclid style source definition



147
148
149
150
151
152
153
154
155
156
# File 'app/cyclid/plugins/api/github/helpers.rb', line 147

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



140
141
142
143
144
# File 'app/cyclid/plugins/api/github/helpers.rb', line 140

def oauth_state
  org = retrieve_organization
  state = "#{org.name}:#{org.salt}:#{org.owner_email}"
  Base64.urlsafe_encode64(Digest::SHA256.digest(state))
end

#pr_baseObject



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

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

#pr_base_repoObject



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

def pr_base_repo
  @pr_base_repo ||= pr_base['repo']
end

#pr_base_urlObject



56
57
58
# File 'app/cyclid/plugins/api/github/helpers.rb', line 56

def pr_base_url
  pr_base_repo['html_url']
end

#pr_headObject



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

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

#pr_head_repoObject



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

def pr_head_repo
  @pr_head_repo ||= pr_head['repo']
end

#pr_head_urlObject



64
65
66
# File 'app/cyclid/plugins/api/github/helpers.rb', line 64

def pr_head_url
  pr_head_repo['html_url']
end

#pr_numberObject



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

def pr_number
  @pr_number ||= pull_request['number']
end

#pr_refObject



48
49
50
# File 'app/cyclid/plugins/api/github/helpers.rb', line 48

def pr_ref
  pr_head['ref']
end

#pr_repositoryObject



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

def pr_repository
  @repo ||= Octokit::Repository.from_url(pr_base_url)
end

#pr_shaObject



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

def pr_sha
  pr_head['sha']
end

#pr_trees_urlObject



68
69
70
71
# File 'app/cyclid/plugins/api/github/helpers.rb', line 68

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



89
90
91
# File 'app/cyclid/plugins/api/github/helpers.rb', line 89

def push_clone_url
  @push_clone_url ||= @payload['repository']['html_url']
end

#push_head_commitObject



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

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

#push_refObject



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

def push_ref
  @payload['ref']
end

#push_repositoryObject



93
94
95
# File 'app/cyclid/plugins/api/github/helpers.rb', line 93

def push_repository
  @push_repo ||= Octokit::Repository.from_url(push_clone_url)
end

#push_shaObject



85
86
87
# File 'app/cyclid/plugins/api/github/helpers.rb', line 85

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