diff --git a/.github/scripts/check_labels.py b/.github/scripts/check_labels.py index a29ef24a414c54..10be42c3fd5647 100755 --- a/.github/scripts/check_labels.py +++ b/.github/scripts/check_labels.py @@ -27,6 +27,12 @@ def parse_args() -> Any: parser = ArgumentParser("Check PR labels") parser.add_argument("pr_num", type=int) + # add a flag to return a non-zero exit code if the PR does not have the required labels + parser.add_argument( + "--exit-non-zero", + action="store_true", + help="Return a non-zero exit code if the PR does not have the required labels", + ) return parser.parse_args() @@ -41,10 +47,13 @@ def main() -> None: if not has_required_labels(pr): print(LABEL_ERR_MSG) add_label_err_comment(pr) + if args.exit_non_zero: + sys.exit(1) else: delete_all_label_err_comments(pr) except Exception as e: - pass + if args.exit_non_zero: + sys.exit(1) sys.exit(0) diff --git a/.github/scripts/test_check_labels.py b/.github/scripts/test_check_labels.py index 2b2cd7b6c5204b..1c921f2eafa9a3 100644 --- a/.github/scripts/test_check_labels.py +++ b/.github/scripts/test_check_labels.py @@ -18,6 +18,7 @@ def mock_parse_args() -> object: class Object: def __init__(self) -> None: self.pr_num = 76123 + self.exit_non_zero = False return Object() diff --git a/.github/workflows/check-labels.yml b/.github/workflows/check-labels.yml index d638d588504f2e..8ad611bd7cc917 100644 --- a/.github/workflows/check-labels.yml +++ b/.github/workflows/check-labels.yml @@ -19,6 +19,10 @@ on: branches: [gh/**/base] workflow_dispatch: + inputs: + pr_number: + description: 'PR number to check labels for' + required: true concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }} @@ -54,7 +58,7 @@ jobs: - name: Check labels env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUM: ${{ github.event.number }} + PR_NUM: ${{ github.event.number || github.event.inputs.pr_number }} run: | set -ex - python3 .github/scripts/check_labels.py "${PR_NUM}" + python3 .github/scripts/check_labels.py --exit-non-zero "${PR_NUM}"