diff --git a/scripts/cicd/backport-conflict-comment.test.ts b/scripts/cicd/backport-conflict-comment.test.ts index fb4115c9028..756d16c5af9 100644 --- a/scripts/cicd/backport-conflict-comment.test.ts +++ b/scripts/cicd/backport-conflict-comment.test.ts @@ -34,6 +34,19 @@ if (!commentStep?.run || !commentStep.env) { } const { run: commentRun, env: commentEnv } = commentStep +const forceRerunExpression = + /^\$\{\{ github\.event_name == '([a-z_]+)' && inputs\.force_rerun \|\| 'false' \}\}$/.exec( + commentEnv.FORCE_RERUN + ) +if (!forceRerunExpression) { + throw new Error( + `unexpected FORCE_RERUN expression in pr-backport.yaml: ${commentEnv.FORCE_RERUN}` + ) +} +const forceRerunEvent = forceRerunExpression[1] +const evaluateForceRerun = (eventName: string, forceRerunInput: boolean) => + eventName === forceRerunEvent && forceRerunInput ? 'true' : 'false' + beforeAll(() => { const missing = ['envsubst', 'jq'].filter( (tool) => spawnSync('bash', ['-c', `command -v ${tool}`]).status !== 0 @@ -78,6 +91,11 @@ case "$1 $2" in "issue create") record "$4" "$6" >> "\${FAKE_DIR}/issues.jsonl" ;; + "pr view") + [[ "$3" == "\${FAKE_PR_NUMBER}" && "$4 $5" == "--json author,mergeCommit" ]] || { + echo "unexpected gh pr view invocation: $*" >&2; exit 64; } + jq -cn --arg oid "\${FAKE_MERGE_COMMIT}" '{author: {login: "pr-view-author"}, mergeCommit: {oid: $oid}}' + ;; "pr list") ;; *) echo "unexpected gh invocation: $*" >&2; exit 64 ;; esac @@ -102,6 +120,7 @@ function createSandbox() { } interface Run { + event?: 'pull_request_target' | 'workflow_dispatch' mergeCommit?: string targetHead?: string failed?: string @@ -114,6 +133,7 @@ interface Run { function reportConflict( sandbox: string, { + event = 'pull_request_target', mergeCommit = firstMergeCommit, targetHead = firstTargetHead, failed = coreConflict, @@ -123,19 +143,23 @@ function reportConflict( prLocked = false }: Run = {} ) { - const event = join(sandbox, 'event.json') + const eventPath = join(sandbox, 'event.json') writeFileSync( - event, - JSON.stringify({ - pull_request: { - number: 21078, - user: { login: 'author' }, - merge_commit_sha: mergeCommit - } - }) + eventPath, + JSON.stringify( + event === 'workflow_dispatch' + ? { inputs: { pr_number: '21078', force_rerun: String(forceRerun) } } + : { + pull_request: { + number: 21078, + user: { login: 'author' }, + merge_commit_sha: mergeCommit + } + } + ) ) const script = commentRun - .replaceAll('${{ github.event_name }}', 'pull_request_target') + .replaceAll('${{ github.event_name }}', event) .replaceAll('${{ inputs.pr_number }}', '21078') .replaceAll('${{ steps.backport.outputs.failed }}', failed) @@ -146,12 +170,14 @@ function reportConflict( BACKPORT_AGENT_PROMPT_TEMPLATE: commentEnv.BACKPORT_AGENT_PROMPT_TEMPLATE, COMMENT_BODY_TEMPLATE: commentEnv.COMMENT_BODY_TEMPLATE, FAKE_DIR: sandbox, + FAKE_MERGE_COMMIT: mergeCommit, + FAKE_PR_NUMBER: '21078', FAKE_LOGIN: login, FAKE_PR_LOCKED: String(prLocked), FAKE_READ: commentsRead, FAKE_TARGET_HEAD: targetHead, - FORCE_RERUN: String(forceRerun), - GITHUB_EVENT_PATH: event, + FORCE_RERUN: evaluateForceRerun(event, forceRerun), + GITHUB_EVENT_PATH: eventPath, GITHUB_REPOSITORY: 'Comfy-Org/ComfyUI_frontend', GITHUB_SERVER_URL: 'https://github.com', PATH: `${sandbox}:${process.env.PATH}` @@ -213,10 +239,20 @@ describe('backport conflict comment', () => { expected: 2 }, { - scenario: 'a manual force rerun', - second: { forceRerun: true }, + scenario: 'a manual dispatch with force_rerun', + second: { event: 'workflow_dispatch', forceRerun: true }, expected: 2 }, + { + scenario: 'a manual dispatch without force_rerun', + second: { event: 'workflow_dispatch' }, + expected: 1 + }, + { + scenario: 'force_rerun set outside a manual dispatch', + second: { forceRerun: true }, + expected: 1 + }, { scenario: 'a failed read of the existing comments', second: { commentsRead: 'error' }, @@ -245,6 +281,23 @@ describe('backport conflict comment', () => { } ) + it('reads the author and merge commit from the PR on a manual dispatch', () => { + const sandbox = createSandbox() + + reportConflict(sandbox, { + event: 'workflow_dispatch', + forceRerun: true, + mergeCommit: secondMergeCommit + }) + + const reports = postedReports(sandbox) + expect(reports).toHaveLength(1) + expect(reports[0]).toContain( + `` + ) + expect(reports[0]).toContain('cc @pr-view-author') + }) + it('reports the same merge commit once per target', () => { const sandbox = createSandbox()