Skip to content

Commit e2852e0

Browse files
committed
fix(presets,workflows): pass auth.json github hosts into remaining release resolvers
Wires preset add --from and workflow add through github_provider_hosts() so private GHES release assets resolve via /api/v3 there too. github#3147 Assisted-by: Claude Code (model: claude-sonnet-4-6, autonomous)
1 parent 48b86b2 commit e2852e0

4 files changed

Lines changed: 202 additions & 3 deletions

File tree

‎src/specify_cli/__init__.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,9 +1128,10 @@ def _validate_and_install_local(yaml_path: Path, source_label: str) -> None:
11281128
raise typer.Exit(1)
11291129

11301130
from specify_cli._github_http import resolve_github_release_asset_api_url as _resolve_gh_asset
1131+
from specify_cli.authentication.http import github_provider_hosts
11311132

11321133
_wf_url_extra_headers = None
1133-
_resolved_wf_url = _resolve_gh_asset(source, _open_url, timeout=30)
1134+
_resolved_wf_url = _resolve_gh_asset(source, _open_url, timeout=30, github_hosts=github_provider_hosts())
11341135
if _resolved_wf_url:
11351136
source = _resolved_wf_url
11361137
_wf_url_extra_headers = {"Accept": "application/octet-stream"}
@@ -1234,10 +1235,11 @@ def _validate_and_install_local(yaml_path: Path, source_label: str) -> None:
12341235

12351236
try:
12361237
from specify_cli.authentication.http import open_url as _open_url
1238+
from specify_cli.authentication.http import github_provider_hosts
12371239
from specify_cli._github_http import resolve_github_release_asset_api_url as _resolve_gh_asset
12381240

12391241
_wf_cat_extra_headers = None
1240-
_resolved_workflow_url = _resolve_gh_asset(workflow_url, _open_url, timeout=30)
1242+
_resolved_workflow_url = _resolve_gh_asset(workflow_url, _open_url, timeout=30, github_hosts=github_provider_hosts())
12411243
if _resolved_workflow_url:
12421244
workflow_url = _resolved_workflow_url
12431245
_wf_cat_extra_headers = {"Accept": "application/octet-stream"}

‎src/specify_cli/presets/_commands.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,13 @@ def _validate_download_redirect(old_url, new_url):
144144
zip_path = Path(tmpdir) / "preset.zip"
145145
try:
146146
from specify_cli.authentication.http import open_url as _open_url
147+
from specify_cli.authentication.http import github_provider_hosts
147148
from specify_cli._github_http import resolve_github_release_asset_api_url
148149

149150
_preset_extra_headers = None
150-
_resolved_from_url = resolve_github_release_asset_api_url(from_url, _open_url)
151+
_resolved_from_url = resolve_github_release_asset_api_url(
152+
from_url, _open_url, github_hosts=github_provider_hosts()
153+
)
151154
if _resolved_from_url:
152155
from_url = _resolved_from_url
153156
_preset_extra_headers = {"Accept": "application/octet-stream"}

