diff --git a/.github/workflows/upstream-refresh.yml b/.github/workflows/upstream-refresh.yml index 1d071e1..a79b831 100644 --- a/.github/workflows/upstream-refresh.yml +++ b/.github/workflows/upstream-refresh.yml @@ -22,15 +22,15 @@ jobs: steps: - name: Validate GitHub App configuration env: - WORKFLOW_APP_CLIENT_ID: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} - WORKFLOW_APP_PRIVATE_KEY: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} + WORKFLOW_APP_CLIENT_ID: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} + WORKFLOW_APP_PRIVATE_KEY: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} run: | if [ -z "$WORKFLOW_APP_CLIENT_ID" ]; then - echo "::error::LIBRECODE_WORKFLOW_APP_CLIENT_ID is not configured." + echo "::error::WORKFLOW_SYNC_APP_CLIENT_ID is not configured." exit 1 fi if [ -z "$WORKFLOW_APP_PRIVATE_KEY" ]; then - echo "::error::LIBRECODE_WORKFLOW_APP_PRIVATE_KEY is not configured." + echo "::error::WORKFLOW_SYNC_APP_PRIVATE_KEY is not configured." exit 1 fi @@ -38,8 +38,8 @@ jobs: id: app-token uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: - client-id: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} - private-key: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} + client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} + private-key: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} owner: LibreCodeCoop repositories: .github permission-contents: write diff --git a/docs/cross-repository-automation.md b/docs/cross-repository-automation.md index a2b825e..78bf13c 100644 --- a/docs/cross-repository-automation.md +++ b/docs/cross-repository-automation.md @@ -26,16 +26,21 @@ The updater manages workflows already installed in the consumer repository. It r ## Authentication -The installed updater selects authentication with the repository variable `WORKFLOW_SYNC_AUTH_MODE`: +The workflow uses organization-neutral settings; each consumer can select its +own credentials. Configure `WORKFLOW_SYNC_AUTH_MODE` as: -- `librecode-app` — LibreCode-managed repositories; uses `LIBRECODE_WORKFLOW_APP_CLIENT_ID` and `LIBRECODE_WORKFLOW_APP_PRIVATE_KEY`. -- `github-app` — independent consumers; uses `WORKFLOW_SYNC_APP_CLIENT_ID` and `WORKFLOW_SYNC_APP_PRIVATE_KEY`. -- `token` — uses `WORKFLOW_SYNC_TOKEN`. -- `github-token` — uses the workflow `GITHUB_TOKEN`. +- `github-app` (default) — `WORKFLOW_SYNC_APP_CLIENT_ID` (repository variable) + and `WORKFLOW_SYNC_APP_PRIVATE_KEY` (Actions secret). +- `token` — `WORKFLOW_SYNC_TOKEN` (Actions secret). +- `github-token` — the built-in `GITHUB_TOKEN`. -For independent projects, prefer a consumer-owned GitHub App. The updater needs repository permissions **Contents: write**, **Pull requests: write**, and **Workflows: write** because it updates files under `.github/workflows`. +The `github-app` mode requires the complete variable/secret pair; it fails +before any write when either value is missing. Use a consumer-owned GitHub App +with **Contents: write**, **Pull requests: write**, and **Workflows: write** +permissions to update files under `.github/workflows`. -The `github-token` mode is explicit because events created with `GITHUB_TOKEN` may not trigger the repository's normal pull-request automation. +The `github-token` mode is explicit because events emitted using +`GITHUB_TOKEN` may not trigger normal pull-request automation. ## Install and update workflows @@ -87,12 +92,12 @@ Generated or experimental workflows should not be added until they are ready for For the npm audit workflow, consumers need a matching local patch and configured GitHub App credentials before enrollment. -## LibreCode catalog credentials +## Catalog credentials Catalog publication uses the **LibreCode Workflow Automation** GitHub App. Its credentials are stored in `LibreCodeCoop/.github`: -- variable `LIBRECODE_WORKFLOW_APP_CLIENT_ID` (GitHub App Client ID); -- secret `LIBRECODE_WORKFLOW_APP_PRIVATE_KEY`. +- variable `WORKFLOW_SYNC_APP_CLIENT_ID` (GitHub App Client ID); +- secret `WORKFLOW_SYNC_APP_PRIVATE_KEY`. Write-capable jobs mint short-lived installation tokens scoped to the destination repository. The private key must never be committed. diff --git a/patches/nextcloud/npm-audit-fix.yml.patch b/patches/nextcloud/npm-audit-fix.yml.patch index 0ce3480..dd1bcd4 100644 --- a/patches/nextcloud/npm-audit-fix.yml.patch +++ b/patches/nextcloud/npm-audit-fix.yml.patch @@ -113,8 +113,8 @@ + if: steps.checkout.outcome == 'success' + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: -+ client-id: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} -+ private-key: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} ++ client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} ++ private-key: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + repositories: ${{ github.event.repository.name }} + permission-contents: write diff --git a/patches/nextcloud/sync-workflow-templates.yml.patch b/patches/nextcloud/sync-workflow-templates.yml.patch index 8081e2b..6bd3752 100644 --- a/patches/nextcloud/sync-workflow-templates.yml.patch +++ b/patches/nextcloud/sync-workflow-templates.yml.patch @@ -1,6 +1,6 @@ --- upstream/vendor/nextcloud/sync-workflow-templates.yml +++ workflow-templates/sync-workflow-templates.yml -@@ -1,13 +1,16 @@ +@@ -1,18 +1,21 @@ # This workflow is provided via the organization template repository # +# https://github.com/LibreCodeCoop/.github @@ -20,11 +20,12 @@ name: Update workflows on: workflow_dispatch: -@@ -14,3 +17,3 @@ schedule: -- - cron: "45 22 * * 6" + - cron: "5 2 * * 0" +- - cron: "45 22 * * 6" + permissions: + contents: read @@ -26,9 +29,6 @@ matrix: branches: @@ -35,8 +36,13 @@ name: Update workflows in ${{ matrix.branches }} -@@ -38,16 +38,123 @@ +@@ -36,18 +36,103 @@ + contents: write + pull-requests: write ++ env: ++ AUTH_MODE: ${{ vars.WORKFLOW_SYNC_AUTH_MODE || 'github-app' }} ++ steps: - name: Check actor permission + if: ${{ github.event_name == 'workflow_dispatch' }} @@ -54,24 +60,15 @@ + - name: Validate workflow sync authentication + shell: bash + env: -+ AUTH_MODE: ${{ vars.WORKFLOW_SYNC_AUTH_MODE || 'librecode-app' }} -+ LIBRECODE_APP_CLIENT_ID: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} -+ LIBRECODE_APP_PRIVATE_KEY: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} -+ CONSUMER_APP_CLIENT_ID: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} -+ CONSUMER_APP_PRIVATE_KEY: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} ++ APP_CLIENT_ID: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} ++ APP_PRIVATE_KEY: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} + CONSUMER_TOKEN: ${{ secrets.WORKFLOW_SYNC_TOKEN }} + run: | + set -euo pipefail + + case "${AUTH_MODE}" in -+ librecode-app) -+ [[ -n "${LIBRECODE_APP_CLIENT_ID}" && -n "${LIBRECODE_APP_PRIVATE_KEY}" ]] || { -+ echo "::error::librecode-app requires LIBRECODE_WORKFLOW_APP_CLIENT_ID and LIBRECODE_WORKFLOW_APP_PRIVATE_KEY" -+ exit 1 -+ } -+ ;; + github-app) -+ [[ -n "${CONSUMER_APP_CLIENT_ID}" && -n "${CONSUMER_APP_PRIVATE_KEY}" ]] || { ++ [[ -n "${APP_CLIENT_ID}" && -n "${APP_PRIVATE_KEY}" ]] || { + echo "::error::github-app requires WORKFLOW_SYNC_APP_CLIENT_ID and WORKFLOW_SYNC_APP_PRIVATE_KEY" + exit 1 + } @@ -90,23 +87,10 @@ + ;; + esac + -+ - name: Create LibreCode GitHub App token -+ if: ${{ vars.WORKFLOW_SYNC_AUTH_MODE == '' || vars.WORKFLOW_SYNC_AUTH_MODE == 'librecode-app' }} -+ id: librecode-app-token -+ uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 -+ with: -+ client-id: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} -+ private-key: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} -+ owner: ${{ github.repository_owner }} -+ repositories: ${{ github.event.repository.name }} -+ permission-contents: write -+ permission-pull-requests: write -+ permission-workflows: write -+ -+ - name: Create consumer GitHub App token -+ if: ${{ vars.WORKFLOW_SYNC_AUTH_MODE == 'github-app' }} -+ id: consumer-app-token -+ uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 ++ - name: Create GitHub App token ++ if: ${{ env.AUTH_MODE == 'github-app' }} ++ id: workflow-app-token ++ uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 - uses: skjnldsv/check-actor-permission@69e92a3c4711150929bca9fcf34448c5bf5526e7 # v3.0 with: + client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} @@ -121,17 +105,14 @@ + id: auth-token + shell: bash + env: -+ AUTH_MODE: ${{ vars.WORKFLOW_SYNC_AUTH_MODE || 'librecode-app' }} -+ LIBRECODE_APP_TOKEN: ${{ steps.librecode-app-token.outputs.token }} -+ CONSUMER_APP_TOKEN: ${{ steps.consumer-app-token.outputs.token }} ++ WORKFLOW_APP_TOKEN: ${{ steps.workflow-app-token.outputs.token }} + CONSUMER_TOKEN: ${{ secrets.WORKFLOW_SYNC_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + + case "${AUTH_MODE}" in -+ librecode-app) token="${LIBRECODE_APP_TOKEN}" ;; -+ github-app) token="${CONSUMER_APP_TOKEN}" ;; ++ github-app) token="${WORKFLOW_APP_TOKEN}" ;; + token) token="${CONSUMER_TOKEN}" ;; + github-token) token="${GITHUB_TOKEN}" ;; + *) exit 1 ;; @@ -162,10 +143,17 @@ - name: Checkout app uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 -@@ -56,112 +163,31 @@ +@@ -56,112 +141,31 @@ path: target ref: ${{ matrix.branches }} ++ - name: Synchronize workflow templates ++ id: sync ++ uses: ./source/actions/sync-workflows ++ with: ++ source: source/workflow-templates ++ target: target ++ catalog-commit: ${{ steps.catalog-revision.outputs.sha }} - - name: Check for active merge freeze - id: freeze - env: @@ -189,14 +177,7 @@ - echo 'active=true' >> $GITHUB_OUTPUT - fi - rm version.php -- -+ - name: Synchronize workflow templates -+ id: sync -+ uses: ./source/actions/sync-workflows -+ with: -+ source: source/workflow-templates -+ target: target -+ catalog-commit: ${{ steps.catalog-revision.outputs.sha }} + - - name: Copy all workflow templates - if: steps.freeze.outputs.active != 'true' - run: | @@ -207,7 +188,7 @@ - if [ -f "$workflow" ]; then - filename=$(basename "$workflow") - target_file="./target/.github/workflows/$filename" - +- - # Only copy if the file exists in the target repository - if [ -f "$target_file" ]; then - if [ -f "./target/.github/actions-lock.txt" ]; then @@ -263,8 +244,8 @@ - echo "DRAFT_ONLY=${draft_only}" >> $GITHUB_ENV - - name: Create Pull Request -- if: steps.freeze.outputs.active != 'true' + if: ${{ steps.sync.outputs.changed == 'true' }} +- if: steps.freeze.outputs.active != 'true' uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 with: + token: ${{ steps.auth-token.outputs.token }} diff --git a/tests/test_npm_audit_workflow_contract.py b/tests/test_npm_audit_workflow_contract.py index 3d48fd2..9f283ca 100644 --- a/tests/test_npm_audit_workflow_contract.py +++ b/tests/test_npm_audit_workflow_contract.py @@ -50,8 +50,8 @@ def test_read_only_workflow_with_scoped_short_lived_app_token(self) -> None: content = self.content self.assertIn("permissions:\n contents: read\n", content) self.assertIn("uses: actions/create-github-app-token@", content) - self.assertIn("client-id: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }}", content) - self.assertIn("private-key: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }}", content) + self.assertIn("client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }}", content) + self.assertIn("private-key: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }}", content) self.assertIn("permission-contents: write", content) self.assertIn("permission-pull-requests: write", content) self.assertIn("token: ${{ steps.app-token.outputs.token }}", content) diff --git a/tests/test_workflow_sync_auth.py b/tests/test_workflow_sync_auth.py new file mode 100644 index 0000000..8f22def --- /dev/null +++ b/tests/test_workflow_sync_auth.py @@ -0,0 +1,89 @@ +# SPDX-FileCopyrightText: 2026 LibreCode coop and contributors +# SPDX-License-Identifier: AGPL-3.0-or-later + +"""Authentication contract for provider-neutral workflow synchronization.""" + +import os +import re +import subprocess +import unittest +from pathlib import Path + +WORKFLOW = ( + Path(__file__).resolve().parents[1] + / "workflow-templates" + / "sync-workflow-templates.yml" +) + + +class WorkflowSyncAuthTest(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.content = WORKFLOW.read_text(encoding="utf-8") + + def step(self, name: str) -> str: + text = self.content.split(" - name: " + name + "\n", 1)[1] + return text.split(" - name: ", 1)[0] + + def test_default_mode_and_app_action(self) -> None: + self.assertIn( + "AUTH_MODE: " + "$" + "{{ vars.WORKFLOW_SYNC_AUTH_MODE || 'github-app' }}", + self.content, + ) + action = self.step("Create GitHub App token") + self.assertIn("env.AUTH_MODE == 'github-app'", action) + self.assertIn("vars.WORKFLOW_SYNC_APP_CLIENT_ID", action) + self.assertIn("secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY", action) + self.assertEqual(self.content.count(" - name: Create GitHub App token\n"), 1) + + def test_resolver_uses_one_app_token(self) -> None: + action = self.step("Resolve workflow sync token") + self.assertIn('github-app) token="' + "$" + '{WORKFLOW_APP_TOKEN}"', action) + self.assertIn('token) token="' + "$" + '{CONSUMER_TOKEN}"', action) + self.assertIn('github-token) token="' + "$" + '{GITHUB_TOKEN}"', action) + self.assertIn("steps.workflow-app-token.outputs.token", action) + self.assertEqual( + len(re.findall(r"^\s*AUTH_MODE:", self.content, re.MULTILINE)), 1 + ) + + def test_validate_credential_modes(self) -> None: + step = self.step("Validate workflow sync authentication") + script = step.split(" run: |\n", 1)[1] + script = "\n".join( + line[10:] if line.startswith(" ") else line + for line in script.splitlines() + ) + "\n" + cases = ( + ("app valid", "github-app", "client-id", "key", "", True), + ("app missing id", "github-app", "", "key", "", False), + ("app missing key", "github-app", "client-id", "", "", False), + ("app missing both", "github-app", "", "", "", False), + ("token valid", "token", "", "", "opaque", True), + ("token missing", "token", "", "", "", False), + ("github token", "github-token", "", "", "", True), + ("unsupported mode", "unknown", "", "", "", False), + ) + for name, mode, client_id, private_key, token, ok in cases: + with self.subTest(case=name): + env = dict( + os.environ, AUTH_MODE=mode, + APP_CLIENT_ID=client_id, APP_PRIVATE_KEY=private_key, + CONSUMER_TOKEN=token, + ) + result = subprocess.run( + ["bash", "-e", "-c", script], env=env, + capture_output=True, text=True, check=False, + ) + self.assertEqual( + result.returncode == 0, ok, + name + ": " + result.stdout + result.stderr, + ) + + def test_action_expressions_are_well_formed(self) -> None: + self.assertNotIn(chr(92) + "$" + "{", self.content) + for line in self.content.splitlines(): + self.assertEqual(line.count("$" + "{{"), line.count("}}"), line) + + +if __name__ == "__main__": + unittest.main() diff --git a/workflow-templates/npm-audit-fix.yml b/workflow-templates/npm-audit-fix.yml index 5f7048d..92c92c5 100644 --- a/workflow-templates/npm-audit-fix.yml +++ b/workflow-templates/npm-audit-fix.yml @@ -74,8 +74,8 @@ jobs: if: steps.checkout.outcome == 'success' uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: - client-id: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} - private-key: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} + client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} + private-key: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} owner: ${{ github.repository_owner }} repositories: ${{ github.event.repository.name }} permission-contents: write diff --git a/workflow-templates/sync-workflow-templates.yml b/workflow-templates/sync-workflow-templates.yml index abdcfe6..e558c96 100644 --- a/workflow-templates/sync-workflow-templates.yml +++ b/workflow-templates/sync-workflow-templates.yml @@ -36,6 +36,9 @@ jobs: contents: write pull-requests: write + env: + AUTH_MODE: ${{ vars.WORKFLOW_SYNC_AUTH_MODE || 'github-app' }} + steps: - name: Check actor permission if: ${{ github.event_name == 'workflow_dispatch' }} @@ -53,24 +56,15 @@ jobs: - name: Validate workflow sync authentication shell: bash env: - AUTH_MODE: ${{ vars.WORKFLOW_SYNC_AUTH_MODE || 'librecode-app' }} - LIBRECODE_APP_CLIENT_ID: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} - LIBRECODE_APP_PRIVATE_KEY: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} - CONSUMER_APP_CLIENT_ID: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} - CONSUMER_APP_PRIVATE_KEY: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} + APP_CLIENT_ID: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} + APP_PRIVATE_KEY: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} CONSUMER_TOKEN: ${{ secrets.WORKFLOW_SYNC_TOKEN }} run: | set -euo pipefail case "${AUTH_MODE}" in - librecode-app) - [[ -n "${LIBRECODE_APP_CLIENT_ID}" && -n "${LIBRECODE_APP_PRIVATE_KEY}" ]] || { - echo "::error::librecode-app requires LIBRECODE_WORKFLOW_APP_CLIENT_ID and LIBRECODE_WORKFLOW_APP_PRIVATE_KEY" - exit 1 - } - ;; github-app) - [[ -n "${CONSUMER_APP_CLIENT_ID}" && -n "${CONSUMER_APP_PRIVATE_KEY}" ]] || { + [[ -n "${APP_CLIENT_ID}" && -n "${APP_PRIVATE_KEY}" ]] || { echo "::error::github-app requires WORKFLOW_SYNC_APP_CLIENT_ID and WORKFLOW_SYNC_APP_PRIVATE_KEY" exit 1 } @@ -89,22 +83,9 @@ jobs: ;; esac - - name: Create LibreCode GitHub App token - if: ${{ vars.WORKFLOW_SYNC_AUTH_MODE == '' || vars.WORKFLOW_SYNC_AUTH_MODE == 'librecode-app' }} - id: librecode-app-token - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 - with: - client-id: ${{ vars.LIBRECODE_WORKFLOW_APP_CLIENT_ID }} - private-key: ${{ secrets.LIBRECODE_WORKFLOW_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-contents: write - permission-pull-requests: write - permission-workflows: write - - - name: Create consumer GitHub App token - if: ${{ vars.WORKFLOW_SYNC_AUTH_MODE == 'github-app' }} - id: consumer-app-token + - name: Create GitHub App token + if: ${{ env.AUTH_MODE == 'github-app' }} + id: workflow-app-token uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} @@ -119,17 +100,14 @@ jobs: id: auth-token shell: bash env: - AUTH_MODE: ${{ vars.WORKFLOW_SYNC_AUTH_MODE || 'librecode-app' }} - LIBRECODE_APP_TOKEN: ${{ steps.librecode-app-token.outputs.token }} - CONSUMER_APP_TOKEN: ${{ steps.consumer-app-token.outputs.token }} + WORKFLOW_APP_TOKEN: ${{ steps.workflow-app-token.outputs.token }} CONSUMER_TOKEN: ${{ secrets.WORKFLOW_SYNC_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | set -euo pipefail case "${AUTH_MODE}" in - librecode-app) token="${LIBRECODE_APP_TOKEN}" ;; - github-app) token="${CONSUMER_APP_TOKEN}" ;; + github-app) token="${WORKFLOW_APP_TOKEN}" ;; token) token="${CONSUMER_TOKEN}" ;; github-token) token="${GITHUB_TOKEN}" ;; *) exit 1 ;;