Use the Action token and accept inputs in workflow dispatch (#877)

This commit is contained in:
Glenn Jocher
2026-08-15 23:55:00 +02:00
committed by GitHub
parent 5aeeb81965
commit 37803d64d1
3 changed files with 20 additions and 18 deletions
+1 -1
View File
@@ -28,4 +28,4 @@
# ├── test_summarize_pr.py
# └── ...
__version__ = "0.3.8"
__version__ = "0.3.9"
+8 -5
View File
@@ -35,9 +35,9 @@ def get_pr_branch(event) -> tuple[str, str | None]:
fork_repo = head["repo"]["full_name"]
fork_branch = head["ref"]
base_repo = event.repository
token = os.environ.get("GITHUB_TOKEN")
token = event.token
if not token:
raise ValueError("GITHUB_TOKEN environment variable is not set")
raise ValueError("A GitHub token is required to push the temporary branch")
with tempfile.TemporaryDirectory() as tmp_dir:
repo_dir = os.path.join(tmp_dir, "repo")
@@ -73,9 +73,11 @@ def get_pr_branch(event) -> tuple[str, str | None]:
def trigger_and_get_workflow_info(
event, branch: str, workflow_files: list[str], temp_branch: str | None = None
event, branch: str, workflow_files: list[str], temp_branch: str | None = None, inputs: dict | None = None
) -> list[dict]:
"""Triggers workflows and returns their information, deleting temp branch if provided."""
"""Triggers workflows with optional workflow_dispatch inputs and returns their information, deleting temp branch if
provided.
"""
repo = event.repository
results = []
failed = {}
@@ -84,7 +86,8 @@ def trigger_and_get_workflow_info(
# Trigger all workflows
for file in workflow_files:
response = event.post(
f"{GITHUB_API_URL}/repos/{repo}/actions/workflows/{file}/dispatches", json={"ref": branch}
f"{GITHUB_API_URL}/repos/{repo}/actions/workflows/{file}/dispatches",
json={"ref": branch, **({"inputs": inputs} if inputs else {})},
)
if isinstance(response.status_code, int) and response.status_code != 204:
failed[file] = response.json().get("message", "workflow dispatch failed")
+11 -12
View File
@@ -3,6 +3,8 @@
from datetime import datetime, timezone
from unittest.mock import MagicMock, patch
import pytest
from actions.dispatch_actions import (
RUN_ALL_KEYWORD,
RUN_CI_KEYWORD,
@@ -40,9 +42,8 @@ def test_get_pr_branch_fork():
"base": {"repo": {"id": 1}},
}
with patch("time.time", return_value=1234567.890), patch("subprocess.run") as mock_run, patch(
"os.environ.get", return_value="test-token"
):
mock_event.token = "test-token"
with patch("time.time", return_value=1234567.890), patch("subprocess.run") as mock_run:
branch, temp_branch = get_pr_branch(mock_event)
assert branch == "temp-ci-456-1234567890"
@@ -60,13 +61,9 @@ def test_get_pr_branch_fork_requires_token():
"base": {"repo": {"id": 1}},
}
try:
with patch("os.environ.get", return_value=None):
get_pr_branch(mock_event)
except ValueError as e:
assert str(e) == "GITHUB_TOKEN environment variable is not set"
else:
raise AssertionError("Expected missing token to raise")
mock_event.token = None
with pytest.raises(ValueError, match="token is required"):
get_pr_branch(mock_event)
def test_get_pr_branch_fork_sanitizes_git_errors():
@@ -82,8 +79,9 @@ def test_get_pr_branch_fork_sanitizes_git_errors():
}
error = subprocess.CalledProcessError(128, ["git"], stderr=b"https://x-access-token:[email protected]")
mock_event.token = "test-token"
try:
with patch("subprocess.run", side_effect=error), patch("os.environ.get", return_value="test-token"):
with patch("subprocess.run", side_effect=error):
get_pr_branch(mock_event)
except RuntimeError as e:
assert "test-token" not in str(e)
@@ -123,9 +121,10 @@ def test_trigger_and_get_workflow_info():
# Use patch to skip time.sleep and limit to one workflow
with patch("time.sleep"):
results = trigger_and_get_workflow_info(mock_event, "feature-branch", ["ci.yml"])
results = trigger_and_get_workflow_info(mock_event, "feature-branch", ["ci.yml"], inputs={"tests": "true"})
# Check results
assert mock_event.post.call_args.kwargs["json"] == {"ref": "feature-branch", "inputs": {"tests": "true"}}
assert len(results) == 1
assert results[0]["name"] == "CI Workflow"
assert results[0]["run_number"] == 42