-
Notifications
You must be signed in to change notification settings - Fork 15
/
pending.py
executable file
·184 lines (154 loc) · 6.59 KB
/
pending.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
#
# Copyright (C) 2019 Collabora Limited
# Author: Guillaume Tucker <[email protected]>
#
# This module is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import argparse
import json
import os
import subprocess
import sys
import kernelci
from kernelci import print_color, shell_cmd, ssh_agent
def pull(args, pr, path):
branch = pr.head.ref
user = pr.head.repo.owner.login
try:
shell_cmd("""\
cd {path}
git pull --quiet --no-ff --no-edit {origin} {branch}
""".format(path=path, origin=pr.head.repo.clone_url, branch=branch))
except subprocess.CalledProcessError:
print_color('yellow', "FAILED to pull")
shell_cmd("""\
cd {path}
git reset --merge
""".format(path=path))
return False
return True
def get_skip_list(args, settings):
raw_skip = args.skip or settings.get('skip', as_list=True) or []
skip = []
for user_branch in raw_skip:
user, _, branch = user_branch.partition('/')
skip.append((user, branch))
return skip
def iterate_prs(repo, skip, users, path):
prs = repo.get_pulls()
for pr in reversed(list(prs)):
branch = pr.head.ref
repo = pr.head.repo
user = pr.head.repo.owner.login if repo else "(unknown)"
labels = list(label.name for label in pr.labels)
include_label = 'staging:{}'.format(args.main)
print("{:4} {:16} {:32} ".format(pr.number, user, branch), end='')
if repo is None:
print_color('red', "SKIP unknown repository")
elif user not in users:
print_color('red', "SKIP untrusted user")
elif (user, branch) in skip:
print_color('yellow', "SKIP")
elif args.skip_label and args.skip_label in labels:
print_color('yellow', "SKIP label: {}".format(args.skip_label))
elif pr.base.ref != args.main and include_label not in labels:
print_color('yellow', "SKIP base: {} labels: {}".format(pr.base.ref, labels))
else:
if pull(args, pr, path):
print_color('green', "OK")
print()
def do_push(args, settings, path, branch, ssh_key):
if not branch:
print_color('red', "No destination branch provided.")
return False
diff = kernelci.origin_diff(path, branch)
if not diff:
print_color('yellow', "No changes, not pushing.")
return True
print(diff)
tag = args.tag or kernelci.date_tag(path, args.tag_prefix)
kernelci.create_tag(path, tag)
print("\nPushing tag ({}) and branch ({})".format(tag, branch))
kernelci.push_tag_and_branch(path, ssh_key, branch, tag)
return True
def do_diff(path, branch):
diff = kernelci.origin_diff(path, branch)
if diff:
print(diff)
return True
return False
def main(args):
settings = kernelci.Settings(args.settings, args.project)
path = os.path.join('checkout', args.project)
namespace = args.namespace or settings.get('namespace') or 'kernelci'
repo_name = '/'.join([namespace, args.project])
repo = kernelci.GITHUB.get_repo(repo_name)
target_branch = args.branch or settings.get('branch') or ''
kernelci.checkout_repository(path, repo, branch=args.main)
skip = get_skip_list(args, settings)
print("\n{:4} {:16} {:32} {}".format("PR", "User", "Branch", "Status"))
print("-------------------------------------------------------------")
users = settings.get('users', as_list=True)
if not users:
print_color('red', "Aborting, no list of trusted users")
return False
iterate_prs(repo, skip, users, path)
patches_path = os.path.join('patches', args.project, target_branch)
if not kernelci.apply_patches(path, patches_path):
print_color('red', "Aborting, all patches must apply.")
return False
if args.diff_only:
return do_diff(path, target_branch)
if args.push:
ssh_key = kernelci.default_ssh_key(args.ssh_key, target_branch)
if not ssh_key:
print_color('red', "No SSH key provided, cannot push.")
return False
if args.tag_limit:
kernelci.delete_old_tags(args, path, ssh_key)
do_push(args, settings, path, target_branch, ssh_key)
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser("\
Create staging.kernelci.org branch with all pending PRs")
parser.add_argument("project",
help="Name of the Github project")
parser.add_argument("--tag",
help="Tag to create, default is to use current date")
parser.add_argument("--tag-prefix", default="staging-",
help="Prefix to create date with current date")
parser.add_argument("--tag-limit", default="20", type=int,
help="Number of previous tags to keep, 0 for all")
parser.add_argument("--branch",
help="Name of the branch to force-push to")
parser.add_argument("--main", default="main",
help="Name of the main branch to filter PRs")
parser.add_argument("--namespace",
help="Github project namespace, default is kernelci")
parser.add_argument("--skip", nargs='+', default=[],
help="Name of user/branch pairs to skip")
parser.add_argument("--skip-label", default="staging-skip",
help="Name of a Github label used to skip PRs")
parser.add_argument("--ssh-key",
help="Path to SSH key to push branches and tags")
parser.add_argument("--push", action="store_true",
help="Push the resulting branch and tag")
parser.add_argument("--diff-only", action="store_true",
help="Return 1 if there is a difference with upstream")
parser.add_argument("--settings", default="data/staging.ini",
help="Path to a settings file")
args = parser.parse_args(sys.argv[1:])
ret = main(args)
sys.exit(0 if ret is True else 1)