diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f21be76..1a584ce 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,6 +26,9 @@ jobs: - name: Run unit tests run: python3 -m unittest discover -s tests -p 'test_*.py' + - name: Test source PR metadata resolution + run: node --test tests/test-submodule-pr-command.test.js + - name: Verify vendored upstream sources run: python3 scripts/sync_upstream.py check upstream/sources.json diff --git a/patches/nextcloud/command-submodule-post-merge.yml.patch b/patches/nextcloud/command-submodule-post-merge.yml.patch new file mode 100644 index 0000000..2f3b18d --- /dev/null +++ b/patches/nextcloud/command-submodule-post-merge.yml.patch @@ -0,0 +1,452 @@ +--- a/.github/workflows/command-submodule-pr.yml ++++ b/.github/workflows/command-submodule-pr.yml +@@ -24,195 +24,260 @@ + 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: ${{ github.token }} +- 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: ${{ github.token }} +- 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 }} +- TARGET_PR_LABELS: ${{ vars.SUBMODULE_PR_LABELS }} +- TARGET_PR_REVIEWER: ${{ vars.SUBMODULE_PR_REVIEWER }} +- COMMAND_ACTOR: ${{ github.event.comment.user.login }} +- 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 +- id: target-scope +- shell: bash +- run: | +- set -euo pipefail +- [[ "$TARGET_REPOSITORY" =~ ^[[:alnum:]_.-]+/[[:alnum:]_.-]+$ ]] || exit 1 +- [[ "$SUBMODULE_PATH" =~ ^[[:alnum:]_.-]+(/[[:alnum:]_.-]+)*$ ]] || exit 1 +- echo "owner=${TARGET_REPOSITORY%%/*}" >> "$GITHUB_OUTPUT" +- echo "repository=${TARGET_REPOSITORY#*/}" >> "$GITHUB_OUTPUT" +- +- - name: Require successful source PR checks +- shell: bash +- env: +- GH_TOKEN: ${{ github.token }} +- run: | +- set -euo pipefail +- source_head="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.head.sha')" +- if [[ "$source_head" != "$HEAD_SHA" ]]; then +- echo "::error::Source PR head changed; rerun after reviewing the new revision" +- exit 1 +- fi +- gh pr checks "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" +- if [[ "$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.head.sha')" != "$HEAD_SHA" ]]; then +- echo "::error::Source PR head changed during CI verification" +- exit 1 +- fi +- +- - 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: Stage submodule reference +- working-directory: parent +- shell: bash +- run: | +- set -euo pipefail +- git update-index --cacheinfo "160000,$HEAD_SHA,$SUBMODULE_PATH" +- +- - name: Create scoped target GitHub App token +- id: target-app-token +- uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 +- with: +- client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} +- private-key: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} +- owner: ${{ steps.target-scope.outputs.owner }} +- repositories: ${{ steps.target-scope.outputs.repository }} +- permission-contents: write +- permission-pull-requests: write +- permission-workflows: write +- +- - name: Resolve GitHub App signing bot +- id: signing-bot +- shell: bash +- env: +- GH_TOKEN: ${{ github.token }} +- APP_SLUG: ${{ steps.target-app-token.outputs.app-slug }} +- run: | +- set -euo pipefail +- [[ "$APP_SLUG" =~ ^[a-zA-Z0-9-]+$ ]] || exit 1 +- bot_user_id="$(gh api "users/${APP_SLUG}[bot]" --jq '.id')" +- [[ "$bot_user_id" =~ ^[0-9]+$ ]] || exit 1 +- echo "name=${APP_SLUG}[bot]" >> "$GITHUB_OUTPUT" +- echo "email=${bot_user_id}+${APP_SLUG}[bot]@users.noreply.github.com" >> "$GITHUB_OUTPUT" +- +- - name: Configure DCO commit identity +- working-directory: parent +- shell: bash +- env: +- BOT_NAME: ${{ steps.signing-bot.outputs.name }} +- BOT_EMAIL: ${{ steps.signing-bot.outputs.email }} +- run: | +- git config --local user.name "$BOT_NAME" +- git config --local user.email "$BOT_EMAIL" +- +- - name: Revalidate source PR before publishing +- shell: bash +- env: +- GH_TOKEN: ${{ github.token }} +- run: | +- set -euo pipefail +- source_head="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.head.sha')" +- if [[ "$source_head" != "$HEAD_SHA" ]]; then +- echo "::error::Source PR head changed before publication" +- exit 1 ++ source_gitlink_sha: ${{ steps.comment-branch.outputs.source_gitlink_sha }} ++ source_merged: ${{ steps.comment-branch.outputs.source_merged }} ++ ++ steps: ++ - 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: ${{ github.token }} ++ repository: ${{ github.event.repository.full_name }} ++ comment-id: ${{ github.event.comment.id }} ++ reactions: '+1' ++ ++ - name: Resolve source pull request ++ uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 ++ id: comment-branch ++ with: ++ script: | ++ const {data: pr} = await github.rest.pulls.get({ ++ owner: context.repo.owner, ++ repo: context.repo.repo, ++ pull_number: context.issue.number, ++ }); ++ const repository = context.repo.owner + '/' + context.repo.repo; ++ if (pr.head?.repo?.full_name !== repository) { ++ throw new Error('Only source pull requests from this repository are supported'); ++ } ++ if (!/^(main|stable\d+)$/.test(pr.base?.ref || '')) { ++ throw new Error('Unsupported source base branch'); ++ } ++ const headRef = pr.head?.ref || ''; ++ if (!/^[a-zA-Z0-9_.\/-]+$/.test(headRef) || ++ headRef.startsWith('/') || headRef.endsWith('/') || ++ headRef.includes('..') || headRef.includes('//') || ++ headRef.endsWith('.lock') || headRef.includes('@{')) { ++ throw new Error('Unsafe source branch name'); ++ } ++ const shaPattern = /^[a-f0-9]{40}$/; ++ if (!shaPattern.test(pr.head?.sha || '')) { ++ throw new Error('Invalid source pull request HEAD'); ++ } ++ const merged = Boolean(pr.merged_at); ++ if (pr.state !== 'open' && !(pr.state === 'closed' && merged)) { ++ throw new Error('The source pull request was closed without being merged'); ++ } ++ if (merged && !shaPattern.test(pr.merge_commit_sha || '')) { ++ throw new Error('Merged pull request lacks a valid merge commit SHA'); ++ } ++ core.setOutput('head_ref', headRef); ++ core.setOutput('head_sha', pr.head.sha); ++ core.setOutput('base_ref', pr.base.ref); ++ core.setOutput('source_gitlink_sha', merged ? pr.merge_commit_sha : pr.head.sha); ++ core.setOutput('source_merged', String(merged)); ++ ++ - name: Add reaction on failure ++ uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 ++ if: failure() ++ with: ++ token: ${{ github.token }} ++ 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 }} ++ TARGET_PR_LABELS: ${{ vars.SUBMODULE_PR_LABELS }} ++ TARGET_PR_REVIEWER: ${{ vars.SUBMODULE_PR_REVIEWER }} ++ COMMAND_ACTOR: ${{ github.event.comment.user.login }} ++ HEAD_REF: ${{ needs.init.outputs.head_ref }} ++ HEAD_SHA: ${{ needs.init.outputs.head_sha }} ++ BASE_REF: ${{ needs.init.outputs.base_ref }} ++ SOURCE_GITLINK_SHA: ${{ needs.init.outputs.source_gitlink_sha }} ++ SOURCE_MERGED: ${{ needs.init.outputs.source_merged }} ++ PR_NUMBER: ${{ github.event.issue.number }} ++ PR_TITLE: ${{ github.event.issue.title }} ++ ++ steps: ++ - name: Validate target configuration ++ id: target-scope ++ shell: bash ++ run: | ++ set -euo pipefail ++ [[ "$TARGET_REPOSITORY" =~ ^[[:alnum:]_.-]+/[[:alnum:]_.-]+$ ]] || exit 1 ++ [[ "$SUBMODULE_PATH" =~ ^[[:alnum:]_.-]+(/[[:alnum:]_.-]+)*$ ]] || exit 1 ++ echo "owner=${TARGET_REPOSITORY%%/*}" >> "$GITHUB_OUTPUT" ++ echo "repository=${TARGET_REPOSITORY#*/}" >> "$GITHUB_OUTPUT" ++ ++ - name: Require successful source PR checks ++ shell: bash ++ env: ++ GH_TOKEN: ${{ github.token }} ++ run: | ++ set -euo pipefail ++ source_head="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.head.sha')" ++ if [[ "$source_head" != "$HEAD_SHA" ]]; then ++ echo "::error::Source PR head changed; rerun after reviewing the new revision" ++ exit 1 ++ fi ++ gh pr checks "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" ++ if [[ "$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.head.sha')" != "$HEAD_SHA" ]]; then ++ echo "::error::Source PR head changed during CI verification" ++ exit 1 ++ fi ++ ++ - 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.source_gitlink_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 ++ shell: bash ++ run: | ++ set -euo pipefail ++ if [[ "$SOURCE_MERGED" == 'true' ]]; then ++ # Compare against the first parent of the merge commit, not today's stable branch. ++ [[ "$(git rev-list --parents -n 1 HEAD | wc -w)" -eq 3 ]] || { ++ echo '::error::Merged dependency PR does not have a two-parent merge commit' ++ exit 1 ++ } ++ base_lock="$(git rev-parse HEAD^1):composer.lock" ++ else ++ base_lock="origin/$BASE_REF:composer.lock" ++ fi ++ 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 "$base_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: Reject obsolete merged source ++ if: env.SOURCE_MERGED == 'true' ++ working-directory: parent ++ shell: bash ++ env: ++ GH_TOKEN: ${{ github.token }} ++ run: | ++ set -euo pipefail ++ current_sha="$(git rev-parse "HEAD:$SUBMODULE_PATH")" ++ [[ "$current_sha" != "$SOURCE_GITLINK_SHA" ]] || { ++ echo '::error::Destination already references the merged source commit' ++ exit 1 ++ } ++ status="$(gh api "repos/$GITHUB_REPOSITORY/compare/${current_sha}...${SOURCE_GITLINK_SHA}" --jq '.status')" ++ [[ "$status" == 'ahead' ]] || { ++ echo '::error::Merged source is not a descendant of the current destination dependency' ++ exit 1 ++ } ++ ++ - name: Stage submodule reference ++ working-directory: parent ++ shell: bash ++ run: | ++ set -euo pipefail ++ git update-index --cacheinfo "160000,$SOURCE_GITLINK_SHA,$SUBMODULE_PATH" ++ ++ - name: Create scoped target GitHub App token ++ id: target-app-token ++ uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 ++ with: ++ client-id: ${{ vars.WORKFLOW_SYNC_APP_CLIENT_ID }} ++ private-key: ${{ secrets.WORKFLOW_SYNC_APP_PRIVATE_KEY }} ++ owner: ${{ steps.target-scope.outputs.owner }} ++ repositories: ${{ steps.target-scope.outputs.repository }} ++ permission-contents: write ++ permission-pull-requests: write ++ permission-workflows: write ++ ++ - name: Resolve GitHub App signing bot ++ id: signing-bot ++ shell: bash ++ env: ++ GH_TOKEN: ${{ github.token }} ++ APP_SLUG: ${{ steps.target-app-token.outputs.app-slug }} ++ run: | ++ set -euo pipefail ++ [[ "$APP_SLUG" =~ ^[a-zA-Z0-9-]+$ ]] || exit 1 ++ bot_user_id="$(gh api "users/${APP_SLUG}[bot]" --jq '.id')" ++ [[ "$bot_user_id" =~ ^[0-9]+$ ]] || exit 1 ++ echo "name=${APP_SLUG}[bot]" >> "$GITHUB_OUTPUT" ++ echo "email=${bot_user_id}+${APP_SLUG}[bot]@users.noreply.github.com" >> "$GITHUB_OUTPUT" ++ ++ - name: Configure DCO commit identity ++ working-directory: parent ++ shell: bash ++ env: ++ BOT_NAME: ${{ steps.signing-bot.outputs.name }} ++ BOT_EMAIL: ${{ steps.signing-bot.outputs.email }} ++ run: | ++ git config --local user.name "$BOT_NAME" ++ git config --local user.email "$BOT_EMAIL" ++ ++ - name: Revalidate source PR before publishing ++ shell: bash ++ env: ++ GH_TOKEN: ${{ github.token }} ++ run: | ++ set -euo pipefail ++ source_json="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")" ++ source_head="$(jq -er '.head.sha' <<< "$source_json")" ++ [[ "$source_head" == "$HEAD_SHA" ]] || { ++ echo '::error::Source PR HEAD changed before publication' ++ exit 1 ++ } ++ if [[ "$SOURCE_MERGED" == 'true' ]]; then ++ jq -e --arg sha "$SOURCE_GITLINK_SHA" \ ++ '.state == "closed" and .merged_at != null and .merge_commit_sha == $sha' \ ++ <<< "$source_json" >/dev/null || { ++ echo '::error::Source merge commit changed before publication' ++ exit 1 ++ } ++ else ++ jq -e '.state == "open" and .merged_at == null' \ ++ <<< "$source_json" >/dev/null || { ++ echo '::error::Source PR merged while preparing destination; rerun with merged SHA' ++ exit 1 ++ } + fi + + - name: Create signed target pull request diff --git a/tests/test-submodule-pr-command.test.js b/tests/test-submodule-pr-command.test.js new file mode 100644 index 0000000..eaf69ae --- /dev/null +++ b/tests/test-submodule-pr-command.test.js @@ -0,0 +1,94 @@ +// 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 { readFileSync } = require('node:fs') +const { join } = require('node:path') + +const workflow = readFileSync(join(__dirname, '..', 'workflow-templates', 'command-submodule-pr.yml'), 'utf8') +const marker = ' - name: Resolve source pull request\n' +const start = workflow.indexOf(marker) +assert.ok(start >= 0, 'source PR resolver must exist') +const body = workflow.slice(start + marker.length) +const scriptStart = body.indexOf(' script: |\n') +assert.ok(scriptStart >= 0, 'resolver must use an inline, testable script') +const lines = body.slice(scriptStart + ' script: |\n'.length).split('\n') +const script = [] +for (const line of lines) { + if (!line.startsWith(' ')) break + script.push(line.slice(12)) +} +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor +const resolve = new AsyncFunction('github', 'context', 'core', script.join('\n')) + +const HEAD = 'a'.repeat(40) +const MERGED = 'b'.repeat(40) +const standard = { + state: 'open', merged_at: null, merge_commit_sha: null, + head: { repo: { full_name: 'LibreSign/3rdparty' }, ref: 'dependabot/composer/stable33/example', sha: HEAD }, + base: { ref: 'stable33' }, +} + +async function evaluate(overrides = {}) { + const pr = structuredClone(standard) + Object.assign(pr, overrides) + const outputs = {} + const github = { rest: { pulls: { get: async () => ({ data: pr }) } } } + try { + await resolve(github, { repo: { owner: 'LibreSign', repo: '3rdparty' }, issue: { number: 77 } }, + { setOutput: (key, value) => { outputs[key] = value } }) + return { outputs } + } catch (error) { + return { error: error.message } + } +} + +test('open source uses its verified HEAD revision', async () => { + const result = await evaluate() + assert.equal(result.outputs.source_gitlink_sha, HEAD) + assert.equal(result.outputs.source_merged, 'false') + assert.equal(result.outputs.base_ref, 'stable33') +}) + +test('merged source uses the merge SHA instead of deleted branch HEAD', async () => { + const result = await evaluate({ state: 'closed', merged_at: '2026-11-22T00:00:00Z', merge_commit_sha: MERGED }) + assert.equal(result.outputs.source_gitlink_sha, MERGED) + assert.equal(result.outputs.source_merged, 'true') +}) + +test('fork is never allowed to supply the dependency ref', async () => { + const result = await evaluate({ head: { ...standard.head, repo: { full_name: 'other/fork' } } }) + assert.match(result.error, /Only source pull requests/) +}) + +test('unmerged closed source is rejected', async () => { + const result = await evaluate({ state: 'closed' }) + assert.match(result.error, /closed without being merged/) +}) + +test('unsupported branch is rejected', async () => { + const result = await evaluate({ base: { ref: 'stable32-other' } }) + assert.match(result.error, /Unsupported source base/) +}) + +test('unsafe source ref is rejected', async () => { + const result = await evaluate({ head: { ...standard.head, ref: '../../override' } }) + assert.match(result.error, /Unsafe source branch/) +}) + +test('merged PR without merge commit is rejected', async () => { + const result = await evaluate({ state: 'closed', merged_at: '2026-11-22T00:00:00Z' }) + assert.match(result.error, /lacks a valid merge/) +}) + +test('invalid HEAD SHA is rejected', async () => { + const result = await evaluate({ head: { ...standard.head, sha: 'invalid' } }) + assert.match(result.error, /Invalid source pull/) +}) + +test('merged gitlink is guarded against rollback', () => { + assert.match(workflow, /compare\/\$\{current_sha\}\.\.\.\$\{SOURCE_GITLINK_SHA\}/) + assert.match(workflow, /status.*ahead/) +}) diff --git a/upstream/templates.json b/upstream/templates.json index 2f2e3ca..bdfc6f6 100644 --- a/upstream/templates.json +++ b/upstream/templates.json @@ -151,7 +151,8 @@ "name": "nextcloud-command-submodule-pr", "source": "upstream/vendor/nextcloud/command-submodule-pr.yml", "patches": [ - "patches/nextcloud/command-submodule-pr.yml.patch" + "patches/nextcloud/command-submodule-pr.yml.patch", + "patches/nextcloud/command-submodule-post-merge.yml.patch" ], "destination": "workflow-templates/command-submodule-pr.yml" } diff --git a/workflow-templates/command-submodule-pr.yml b/workflow-templates/command-submodule-pr.yml index 2d726f6..7fa5ec9 100644 --- a/workflow-templates/command-submodule-pr.yml +++ b/workflow-templates/command-submodule-pr.yml @@ -24,30 +24,10 @@ jobs: head_ref: ${{ steps.comment-branch.outputs.head_ref }} head_sha: ${{ steps.comment-branch.outputs.head_sha }} base_ref: ${{ steps.comment-branch.outputs.base_ref }} + source_gitlink_sha: ${{ steps.comment-branch.outputs.source_gitlink_sha }} + source_merged: ${{ steps.comment-branch.outputs.source_merged }} 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: @@ -61,9 +41,46 @@ jobs: comment-id: ${{ github.event.comment.id }} reactions: '+1' - - name: Init branch - uses: xt0rted/pull-request-comment-branch@e8b8daa837e8ea7331c0003c9c316a64c6d8b0b1 # v3.0.0 + - name: Resolve source pull request + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 id: comment-branch + with: + script: | + const {data: pr} = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number, + }); + const repository = context.repo.owner + '/' + context.repo.repo; + if (pr.head?.repo?.full_name !== repository) { + throw new Error('Only source pull requests from this repository are supported'); + } + if (!/^(main|stable\d+)$/.test(pr.base?.ref || '')) { + throw new Error('Unsupported source base branch'); + } + const headRef = pr.head?.ref || ''; + if (!/^[a-zA-Z0-9_.\/-]+$/.test(headRef) || + headRef.startsWith('/') || headRef.endsWith('/') || + headRef.includes('..') || headRef.includes('//') || + headRef.endsWith('.lock') || headRef.includes('@{')) { + throw new Error('Unsafe source branch name'); + } + const shaPattern = /^[a-f0-9]{40}$/; + if (!shaPattern.test(pr.head?.sha || '')) { + throw new Error('Invalid source pull request HEAD'); + } + const merged = Boolean(pr.merged_at); + if (pr.state !== 'open' && !(pr.state === 'closed' && merged)) { + throw new Error('The source pull request was closed without being merged'); + } + if (merged && !shaPattern.test(pr.merge_commit_sha || '')) { + throw new Error('Merged pull request lacks a valid merge commit SHA'); + } + core.setOutput('head_ref', headRef); + core.setOutput('head_sha', pr.head.sha); + core.setOutput('base_ref', pr.base.ref); + core.setOutput('source_gitlink_sha', merged ? pr.merge_commit_sha : pr.head.sha); + core.setOutput('source_merged', String(merged)); - name: Add reaction on failure uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 @@ -87,6 +104,8 @@ jobs: HEAD_REF: ${{ needs.init.outputs.head_ref }} HEAD_SHA: ${{ needs.init.outputs.head_sha }} BASE_REF: ${{ needs.init.outputs.base_ref }} + SOURCE_GITLINK_SHA: ${{ needs.init.outputs.source_gitlink_sha }} + SOURCE_MERGED: ${{ needs.init.outputs.source_merged }} PR_NUMBER: ${{ github.event.issue.number }} PR_TITLE: ${{ github.event.issue.title }} @@ -124,7 +143,7 @@ jobs: path: source persist-credentials: false fetch-depth: 0 - ref: ${{ needs.init.outputs.head_sha }} + ref: ${{ needs.init.outputs.source_gitlink_sha }} - name: Set up php uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 @@ -134,13 +153,25 @@ jobs: - name: Generate composer diff working-directory: source + shell: bash run: | + set -euo pipefail + if [[ "$SOURCE_MERGED" == 'true' ]]; then + # Compare against the first parent of the merge commit, not today's stable branch. + [[ "$(git rev-list --parents -n 1 HEAD | wc -w)" -eq 3 ]] || { + echo '::error::Merged dependency PR does not have a two-parent merge commit' + exit 1 + } + base_lock="$(git rev-parse HEAD^1):composer.lock" + else + base_lock="origin/$BASE_REF:composer.lock" + fi 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 + composer diff --with-links --base "$base_lock" --target composer.lock } > "$RUNNER_TEMP/pr-body.md" cat "$RUNNER_TEMP/pr-body.md" @@ -160,12 +191,31 @@ jobs: exit 1 fi + - name: Reject obsolete merged source + if: env.SOURCE_MERGED == 'true' + working-directory: parent + shell: bash + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + current_sha="$(git rev-parse "HEAD:$SUBMODULE_PATH")" + [[ "$current_sha" != "$SOURCE_GITLINK_SHA" ]] || { + echo '::error::Destination already references the merged source commit' + exit 1 + } + status="$(gh api "repos/$GITHUB_REPOSITORY/compare/${current_sha}...${SOURCE_GITLINK_SHA}" --jq '.status')" + [[ "$status" == 'ahead' ]] || { + echo '::error::Merged source is not a descendant of the current destination dependency' + exit 1 + } + - name: Stage submodule reference working-directory: parent shell: bash run: | set -euo pipefail - git update-index --cacheinfo "160000,$HEAD_SHA,$SUBMODULE_PATH" + git update-index --cacheinfo "160000,$SOURCE_GITLINK_SHA,$SUBMODULE_PATH" - name: Create scoped target GitHub App token id: target-app-token @@ -209,10 +259,25 @@ jobs: GH_TOKEN: ${{ github.token }} run: | set -euo pipefail - source_head="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '.head.sha')" - if [[ "$source_head" != "$HEAD_SHA" ]]; then - echo "::error::Source PR head changed before publication" + source_json="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")" + source_head="$(jq -er '.head.sha' <<< "$source_json")" + [[ "$source_head" == "$HEAD_SHA" ]] || { + echo '::error::Source PR HEAD changed before publication' exit 1 + } + if [[ "$SOURCE_MERGED" == 'true' ]]; then + jq -e --arg sha "$SOURCE_GITLINK_SHA" \ + '.state == "closed" and .merged_at != null and .merge_commit_sha == $sha' \ + <<< "$source_json" >/dev/null || { + echo '::error::Source merge commit changed before publication' + exit 1 + } + else + jq -e '.state == "open" and .merged_at == null' \ + <<< "$source_json" >/dev/null || { + echo '::error::Source PR merged while preparing destination; rerun with merged SHA' + exit 1 + } fi - name: Create signed target pull request