From 4a89926f8427188ea9e25678f11ebd4b50b028ec Mon Sep 17 00:00:00 2001 From: Jarek Potiuk Date: Mon, 13 Apr 2026 02:54:12 +0200 Subject: [PATCH] Fix CI Slack notifications never firing recovery alerts The artifact lookup for the previous notification state was using 'gh api' with -f parameters, which makes the CLI default to POST instead of GET. The artifacts endpoint returns 404 on POST, so the script always treated each run as the first one with no prior state. That meant the 'all tests passing' recovery notification could never fire, since it requires the previous state to contain failures. Force --method GET on the artifact lookup and add unit tests covering both the GET requirement and the determine_action state machine. --- scripts/ci/slack_notification_state.py | 5 ++ .../tests/ci/test_slack_notification_state.py | 71 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 scripts/tests/ci/test_slack_notification_state.py diff --git a/scripts/ci/slack_notification_state.py b/scripts/ci/slack_notification_state.py index f6dc0a035c234..3bedf4545b5e4 100644 --- a/scripts/ci/slack_notification_state.py +++ b/scripts/ci/slack_notification_state.py @@ -54,10 +54,15 @@ def download_previous_state(artifact_name: str, repo: str) -> dict | None: """Download previous notification state artifact from GitHub Actions.""" + # `gh api` defaults to POST when -f/-F parameters are passed, which makes + # the artifacts list endpoint return 404. Force GET so the parameters are + # encoded as query string. result = subprocess.run( [ "gh", "api", + "--method", + "GET", f"repos/{repo}/actions/artifacts", "-f", f"name={artifact_name}", diff --git a/scripts/tests/ci/test_slack_notification_state.py b/scripts/tests/ci/test_slack_notification_state.py new file mode 100644 index 0000000000000..6400dd1fdf4c5 --- /dev/null +++ b/scripts/tests/ci/test_slack_notification_state.py @@ -0,0 +1,71 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import importlib.util +import subprocess +import sys +from pathlib import Path +from unittest.mock import patch + +import pytest + +MODULE_PATH = Path(__file__).resolve().parents[3] / "scripts" / "ci" / "slack_notification_state.py" + + +@pytest.fixture +def slack_state_module(): + module_name = "test_slack_notification_state_module" + sys.modules.pop(module_name, None) + spec = importlib.util.spec_from_file_location(module_name, MODULE_PATH) + assert spec is not None + assert spec.loader is not None + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +class TestDownloadPreviousState: + def test_uses_explicit_get_method(self, slack_state_module): + """`gh api` defaults to POST when -f is passed; we must force GET to avoid 404.""" + completed = subprocess.CompletedProcess(args=[], returncode=0, stdout="null", stderr="") + with patch.object(subprocess, "run", return_value=completed) as mock_run: + slack_state_module.download_previous_state("artifact", "apache/airflow") + args = mock_run.call_args[0][0] + assert "--method" in args + method_index = args.index("--method") + assert args[method_index + 1] == "GET" + + +class TestDetermineAction: + def test_no_failures_no_prev_state(self, slack_state_module): + assert slack_state_module.determine_action([], None) == "skip" + + def test_no_failures_no_prev_failures(self, slack_state_module): + prev = {"failures": [], "last_notified": "2025-01-01T00:00:00+00:00"} + assert slack_state_module.determine_action([], prev) == "skip" + + def test_recovery_when_clear_after_failures(self, slack_state_module): + prev = {"failures": ["job-a"], "last_notified": "2025-01-01T00:00:00+00:00"} + assert slack_state_module.determine_action([], prev) == "notify_recovery" + + def test_notify_new_on_first_failure(self, slack_state_module): + assert slack_state_module.determine_action(["job-a"], None) == "notify_new" + + def test_notify_new_on_changed_failures(self, slack_state_module): + prev = {"failures": ["job-a"], "last_notified": "2025-01-01T00:00:00+00:00"} + assert slack_state_module.determine_action(["job-b"], prev) == "notify_new"