Fix and improve cancel-rerun lambda

- Fix rerun for DocsCheck
- Additional check for workflow head sha1
- Fix workflow debug info
- Fix event_data parsing
- Always print debug info
This commit is contained in:
Mikhail f. Shiryaev 2022-04-12 09:51:26 +02:00
parent 11e63f76a1
commit abf8c3c5ad
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
from collections import namedtuple
from typing import Any, Dict
from typing import Any, Dict, List
import json
import time
@ -11,7 +11,7 @@ import boto3 # type: ignore
NEED_RERUN_OR_CANCELL_WORKFLOWS = {
"PullRequestCI",
"Docs",
"DocsCheck",
"DocsRelease",
"BackportPR",
}
@ -93,7 +93,9 @@ WorkflowDescription = namedtuple(
def get_workflows_description_for_pull_request(pull_request_event):
head_repo = pull_request_event["head"]["repo"]["full_name"]
head_branch = pull_request_event["head"]["ref"]
head_sha = pull_request_event["head"]["sha"]
print("PR", pull_request_event["number"], "has head ref", head_branch)
workflows_data = []
workflows = _exec_get_with_retry(
@ -111,17 +113,23 @@ def get_workflows_description_for_pull_request(pull_request_event):
print("Too many workflows found")
break
DEBUG_INFO["workflows"] = [] # type: List[Dict[str, str]]
workflow_descriptions = []
for workflow in workflows_data:
DEBUG_INFO["workflow"] = workflow
# Some time workflow["head_repository"]["full_name"] is None
if workflow["head_repository"] is None:
continue
DEBUG_INFO["workflows"].append(
{
"full_name": workflow["head_repository"]["full_name"],
"name": workflow["name"],
}
)
# unfortunately we cannot filter workflows from forks in request to API
# so doing it manually
if (
workflow["head_repository"]["full_name"]
== pull_request_event["head"]["repo"]["full_name"]
workflow["head_sha"] == head_sha
and workflow["head_repository"]["full_name"] == head_repo
and workflow["name"] in NEED_RERUN_OR_CANCELL_WORKFLOWS
):
workflow_descriptions.append(
@ -170,7 +178,7 @@ def exec_workflow_url(urls_to_cancel, token):
def main(event):
token = get_token_from_aws()
DEBUG_INFO["event_body"] = event["body"]
event_data = event["body"]
event_data = json.loads(event["body"])
print("Got event for PR", event_data["number"])
action = event_data["action"]
@ -220,7 +228,6 @@ def main(event):
def handler(event, _):
try:
main(event)
except Exception:
finally:
for name, value in DEBUG_INFO.items():
print(f"Value of {name}: ", value)
raise