forked from mozilla-mobile/focus-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-pull-request.py
More file actions
71 lines (57 loc) · 1.94 KB
/
create-pull-request.py
File metadata and controls
71 lines (57 loc) · 1.94 KB
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
This scripts pushes the passed in branch (argument) to the bot's
repository and creates a pull request for it.
"""
import os
import re
import subprocess
import sys
import taskcluster
from github import Github
# Make sure we have all needed arguments
if len(sys.argv) != 2:
print "Usage", sys.argv[0], "BRANCH"
exit(1)
# Get token for GitHub bot account from secrets service
secrets = taskcluster.Secrets({'baseUrl': 'http://taskcluster/secrets/v1'})
data = secrets.get('project/focus/github')
token = data['secret']['botAccountToken']
BRANCH = sys.argv[1]
OWNER = 'mozilla-mobile'
USER = 'MickeyMoz'
REPO = 'focus-android'
BASE = 'master'
HEAD = "MickeyMoz:%s" % BRANCH
URL = "https://%s:%s@github.com/%s/%s/" % (USER, token, USER, REPO)
github = Github(login_or_token=token)
repo = github.get_user(OWNER).get_repo(REPO)
# Check if there's already an umerged pull request.
for request in repo.get_pulls(state='open'):
if request.user.login == USER:
print "There's already an unmerged pull request. Doing nothing."
exit(0)
# Push local state to branch
print subprocess.check_output(['git', 'push', URL, BRANCH])
# Read the log file and create the pull request body from it
log_path = os.path.join(os.path.dirname(__file__), '../../import-log.txt')
with open(log_path) as log_file:
# Remove color codes from android2po output
ansi_escape = re.compile(r'\x1b[^m]*m')
log = ansi_escape.sub('', log_file.read())
body = """
Automated import %s
Log:
```
%s
```
""" % (BRANCH, log)
# Create pull request
pull_request = repo.create_pull(title='String import ' + BRANCH, body=body, base=BASE, head=HEAD)
print pull_request
# Add the [needs merge] label to the pull request
issue = repo.get_issue(pull_request.number)
issue.add_to_labels("needs merge")
print issue