Skip to content

Commit d02102e

Browse files
Merge pull request #46 from cbusillo/fix/publish-source-token-env
Support source token env for artifact selectors
2 parents d85d062 + ab1361f commit d02102e

3 files changed

Lines changed: 62 additions & 13 deletions

File tree

docs/tooling/workspace-cli.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,11 @@ Notes
249249
- Publish-time GHCR credentials can be split by purpose. Private base image
250250
reads prefer `GHCR_READ_TOKEN`, artifact image pushes prefer `GHCR_TOKEN`,
251251
and private source checkout secrets still belong in the transient runtime
252-
payload as `GITHUB_TOKEN`. This lets CI use a repo-scoped package-write token
253-
for the tenant artifact while using a separate read token for shared private
254-
base images.
252+
payload as `GITHUB_TOKEN`. For selector resolution before the build context
253+
exists, CI can also provide `ODOO_DEVKIT_SOURCE_GITHUB_TOKEN` or
254+
`ODOO_SOURCE_GITHUB_TOKEN`. This lets CI use a repo-scoped package-write
255+
token for the tenant artifact while using separate credentials for shared
256+
private source repositories and private base images.
255257
- When a repo-owned `artifact-inputs.toml` lives beside `workspace.toml`,
256258
`platform runtime` commands use it as the repo-owned source-input contract.
257259
Runtime and publish do not fall back to `stack.toml` source selector fields.

