Module: Cyclid::API::Plugins::ApiExtension::GithubMethods::Push

Included in:
Cyclid::API::Plugins::ApiExtension::GithubMethods
Defined in:
app/cyclid/plugins/api/github/push.rb

Overview

Handle a Pull Request event

Instance Method Summary collapse

Instance Method Details

#event_push(config) ⇒ Object

Handle a Github Push event



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/cyclid/plugins/api/github/push.rb', line 31

def event_push(config)
  # Safely load the JSON event data
  @payload = parse_request_body
  Cyclid.logger.debug "hook payload=#{@payload.inspect}"

  # Get the list of files in the root of the repository in the
  # push branch
  clone_url = URI(push_clone_url)

  # Get the authentication key
  auth_token = find_oauth_token(config, clone_url)

  return_failure(400, "can not find a valid OAuth token for #{clone_url}") \
    if auth_token.nil?

  # Create an Octokit client
  @client = Octokit::Client.new(access_token: auth_token)

  # Get the push head
  tree = @client.tree(push_repository, push_sha, recursive: false)
  Cyclid.logger.debug "tree=#{tree.to_hash}"

  # Find the Cyclid job file (if it exists)
  job_sha, job_type = find_job_file(tree)
  Cyclid.logger.debug "job_sha=#{job_sha}"

  if job_sha.nil?
    @client.create_status(push_repository, push_sha, 'error',
                          context: 'Cyclid', description: 'No Cyclid job file found')
    return_failure(400, 'not a Cyclid repository')
  end

  # Get the job file
  begin
    job_definition = load_job_file(push_repository, job_sha, job_type)

    # Insert this repository & branch into the sources
    #
    # XXX Could this cause collisions between the existing sources in
    # the job definition? Not entirely sure what the workflow will
    # look like.
    job_sources = job_definition['sources'] || []
    job_sources << { 'type' => 'git',
                     'url' => clone_url.to_s,
                     'branch' => push_ref,
                     'token' => auth_token }
    job_definition['sources'] = job_sources

    Cyclid.logger.debug "sources=#{job_definition['sources']}"
  rescue StandardError => ex
    Cyclid.logger.error "failed to retrieve Github Push job: #{ex}"

    @client.create_status(push_repository, push_sha, 'error',
                          context: 'Cyclid',
                          description: "Couldn't retrieve Cyclid job file")
    return_failure(400, 'not a Cyclid repository')
  end

  Cyclid.logger.debug "job_definition=#{job_definition}"

  begin
    # Retrieve the plugin configuration
    plugins_config = Cyclid.config.plugins
    github_config = load_github_config(plugins_config)

    ui_url = github_config[:ui_url]
    linkback_url = "#{ui_url}/#{organization_name}"

    # Inject some useful context data
    ctx = { github_event: 'push',
            github_user: @payload['sender']['login'],
            github_ref: push_ref,
            github_comment: push_head_commit['message'] }

    callback = GithubCallback.new(auth_token, push_repository, push_sha, linkback_url)
    job_from_definition(job_definition, callback, ctx)
  rescue NotFoundError
    @client.create_status(pr_repository, pr_sha, 'error',
                          context: 'Cyclid',
                          description: 'The Organization does not exist')

    return_failure(404, 'organization does not exist')
  rescue InvalidObjectError
    @client.create_status(pr_repository, pr_sha, 'error',
                          context: 'Cyclid',
                          description: 'The Job definition contains errors')

    return_failure(400, 'job definition contains errors')
  rescue InternalError
    @client.create_status(pr_repository, pr_sha, 'error',
                          context: 'Cyclid', description: 'An Internal Error occurred')

    return_failure(4500, "internal error: #{ex}")
  rescue StandardError
    @client.create_status(push_repository, push_sha, 'error',
                          context: 'Cyclid', description: 'An unknown error occurred')

    return_failure(500, 'job failed')
  end

  return true
end