forked from mozilla-conduit/autoland-transplant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-to-autoland
executable file
·87 lines (73 loc) · 2.35 KB
/
post-to-autoland
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
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
#!/usr/bin/env python2
import ConfigParser
import StringIO
import base64
import email.utils
import json
import os
import re
import subprocess
import sys
ROOT = os.path.abspath(os.path.dirname(__file__))
if 'VIRTUAL_ENV' not in os.environ:
activate = os.path.join(ROOT, 'venv', 'bin', 'activate_this.py')
execfile(activate, dict(__file__=activate))
sys.executable = os.path.join(ROOT, 'venv', 'bin', 'python')
import requests
from requests.auth import HTTPBasicAuth
os.environ['HGRCPATH'] = ''
# Load .env file.
env = ConfigParser.ConfigParser()
with open(os.path.join(ROOT, '.env'), 'r') as f:
env.readfp(StringIO.StringIO('[config]\n%s' % f.read()))
if not os.path.exists('.hg'):
print('You must run this script from the test-repo clone')
sys.exit(1)
autoland_url = 'http://localhost:%s/autoland' % env.get('config',
'HOST_AUTOLAND')
# Post all commits since last public revision.
commit_log = subprocess.check_output([
'hg', 'log',
'-r', 'children(last(public()))::.',
'-T', '{rev} {node} {author}\n',
])
if not commit_log:
print('Failed to find any non-public revisions to post')
sys.exit(1)
# Basic job template.
job = dict(
destination='land-repo',
ldap_username=None,
patch=None,
pingback_url='http://localhost/',
rev=None,
tree='land-repo',
)
commit_log_re = re.compile(
r'^(?P<rev>\d+)'
r' (?P<node>[a-z0-9]+)'
r' (?P<user>.+)\s*$')
for commit_line in commit_log.splitlines():
m = commit_log_re.match(commit_line)
if not m:
continue
# Fill in job details from commit.
job['rev'] = m.group('rev')
job['ldap_username'] = email.utils.parseaddr(m.group('user'))[1]
job['patch'] = base64.b64encode(
subprocess.check_output(['hg', 'export', '-r', m.group('node')])
).decode('utf-8')
print('Posting %s' % m.group('node'))
# Submit
r = requests.post(
autoland_url,
data=json.dumps(job, indent=0, sort_keys=True).encode('utf8'),
auth=HTTPBasicAuth('autoland', env.get('config', 'AUTOLAND_KEY')),
headers={'Content-Type': 'application/json'},
)
response = r.json()
# Show result.
if 'request_id' not in response:
print('Submission failed.\n%s' % response)
sys.exit(1)
print('Submission success: request_id %s' % response['request_id'])