odoo_devkit/local_runtime.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
DEFAULT_ARTIFACT_IMAGE_PLATFORMS = ("linux/amd64", "linux/arm64")
3131
GIT_SHA_PATTERN = re.compile(r"[0-9a-fA-F]{7,40}")
3232
ARTIFACT_SOURCE_ENV_KEYS = ("ODOO_ADDON_REPOSITORIES", "OPENUPGRADE_ADDON_REPOSITORY")
33+
SOURCE_GITHUB_TOKEN_ENV_KEYS = ("ODOO_DEVKIT_SOURCE_GITHUB_TOKEN", "ODOO_SOURCE_GITHUB_TOKEN")
3334
RUNTIME_ENVIRONMENT_PAYLOAD_ENV_VAR = "ODOO_DEVKIT_RUNTIME_ENVIRONMENT_JSON"
3435
ODOO_INSTANCE_OVERRIDES_PAYLOAD_ENV_KEY = "ODOO_INSTANCE_OVERRIDES_PAYLOAD_B64"
3536
LEGACY_CONFIG_PARAM_PREFIX = "ENV_OVERRIDE_CONFIG_PARAM__"
@@ -2087,9 +2088,7 @@ def resolve_artifact_runtime_source_repository_refs(
20872088
runtime_values: dict[str, str],
20882089
) -> tuple[dict[str, str], tuple[dict[str, str], ...]]:
20892090
resolved_values = dict(runtime_values)
2090-
github_token = clean_optional_value(runtime_values.get("GITHUB_TOKEN")) or first_clean_optional_value(
2091-
(os.environ.get("GITHUB_TOKEN"), os.environ.get("GH_TOKEN"))
2092-
)
2091+
github_token = resolve_source_github_token(runtime_values)
20932092
selector_metadata: list[dict[str, str]] = []
20942093
for env_key in ARTIFACT_SOURCE_ENV_KEYS:
20952094
raw_value = runtime_values.get(env_key, "")
@@ -2117,6 +2116,12 @@ def resolve_artifact_runtime_source_repository_refs(
21172116
return resolved_values, tuple(selector_metadata)
21182117

21192118

2119+
def resolve_source_github_token(runtime_values: dict[str, str]) -> str | None:
2120+
return clean_optional_value(runtime_values.get("GITHUB_TOKEN")) or first_clean_optional_value(
2121+
os.environ.get(environment_key) for environment_key in (*SOURCE_GITHUB_TOKEN_ENV_KEYS, "GITHUB_TOKEN", "GH_TOKEN")
2122+
)
2123+
2124+
21202125
def resolve_source_repository_ref_to_git_sha(*, repository: str, ref: str, github_token: str | None = None) -> str:
21212126
normalized_repository = repository.strip()
21222127
normalized_ref = ref.strip()

tests/test_runtime.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,10 +1619,8 @@ def test_resolve_artifact_runtime_source_refs_uses_environment_token_fallback(se
16191619
"odoo_devkit.local_runtime.resolve_source_repository_ref_to_git_sha",
16201620
return_value="411f6b8e85cac72dc7aa2e2dc5540001043c327d",
16211621
) as resolve_ref_mock:
1622-
resolved_values, selector_metadata = (
1623-
local_runtime.resolve_artifact_runtime_source_repository_refs(
1624-
runtime_values=runtime_values
1625-
)
1622+
resolved_values, selector_metadata = local_runtime.resolve_artifact_runtime_source_repository_refs(
1623+
runtime_values=runtime_values
16261624
)
16271625

16281626
resolve_ref_mock.assert_called_once_with(
@@ -1645,6 +1643,52 @@ def test_resolve_artifact_runtime_source_refs_uses_environment_token_fallback(se
16451643
),
16461644
)
16471645

1646+
def test_resolve_artifact_runtime_source_refs_uses_dedicated_source_token_env(self) -> None:
1647+
runtime_values = {
1648+
"ODOO_ADDON_REPOSITORIES": "cbusillo/disable_odoo_online@main",
1649+
}
1650+
with mock.patch.dict(
1651+
os.environ,
1652+
{
1653+
"ODOO_DEVKIT_SOURCE_GITHUB_TOKEN": "source-env-token",
1654+
"GITHUB_TOKEN": "github-env-token",
1655+
},
1656+
):
1657+
with mock.patch(
1658+
"odoo_devkit.local_runtime.resolve_source_repository_ref_to_git_sha",
1659+
return_value="411f6b8e85cac72dc7aa2e2dc5540001043c327d",
1660+
) as resolve_ref_mock:
1661+
local_runtime.resolve_artifact_runtime_source_repository_refs(runtime_values=runtime_values)
1662+
1663+
resolve_ref_mock.assert_called_once_with(
1664+
repository="cbusillo/disable_odoo_online",
1665+
ref="main",
1666+
github_token="source-env-token",
1667+
)
1668+
1669+
def test_resolve_artifact_runtime_source_refs_supports_ci_source_token_env(self) -> None:
1670+
runtime_values = {
1671+
"ODOO_ADDON_REPOSITORIES": "cbusillo/disable_odoo_online@main",
1672+
}
1673+
with mock.patch.dict(
1674+
os.environ,
1675+
{
1676+
"ODOO_SOURCE_GITHUB_TOKEN": "ci-source-token",
1677+
"GITHUB_TOKEN": "github-env-token",
1678+
},
1679+
):
1680+
with mock.patch(
1681+
"odoo_devkit.local_runtime.resolve_source_repository_ref_to_git_sha",
1682+
return_value="411f6b8e85cac72dc7aa2e2dc5540001043c327d",
1683+
) as resolve_ref_mock:
1684+
local_runtime.resolve_artifact_runtime_source_repository_refs(runtime_values=runtime_values)
1685+
1686+
resolve_ref_mock.assert_called_once_with(
1687+
repository="cbusillo/disable_odoo_online",
1688+
ref="main",
1689+
github_token="ci-source-token",
1690+
)
1691+
16481692
def test_resolve_artifact_runtime_source_refs_prefers_runtime_github_token(self) -> None:
16491693
runtime_values = {
16501694
"GITHUB_TOKEN": "source-token",
@@ -1655,9 +1699,7 @@ def test_resolve_artifact_runtime_source_refs_prefers_runtime_github_token(self)
16551699
"odoo_devkit.local_runtime.resolve_source_repository_ref_to_git_sha",
16561700
return_value="411f6b8e85cac72dc7aa2e2dc5540001043c327d",
16571701
) as resolve_ref_mock:
1658-
local_runtime.resolve_artifact_runtime_source_repository_refs(
1659-
runtime_values=runtime_values
1660-
)
1702+
local_runtime.resolve_artifact_runtime_source_repository_refs(runtime_values=runtime_values)
16611703

16621704
resolve_ref_mock.assert_called_once_with(
16631705
repository="cbusillo/disable_odoo_online",

0 commit comments

Comments
 (0)