From 54e4c996d3158c6c98b0b921a85e85b1b37e1b24 Mon Sep 17 00:00:00 2001 From: Florian Heilmann Date: Sun, 31 Dec 2023 12:59:01 +0100 Subject: [PATCH] fix: If parent workflow was skipped, dismiss all CI labels --- .pre-commit-config.yaml | 2 +- voron_toolkit/constants.py | 2 +- voron_toolkit/utils/pr_helper.py | 26 +++++++++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 48ae120..8b9ba39 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,5 +11,5 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.6 hooks: - - id: ruff - id: ruff-format + - id: ruff diff --git a/voron_toolkit/constants.py b/voron_toolkit/constants.py index cfb510d..983c67c 100644 --- a/voron_toolkit/constants.py +++ b/voron_toolkit/constants.py @@ -9,7 +9,7 @@ CI_ERROR_LABEL: str = "Warning: CI Error" READY_FOR_CI_LABEL: str = "Ready for CI" PR_COMMENT_TAG: str = "" -ALL_CI_LABELS: list[str] = [CI_PASSED_LABEL, CI_FAILURE_LABEL, CI_ERROR_LABEL] +ALL_CI_LABELS: list[str] = [CI_PASSED_LABEL, CI_FAILURE_LABEL, CI_ERROR_LABEL, READY_FOR_CI_LABEL] class ExtendedResult(NamedTuple): diff --git a/voron_toolkit/utils/pr_helper.py b/voron_toolkit/utils/pr_helper.py index ab21019..30c02a5 100644 --- a/voron_toolkit/utils/pr_helper.py +++ b/voron_toolkit/utils/pr_helper.py @@ -1,4 +1,3 @@ -import contextlib import os import sys import tempfile @@ -14,7 +13,6 @@ CI_ERROR_LABEL, CI_FAILURE_LABEL, CI_PASSED_LABEL, - READY_FOR_CI_LABEL, VORONUSERS_PR_COMMENT_SECTIONS, ExtendedResultEnum, ItemResult, @@ -158,6 +156,9 @@ def _get_pr_number(self: Self) -> int: sys.exit(255) return int(Path(self.tmp_path, "pr_number.txt").read_text()) + def _check_if_parent_workflow_skipped(self: Self) -> bool: + return bool(Path(self.tmp_path, "ci_skipped.txt").exists()) + def run(self: Self) -> None: logger.info("Downloading artifact '{}' from workflow '{}'", self.artifact_name, self.workflow_run_id) with tempfile.TemporaryDirectory() as tmpdir: @@ -177,8 +178,9 @@ def run(self: Self) -> None: return pr_number: int = self._get_pr_number() + parent_workflow_skipped: bool = self._check_if_parent_workflow_skipped() logger.info("Post Processing PR #{}", pr_number) - if pr_number > 0: + if pr_number > 0 and not parent_workflow_skipped: labels_to_set: set[str] = self._parse_artifact_and_get_labels() labels_on_pr: list[str] = GithubActionHelper.get_labels_on_pull_request( repo=self.github_repository, @@ -188,10 +190,6 @@ def run(self: Self) -> None: updated_labels: list[str] = [*labels_to_set, *labels_to_preserve] - # Make sure to remove the "Ready for CI" label if it is present - with contextlib.suppress(ValueError): - updated_labels.remove(READY_FOR_CI_LABEL) - GithubActionHelper.set_labels_on_pull_request( repo=self.github_repository, pull_request_number=pr_number, @@ -203,6 +201,20 @@ def run(self: Self) -> None: pull_request_number=pr_number, comment_body=pr_comment, ) + else: + logger.info("Workflow {} for PR #{} was skipped. Dismissing all labels!", self.workflow_run_id, pr_number) + GithubActionHelper.set_labels_on_pull_request( + repo=self.github_repository, + pull_request_number=pr_number, + labels=[ + label + for label in GithubActionHelper.get_labels_on_pull_request( + repo=self.github_repository, + pull_request_number=pr_number, + ) + if label not in ALL_CI_LABELS + ], + ) def main() -> None: