Created
May 4, 2015 15:36
-
-
Save jacobian/736a69472a71829f205a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import github3 | |
import logging | |
from django.http import HttpResponse | |
from django.views.decorators.csrf import csrf_exempt | |
gh = github3.login(token=os.environ['GITHUB_TOKEN']) | |
log = logging.getLogger(__name__) | |
@csrf_exempt | |
def webhook(request): | |
payload = json.loads(request.body) | |
event = request.META['HTTP_X_GITHUB_EVENT'] | |
log.info('at=webhook event=%s', event) | |
if event == 'pull_request': | |
if payload['action'] == 'opened': | |
repo_name = payload['pull_request']['head']['repo']['full_name'] | |
log.info('at=webhook pr-opened repo=%s', repo_name) | |
repo = gh.repository(*repo_name.split('/')) | |
repo.create_status( | |
sha = payload['pull_request']['head']['sha'], | |
state = 'error', | |
description = 'requires change control approval') | |
log.info('at=webhook pr-opened repo=%s status=approval-required', repo_name) | |
elif event == 'issue_comment': | |
if '+1' in payload['comment']['body'].strip(): | |
repo_name = payload['repository']['full_name'] | |
pr_number = payload['issue']['number'] | |
log.info('at=webhook issue_comment repo=%s pr=%s', repo_name, pr_number) | |
repo = gh.repository(*repo_name.split('/')) | |
pr = repo.pull_request(pr_number) | |
repo.create_status( | |
sha = pr.head.sha, | |
state = 'success', | |
description = 'change approved', | |
) | |
log.info('at=webhook issue)comment repo=%s status=approved', repo_name) | |
return HttpResponse('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment