summaryrefslogtreecommitdiff
path: root/tools/retry-github-test
blob: e47ecc2fdbf2b80f00d545270d10ed69adc466c3 (plain)
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
#!/usr/bin/python3
'''Retry a GitHub PR test request to autopkgtest.ubuntu.com'''

import os
import sys
import hmac
import json
import urllib.request
import urllib.error
import argparse

p = argparse.ArgumentParser(description='Retry a GitHub PR test request to autopkgtest.ubuntu.com')
p.add_argument('pr_api_url',
               help='GitHub PR API URL (e. g. https://api.github.com/repos/JoeDev/coolproj/pulls/1')
p.add_argument('test_url',
               help='autopkgtest URL (https://autopkgtest.ubuntu.com/request.cgi?release=xenial&arch=i386&...)')
p.add_argument('secret_file', type=argparse.FileType('rb'),
               help='Path to the GitHub secret for this test web hook')
args = p.parse_args()

with urllib.request.urlopen(args.pr_api_url) as f:
    api_info = json.loads(f.read().decode())

payload = json.dumps({'action': 'synchronize',
                      'number': os.path.basename(args.pr_api_url),
                      'pull_request': {'statuses_url': api_info['statuses_url'],
                                       'base': api_info['base']}})
payload = payload.encode()
payload_sig = hmac.new(args.secret_file.read().strip(), payload, 'sha1').hexdigest()

req = urllib.request.Request(args.test_url, data=payload,
                             headers={'X-Hub-Signature': 'sha1=' + payload_sig,
                                      'Content-Type': 'application/json'})
try:
    with urllib.request.urlopen(req) as f:
        print(f.read().decode())
except urllib.error.HTTPError as e:
    sys.stderr.write('Request failed with code %i: %s' % (e.code, e.msg))
    sys.stderr.write(e.fp.read().decode('UTF-8', 'replace'))
    sys.exit(1)