This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
webhook_handler.py
61 lines (48 loc) · 2 KB
/
webhook_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""This module contains the TravisCI webhook handler."""
import ConfigParser
from github import GitHub
from travis import Travis
from log_parser import parse_logs
import logging
import requests
logging.basicConfig(filename='prbuildbot.log', level=logging.DEBUG)
CONFIG = ConfigParser.ConfigParser()
CONFIG.readfp(open(r'config.txt'))
GH_TOKEN = CONFIG.get('GitHub', 'GH_TOKEN')
ORG = CONFIG.get('GitHub', 'ORG')
REPO = CONFIG.get('GitHub', 'REPO')
def webhook_handler(payload, signature):
"""Respond to Travis webhook."""
travis = Travis()
github = GitHub()
# The payload comes in the request, but we need to make sure it is
# really signed by Travis CI. If not, respond to this request with
# an error.
verified_payload = travis.get_verified_payload(payload, signature)
error = verified_payload.get('error')
if error:
return error.get('message'), error.get('code')
# Ensure only builds for this repository can comment here.
repository = verified_payload.get("repository")
owner_name = repository.get("owner_name")
repo_name = repository.get("name")
if owner_name != ORG or repo_name != REPO:
return "Forbidden: Repository Mismatch. Build for %s/%s attempting to comment on %s/%s" % (owner_name, repo_name, ORG, REPO), 403
issue_number = int(verified_payload.get('pull_request_number'))
logs = travis.get_logs(verified_payload)
comments = parse_logs(logs)
# Create a separate comment for every job
for comment in comments:
try:
log_url = Travis.job_url(github.org, github.repo,
comment['job_id'])
github.post_comment(issue_number,
comment['text'],
comment['title'],
log_url)
except requests.RequestException as err:
logging.error(err.response.text)
return err.response.text, 500
return "OK", 200