‎tests/test_presets.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4754,6 +4754,69 @@ def fake_open_url(url, timeout=None, extra_headers=None, redirect_validator=None
47544754
assert captured_urls[0][0] == "https://api.github.com/repos/org/repo/releases/assets/42"
47554755
assert captured_urls[0][1] == {"Accept": "application/octet-stream"}
47564756

4757+
def test_preset_add_from_ghes_release_url_resolves_via_api_v3(self, project_dir, monkeypatch):
4758+
"""'preset add --from <ghes-release-url>' resolves via GHES /api/v3 endpoint."""
4759+
from typer.testing import CliRunner
4760+
from unittest.mock import patch
4761+
from specify_cli import app
4762+
from specify_cli.authentication import http as _auth_http
4763+
from specify_cli.authentication.config import AuthConfigEntry
4764+
4765+
monkeypatch.setattr(_auth_http, "_config_override", [
4766+
AuthConfigEntry(hosts=("ghes.example",), provider="github", auth="bearer", token="t"),
4767+
])
4768+
4769+
manifest_content = yaml.dump({
4770+
"schema_version": "1.0",
4771+
"preset": {"id": "my-preset", "name": "My Preset", "version": "1.0.0", "description": "Test preset", "author": "Test", "license": "MIT"},
4772+
"requires": {"speckit_version": ">=0.1.0"},
4773+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
4774+
})
4775+
zip_buf = __import__("io").BytesIO()
4776+
with zipfile.ZipFile(zip_buf, "w") as zf:
4777+
zf.writestr("preset.yml", manifest_content)
4778+
zip_bytes = zip_buf.getvalue()
4779+
4780+
captured_urls = []
4781+
4782+
class FakeResponse:
4783+
def __init__(self, data):
4784+
self._data = data
4785+
4786+
def read(self):
4787+
return self._data
4788+
4789+
def __enter__(self):
4790+
return self
4791+
4792+
def __exit__(self, *a):
4793+
return False
4794+
4795+
def fake_open_url(url, timeout=None, extra_headers=None, redirect_validator=None):
4796+
captured_urls.append((url, extra_headers))
4797+
if "releases/tags/" in url:
4798+
return FakeResponse(json.dumps({
4799+
"assets": [{"name": "preset.zip", "url": "https://ghes.example/api/v3/repos/org/repo/releases/assets/42"}]
4800+
}).encode())
4801+
return FakeResponse(zip_bytes)
4802+
4803+
runner = CliRunner()
4804+
with patch.object(Path, "cwd", return_value=project_dir), \
4805+
patch("specify_cli.get_speckit_version", return_value="1.0.0"), \
4806+
patch("specify_cli.authentication.http.open_url", side_effect=fake_open_url):
4807+
result = runner.invoke(app, [
4808+
"preset", "add",
4809+
"--from", "https://ghes.example/org/repo/releases/download/v1.0/preset.zip",
4810+
])
4811+
4812+
assert result.exit_code == 0, result.output
4813+
# The tag-lookup call must use the GHES /api/v3 endpoint
4814+
assert any("ghes.example/api/v3/repos/org/repo/releases/tags/v1.0" in url for url, _ in captured_urls)
4815+
# The asset download call must carry Accept: application/octet-stream
4816+
asset_calls = [(url, h) for url, h in captured_urls if "releases/assets/" in url]
4817+
assert len(asset_calls) >= 1
4818+
assert asset_calls[0][1] == {"Accept": "application/octet-stream"}
4819+
47574820

47584821
class TestWrapStrategy:
47594822
"""Tests for strategy: wrap preset command substitution."""

‎tests/test_workflows.py‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5477,6 +5477,137 @@ def fake_open_url(url, timeout=None, extra_headers=None):
54775477
assert len(asset_calls) >= 1
54785478
assert asset_calls[0][1] == {"Accept": "application/octet-stream"}
54795479

5480+
def test_workflow_add_from_ghes_release_url_resolves_via_api_v3(self, project_dir, monkeypatch):
5481+
"""'workflow add <ghes-release-url>' resolves via GHES /api/v3 endpoint."""
5482+
from typer.testing import CliRunner
5483+
from unittest.mock import patch
5484+
from specify_cli import app
5485+
from specify_cli.authentication import http as _auth_http
5486+
from specify_cli.authentication.config import AuthConfigEntry
5487+
5488+
monkeypatch.setattr(_auth_http, "_config_override", [
5489+
AuthConfigEntry(hosts=("ghes.example",), provider="github", auth="bearer", token="t"),
5490+
])
5491+
5492+
captured_urls = []
5493+
5494+
class FakeResponse:
5495+
def __init__(self, data, url=None):
5496+
self._data = data
5497+
self._url = url or "https://ghes.example/api/v3/repos/org/repo/releases/assets/42"
5498+
5499+
def read(self):
5500+
return self._data
5501+
5502+
def geturl(self):
5503+
return self._url
5504+
5505+
def __enter__(self):
5506+
return self
5507+
5508+
def __exit__(self, *a):
5509+
return False
5510+
5511+
def fake_open_url(url, timeout=None, extra_headers=None):
5512+
captured_urls.append((url, extra_headers))
5513+
if "releases/tags/" in url:
5514+
return FakeResponse(json.dumps({
5515+
"assets": [{"name": "workflow.yml", "url": "https://ghes.example/api/v3/repos/org/repo/releases/assets/42"}]
5516+
}).encode())
5517+
return FakeResponse(self.VALID_WORKFLOW_YAML.encode())
5518+
5519+
runner = CliRunner()
5520+
with patch.object(Path, "cwd", return_value=project_dir), \
5521+
patch("specify_cli.authentication.http.open_url", side_effect=fake_open_url):
5522+
result = runner.invoke(app, [
5523+
"workflow", "add",
5524+
"https://ghes.example/org/repo/releases/download/v1.0/workflow.yml",
5525+
])
5526+
5527+
assert result.exit_code == 0, result.output
5528+
# Tag lookup must use the GHES /api/v3 endpoint
5529+
assert any("ghes.example/api/v3/repos/org/repo/releases/tags/v1.0" in url for url, _ in captured_urls)
5530+
# Asset download must carry Accept: application/octet-stream
5531+
asset_calls = [(url, h) for url, h in captured_urls if "releases/assets/" in url]
5532+
assert len(asset_calls) >= 1
5533+
assert asset_calls[0][1] == {"Accept": "application/octet-stream"}
5534+
5535+
def test_workflow_add_catalog_based_ghes_release_url_resolves_via_api_v3(self, project_dir, monkeypatch):
5536+
"""'workflow add <id>' with a GHES catalog URL resolves via /api/v3."""
5537+
from typer.testing import CliRunner
5538+
from unittest.mock import patch
5539+
from specify_cli import app
5540+
from specify_cli.authentication import http as _auth_http
5541+
from specify_cli.authentication.config import AuthConfigEntry
5542+
5543+
monkeypatch.setattr(_auth_http, "_config_override", [
5544+
AuthConfigEntry(hosts=("ghes.example",), provider="github", auth="bearer", token="t"),
5545+
])
5546+
5547+
captured_urls = []
5548+
5549+
class FakeResponse:
5550+
def __init__(self, data, url=None):
5551+
self._data = data
5552+
self._url = url or "https://ghes.example/api/v3/repos/org/repo/releases/assets/55"
5553+
5554+
def read(self):
5555+
return self._data
5556+
5557+
def geturl(self):
5558+
return self._url
5559+
5560+
def __enter__(self):
5561+
return self
5562+
5563+
def __exit__(self, *a):
5564+
return False
5565+
5566+
ghes_wf_yaml = """
5567+
schema_version: "1.0"
5568+
workflow:
5569+
id: "my-wf"
5570+
name: "My GHES Workflow"
5571+
version: "1.0.0"
5572+
description: "A GHES catalog workflow"
5573+
steps:
5574+
- id: step-one
5575+
type: shell
5576+
run: "echo hello"
5577+
"""
5578+
5579+
def fake_open_url(url, timeout=None, extra_headers=None):
5580+
captured_urls.append((url, extra_headers))
5581+
if "releases/tags/" in url:
5582+
return FakeResponse(json.dumps({
5583+
"assets": [{"name": "workflow.yml", "url": "https://ghes.example/api/v3/repos/org/repo/releases/assets/55"}]
5584+
}).encode())
5585+
return FakeResponse(ghes_wf_yaml.encode())
5586+
5587+
fake_catalog_info = {
5588+
"id": "my-wf",
5589+
"name": "My GHES Workflow",
5590+
"version": "1.0.0",
5591+
"url": "https://ghes.example/org/repo/releases/download/v2.0/workflow.yml",
5592+
"_install_allowed": True,
5593+
}
5594+
5595+
runner = CliRunner()
5596+
with patch.object(Path, "cwd", return_value=project_dir), \
5597+
patch("specify_cli.authentication.http.open_url", side_effect=fake_open_url), \
5598+
patch("specify_cli.workflows.catalog.WorkflowCatalog.get_workflow_info", return_value=fake_catalog_info):
5599+
result = runner.invoke(app, ["workflow", "add", "my-wf"])
5600+
5601+
assert result.exit_code == 0, result.output
5602+
# Tag lookup must use GHES /api/v3
5603+
tag_calls = [url for url, _ in captured_urls if "releases/tags/" in url]
5604+
assert len(tag_calls) == 1
5605+
assert "ghes.example/api/v3/repos/org/repo/releases/tags/v2.0" in tag_calls[0]
5606+
# Asset download must carry Accept: application/octet-stream
5607+
asset_calls = [(url, h) for url, h in captured_urls if "releases/assets/" in url]
5608+
assert len(asset_calls) >= 1
5609+
assert asset_calls[0][1] == {"Accept": "application/octet-stream"}
5610+
54805611

54815612
class TestWorkflowRunExitCodes:
54825613
"""CLI-level tests for the run/resume process exit codes."""

0 commit comments

Comments
 (0)