Reuse code, do not update Mergeable Check unnecessary

This commit is contained in:
Mikhail f. Shiryaev 2023-02-23 15:25:29 +01:00
parent 0ab9d6787e
commit 067e656ff3
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4
2 changed files with 33 additions and 30 deletions

View File

@ -3,7 +3,7 @@
import csv
import os
import time
from typing import List
from typing import List, Literal
import logging
from ci_config import CI_CONFIG, REQUIRED_CHECKS
@ -15,6 +15,7 @@ from pr_info import PRInfo, SKIP_MERGEABLE_CHECK_LABEL
RETRY = 5
CommitStatuses = List[CommitStatus]
MERGEABLE_NAME = "Mergeable Check"
def override_status(status: str, check_name: str, invert: bool = False) -> str:
@ -102,26 +103,21 @@ def post_labels(gh: Github, pr_info: PRInfo, labels_names: List[str]) -> None:
pull_request.add_to_labels(label)
def fail_mergeable_check(commit: Commit, description: str) -> None:
commit.create_status(
context="Mergeable Check",
description=description,
state="failure",
target_url=GITHUB_RUN_URL,
)
def format_description(description: str) -> str:
if len(description) > 140:
description = description[:137] + "..."
return description
def reset_mergeable_check(commit: Commit, description: str = "") -> None:
def set_mergeable_check(
commit: Commit,
description: str = "",
state: Literal["success", "failure"] = "success",
) -> None:
commit.create_status(
context="Mergeable Check",
context=MERGEABLE_NAME,
description=description,
state="success",
state=state,
target_url=GITHUB_RUN_URL,
)
@ -133,32 +129,39 @@ def update_mergeable_check(gh: Github, pr_info: PRInfo, check_name: str) -> None
logging.info("Update Mergeable Check by %s", check_name)
commit = get_commit(gh, pr_info.sha)
checks = {
check.context: check.state
for check in filter(
lambda check: (check.context in REQUIRED_CHECKS),
# get_statuses() returns generator, which cannot be reversed - we need comprehension
# pylint: disable=unnecessary-comprehension
reversed([status for status in commit.get_statuses()]),
)
}
statuses = get_commit_filtered_statuses(commit)
required_checks = [
status for status in statuses if status.context in REQUIRED_CHECKS
]
mergeable_status = None
for status in statuses:
if status.context == MERGEABLE_NAME:
mergeable_status = status
break
success = []
fail = []
for name, state in checks.items():
if state == "success":
success.append(name)
for status in required_checks:
if status.state == "success":
success.append(status.context)
else:
fail.append(name)
fail.append(status.context)
if fail:
description = "failed: " + ", ".join(fail)
if success:
description += "; succeeded: " + ", ".join(success)
description = format_description(description)
fail_mergeable_check(commit, description)
if mergeable_status is not None and mergeable_status.description == description:
return
set_mergeable_check(commit, description, "failure")
return
description = ", ".join(success)
description = format_description(description)
reset_mergeable_check(commit, description)
if mergeable_status is not None and mergeable_status.description == description:
return
set_mergeable_check(commit, description)

View File

@ -11,7 +11,7 @@ from commit_status_helper import (
get_commit,
post_labels,
remove_labels,
reset_mergeable_check,
set_mergeable_check,
)
from env_helper import GITHUB_RUN_URL, GITHUB_REPOSITORY, GITHUB_SERVER_URL
from get_robot_token import get_best_robot_token
@ -232,7 +232,7 @@ if __name__ == "__main__":
if pr_labels_to_remove:
remove_labels(gh, pr_info, pr_labels_to_remove)
reset_mergeable_check(commit, "skipped")
set_mergeable_check(commit, "skipped")
if description_error:
print(