Allow to filter PRs by login

This commit is contained in:
Ivan Lezhankin 2019-04-11 19:16:57 +03:00
parent af7ab86ab6
commit 722e022e54
3 changed files with 34 additions and 19 deletions

View File

@ -1,4 +1,3 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
@ -28,8 +27,6 @@ import sys
CHECK_MARK = '🗸'
CROSS_MARK = '🗙'
WARN_MARK = ''
WAIT_MARK = ''
LABEL_MARK = '🏷'
@ -42,6 +39,8 @@ parser.add_argument('-n', type=int, default=3, dest='number',
help='number of last stable branches to consider')
parser.add_argument('--token', type=str, required=True,
help='token for Github access')
parser.add_argument('--login', type = str,
help='filter authorship by login')
args = parser.parse_args()
@ -57,7 +56,7 @@ else:
print(f'{CHECK_MARK} {stable[0]} forked from {stable[1]}')
first_commit = stables[0][1]
pull_requests = github.get_pull_requests(first_commit)
pull_requests = github.get_pull_requests(first_commit, args.login)
good_commits = set(pull_request['mergeCommit']['oid'] for pull_request in pull_requests)
bad_commits = [] # collect and print them in the end
@ -93,14 +92,8 @@ if bad_pull_requests:
if bad_commits:
print('\nCommits not referenced by any pull-request:')
bad_authors = set()
for bad in bad_commits:
print(f'{CROSS_MARK} {bad}')
bad_authors.add(bad.author)
print('\nTell these authors not to push without pull-request and not to merge with rebase :)')
for author in sorted(bad_authors, key=lambda x : x.name):
print(f'{WARN_MARK} {author}')
print(f'{CROSS_MARK} {bad} {bad.author}')
# TODO: check backports.
if need_backporting:
@ -141,5 +134,4 @@ if need_backporting:
print('\nLegend:')
print(f'{CHECK_MARK} - good')
print(f'{CROSS_MARK} - bad')
print(f'{WARN_MARK} - pay attention!')
print(f'{LABEL_MARK} - backport is detected via label')

View File

@ -52,6 +52,7 @@ class Query:
return labels
_MAX_PULL_REQUESTS = 5
_PULL_REQUESTS = '''
{{
repository(owner: "yandex" name: "ClickHouse") {{
@ -66,7 +67,7 @@ class Query:
}}
nodes {{
oid
associatedPullRequests(first: 5) {{
associatedPullRequests(first: {max_pull_requests}) {{
totalCount
nodes {{
... on PullRequest {{
@ -79,6 +80,9 @@ class Query:
mergeCommit {{
oid
author {{
user {{
id
}}
name
}}
}}
@ -103,18 +107,20 @@ class Query:
}}
}}
'''
def get_pull_requests(self, before_commit):
def get_pull_requests(self, before_commit, author):
'''Get all merged pull-requests from the HEAD of default branch to the last commit (excluding)
Args:
before_commit (string-convertable): commit sha of the last commit (excluding)
author (string): filter pull-requests by author name
Returns:
pull_requests: a list of JSON nodes with pull-requests' details
'''
pull_requests = []
query = Query._PULL_REQUESTS.format(max_page_size=self._max_page_size, next='')
query = Query._PULL_REQUESTS.format(max_page_size=self._max_page_size, max_pull_requests=Query._MAX_PULL_REQUESTS, next='')
not_end = True
user_id = self.get_user(author) if author else None
while not_end:
result = self._run(query)['data']['repository']['defaultBranchRef']
@ -127,17 +133,18 @@ class Query:
not_end = False
break
# TODO: use helper to fetch all pull-requests that were merged in a single commit.
assert commit['associatedPullRequests']['totalCount'] <= self._max_page_size, \
# TODO: fetch all pull-requests that were merged in a single commit.
assert commit['associatedPullRequests']['totalCount'] <= Query._MAX_PULL_REQUESTS, \
f'there are {commit["associatedPullRequests"]["totalCount"]} pull-requests merged in commit {commit["oid"]}'
for pull_request in commit['associatedPullRequests']['nodes']:
if(pull_request['baseRepository']['nameWithOwner'] == 'yandex/ClickHouse' and
pull_request['baseRefName'] == default_branch_name and
pull_request['mergeCommit']['oid'] == commit['oid']):
pull_request['mergeCommit']['oid'] == commit['oid'] and
(not user_id or pull_request['mergeCommit']['author']['user']['id'] == user_id)):
pull_requests.append(pull_request)
query = Query._PULL_REQUESTS.format(max_page_size=self._max_page_size, next=f'after: "{result["pageInfo"]["endCursor"]}"')
query = Query._PULL_REQUESTS.format(max_page_size=self._max_page_size, max_pull_requests=Query._MAX_PULL_REQUESTS, next=f'after: "{result["pageInfo"]["endCursor"]}"')
return pull_requests
@ -158,6 +165,18 @@ class Query:
'''
return self._run(Query._DEFAULT)['data']['repository']['defaultBranchRef']['name']
_USER = '''
{{
user(login: "{login}") {{
id
}}
}}
'''
def get_user(self, login):
'''Returns id by user login
'''
return self._run(Query._USER.format(login=login))['data']['user']['id']
def _run(self, query):
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

4
utils/list_backports.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
set -e
python3 -m github "$@"