Make great again

This commit is contained in:
alesapin 2021-12-22 10:54:50 +03:00
parent decd53b1e5
commit 7a00bc96ed
5 changed files with 39 additions and 14 deletions

View File

@ -70,19 +70,19 @@ def _exec_get_with_retry(url):
raise Exception("Cannot execute GET request with retries")
def get_workflows_cancel_urls_for_pull_request(pull_request_event):
def get_workflows_urls_for_pull_request(pull_request_event, url_name, check_status):
head_branch = pull_request_event['head']['ref']
print("PR", pull_request_event['number'], "has head ref", head_branch)
workflows = _exec_get_with_retry(API_URL + f"/actions/runs?branch={head_branch}")
workflows_urls_to_cancel = set([])
workflows_urls = set([])
for workflow in workflows['workflow_runs']:
if workflow['status'] != 'completed':
print("Workflow", workflow['url'], "not finished, going to be cancelled")
workflows_urls_to_cancel.add(workflow['cancel_url'])
if check_status(workflow['status']):
print("Workflow", workflow['url'], "going to check workflow")
workflows_urls.add(workflow[url_name])
else:
print("Workflow", workflow['url'], "already finished, will not try to cancel")
print("Workflow", workflow['url'], "doesn't satisfy status condition")
return workflows_urls_to_cancel
return workflows_urls
def _exec_post_with_retry(url, token):
headers = {
@ -99,7 +99,7 @@ def _exec_post_with_retry(url, token):
raise Exception("Cannot execute POST request with retry")
def cancel_workflows(urls_to_cancel, token):
def exec_workflow_url(urls_to_cancel, token):
for url in urls_to_cancel:
print("Cancelling workflow using url", url)
_exec_post_with_retry(url, token)
@ -117,9 +117,25 @@ def main(event):
print("PR has labels", labels)
if action == 'closed' or 'do not test' in labels:
print("PR merged/closed or manually labeled 'do not test' will kill workflows")
workflows_to_cancel = get_workflows_cancel_urls_for_pull_request(pull_request)
def check_status(status):
return status != 'completed'
workflows_to_cancel = get_workflows_urls_for_pull_request(pull_request, 'cancel_url', check_status)
print(f"Found {len(workflows_to_cancel)} workflows to cancel")
cancel_workflows(workflows_to_cancel, token)
exec_workflow_url(workflows_to_cancel, token)
elif action == 'labeled' and 'can be tested' in labels:
print("PR marked with can be tested label, rerun workflow")
def check_status_for_cancell(status):
return status != 'completed'
workflows_to_cancel = get_workflows_urls_for_pull_request(pull_request, 'cancel_url', check_status_for_cancell)
print("Cancelling all previous workflows")
print(f"Found {len(workflows_to_cancel)} workflows to cancel")
exec_workflow_url(workflows_to_cancel, token)
def check_status_for_rerun(status):
return status in ('completed', 'cancelled')
workflows_to_rerun = get_workflows_urls_for_pull_request(pull_request, 'rerun_url', check_status_for_rerun)
print(f"Found {len(workflows_to_rerun)} workflows")
exec_workflow_url(workflows_to_rerun, token)
else:
print("Nothing to do")

View File

@ -33,7 +33,7 @@ def get_pr_for_commit(sha, ref):
class PRInfo:
def __init__(self, github_event=None, need_orgs=False, need_changed_files=False):
def __init__(self, github_event=None, need_orgs=False, need_changed_files=False, labels_from_api=False):
if not github_event:
if GITHUB_EVENT_PATH:
with open(GITHUB_EVENT_PATH, 'r', encoding='utf-8') as event_file:
@ -61,7 +61,12 @@ class PRInfo:
self.head_ref = github_event['pull_request']['head']['ref']
self.head_name = github_event['pull_request']['head']['repo']['full_name']
self.labels = {l['name'] for l in github_event['pull_request']['labels']}
if labels_from_api:
self.labels = {l['name'] for l in github_event['pull_request']['labels']}
else:
response = requests.get("https://api.github.com/repos/{GITHUB_REPOSITORY}/issues/{self.number}/labels")
self.labels = {l['name'] for l in response.json()}
self.user_login = github_event['pull_request']['user']['login']
self.user_orgs = set([])
if need_orgs:
@ -90,7 +95,11 @@ class PRInfo:
f"https://api.github.com/repos/{GITHUB_REPOSITORY}/compare/{github_event['before']}...{self.sha}"
else:
self.number = pull_request['number']
self.labels = {l['name'] for l in pull_request['labels']}
if labels_from_api:
self.labels = {l['name'] for l in pull_request['labels']}
else:
response = requests.get("https://api.github.com/repos/{GITHUB_REPOSITORY}/issues/{self.number}/labels")
self.labels = {l['name'] for l in response.json()}
self.base_ref = pull_request['base']['ref']
self.base_name = pull_request['base']['repo']['full_name']
self.head_ref = pull_request['head']['ref']

View File

@ -109,7 +109,7 @@ def should_run_checks_for_pr(pr_info):
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
pr_info = PRInfo(need_orgs=True)
pr_info = PRInfo(need_orgs=True, labels_from_api=True)
can_run, description = should_run_checks_for_pr(pr_info)
gh = Github(get_best_robot_token())
commit = get_commit(gh, pr_info.sha)