Skip to content

Commit

Permalink
WIP: draft payload processing
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Jul 21, 2019
1 parent 321dbe5 commit c40c2a8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
36 changes: 34 additions & 2 deletions app/controllers/incoming_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,40 @@ class IncomingController < ApplicationController

def github
# parse the JSON payload, that we get as string, to a hash
useable_body = JSON.parse(request.body.read).to_h
json_body = JSON.parse(request.body.read)

Raven.capture_message('Received hook', extra: useable_body)
Raven.capture_message('Received hook', extra: useable_body.to_h)

# parse the payload and trigger different actions
# todo: update the github app to provide us a token that we can validate
event_type = request.headers['X-GitHub-Event']
case event_type
when 'pull_request'
parse_pull_request(json_body)
# else # in case we handle all events, we should log unhandled payloads
# Raven.capture_message('Unhandled payload', extra: useable_body.to_h)
end
end

# this is now a valid route and sucks balls
# should this be part of the PullRequest model?
# How do we serialize the payload to a PullRequest model / how do we sync with our redis/database
def parse_pull_request(payload)
# TODO: does it make sense to parse payload['action'] here?

# get all labels on a PR
labels = payload['pull_request']['labels']
# github-wide unique ids
repository_id = payload['repository']['id']
pr_id = payload = payload['pull_request']['id']
# check if a PR is mergeable or not. Set/remove the related label
if payload['pull_request']['mergeable']
# isn't mergeable, we need to set a label if it isn't already present
add_label_on_repo('needs-rebase', repository_id) unless github.repo_label?('needs-rebase', repository_id)
add_label_on_pr('needs-rebase', repository_id, pr_id) unless labels.any? { |label| label[:name] == 'bla' }
elsif labels.any? { |label| label['name'] == 'needs-rebase' }
# is mergeable, we need to remove a label if it is present
remove_label_on_pr('needs-rebase', repository_id, pr_id)
end
end
end
34 changes: 34 additions & 0 deletions app/lib/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,38 @@ def self.client
access_token: Rails.application.credentials.github[Rails.env.to_sym][:bot_token]
)
end

# some convenience methods
# todo: store labels as json/yaml. We already have someting similar in the plumbing repo

def remove_label_on_pr(label, repository)
client.remove_label(repository, pr, label)
end

def add_label_on_pr(label, repository, pr_id)
# We assume that the label exists on the repository
client.add_labels_to_an_issue(repository, pr_id, [label])
end

def add_labels_on_pr(labels, repository, pr_id)
# We assume that the labels exists on the repository
client.add_labels_to_an_issue(repository, pr_id, labels)
end

def remove_label_on_repo(label, repository)
# this method should only get the label name and do a lookup of the rest (see line 10)
# https://octokit.github.io/octokit.rb/Octokit/Client/Labels.html#add_label-instance_method
# todo: how to add a description
cient.add_label(repository, label, 'cccccc')
end

def add_label_on_repo(label, repository)
cient.delete_label!(repository, label)
end

def repo_label?(label, repository)
# check if a repository already has a specific label
# we could also catch Octokit::NotFound:
client.label(repository, label) ? true : false
end
end

0 comments on commit c40c2a8

Please sign in to comment.