2019-04-08 15:33:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
|
|
|
Rules for commit messages, branch names and everything:
|
|
|
|
|
2019-04-12 13:26:02 +00:00
|
|
|
- All important(!) commits to master branch must originate from pull-requests.
|
2019-04-08 19:28:08 +00:00
|
|
|
- All pull-requests must be squash-merged or explicitly merged without rebase.
|
|
|
|
- All pull-requests to master must have at least one label prefixed with `pr-`.
|
|
|
|
- Labels that require pull-request to be backported must be red colored (#ff0000).
|
2020-05-25 11:10:14 +00:00
|
|
|
- Release branch name must be of form `YY.NUMBER`.
|
|
|
|
- All release branches must be forked directly from the master branch and never be merged back,
|
2019-04-08 15:33:05 +00:00
|
|
|
or merged with any other branches based on the master branch (including master branch itself).
|
|
|
|
|
2019-04-08 19:28:08 +00:00
|
|
|
Output of this script:
|
2019-04-08 15:33:05 +00:00
|
|
|
|
2019-04-08 19:28:08 +00:00
|
|
|
- Commits without references from pull-requests.
|
|
|
|
- Pull-requests to master without proper labels.
|
2020-05-25 11:10:14 +00:00
|
|
|
- Pull-requests that need to be backported, with statuses per release branch.
|
2019-04-08 15:33:05 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
from . import local, query
|
2019-09-19 18:01:47 +00:00
|
|
|
from . import parser as parse_description
|
2019-04-12 13:26:02 +00:00
|
|
|
|
2019-04-08 15:33:05 +00:00
|
|
|
import argparse
|
2019-04-09 10:19:08 +00:00
|
|
|
import re
|
2019-04-08 15:33:05 +00:00
|
|
|
import sys
|
|
|
|
|
2019-09-19 18:01:47 +00:00
|
|
|
try:
|
|
|
|
from termcolor import colored # `pip install termcolor`
|
|
|
|
except ImportError:
|
2020-05-25 11:10:14 +00:00
|
|
|
sys.exit("Package 'termcolor' not found. Try run: `pip3 install [--user] termcolor`")
|
2019-09-19 18:01:47 +00:00
|
|
|
|
|
|
|
|
2019-04-12 13:26:02 +00:00
|
|
|
CHECK_MARK = colored('🗸', 'green')
|
|
|
|
CROSS_MARK = colored('🗙', 'red')
|
2020-03-17 11:31:01 +00:00
|
|
|
BACKPORT_LABEL_MARK = colored('🏷', 'yellow')
|
|
|
|
CONFLICT_LABEL_MARK = colored('☁', 'yellow')
|
2020-04-23 13:10:34 +00:00
|
|
|
NO_BACKPORT_LABEL_MARK = colored('━', 'yellow')
|
2019-04-12 13:26:02 +00:00
|
|
|
CLOCK_MARK = colored('↻', 'cyan')
|
2019-04-09 09:25:18 +00:00
|
|
|
|
2019-04-08 15:33:05 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Helper for the ClickHouse Release machinery')
|
|
|
|
parser.add_argument('--repo', '-r', type=str, default='', metavar='PATH',
|
|
|
|
help='path to the root of the ClickHouse repository')
|
|
|
|
parser.add_argument('--remote', type=str, default='origin',
|
2019-10-09 11:35:38 +00:00
|
|
|
help='remote name of the "ClickHouse/ClickHouse" upstream')
|
2019-04-08 15:33:05 +00:00
|
|
|
parser.add_argument('--token', type=str, required=True,
|
|
|
|
help='token for Github access')
|
2019-04-12 13:26:02 +00:00
|
|
|
parser.add_argument('--login', type=str,
|
2019-04-11 16:16:57 +00:00
|
|
|
help='filter authorship by login')
|
2020-03-16 15:27:07 +00:00
|
|
|
parser.add_argument('--auto-label', action='store_true', dest='autolabel', default=True,
|
2019-09-19 18:01:47 +00:00
|
|
|
help='try to automatically parse PR description and put labels')
|
2019-04-08 15:33:05 +00:00
|
|
|
|
2020-05-25 11:10:14 +00:00
|
|
|
# Either select last N release branches, or specify them manually.
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument('-n', type=int, default=3, dest='number',
|
|
|
|
help='number of last release branches to consider')
|
|
|
|
group.add_argument('--branch', type=str, action='append', metavar='BRANCH',
|
|
|
|
help='specific release branch name to consider')
|
|
|
|
|
2019-04-08 15:33:05 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-09-06 10:36:15 +00:00
|
|
|
github = query.Query(args.token, 30)
|
2019-04-08 15:33:05 +00:00
|
|
|
repo = local.Local(args.repo, args.remote, github.get_default_branch())
|
|
|
|
|
2020-05-25 11:10:14 +00:00
|
|
|
if not args.branch:
|
|
|
|
release_branches = repo.get_release_branches()[-args.number:] # [(branch name, base)]
|
|
|
|
else:
|
|
|
|
release_branches = []
|
|
|
|
all_release_branches = repo.get_release_branches()
|
|
|
|
for branch in all_release_branches:
|
|
|
|
if branch[0] in args.branch:
|
|
|
|
release_branches.append(branch)
|
|
|
|
|
|
|
|
if not release_branches:
|
2019-09-06 10:36:15 +00:00
|
|
|
sys.exit('No release branches found!')
|
2019-04-08 16:11:13 +00:00
|
|
|
else:
|
2019-09-06 10:36:15 +00:00
|
|
|
print('Found release branches:')
|
2020-05-25 11:10:14 +00:00
|
|
|
for branch in release_branches:
|
|
|
|
print(f'{CHECK_MARK} {branch[0]} forked from {branch[1]}')
|
2019-04-08 15:33:05 +00:00
|
|
|
|
2020-05-25 11:10:14 +00:00
|
|
|
first_commit = release_branches[0][1]
|
2019-04-11 16:16:57 +00:00
|
|
|
pull_requests = github.get_pull_requests(first_commit, args.login)
|
2019-04-09 10:19:08 +00:00
|
|
|
good_commits = set(pull_request['mergeCommit']['oid'] for pull_request in pull_requests)
|
2019-04-08 15:33:05 +00:00
|
|
|
|
2019-04-09 09:25:18 +00:00
|
|
|
bad_commits = [] # collect and print them in the end
|
2019-04-08 15:33:05 +00:00
|
|
|
from_commit = repo.get_head_commit()
|
2020-05-25 11:10:14 +00:00
|
|
|
for i in reversed(range(len(release_branches))):
|
|
|
|
for commit in repo.iterate(from_commit, release_branches[i][1]):
|
2019-04-09 09:25:18 +00:00
|
|
|
if str(commit) not in good_commits and commit.author.name != 'robot-clickhouse':
|
|
|
|
bad_commits.append(commit)
|
2019-04-08 15:33:05 +00:00
|
|
|
|
2020-05-25 11:10:14 +00:00
|
|
|
from_commit = release_branches[i][1]
|
2019-04-08 16:11:13 +00:00
|
|
|
|
2019-10-09 11:35:38 +00:00
|
|
|
members = set(github.get_members("ClickHouse", "ClickHouse"))
|
2019-04-12 13:26:02 +00:00
|
|
|
def print_responsible(pull_request):
|
2020-03-16 15:27:07 +00:00
|
|
|
if "author" not in pull_request or pull_request["author"] is None:
|
|
|
|
return "No author"
|
2019-04-12 13:26:02 +00:00
|
|
|
if pull_request["author"]["login"] in members:
|
|
|
|
return colored(pull_request["author"]["login"], 'green')
|
|
|
|
elif pull_request["mergedBy"]["login"] in members:
|
|
|
|
return f'{pull_request["author"]["login"]} → {colored(pull_request["mergedBy"]["login"], "green")}'
|
|
|
|
else:
|
|
|
|
return f'{pull_request["author"]["login"]} → {pull_request["mergedBy"]["login"]}'
|
|
|
|
|
2020-04-24 00:35:27 +00:00
|
|
|
LABEL_NO_BACKPORT = 'pr-no-backport'
|
2019-04-09 09:55:10 +00:00
|
|
|
bad_pull_requests = [] # collect and print if not empty
|
|
|
|
need_backporting = []
|
2019-04-09 10:19:08 +00:00
|
|
|
for pull_request in pull_requests:
|
2019-09-19 18:01:47 +00:00
|
|
|
|
|
|
|
def find_label():
|
2020-04-24 00:35:27 +00:00
|
|
|
labels = github.get_labels(pull_request)
|
|
|
|
backport_allowed = LABEL_NO_BACKPORT not in map(lambda label: label['name'], labels)
|
|
|
|
for label in labels:
|
2019-09-19 18:01:47 +00:00
|
|
|
if label['name'].startswith('pr-'):
|
2020-04-24 00:35:27 +00:00
|
|
|
if label['color'] == 'ff0000' and backport_allowed:
|
2019-09-19 18:01:47 +00:00
|
|
|
need_backporting.append(pull_request)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
label_found = find_label()
|
|
|
|
|
|
|
|
if not label_found and args.autolabel:
|
|
|
|
print(f"Trying to auto-label pull-request: {pull_request['number']}")
|
|
|
|
description = parse_description.Description(pull_request)
|
|
|
|
if description.label_name:
|
|
|
|
github.set_label(pull_request, description.label_name)
|
|
|
|
label_found = find_label()
|
2019-04-08 16:11:13 +00:00
|
|
|
|
|
|
|
if not label_found:
|
2019-04-09 10:19:08 +00:00
|
|
|
bad_pull_requests.append(pull_request)
|
2019-04-09 09:25:18 +00:00
|
|
|
|
|
|
|
if bad_pull_requests:
|
2019-04-09 09:55:10 +00:00
|
|
|
print('\nPull-requests without description label:')
|
2019-04-09 10:19:08 +00:00
|
|
|
for bad in reversed(sorted(bad_pull_requests, key = lambda x : x['number'])):
|
2019-04-12 13:26:02 +00:00
|
|
|
print(f'{CROSS_MARK} {bad["number"]}: {bad["url"]} ({print_responsible(bad)})')
|
2019-04-09 09:25:18 +00:00
|
|
|
|
2019-04-09 09:55:10 +00:00
|
|
|
# FIXME: compatibility logic, until the direct modification of master is not prohibited.
|
2019-04-12 13:26:02 +00:00
|
|
|
if bad_commits and not args.login:
|
2019-04-09 09:55:10 +00:00
|
|
|
print('\nCommits not referenced by any pull-request:')
|
2019-04-09 09:25:18 +00:00
|
|
|
|
|
|
|
for bad in bad_commits:
|
2019-04-11 16:16:57 +00:00
|
|
|
print(f'{CROSS_MARK} {bad} {bad.author}')
|
2019-04-09 09:55:10 +00:00
|
|
|
|
|
|
|
# TODO: check backports.
|
|
|
|
if need_backporting:
|
2019-04-09 10:19:08 +00:00
|
|
|
re_vlabel = re.compile(r'^v\d+\.\d+$')
|
2020-04-13 11:20:13 +00:00
|
|
|
re_vlabel_backported = re.compile(r'^v\d+\.\d+-backported$')
|
2020-03-17 11:31:01 +00:00
|
|
|
re_vlabel_conflicts = re.compile(r'^v\d+\.\d+-conflicts$')
|
2020-04-23 13:10:34 +00:00
|
|
|
re_vlabel_no_backport = re.compile(r'^v\d+\.\d+-no-backport$')
|
2019-04-09 10:19:08 +00:00
|
|
|
|
2019-04-09 09:55:10 +00:00
|
|
|
print('\nPull-requests need to be backported:')
|
2019-04-09 10:19:08 +00:00
|
|
|
for pull_request in reversed(sorted(need_backporting, key=lambda x: x['number'])):
|
|
|
|
targets = [] # use common list for consistent order in output
|
|
|
|
good = set()
|
2020-03-17 11:31:01 +00:00
|
|
|
backport_labeled = set()
|
|
|
|
conflict_labeled = set()
|
2020-04-23 13:10:34 +00:00
|
|
|
no_backport_labeled = set()
|
2019-04-12 13:26:02 +00:00
|
|
|
wait = set()
|
2019-04-09 10:19:08 +00:00
|
|
|
|
2020-05-25 11:10:14 +00:00
|
|
|
for branch in release_branches:
|
|
|
|
if repo.comparator(branch[1]) < repo.comparator(pull_request['mergeCommit']['oid']):
|
|
|
|
targets.append(branch[0])
|
2019-04-09 10:19:08 +00:00
|
|
|
|
|
|
|
# FIXME: compatibility logic - check for a manually set label, that indicates status 'backported'.
|
2020-05-25 11:10:14 +00:00
|
|
|
# FIXME: O(n²) - no need to iterate all labels for every `branch`
|
2019-04-09 10:19:08 +00:00
|
|
|
for label in github.get_labels(pull_request):
|
2020-04-13 11:20:13 +00:00
|
|
|
if re_vlabel.match(label['name']) or re_vlabel_backported.match(label['name']):
|
2020-05-25 11:10:14 +00:00
|
|
|
if f'v{branch[0]}' == label['name'] or f'v{branch[0]}-backported' == label['name']:
|
|
|
|
backport_labeled.add(branch[0])
|
2020-03-17 11:31:01 +00:00
|
|
|
if re_vlabel_conflicts.match(label['name']):
|
2020-05-25 11:10:14 +00:00
|
|
|
if f'v{branch[0]}-conflicts' == label['name']:
|
|
|
|
conflict_labeled.add(branch[0])
|
2020-04-23 13:10:34 +00:00
|
|
|
if re_vlabel_no_backport.match(label['name']):
|
2020-05-25 11:10:14 +00:00
|
|
|
if f'v{branch[0]}-no-backport' == label['name']:
|
|
|
|
no_backport_labeled.add(branch[0])
|
2019-04-12 13:26:02 +00:00
|
|
|
|
|
|
|
for event in github.get_timeline(pull_request):
|
|
|
|
if(event['isCrossRepository'] or
|
|
|
|
event['target']['number'] != pull_request['number'] or
|
|
|
|
event['source']['baseRefName'] not in targets):
|
|
|
|
continue
|
|
|
|
|
|
|
|
found_label = False
|
|
|
|
for label in github.get_labels(event['source']):
|
|
|
|
if label['name'] == 'pr-backport':
|
|
|
|
found_label = True
|
|
|
|
break
|
|
|
|
if not found_label:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if event['source']['merged']:
|
|
|
|
good.add(event['source']['baseRefName'])
|
|
|
|
else:
|
|
|
|
wait.add(event['source']['baseRefName'])
|
2019-04-09 10:19:08 +00:00
|
|
|
|
|
|
|
# print pull-request's status
|
2020-04-23 13:10:34 +00:00
|
|
|
if len(good) + len(backport_labeled) + len(conflict_labeled) + len(no_backport_labeled) == len(targets):
|
2019-04-09 10:19:08 +00:00
|
|
|
print(f'{CHECK_MARK}', end=' ')
|
|
|
|
else:
|
|
|
|
print(f'{CROSS_MARK}', end=' ')
|
|
|
|
print(f'{pull_request["number"]}', end=':')
|
|
|
|
for target in targets:
|
|
|
|
if target in good:
|
2019-04-12 13:26:02 +00:00
|
|
|
print(f'\t{CHECK_MARK} {target}', end='')
|
2020-03-17 11:31:01 +00:00
|
|
|
elif target in backport_labeled:
|
|
|
|
print(f'\t{BACKPORT_LABEL_MARK} {target}', end='')
|
|
|
|
elif target in conflict_labeled:
|
|
|
|
print(f'\t{CONFLICT_LABEL_MARK} {target}', end='')
|
2020-04-23 13:10:34 +00:00
|
|
|
elif target in no_backport_labeled:
|
|
|
|
print(f'\t{NO_BACKPORT_LABEL_MARK} {target}', end='')
|
2019-04-12 13:26:02 +00:00
|
|
|
elif target in wait:
|
|
|
|
print(f'\t{CLOCK_MARK} {target}', end='')
|
2019-04-09 10:19:08 +00:00
|
|
|
else:
|
2019-04-12 13:26:02 +00:00
|
|
|
print(f'\t{CROSS_MARK} {target}', end='')
|
|
|
|
print(f'\t{pull_request["url"]} ({print_responsible(pull_request)})')
|
2019-04-09 10:19:08 +00:00
|
|
|
|
|
|
|
# print legend
|
|
|
|
print('\nLegend:')
|
|
|
|
print(f'{CHECK_MARK} - good')
|
|
|
|
print(f'{CROSS_MARK} - bad')
|
2020-03-17 11:31:01 +00:00
|
|
|
print(f'{BACKPORT_LABEL_MARK} - backport is detected via label')
|
|
|
|
print(f'{CONFLICT_LABEL_MARK} - backport conflict is detected via label')
|
2020-04-23 13:47:19 +00:00
|
|
|
print(f'{NO_BACKPORT_LABEL_MARK} - backport to this release is not needed')
|
2019-04-12 13:26:02 +00:00
|
|
|
print(f'{CLOCK_MARK} - backport is waiting to merge')
|
2019-09-06 12:42:15 +00:00
|
|
|
|
|
|
|
# print API costs
|
|
|
|
print('\nGitHub API total costs per query:')
|
|
|
|
for name, value in github.api_costs.items():
|
|
|
|
print(f'{name} : {value}')
|