Skip to content

Commit 83749b5

Browse files
committed
Refactor input process for GITHUB_TOKEN in action scripts
1 parent 073d12e commit 83749b5

8 files changed

Lines changed: 163 additions & 30 deletions

File tree

  • .github
    • actions
      • check-prerequisites-and-locate-build-artifact/src
      • create-github-deployment-preview/src
      • create-github-deployment-production/src
      • download-build-artifact/src
      • mark-deployment-in-progress/src
      • update-deployment-status/src
    • workflows
  • test/__mocks__/astro:transitions

.github/actions/check-prerequisites-and-locate-build-artifact/src/main.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ class WorkflowDescriptor:
1616
label: str
1717

1818

19+
def get_input_compat(name: str, *, required: bool = False) -> str:
20+
"""Reads action inputs while tolerating runner normalization.
21+
22+
Unit tests for these actions frequently monkeypatch `core.get_input`.
23+
We prefer `core.get_input` first, then fall back to direct env lookups
24+
that accept both hyphen and underscore variants.
25+
"""
26+
27+
value = core.get_input(name, required=False)
28+
if value:
29+
return value.strip()
30+
31+
normalized = name.replace(" ", "_").upper()
32+
candidates = {normalized, normalized.replace("-", "_"), normalized.replace("_", "-")}
33+
for candidate in candidates:
34+
env_value = os.getenv(f"INPUT_{candidate}", "")
35+
if env_value:
36+
return env_value.strip()
37+
38+
if required:
39+
# Preserve the same error message format as actions_toolkit.
40+
return core.get_input(name, required=True)
41+
return ""
42+
43+
1944
def get_github_api_base_url() -> str:
2045
raw = (os.environ.get("GITHUB_API_URL") or "https://api.github.com").strip()
2146
parsed = urlparse(raw)
@@ -114,17 +139,17 @@ def find_artifact_download_url(
114139

115140
def run() -> None:
116141
try:
117-
token = core.get_input("github-token", required=True)
118-
sha = core.get_input("sha", required=True).strip()
119-
trigger_event = core.get_input("trigger-event")
120-
head_branch = core.get_input("head-branch")
121-
is_fork = (core.get_input("is-fork") or "false").strip().lower() == "true"
122-
skip_hotfix = (core.get_input("skip-hotfix") or "false").strip().lower() == "true"
123-
skip_forks = (core.get_input("skip-forks") or "false").strip().lower() == "true"
124-
require_trigger_event = (core.get_input("require-trigger-event") or "").strip()
125-
build_workflow_file = core.get_input("build-workflow-file", required=True).strip()
126-
artifact_name = core.get_input("artifact-name", required=True).strip()
127-
required_workflows = parse_required_workflows(core.get_input("required-workflows-json", required=True))
142+
token = get_input_compat("github-token", required=True)
143+
sha = get_input_compat("sha", required=True).strip()
144+
trigger_event = get_input_compat("trigger-event")
145+
head_branch = get_input_compat("head-branch")
146+
is_fork = (get_input_compat("is-fork") or "false").strip().lower() == "true"
147+
skip_hotfix = (get_input_compat("skip-hotfix") or "false").strip().lower() == "true"
148+
skip_forks = (get_input_compat("skip-forks") or "false").strip().lower() == "true"
149+
require_trigger_event = (get_input_compat("require-trigger-event") or "").strip()
150+
build_workflow_file = get_input_compat("build-workflow-file", required=True).strip()
151+
artifact_name = get_input_compat("artifact-name", required=True).strip()
152+
required_workflows = parse_required_workflows(get_input_compat("required-workflows-json", required=True))
128153

129154
if require_trigger_event and trigger_event and trigger_event != require_trigger_event:
130155
core.notice(

.github/actions/create-github-deployment-preview/src/main.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
from actions_toolkit import core
88

99

10+
def get_input_compat(name: str, *, required: bool = False) -> str:
11+
value = core.get_input(name, required=False)
12+
if value:
13+
return value.strip()
14+
15+
normalized = name.replace(" ", "_").upper()
16+
candidates = {normalized, normalized.replace("-", "_"), normalized.replace("_", "-")}
17+
for candidate in candidates:
18+
env_value = os.getenv(f"INPUT_{candidate}", "")
19+
if env_value:
20+
return env_value.strip()
21+
22+
if required:
23+
return core.get_input(name, required=True)
24+
return ""
25+
26+
1027
def get_github_api_base_url() -> str:
1128
raw = (os.environ.get("GITHUB_API_URL") or "https://api.github.com").strip()
1229
parsed = urlparse(raw)
@@ -17,8 +34,8 @@ def get_github_api_base_url() -> str:
1734

1835
def run() -> None:
1936
try:
20-
token = core.get_input("github-token", required=True)
21-
sha = core.get_input("sha", required=True).strip()
37+
token = get_input_compat("github-token", required=True)
38+
sha = get_input_compat("sha", required=True).strip()
2239

2340
repo_full = (os.environ.get("GITHUB_REPOSITORY") or "").strip()
2441
if "/" not in repo_full:

.github/actions/create-github-deployment-production/src/main.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
from actions_toolkit import core
88

99

10+
def get_input_compat(name: str, *, required: bool = False) -> str:
11+
value = core.get_input(name, required=False)
12+
if value:
13+
return value.strip()
14+
15+
normalized = name.replace(" ", "_").upper()
16+
candidates = {normalized, normalized.replace("-", "_"), normalized.replace("_", "-")}
17+
for candidate in candidates:
18+
env_value = os.getenv(f"INPUT_{candidate}", "")
19+
if env_value:
20+
return env_value.strip()
21+
22+
if required:
23+
return core.get_input(name, required=True)
24+
return ""
25+
26+
1027
def get_github_api_base_url() -> str:
1128
raw = (os.environ.get("GITHUB_API_URL") or "https://api.github.com").strip()
1229
parsed = urlparse(raw)
@@ -17,8 +34,8 @@ def get_github_api_base_url() -> str:
1734

1835
def run() -> None:
1936
try:
20-
token = core.get_input("github-token", required=True)
21-
sha = core.get_input("sha", required=True).strip()
37+
token = get_input_compat("github-token", required=True)
38+
sha = get_input_compat("sha", required=True).strip()
2239

2340
repo_full = (os.environ.get("GITHUB_REPOSITORY") or "").strip()
2441
if "/" not in repo_full:

.github/actions/download-build-artifact/src/main.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
from actions_toolkit import core
1313

1414

15+
def get_input_compat(name: str, *, required: bool = False) -> str:
16+
value = core.get_input(name, required=False)
17+
if value:
18+
return value.strip()
19+
20+
normalized = name.replace(" ", "_").upper()
21+
candidates = {normalized, normalized.replace("-", "_"), normalized.replace("_", "-")}
22+
for candidate in candidates:
23+
env_value = os.getenv(f"INPUT_{candidate}", "")
24+
if env_value:
25+
return env_value.strip()
26+
27+
if required:
28+
return core.get_input(name, required=True)
29+
return ""
30+
31+
1532
def get_github_api_base_url() -> str:
1633
raw = (os.environ.get("GITHUB_API_URL") or "https://api.github.com").strip()
1734
parsed = urlparse(raw)
@@ -27,8 +44,8 @@ def is_allowed_fetch_url(url: str, allowed_hosts: set[str]) -> bool:
2744

2845
def run() -> None:
2946
try:
30-
token = core.get_input("github-token", required=True)
31-
download_url = core.get_input("artifact-download-url", required=True).strip()
47+
token = get_input_compat("github-token", required=True)
48+
download_url = get_input_compat("artifact-download-url", required=True).strip()
3249

3350
allowed_hosts = {urlparse(get_github_api_base_url()).hostname}
3451
if not is_allowed_fetch_url(download_url, allowed_hosts):

.github/actions/mark-deployment-in-progress/src/main.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
from actions_toolkit import core
88

99

10+
def get_input_compat(name: str, *, required: bool = False) -> str:
11+
value = core.get_input(name, required=False)
12+
if value:
13+
return value.strip()
14+
15+
normalized = name.replace(" ", "_").upper()
16+
candidates = {normalized, normalized.replace("-", "_"), normalized.replace("_", "-")}
17+
for candidate in candidates:
18+
env_value = os.getenv(f"INPUT_{candidate}", "")
19+
if env_value:
20+
return env_value.strip()
21+
22+
if required:
23+
return core.get_input(name, required=True)
24+
return ""
25+
26+
1027
def get_github_api_base_url() -> str:
1128
raw = (os.environ.get("GITHUB_API_URL") or "https://api.github.com").strip()
1229
parsed = urlparse(raw)
@@ -17,9 +34,9 @@ def get_github_api_base_url() -> str:
1734

1835
def run() -> None:
1936
try:
20-
token = core.get_input("github-token", required=True)
21-
deployment_id = core.get_input("deployment-id", required=True).strip()
22-
description = core.get_input("description", required=True).strip()
37+
token = get_input_compat("github-token", required=True)
38+
deployment_id = get_input_compat("deployment-id", required=True).strip()
39+
description = get_input_compat("description", required=True).strip()
2340

2441
repo_full = (os.environ.get("GITHUB_REPOSITORY") or "").strip()
2542
if "/" not in repo_full:

.github/actions/update-deployment-status/src/main.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
from actions_toolkit import core
88

99

10+
def get_input_compat(name: str, *, required: bool = False) -> str:
11+
value = core.get_input(name, required=False)
12+
if value:
13+
return value.strip()
14+
15+
normalized = name.replace(" ", "_").upper()
16+
candidates = {normalized, normalized.replace("-", "_"), normalized.replace("_", "-")}
17+
for candidate in candidates:
18+
env_value = os.getenv(f"INPUT_{candidate}", "")
19+
if env_value:
20+
return env_value.strip()
21+
22+
if required:
23+
return core.get_input(name, required=True)
24+
return ""
25+
26+
1027
def get_github_api_base_url() -> str:
1128
raw = (os.environ.get("GITHUB_API_URL") or "https://api.github.com").strip()
1229
parsed = urlparse(raw)
@@ -17,12 +34,12 @@ def get_github_api_base_url() -> str:
1734

1835
def run() -> None:
1936
try:
20-
token = core.get_input("github-token", required=True)
21-
deployment_id = core.get_input("deployment-id", required=True).strip()
22-
exit_code = int(core.get_input("exit-code", required=True).strip() or "1")
23-
environment_url = (core.get_input("environment-url") or "").strip()
24-
success_description = core.get_input("success-description", required=True).strip()
25-
failure_description = core.get_input("failure-description", required=True).strip()
37+
token = get_input_compat("github-token", required=True)
38+
deployment_id = get_input_compat("deployment-id", required=True).strip()
39+
exit_code = int(get_input_compat("exit-code", required=True).strip() or "1")
40+
environment_url = (get_input_compat("environment-url") or "").strip()
41+
success_description = get_input_compat("success-description", required=True).strip()
42+
failure_description = get_input_compat("failure-description", required=True).strip()
2643

2744
repo_full = (os.environ.get("GITHUB_REPOSITORY") or "").strip()
2845
if "/" not in repo_full:

.github/workflows/cron.yml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ concurrency:
1313
cancel-in-progress: true
1414

1515
jobs:
16-
ping:
16+
ping-production:
1717
name: Ping Turso Production DB
1818
runs-on: ubuntu-latest
19+
environment: production
1920

2021
steps:
2122
- name: Checkout repository
@@ -33,5 +34,29 @@ jobs:
3334
- name: Execute keep-alive query
3435
uses: './.github/actions/keep-alive'
3536
with:
36-
astro-db-remote-url: ${{ secrets.ASTRO_DB_REMOTE_URL }}
37+
astro-db-remote-url: ${{ vars.ASTRO_DB_REMOTE_URL }}
38+
astro-db-app-token: ${{ secrets.ASTRO_DB_APP_TOKEN }}
39+
40+
ping-preview:
41+
name: Ping Turso Preview DB
42+
runs-on: ubuntu-latest
43+
environment: preview
44+
45+
steps:
46+
- name: Checkout repository
47+
uses: actions/checkout@v6.0.1
48+
49+
- name: Setup Python
50+
uses: actions/setup-python@v6.1.0
51+
with:
52+
python-version: '3.13'
53+
cache: 'pip'
54+
55+
- name: Install Python dependencies
56+
run: python3 -m pip install -r requirements.txt
57+
58+
- name: Execute keep-alive query
59+
uses: './.github/actions/keep-alive'
60+
with:
61+
astro-db-remote-url: ${{ vars.ASTRO_DB_REMOTE_URL }}
3762
astro-db-app-token: ${{ secrets.ASTRO_DB_APP_TOKEN }}

test/__mocks__/astro:transitions/client.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ export const navigate = vi.fn().mockImplementation((_url: string | URL) => {
88
// Mock navigation - just resolve immediately
99
return Promise.resolve()
1010
})
11-
12-
export const isTransitionBeforeSwapEvent = vi.fn().mockReturnValue(false)

0 commit comments

Comments
 (0)