Skip to content

Commit 35e002a

Browse files
authored
Fix CI Slack notifications never firing recovery alerts (#65119)
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.
1 parent e5a047c commit 35e002a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

scripts/ci/slack_notification_state.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@
5454

5555
def download_previous_state(artifact_name: str, repo: str) -> dict | None:
5656
"""Download previous notification state artifact from GitHub Actions."""
57+
# `gh api` defaults to POST when -f/-F parameters are passed, which makes
58+
# the artifacts list endpoint return 404. Force GET so the parameters are
59+
# encoded as query string.
5760
result = subprocess.run(
5861
[
5962
"gh",
6063
"api",
64+
"--method",
65+
"GET",
6166
f"repos/{repo}/actions/artifacts",
6267
"-f",
6368
f"name={artifact_name}",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from __future__ import annotations
18+
19+
import importlib.util
20+
import subprocess
21+
import sys
22+
from pathlib import Path
23+
from unittest.mock import patch
24+
25+
import pytest
26+
27+
MODULE_PATH = Path(__file__).resolve().parents[3] / "scripts" / "ci" / "slack_notification_state.py"
28+
29+
30+
@pytest.fixture
31+
def slack_state_module():
32+
module_name = "test_slack_notification_state_module"
33+
sys.modules.pop(module_name, None)
34+
spec = importlib.util.spec_from_file_location(module_name, MODULE_PATH)
35+
assert spec is not None
36+
assert spec.loader is not None
37+
module = importlib.util.module_from_spec(spec)
38+
spec.loader.exec_module(module)
39+
return module
40+
41+
42+
class TestDownloadPreviousState:
43+
def test_uses_explicit_get_method(self, slack_state_module):
44+
"""`gh api` defaults to POST when -f is passed; we must force GET to avoid 404."""
45+
completed = subprocess.CompletedProcess(args=[], returncode=0, stdout="null", stderr="")
46+
with patch.object(subprocess, "run", return_value=completed) as mock_run:
47+
slack_state_module.download_previous_state("artifact", "apache/airflow")
48+
args = mock_run.call_args[0][0]
49+
assert "--method" in args
50+
method_index = args.index("--method")
51+
assert args[method_index + 1] == "GET"
52+
53+
54+
class TestDetermineAction:
55+
def test_no_failures_no_prev_state(self, slack_state_module):
56+
assert slack_state_module.determine_action([], None) == "skip"
57+
58+
def test_no_failures_no_prev_failures(self, slack_state_module):
59+
prev = {"failures": [], "last_notified": "2025-01-01T00:00:00+00:00"}
60+
assert slack_state_module.determine_action([], prev) == "skip"
61+
62+
def test_recovery_when_clear_after_failures(self, slack_state_module):
63+
prev = {"failures": ["job-a"], "last_notified": "2025-01-01T00:00:00+00:00"}
64+
assert slack_state_module.determine_action([], prev) == "notify_recovery"
65+
66+
def test_notify_new_on_first_failure(self, slack_state_module):
67+
assert slack_state_module.determine_action(["job-a"], None) == "notify_new"
68+
69+
def test_notify_new_on_changed_failures(self, slack_state_module):
70+
prev = {"failures": ["job-a"], "last_notified": "2025-01-01T00:00:00+00:00"}
71+
assert slack_state_module.determine_action(["job-b"], prev) == "notify_new"

0 commit comments

Comments
 (0)