2021-09-29 07:47:15 +00:00
|
|
|
#!/usr/bin/env python3
|
2024-06-13 14:14:57 +00:00
|
|
|
import argparse
|
2021-09-29 07:47:15 +00:00
|
|
|
import logging
|
2024-04-25 11:28:06 +00:00
|
|
|
import sys
|
2024-02-06 12:39:34 +00:00
|
|
|
|
2021-10-27 07:03:23 +00:00
|
|
|
from github import Github
|
2021-11-26 14:00:09 +00:00
|
|
|
|
2024-04-17 13:11:42 +00:00
|
|
|
from ci_config import StatusNames
|
2023-04-06 09:40:32 +00:00
|
|
|
from commit_status_helper import (
|
|
|
|
get_commit,
|
|
|
|
get_commit_filtered_statuses,
|
|
|
|
post_commit_status,
|
2024-04-25 11:28:06 +00:00
|
|
|
set_mergeable_check,
|
2024-03-06 11:45:59 +00:00
|
|
|
trigger_mergeable_check,
|
2024-05-18 18:36:04 +00:00
|
|
|
update_upstream_sync_status,
|
2023-04-06 09:40:32 +00:00
|
|
|
)
|
2024-06-04 10:36:13 +00:00
|
|
|
from env_helper import GITHUB_REPOSITORY, GITHUB_UPSTREAM_REPOSITORY
|
2023-04-20 11:55:33 +00:00
|
|
|
from get_robot_token import get_best_robot_token
|
|
|
|
from pr_info import PRInfo
|
2024-06-04 10:36:13 +00:00
|
|
|
from report import FAILURE, PENDING, SUCCESS, StatusType
|
2024-05-18 18:36:04 +00:00
|
|
|
from synchronizer_utils import SYNC_BRANCH_PREFIX
|
2021-09-29 07:47:15 +00:00
|
|
|
|
2021-11-26 14:00:09 +00:00
|
|
|
|
2024-06-13 14:14:57 +00:00
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
description="Script to merge the given PR. Additional checks for approved "
|
|
|
|
"status and green commit statuses could be done",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--wf-status",
|
|
|
|
type=str,
|
|
|
|
default="",
|
|
|
|
help="overall workflow status [success|failure]",
|
|
|
|
)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2023-04-20 11:55:33 +00:00
|
|
|
def main():
|
2021-09-29 07:47:15 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
2024-06-13 14:14:57 +00:00
|
|
|
args = parse_args()
|
2021-09-29 07:47:15 +00:00
|
|
|
|
2024-06-13 14:14:57 +00:00
|
|
|
has_workflow_failures = args.wf_status == FAILURE
|
2024-04-25 11:28:06 +00:00
|
|
|
|
2021-11-26 14:00:09 +00:00
|
|
|
pr_info = PRInfo(need_orgs=True)
|
2022-07-30 05:07:22 +00:00
|
|
|
gh = Github(get_best_robot_token(), per_page=100)
|
2021-09-29 07:47:15 +00:00
|
|
|
commit = get_commit(gh, pr_info.sha)
|
2024-04-25 11:28:06 +00:00
|
|
|
|
|
|
|
if pr_info.is_merge_queue:
|
2024-06-13 14:14:57 +00:00
|
|
|
# in MQ Mergeable check status must never be green if any failures in the workflow
|
|
|
|
if has_workflow_failures:
|
|
|
|
set_mergeable_check(commit, "workflow failed", FAILURE)
|
2024-04-25 11:28:06 +00:00
|
|
|
else:
|
2024-04-25 15:05:02 +00:00
|
|
|
# This must be the only place where green MCheck is set in the MQ (in the end of CI) to avoid early merge
|
2024-06-13 14:14:57 +00:00
|
|
|
set_mergeable_check(commit, "workflow passed", SUCCESS)
|
2024-04-25 11:28:06 +00:00
|
|
|
else:
|
|
|
|
statuses = get_commit_filtered_statuses(commit)
|
2024-05-18 18:36:04 +00:00
|
|
|
state = trigger_mergeable_check(commit, statuses, set_if_green=True)
|
|
|
|
|
|
|
|
# Process upstream StatusNames.SYNC
|
|
|
|
if (
|
|
|
|
pr_info.head_ref.startswith(f"{SYNC_BRANCH_PREFIX}/pr/")
|
|
|
|
and GITHUB_REPOSITORY != GITHUB_UPSTREAM_REPOSITORY
|
|
|
|
):
|
|
|
|
upstream_pr_number = int(pr_info.head_ref.split("/pr/", maxsplit=1)[1])
|
2024-05-20 14:09:17 +00:00
|
|
|
update_upstream_sync_status(
|
|
|
|
upstream_pr_number,
|
|
|
|
pr_info.number,
|
|
|
|
gh,
|
|
|
|
state,
|
|
|
|
can_set_green_mergeable_status=True,
|
|
|
|
)
|
2024-03-06 11:45:59 +00:00
|
|
|
|
2024-05-31 12:15:14 +00:00
|
|
|
ci_running_statuses = [s for s in statuses if s.context == StatusNames.CI]
|
|
|
|
if not ci_running_statuses:
|
2024-04-11 11:58:44 +00:00
|
|
|
return
|
|
|
|
# Take the latest status
|
2024-05-31 12:15:14 +00:00
|
|
|
ci_status = ci_running_statuses[-1]
|
|
|
|
|
|
|
|
has_failure = False
|
|
|
|
has_pending = False
|
2024-06-13 14:14:57 +00:00
|
|
|
error_cnt = 0
|
2024-05-31 12:15:14 +00:00
|
|
|
for status in statuses:
|
|
|
|
if status.context in (StatusNames.MERGEABLE, StatusNames.CI):
|
|
|
|
# do not account these statuses
|
|
|
|
continue
|
|
|
|
if status.state == PENDING:
|
|
|
|
if status.context == StatusNames.SYNC:
|
|
|
|
# do not account sync status if pending - it's a different WF
|
|
|
|
continue
|
|
|
|
has_pending = True
|
|
|
|
elif status.state == SUCCESS:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
has_failure = True
|
2024-06-13 14:14:57 +00:00
|
|
|
error_cnt += 1
|
2024-05-31 12:15:14 +00:00
|
|
|
|
2024-06-04 10:36:13 +00:00
|
|
|
ci_state = SUCCESS # type: StatusType
|
2024-06-13 14:14:57 +00:00
|
|
|
description = "All checks finished"
|
2024-05-31 12:15:14 +00:00
|
|
|
if has_failure:
|
|
|
|
ci_state = FAILURE
|
2024-06-13 14:14:57 +00:00
|
|
|
description = f"All checks finished. {error_cnt} jobs failed"
|
|
|
|
elif has_workflow_failures:
|
|
|
|
ci_state = FAILURE
|
|
|
|
description = "All checks finished. Workflow has failures."
|
2024-05-31 12:15:14 +00:00
|
|
|
elif has_pending:
|
|
|
|
print("ERROR: CI must not have pending jobs by the time of finish check")
|
2024-06-13 14:14:57 +00:00
|
|
|
description = "ERROR: workflow has pending jobs"
|
2024-05-31 12:15:14 +00:00
|
|
|
ci_state = FAILURE
|
|
|
|
|
|
|
|
if ci_status.state == PENDING:
|
2024-04-11 11:58:44 +00:00
|
|
|
post_commit_status(
|
|
|
|
commit,
|
2024-05-31 12:15:14 +00:00
|
|
|
ci_state,
|
|
|
|
ci_status.target_url,
|
2024-06-13 14:14:57 +00:00
|
|
|
description,
|
2024-04-17 13:11:42 +00:00
|
|
|
StatusNames.CI,
|
2024-04-11 11:58:44 +00:00
|
|
|
pr_info,
|
|
|
|
dump_to_file=True,
|
|
|
|
)
|
2023-04-20 11:55:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|