diff --git a/actions/update-submodule/action.yml b/actions/update-submodule/action.yml new file mode 100644 index 0000000..c2284e8 --- /dev/null +++ b/actions/update-submodule/action.yml @@ -0,0 +1,30 @@ +# SPDX-FileCopyrightText: 2026 LibreCode coop and contributors +# SPDX-License-Identifier: AGPL-3.0-or-later + +name: Update a linked submodule from a merged pull request +description: Updates a gitlink through the GitHub API without checking out PR code. + +inputs: + token: + description: Token limited to the parent and source repository operations + required: true + source-repository: + description: Repository containing the already merged source pull request + required: true + submodule-path: + description: Gitlink path within the parent repository + required: true + +runs: + using: composite + steps: + - name: Verify and update submodule gitlink + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + SUBMODULE_PATH: ${{ inputs.submodule-path }} + SOURCE_REPOSITORY: ${{ inputs.source-repository }} + with: + github-token: ${{ inputs.token }} + script: | + const run = require('${{ github.action_path }}/update.js') + await run({ github, context, core, env: process.env }) diff --git a/actions/update-submodule/update.js b/actions/update-submodule/update.js new file mode 100644 index 0000000..4af0622 --- /dev/null +++ b/actions/update-submodule/update.js @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors +// SPDX-License-Identifier: AGPL-3.0-or-later + +'use strict' + +const BOT = { + name: 'github-actions[bot]', + email: '41898282+github-actions[bot]@users.noreply.github.com', +} + +function validatePath(path) { + return /^[a-zA-Z0-9_.-]+(\/[a-zA-Z0-9_.-]+)*$/.test(path) && + !path.split('/').some(part => part === '.' || part === '..') +} + +async function updateSubmodule({ github, context, core, env }) { + const owner = context.repo.owner + const repo = context.repo.repo + const submodule = env.SUBMODULE_PATH + const sourceRepository = env.SOURCE_REPOSITORY + if (!validatePath(submodule) || + !/^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.-]+$/.test(sourceRepository)) { + throw new Error('Invalid submodule path or source repository configuration') + } + + const { data: pr } = await github.rest.pulls.get({ + owner, repo, pull_number: context.issue.number, + }) + if (pr.state !== 'open' || + pr.head.repo?.full_name !== owner + '/' + repo || + !/^(main|stable\d+)$/.test(pr.base.ref)) { + throw new Error('Only open, same-repository PRs targeting main or stable are supported') + } + + const prefix = submodule + ': https://github.com/' + sourceRepository + '/pull/' + const matches = (pr.body || '').split(/\r?\n/) + .map(line => line.match(/^\s*-\s*\[[ xX]\]\s*(.*)$/)) + .filter(Boolean) + .map(match => match[1].trim()) + .filter(text => text.startsWith(prefix)) + if (matches.length !== 1) { + throw new Error('Expected exactly one linked submodule PR') + } + const sourceNumber = matches[0].slice(prefix.length).trim() + if (!/^[1-9]\d*$/.test(sourceNumber)) { + throw new Error('Invalid linked submodule PR number') + } + const [sourceOwner, sourceName] = sourceRepository.split('/') + const { data: source } = await github.rest.pulls.get({ + owner: sourceOwner, repo: sourceName, pull_number: Number(sourceNumber), + }) + if (!source.merged_at || !/^[a-f0-9]{40}$/.test(source.merge_commit_sha || '') || + source.base.ref !== pr.base.ref) { + throw new Error('The linked dependency PR must be merged into the same target branch') + } + + const { data: commit } = await github.rest.git.getCommit({ + owner, repo, commit_sha: pr.head.sha, + }) + const { data: currentTree } = await github.rest.git.getTree({ + owner, repo, tree_sha: commit.tree.sha, + }) + const gitlink = currentTree.tree.find(entry => + entry.path === submodule && entry.mode === '160000') + if (!gitlink) { + throw new Error('The configured path is not a git submodule') + } + if (gitlink.sha === source.merge_commit_sha) { + core.notice('Submodule already references the merged dependency') + return + } + + const { data: tree } = await github.rest.git.createTree({ + owner, repo, base_tree: commit.tree.sha, + tree: [{ path: submodule, mode: '160000', type: 'commit', + sha: source.merge_commit_sha }], + }) + const subject = 'chore(deps): update ' + submodule + + ' from merged PR #' + sourceNumber + const message = subject + '\n\nSigned-off-by: ' + + BOT.name + ' <' + BOT.email + '>' + const { data: updated } = await github.rest.git.createCommit({ + owner, repo, message, tree: tree.sha, parents: [pr.head.sha], + author: BOT, committer: BOT, + }) + await github.rest.git.updateRef({ + owner, repo, ref: 'heads/' + pr.head.ref, sha: updated.sha, force: false, + }) + await github.rest.issues.createComment({ + owner, repo, issue_number: context.issue.number, + body: 'Updated ' + submodule + ' to ' + sourceRepository + '#' + + sourceNumber + ' (' + source.merge_commit_sha + ').', + }) +} + +module.exports = updateSubmodule diff --git a/actions/update-submodule/update.test.js b/actions/update-submodule/update.test.js new file mode 100644 index 0000000..2adfbf1 --- /dev/null +++ b/actions/update-submodule/update.test.js @@ -0,0 +1,103 @@ +// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors +// SPDX-License-Identifier: AGPL-3.0-or-later + +'use strict' + +const test = require('node:test') +const assert = require('node:assert/strict') +const update = require('./update.js') + +const sha = chr => chr.repeat(40) + +function fixture(change = () => {}, envChange = {}) { + const parent = { + state: 'open', + head: { repo: {full_name: 'LibreSign/libresign'}, ref: 'deps/upgrade', sha: sha('a') }, + base: {ref: 'main'}, + body: '- [ ] 3rdparty: https://github.com/LibreSign/3rdparty/pull/17', + } + const source = { + merged_at: '2026-11-17T12:00:00Z', + merge_commit_sha: sha('b'), + base: {ref: 'main'}, + } + const gitlink = {path: '3rdparty', mode: '160000', type: 'commit', sha: sha('c')} + change({parent, source, gitlink}) + const calls = [] + const wrap = (name, fn) => async args => { + calls.push({name, args}) + return {data: await fn(args)} + } + const github = {rest: { + pulls: {get: wrap('pulls.get', args => args.repo === '3rdparty' ? source : parent)}, + git: { + getCommit: wrap('git.getCommit', () => ({tree: {sha: sha('d')}})), + getTree: wrap('git.getTree', () => ({tree: [gitlink]})), + createTree: wrap('git.createTree', () => ({sha: sha('e')})), + createCommit: wrap('git.createCommit', () => ({sha: sha('f')})), + updateRef: wrap('git.updateRef', () => ({})), + }, + issues: {createComment: wrap('issues.createComment', () => ({}))}, + }} + const notices = [] + const context = {repo: {owner: 'LibreSign', repo: 'libresign'}, issue: {number: 42}} + const core = {notice: text => notices.push(text)} + const env = {SUBMODULE_PATH: '3rdparty', SOURCE_REPOSITORY: 'LibreSign/3rdparty', ...envChange} + return {github, context, core, env, calls, notices} +} + +test('updates only the gitlink at the exact merged SHA, with DCO and no force push', async () => { + const f = fixture() + await update(f) + const tree = f.calls.find(x => x.name === 'git.createTree').args + assert.deepEqual(tree.tree, [{path: '3rdparty', mode: '160000', type: 'commit', sha: sha('b')}]) + assert.equal(tree.base_tree, sha('d')) + const commit = f.calls.find(x => x.name === 'git.createCommit').args + assert.deepEqual(commit.parents, [sha('a')]) + assert.match(commit.message, /Signed-off-by: github-actions\[bot\]/) + const pushed = f.calls.find(x => x.name === 'git.updateRef').args + assert.equal(pushed.force, false) + assert.equal(pushed.ref, 'heads/deps/upgrade') + assert.equal(f.calls.at(-1).name, 'issues.createComment') +}) + +const cases = [ + ['fork is rejected', ({parent}) => {parent.head.repo.full_name = 'fork/libresign'}], + ['closed target is rejected', ({parent}) => {parent.state = 'closed'}], + ['unsupported target branch is rejected', ({parent}) => {parent.base.ref = 'feature'}], + ['missing linked source PR is rejected', ({parent}) => {parent.body = ''}], + ['duplicate linked source PRs are rejected', ({parent}) => {parent.body += '\n' + parent.body}], + ['unmerged source is rejected', ({source}) => {source.merged_at = null}], + ['mismatched source branch is rejected', ({source}) => {source.base.ref = 'stable35'}], + ['unrecognized submodule is rejected', ({gitlink}) => {gitlink.path = 'other'}], +] +for (const [name, change] of cases) { + test(name, async () => { + const f = fixture(change) + await assert.rejects(update(f)) + assert.equal(f.calls.some(x => x.name === 'git.createCommit'), false) + }) +} + +test('path traversal is rejected before any API access', async () => { + const f = fixture(() => {}, {SUBMODULE_PATH: '../3rdparty'}) + await assert.rejects(update(f), /Invalid submodule path/) + assert.equal(f.calls.length, 0) +}) + +test('idempotent update does not create commits or comments', async () => { + const f = fixture(({source, gitlink}) => {gitlink.sha = source.merge_commit_sha}) + await update(f) + assert.equal(f.calls.some(x => x.name === 'git.createCommit'), false) + assert.equal(f.calls.some(x => x.name === 'issues.createComment'), false) + assert.equal(f.notices.length, 1) +}) + +test('concurrent ref movement rejects without reporting success', async () => { + const f = fixture() + f.github.rest.git.updateRef = async () => { + throw new Error('Reference changed') + } + await assert.rejects(update(f), /Reference changed/) + assert.equal(f.calls.some(x => x.name === 'issues.createComment'), false) +}) diff --git a/docs/cross-repository-automation.md b/docs/cross-repository-automation.md index 4eb27d7..a2b825e 100644 --- a/docs/cross-repository-automation.md +++ b/docs/cross-repository-automation.md @@ -52,6 +52,32 @@ Consumer-specific changes belong in `.github/workflows/.patch`. Do not The catalog updater targets only the consumer's default branch by default. Projects with maintained long-lived release branches should declare those branches through a consumer-local patch to `sync-workflow-templates.yml`; branch lifecycle is consumer policy and must not be hard-coded in the organization template. +## Submodule dependency commands + +`command-submodule-pr.yml` tracks a pinned upstream workflow from +`nextcloud/3rdparty`; its generic changes live in `patches/nextcloud/` +and are verified against immutable upstream checksums. + +`command-update-submodule.yml` is LibreCode-owned. It delegates to the +version-pinned, tested `actions/update-submodule` Action: the Nextcloud Server +command is a conceptual reference, but its implementation is intentionally +not copied because it updates a moving branch tip and can rewrite commits. + +A consumer must install each workflow and configure its parameters using +Actions variables or a consumer-local `.github/workflows/.yml.patch`: + +- `command-submodule-pr.yml`: `SUBMODULE_PR_COMMAND`, + `SUBMODULE_TARGET_REPOSITORY` and `SUBMODULE_PATH`. +- `command-update-submodule.yml`: `SUBMODULE_UPDATE_COMMAND`, + `SUBMODULE_SOURCE_REPOSITORY` and `SUBMODULE_PATH`. + +The creation command expects a Composer lockfile in the source PR. The update +command requires a linked source PR already merged into the same target branch +and uses its exact merged SHA. Both commands require maintainer authorization, +repository-hosted PR branches and a scoped publishing credential. No force +push is performed. Publishing credentials are not available to dependency +analysis or staging steps. + ## Publishing templates `LibreCodeCoop/.github` is the source of truth for LibreCode-managed workflow templates and the distribution catalog consumed by repository updaters. Templates derived from external upstream sources are refreshed through the pinned upstream/patch pipeline rather than edited as independent downstream implementations. diff --git a/patches/nextcloud/command-submodule-pr.yml.patch b/patches/nextcloud/command-submodule-pr.yml.patch new file mode 100644 index 0000000..b23a68e --- /dev/null +++ b/patches/nextcloud/command-submodule-pr.yml.patch @@ -0,0 +1,132 @@ +--- upstream/vendor/nextcloud/command-submodule-pr.yml ++++ workflow-templates/command-submodule-pr.yml +@@ -1,6 +1,6 @@ + # SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + # SPDX-License-Identifier: MIT ++name: Open submodule pull request +-name: Server PR comment + + on: + issue_comment: # zizmor: ignore[dangerous-triggers] +@@ -13,8 +13,8 @@ + init: + runs-on: ubuntu-latest-low + ++ # A consumer patch or repository variable configures the command. ++ if: github.event.issue.pull_request != '' && vars.SUBMODULE_PR_COMMAND != '' && github.event.comment.body == vars.SUBMODULE_PR_COMMAND +- # On pull requests and if the comment starts with `/server-pr` +- if: github.event.issue.pull_request != '' && startsWith(github.event.comment.body, '/server-pr') + + outputs: + head_ref: ${{ steps.comment-branch.outputs.head_ref }} +@@ -75,19 +75,26 @@ + needs: init + + env: ++ TARGET_REPOSITORY: ${{ vars.SUBMODULE_TARGET_REPOSITORY }} ++ SUBMODULE_PATH: ${{ vars.SUBMODULE_PATH }} + HEAD_REF: ${{ needs.init.outputs.head_ref }} + HEAD_SHA: ${{ needs.init.outputs.head_sha }} + BASE_REF: ${{ needs.init.outputs.base_ref }} + PR_NUMBER: ${{ github.event.issue.number }} + PR_TITLE: ${{ github.event.issue.title }} +- REVIEWER: ${{ github.event.comment.user.login }} +- GH_TOKEN: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + + steps: ++ - name: Validate target configuration ++ shell: bash ++ run: | ++ set -euo pipefail ++ [[ "$TARGET_REPOSITORY" =~ ^[[:alnum:]_.-]+/[[:alnum:]_.-]+$ ]] || exit 1 ++ [[ "$SUBMODULE_PATH" =~ ^[[:alnum:]_.-]+(/[[:alnum:]_.-]+)*$ ]] || exit 1 ++ + - name: Checkout 3rdparty ${{ needs.init.outputs.head_ref }} + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: ++ path: source +- path: 3rdparty + persist-credentials: false + fetch-depth: 0 + ref: ${{ needs.init.outputs.head_sha }} +@@ -99,57 +106,56 @@ + coverage: none + + - name: Generate composer diff ++ working-directory: source +- working-directory: 3rdparty + run: | + composer global config --no-interaction allow-plugins.ion-bazan/composer-diff true + composer global require --no-interaction ion-bazan/composer-diff + { ++ echo "- [ ] $SUBMODULE_PATH: ${{ github.server_url }}/${{ github.repository }}/pull/$PR_NUMBER" +- echo "- [ ] 3rdparty: ${{ github.server_url }}/${{ github.repository }}/pull/$PR_NUMBER" + echo + composer diff --with-links --base "origin/$BASE_REF:composer.lock" --target composer.lock + } > "$RUNNER_TEMP/pr-body.md" + cat "$RUNNER_TEMP/pr-body.md" + ++ - name: Checkout target repository ${{ needs.init.outputs.base_ref }} +- - name: Checkout server ${{ needs.init.outputs.base_ref }} + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: ++ repository: ${{ env.TARGET_REPOSITORY }} ++ path: parent ++ persist-credentials: false +- repository: nextcloud/server +- path: server +- persist-credentials: true +- # Pushing a new branch to server needs the `workflow` scope, as GitHub can not always verify in time that no workflow is changed +- token: ${{ secrets.COMMAND_BOT_WORKFLOWS }} # zizmor: ignore[secrets-outside-env] + ref: ${{ needs.init.outputs.base_ref }} + ++ - name: Fail if target branch already exists ++ working-directory: parent +- - name: Fail if branch already exists on server +- working-directory: server + run: | + if git ls-remote --exit-code --heads origin "refs/heads/$HEAD_REF"; then ++ echo "Branch $HEAD_REF already exists on $TARGET_REPOSITORY, not overwriting it" +- echo "Branch $HEAD_REF already exists on nextcloud/server, not overwriting it" + exit 1 + fi + + - name: Setup git ++ working-directory: parent +- working-directory: server + run: | ++ git config --local user.email '41898282+github-actions[bot]@users.noreply.github.com' ++ git config --local user.name 'github-actions[bot]' +- git config --local user.email 'nextcloud-command@users.noreply.github.com' +- git config --local user.name 'nextcloud-command' + ++ - name: Commit submodule reference ++ working-directory: parent +- - name: Commit 3rdparty update and push +- working-directory: server + run: | + git checkout -b "$HEAD_REF" ++ git update-index --cacheinfo "160000,$HEAD_SHA,$SUBMODULE_PATH" +- git update-index --cacheinfo "160000,$HEAD_SHA,3rdparty" + git commit --signoff -m "$(sed -E 's/^\[stable[0-9.]*\] *//' <<< "$PR_TITLE")" ++ - name: Push branch and create target pull request ++ env: ++ GH_TOKEN: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] +- git push origin "$HEAD_REF" +- +- - name: Create server pull request + run: | ++ set -euo pipefail ++ gh auth setup-git ++ git -C parent push origin "$HEAD_REF" ++ url=$(gh pr create --repo "$TARGET_REPOSITORY" --base "$BASE_REF" --head "$HEAD_REF" \ +- url=$(gh pr create --repo nextcloud/server --base "$BASE_REF" --head "$HEAD_REF" \ + --title "$PR_TITLE" \ +- --label dependencies --label '3. to review' \ +- --reviewer "$REVIEWER" \ + --body-file "$RUNNER_TEMP/pr-body.md") ++ gh pr comment "$PR_NUMBER" --repo '${{ github.repository }}' --body "- Parent PR: $url" +- gh pr comment "$PR_NUMBER" --repo '${{ github.repository }}' --body "- Server PR: $url" + + - name: Add reaction on failure + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 diff --git a/tests/test_submodule_workflows.py b/tests/test_submodule_workflows.py new file mode 100644 index 0000000..aef5412 --- /dev/null +++ b/tests/test_submodule_workflows.py @@ -0,0 +1,76 @@ +# SPDX-FileCopyrightText: 2026 LibreCode coop and contributors +# SPDX-License-Identifier: AGPL-3.0-or-later + +"""Contracts for inherited, testable submodule commands.""" + +import hashlib +import json +import re +import shutil +import subprocess +import unittest +from pathlib import Path + +from scripts.render_upstream import check, load_templates + +ROOT = Path(__file__).resolve().parents[1] + + +class SubmoduleCommandTest(unittest.TestCase): + def test_upstream_provenance_and_rendering(self) -> None: + manifest = json.loads((ROOT / "upstream/sources.json").read_text()) + expected = { + "nextcloud-command-submodule-pr": "nextcloud/3rdparty", + } + for name, repo in expected.items(): + with self.subTest(name=name): + item = next(x for x in manifest["sources"] if x["name"] == name) + self.assertEqual(item["repository"], repo) + self.assertRegex(item["url"], r"/[0-9a-f]{40}/") + source = (ROOT / item["destination"]).read_bytes() + self.assertEqual(hashlib.sha256(source).hexdigest(), item["sha256"]) + check(load_templates(ROOT / "upstream/templates.json"), ROOT) + + def test_no_publish_secrets_during_composer_analysis(self) -> None: + source = (ROOT / "workflow-templates/command-submodule-pr.yml").read_text() + process = source.split(" process:\n", 1)[1] + before_publish = process.split( + " - name: Push branch and create target pull request", 1 + )[0] + self.assertNotIn("secrets.COMMAND_BOT_PAT", before_publish) + self.assertIn("composer diff --with-links", before_publish) + self.assertIn("persist-credentials: false", before_publish) + self.assertIn("gh auth setup-git", process) + + def test_update_is_composite_action_without_pr_checkout(self) -> None: + workflow = (ROOT / "workflow-templates/command-update-submodule.yml").read_text() + self.assertNotIn("actions/checkout", workflow) + self.assertNotIn("git read-tree", workflow) + self.assertIn("Check actor permission", workflow) + self.assertRegex( + workflow, + r"uses: LibreCodeCoop/\.github/actions/update-submodule@[0-9a-f]{40}", + ) + action = (ROOT / "actions/update-submodule/action.yml").read_text() + self.assertIn("uses: actions/github-script@", action) + self.assertIn("source-repository:", action) + self.assertIn("submodule-path:", action) + impl = (ROOT / "actions/update-submodule/update.js").read_text() + for contract in ("force: false", "Signed-off-by:", "createTree", + "createCommit", "updateRef"): + self.assertIn(contract, impl) + + def test_update_action_negative_and_positive_cases(self) -> None: + if not shutil.which("node"): + self.skipTest("Node.js unavailable") + test_file = ROOT / "actions/update-submodule/update.test.js" + result = subprocess.run( + ["node", "--test", str(test_file)], + check=False, capture_output=True, text=True, + ) + self.assertEqual(result.returncode, 0, result.stderr + result.stdout) + self.assertIn("fail 0", result.stdout) + + +if __name__ == "__main__": + unittest.main() diff --git a/upstream/sources.json b/upstream/sources.json index 72081ab..5232210 100644 --- a/upstream/sources.json +++ b/upstream/sources.json @@ -179,6 +179,15 @@ "url": "https://raw.githubusercontent.com/nextcloud/.github/c2798fc61844a6420779add9f5e1b51f8aabfd87/workflow-templates/playwright.yml", "sha256": "26106f8ae4bef2a191c445fb3a246ae6e1ab3f43348b8940feaf356768091a38", "destination": "upstream/vendor/nextcloud/playwright.yml" + }, + { + "name": "nextcloud-command-submodule-pr", + "repository": "nextcloud/3rdparty", + "ref": "master", + "path": ".github/workflows/command-server-pr.yml", + "url": "https://raw.githubusercontent.com/nextcloud/3rdparty/ee8d3bf7a2bfcbc1946da3ffd5afb1b08e317527/.github/workflows/command-server-pr.yml", + "sha256": "856720db49f76fba680b32582bdba88be4d7fc5064aaa7090b3fb77ae03e4ba9", + "destination": "upstream/vendor/nextcloud/command-submodule-pr.yml" } ] } diff --git a/upstream/templates.json b/upstream/templates.json index a9b82c3..2f2e3ca 100644 --- a/upstream/templates.json +++ b/upstream/templates.json @@ -146,6 +146,14 @@ "source": "upstream/vendor/nextcloud/playwright.yml", "patches": [], "destination": "workflow-templates/playwright.yml" + }, + { + "name": "nextcloud-command-submodule-pr", + "source": "upstream/vendor/nextcloud/command-submodule-pr.yml", + "patches": [ + "patches/nextcloud/command-submodule-pr.yml.patch" + ], + "destination": "workflow-templates/command-submodule-pr.yml" } ], "runner_aliases": { diff --git a/upstream/vendor/nextcloud/command-submodule-pr.yml b/upstream/vendor/nextcloud/command-submodule-pr.yml new file mode 100644 index 0000000..d46c595 --- /dev/null +++ b/upstream/vendor/nextcloud/command-submodule-pr.yml @@ -0,0 +1,161 @@ +# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors +# SPDX-License-Identifier: MIT +name: Server PR comment + +on: + issue_comment: # zizmor: ignore[dangerous-triggers] + types: [created] + +permissions: + contents: read + +jobs: + init: + runs-on: ubuntu-latest-low + + # On pull requests and if the comment starts with `/server-pr` + if: github.event.issue.pull_request != '' && startsWith(github.event.comment.body, '/server-pr') + + outputs: + head_ref: ${{ steps.comment-branch.outputs.head_ref }} + head_sha: ${{ steps.comment-branch.outputs.head_sha }} + base_ref: ${{ steps.comment-branch.outputs.base_ref }} + + steps: + - name: Get repository from pull request comment + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + id: get-repository + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const pull = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number + }); + + const repositoryName = pull.data.head?.repo?.full_name + console.log(repositoryName) + return repositoryName + + - name: Disabled on forks + if: ${{ fromJSON(steps.get-repository.outputs.result) != github.repository }} + run: | + echo 'Can not execute /server-pr on forks' + exit 1 + + - name: Check actor permission + uses: skjnldsv/check-actor-permission@69e92a3c4711150929bca9fcf34448c5bf5526e7 # v3.0 + with: + require: write + + - name: Add reaction on start + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 + with: + token: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + repository: ${{ github.event.repository.full_name }} + comment-id: ${{ github.event.comment.id }} + reactions: '+1' + + - name: Init branch + uses: xt0rted/pull-request-comment-branch@e8b8daa837e8ea7331c0003c9c316a64c6d8b0b1 # v3.0.0 + id: comment-branch + + - name: Add reaction on failure + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 + if: failure() + with: + token: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + repository: ${{ github.event.repository.full_name }} + comment-id: ${{ github.event.comment.id }} + reactions: '-1' + + process: + runs-on: ubuntu-latest-low + needs: init + + env: + HEAD_REF: ${{ needs.init.outputs.head_ref }} + HEAD_SHA: ${{ needs.init.outputs.head_sha }} + BASE_REF: ${{ needs.init.outputs.base_ref }} + PR_NUMBER: ${{ github.event.issue.number }} + PR_TITLE: ${{ github.event.issue.title }} + REVIEWER: ${{ github.event.comment.user.login }} + GH_TOKEN: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + + steps: + - name: Checkout 3rdparty ${{ needs.init.outputs.head_ref }} + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + path: 3rdparty + persist-credentials: false + fetch-depth: 0 + ref: ${{ needs.init.outputs.head_sha }} + + - name: Set up php + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: 8.3 + coverage: none + + - name: Generate composer diff + working-directory: 3rdparty + run: | + composer global config --no-interaction allow-plugins.ion-bazan/composer-diff true + composer global require --no-interaction ion-bazan/composer-diff + { + echo "- [ ] 3rdparty: ${{ github.server_url }}/${{ github.repository }}/pull/$PR_NUMBER" + echo + composer diff --with-links --base "origin/$BASE_REF:composer.lock" --target composer.lock + } > "$RUNNER_TEMP/pr-body.md" + cat "$RUNNER_TEMP/pr-body.md" + + - name: Checkout server ${{ needs.init.outputs.base_ref }} + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: nextcloud/server + path: server + persist-credentials: true + # Pushing a new branch to server needs the `workflow` scope, as GitHub can not always verify in time that no workflow is changed + token: ${{ secrets.COMMAND_BOT_WORKFLOWS }} # zizmor: ignore[secrets-outside-env] + ref: ${{ needs.init.outputs.base_ref }} + + - name: Fail if branch already exists on server + working-directory: server + run: | + if git ls-remote --exit-code --heads origin "refs/heads/$HEAD_REF"; then + echo "Branch $HEAD_REF already exists on nextcloud/server, not overwriting it" + exit 1 + fi + + - name: Setup git + working-directory: server + run: | + git config --local user.email 'nextcloud-command@users.noreply.github.com' + git config --local user.name 'nextcloud-command' + + - name: Commit 3rdparty update and push + working-directory: server + run: | + git checkout -b "$HEAD_REF" + git update-index --cacheinfo "160000,$HEAD_SHA,3rdparty" + git commit --signoff -m "$(sed -E 's/^\[stable[0-9.]*\] *//' <<< "$PR_TITLE")" + git push origin "$HEAD_REF" + + - name: Create server pull request + run: | + url=$(gh pr create --repo nextcloud/server --base "$BASE_REF" --head "$HEAD_REF" \ + --title "$PR_TITLE" \ + --label dependencies --label '3. to review' \ + --reviewer "$REVIEWER" \ + --body-file "$RUNNER_TEMP/pr-body.md") + gh pr comment "$PR_NUMBER" --repo '${{ github.repository }}' --body "- Server PR: $url" + + - name: Add reaction on failure + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 + if: failure() + with: + token: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + repository: '${{ github.event.repository.full_name }}' + comment-id: '${{ github.event.comment.id }}' + reactions: '-1' diff --git a/workflow-templates/command-submodule-pr.properties.json b/workflow-templates/command-submodule-pr.properties.json new file mode 100644 index 0000000..229a3f2 --- /dev/null +++ b/workflow-templates/command-submodule-pr.properties.json @@ -0,0 +1,8 @@ +{ + "name": "Open parent PR for a Composer submodule", + "description": "Create a reviewable parent-repository pull request for a dependency submodule; configure command, repository and path per consumer.", + "iconName": "octicon git-pull-request", + "categories": [ + "Continuous integration" + ] +} diff --git a/workflow-templates/command-submodule-pr.yml b/workflow-templates/command-submodule-pr.yml new file mode 100644 index 0000000..30087a1 --- /dev/null +++ b/workflow-templates/command-submodule-pr.yml @@ -0,0 +1,167 @@ +# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors +# SPDX-License-Identifier: MIT +name: Open submodule pull request + +on: + issue_comment: # zizmor: ignore[dangerous-triggers] + types: [created] + +permissions: + contents: read + +jobs: + init: + runs-on: ubuntu-latest + + # A consumer patch or repository variable configures the command. + if: github.event.issue.pull_request != '' && vars.SUBMODULE_PR_COMMAND != '' && github.event.comment.body == vars.SUBMODULE_PR_COMMAND + + outputs: + head_ref: ${{ steps.comment-branch.outputs.head_ref }} + head_sha: ${{ steps.comment-branch.outputs.head_sha }} + base_ref: ${{ steps.comment-branch.outputs.base_ref }} + + steps: + - name: Get repository from pull request comment + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + id: get-repository + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const pull = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number + }); + + const repositoryName = pull.data.head?.repo?.full_name + console.log(repositoryName) + return repositoryName + + - name: Disabled on forks + if: ${{ fromJSON(steps.get-repository.outputs.result) != github.repository }} + run: | + echo 'Can not execute /server-pr on forks' + exit 1 + + - name: Check actor permission + uses: skjnldsv/check-actor-permission@69e92a3c4711150929bca9fcf34448c5bf5526e7 # v3.0 + with: + require: write + + - name: Add reaction on start + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 + with: + token: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + repository: ${{ github.event.repository.full_name }} + comment-id: ${{ github.event.comment.id }} + reactions: '+1' + + - name: Init branch + uses: xt0rted/pull-request-comment-branch@e8b8daa837e8ea7331c0003c9c316a64c6d8b0b1 # v3.0.0 + id: comment-branch + + - name: Add reaction on failure + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 + if: failure() + with: + token: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + repository: ${{ github.event.repository.full_name }} + comment-id: ${{ github.event.comment.id }} + reactions: '-1' + + process: + runs-on: ubuntu-latest + needs: init + + env: + TARGET_REPOSITORY: ${{ vars.SUBMODULE_TARGET_REPOSITORY }} + SUBMODULE_PATH: ${{ vars.SUBMODULE_PATH }} + HEAD_REF: ${{ needs.init.outputs.head_ref }} + HEAD_SHA: ${{ needs.init.outputs.head_sha }} + BASE_REF: ${{ needs.init.outputs.base_ref }} + PR_NUMBER: ${{ github.event.issue.number }} + PR_TITLE: ${{ github.event.issue.title }} + + steps: + - name: Validate target configuration + shell: bash + run: | + set -euo pipefail + [[ "$TARGET_REPOSITORY" =~ ^[[:alnum:]_.-]+/[[:alnum:]_.-]+$ ]] || exit 1 + [[ "$SUBMODULE_PATH" =~ ^[[:alnum:]_.-]+(/[[:alnum:]_.-]+)*$ ]] || exit 1 + + - name: Checkout 3rdparty ${{ needs.init.outputs.head_ref }} + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + path: source + persist-credentials: false + fetch-depth: 0 + ref: ${{ needs.init.outputs.head_sha }} + + - name: Set up php + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: 8.3 + coverage: none + + - name: Generate composer diff + working-directory: source + run: | + composer global config --no-interaction allow-plugins.ion-bazan/composer-diff true + composer global require --no-interaction ion-bazan/composer-diff + { + echo "- [ ] $SUBMODULE_PATH: ${{ github.server_url }}/${{ github.repository }}/pull/$PR_NUMBER" + echo + composer diff --with-links --base "origin/$BASE_REF:composer.lock" --target composer.lock + } > "$RUNNER_TEMP/pr-body.md" + cat "$RUNNER_TEMP/pr-body.md" + + - name: Checkout target repository ${{ needs.init.outputs.base_ref }} + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ env.TARGET_REPOSITORY }} + path: parent + persist-credentials: false + ref: ${{ needs.init.outputs.base_ref }} + + - name: Fail if target branch already exists + working-directory: parent + run: | + if git ls-remote --exit-code --heads origin "refs/heads/$HEAD_REF"; then + echo "Branch $HEAD_REF already exists on $TARGET_REPOSITORY, not overwriting it" + exit 1 + fi + + - name: Setup git + working-directory: parent + run: | + git config --local user.email '41898282+github-actions[bot]@users.noreply.github.com' + git config --local user.name 'github-actions[bot]' + + - name: Commit submodule reference + working-directory: parent + run: | + git checkout -b "$HEAD_REF" + git update-index --cacheinfo "160000,$HEAD_SHA,$SUBMODULE_PATH" + git commit --signoff -m "$(sed -E 's/^\[stable[0-9.]*\] *//' <<< "$PR_TITLE")" + - name: Push branch and create target pull request + env: + GH_TOKEN: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + run: | + set -euo pipefail + gh auth setup-git + git -C parent push origin "$HEAD_REF" + url=$(gh pr create --repo "$TARGET_REPOSITORY" --base "$BASE_REF" --head "$HEAD_REF" \ + --title "$PR_TITLE" \ + --body-file "$RUNNER_TEMP/pr-body.md") + gh pr comment "$PR_NUMBER" --repo '${{ github.repository }}' --body "- Parent PR: $url" + + - name: Add reaction on failure + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 + if: failure() + with: + token: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + repository: '${{ github.event.repository.full_name }}' + comment-id: '${{ github.event.comment.id }}' + reactions: '-1' diff --git a/workflow-templates/command-update-submodule.properties.json b/workflow-templates/command-update-submodule.properties.json new file mode 100644 index 0000000..9e3a387 --- /dev/null +++ b/workflow-templates/command-update-submodule.properties.json @@ -0,0 +1,8 @@ +{ + "name": "Update parent PR submodule from merged dependency PR", + "description": "Update a submodule pointer using only a verified merged source PR; set the command, repository and path for each consumer.", + "iconName": "octicon git-pull-request", + "categories": [ + "Continuous integration" + ] +} diff --git a/workflow-templates/command-update-submodule.yml b/workflow-templates/command-update-submodule.yml new file mode 100644 index 0000000..f1eee96 --- /dev/null +++ b/workflow-templates/command-update-submodule.yml @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: 2026 LibreCode coop and contributors +# SPDX-License-Identifier: AGPL-3.0-or-later +name: Update submodule reference + +on: + issue_comment: # zizmor: ignore[dangerous-triggers] + types: [created] + +permissions: + contents: read + pull-requests: read + +jobs: + update: + if: github.event.issue.pull_request != '' && vars.SUBMODULE_UPDATE_COMMAND != '' && github.event.comment.body == vars.SUBMODULE_UPDATE_COMMAND + runs-on: ubuntu-latest + steps: + - name: Check actor permission + uses: skjnldsv/check-actor-permission@69e92a3c4711150929bca9fcf34448c5bf5526e7 # v3.0 + with: + require: write + + - name: Update merged submodule reference + uses: LibreCodeCoop/.github/actions/update-submodule@2a75cff7318334499053ced8b72d04bc0f795baf # source pinned + with: + token: ${{ secrets.COMMAND_BOT_PAT }} # zizmor: ignore[secrets-outside-env] + source-repository: ${{ vars.SUBMODULE_SOURCE_REPOSITORY }} + submodule-path: ${{ vars.SUBMODULE_PATH }}