Repository navigation
fix: skip backport targets whose backport PR already merged #21994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2a44b7e
fix: skip backport targets whose backport PR already merged
mattmillerai 79be713
fix: ignore fork backport PRs and clear label when all targets merged
mattmillerai c40ad07
test: project gh stub onto requested fields and cover mixed completion
mattmillerai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| import { spawnSync } from 'node:child_process' | ||
| import { | ||
| chmodSync, | ||
| mkdtempSync, | ||
| readFileSync, | ||
| rmSync, | ||
| writeFileSync | ||
| } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest' | ||
| import { parse } from 'yaml' | ||
| import { z } from 'zod' | ||
|
|
||
| const workflowSchema = z.object({ | ||
| jobs: z.object({ | ||
| backport: z.object({ | ||
| steps: z.array( | ||
| z.object({ id: z.string().optional(), run: z.string().optional() }) | ||
| ) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| const filterScript = () => { | ||
| const workflow = workflowSchema.parse( | ||
| parse(readFileSync('.github/workflows/pr-backport.yaml', 'utf8')) | ||
| ) | ||
| const step = workflow.jobs.backport.steps.find( | ||
| (step) => step.id === 'filter-targets' | ||
| ) | ||
| if (!step?.run) throw new Error('Missing filter-targets script') | ||
| return step.run | ||
| } | ||
|
|
||
| const GH_STUB = `#!/usr/bin/env bash | ||
| state="" head="" base="" filter="." fields="" | ||
| while [ $# -gt 0 ]; do | ||
| case "$1" in | ||
| --state) state="$2"; shift 2 ;; | ||
| --head) head="$2"; shift 2 ;; | ||
| --base) base="$2"; shift 2 ;; | ||
| --json) fields="$2"; shift 2 ;; | ||
| --jq) filter="$2"; shift 2 ;; | ||
| *) shift ;; | ||
| esac | ||
| done | ||
| matches="[]" | ||
| while IFS='|' read -r s h b n cross; do | ||
| [ -z "$s" ] && continue | ||
| if [ "$s" = "$state" ] && [ "$h" = "$head" ] && { [ -z "$base" ] || [ "$b" = "$base" ]; }; then | ||
| matches=$(jq -c --argjson n "$n" --argjson cross "$cross" '. + [{number: $n, isCrossRepository: $cross}]' <<< "$matches") | ||
| fi | ||
| done <<< "$STUB_PRS" | ||
| matches=$(jq -c --arg fields "$fields" 'map(with_entries(select(.key as $key | $fields | split(",") | index($key))))' <<< "$matches") | ||
| jq -r "$filter" <<< "$matches" | ||
| ` | ||
|
|
||
| const GIT_STUB = `#!/usr/bin/env bash | ||
| for branch in $STUB_BRANCHES; do | ||
| printf '0000000\\trefs/heads/%s\\n' "$branch" | ||
| done | ||
| ` | ||
|
|
||
| interface StubPr { | ||
| state: 'open' | 'merged' | ||
| head: string | ||
| base: string | ||
| number: number | ||
| crossRepository?: boolean | ||
| } | ||
|
|
||
| interface Scenario { | ||
| targets: string[] | ||
| prs?: StubPr[] | ||
| branches?: string[] | ||
| forceRerun?: boolean | ||
| } | ||
|
|
||
| let dir: string | ||
|
|
||
| beforeEach(() => { | ||
| dir = mkdtempSync(join(tmpdir(), 'pr-backport-filter-')) | ||
| for (const [name, body] of [ | ||
| ['gh', GH_STUB], | ||
| ['git', GIT_STUB] | ||
| ]) { | ||
| writeFileSync(join(dir, name), body) | ||
| chmodSync(join(dir, name), 0o755) | ||
| } | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| function runFilter({ | ||
| targets, | ||
| prs = [], | ||
| branches = [], | ||
| forceRerun = false | ||
| }: Scenario) { | ||
| const outputFile = join(dir, 'output') | ||
| writeFileSync(outputFile, '') | ||
| const result = spawnSync( | ||
| 'bash', | ||
| ['--noprofile', '--norc', '-c', filterScript()], | ||
| { | ||
| env: { | ||
| PATH: `${dir}:${process.env.PATH}`, | ||
| GITHUB_OUTPUT: outputFile, | ||
| EVENT_NAME: forceRerun ? 'workflow_dispatch' : 'pull_request_target', | ||
| FORCE_RERUN_INPUT: String(forceRerun), | ||
| PR_NUMBER: '21829', | ||
| REQUESTED_TARGETS: targets.join(' '), | ||
| STUB_PRS: prs | ||
| .map((pr) => | ||
| [ | ||
| pr.state, | ||
| pr.head, | ||
| pr.base, | ||
| pr.number, | ||
| pr.crossRepository ?? false | ||
| ].join('|') | ||
| ) | ||
| .join('\n'), | ||
| STUB_BRANCHES: branches.join(' ') | ||
| }, | ||
| encoding: 'utf8' | ||
| } | ||
| ) | ||
| if (result.status !== 0) throw new Error(result.stderr) | ||
| return Object.fromEntries( | ||
| readFileSync(outputFile, 'utf8') | ||
| .split('\n') | ||
| .filter(Boolean) | ||
| .map((line) => { | ||
| const index = line.indexOf('=') | ||
| return [line.slice(0, index), line.slice(index + 1)] | ||
| }) | ||
| ) | ||
| } | ||
|
|
||
| const mergedCore157: StubPr = { | ||
| state: 'merged', | ||
| head: 'backport-21829-to-core-1.57', | ||
| base: 'core/1.57', | ||
| number: 21880 | ||
| } | ||
|
|
||
| describe('pr-backport filter-targets', () => { | ||
| it('attempts only the new target when an old target was already merged', () => { | ||
| expect( | ||
| runFilter({ targets: ['core/1.57', 'cloud/1.58'], prs: [mergedCore157] }) | ||
| ).toMatchObject({ | ||
| skip: 'false', | ||
| 'pending-targets': 'cloud/1.58', | ||
| 'already-exists': 'core/1.57 (merged PR #21880)', | ||
| 'all-merged': 'false' | ||
| }) | ||
| }) | ||
|
mattmillerai marked this conversation as resolved.
Resolved
|
||
|
|
||
| it('keeps completion false when a merged target and an open target are skipped', () => { | ||
| expect( | ||
| runFilter({ | ||
| targets: ['core/1.57', 'cloud/1.58'], | ||
| branches: ['backport-21829-to-cloud-1.58'], | ||
| prs: [ | ||
| mergedCore157, | ||
| { | ||
| state: 'open', | ||
| head: 'backport-21829-to-cloud-1.58', | ||
| base: 'cloud/1.58', | ||
| number: 21881 | ||
| } | ||
| ] | ||
| }) | ||
| ).toMatchObject({ skip: 'true', 'all-merged': 'false' }) | ||
| }) | ||
|
|
||
| it('skips the run when every target was already merged', () => { | ||
| expect( | ||
| runFilter({ targets: ['core/1.57'], prs: [mergedCore157] }) | ||
| ).toMatchObject({ | ||
| skip: 'true', | ||
| 'pending-targets': '', | ||
| 'all-merged': 'true' | ||
| }) | ||
| }) | ||
|
|
||
| it.for([ | ||
| { name: 'a different base', pr: { ...mergedCore157, base: 'main' } }, | ||
| { name: 'a fork', pr: { ...mergedCore157, crossRepository: true } } | ||
| ])('ignores a merged PR from $name', ({ pr }) => { | ||
| expect(runFilter({ targets: ['core/1.57'], prs: [pr] })).toMatchObject({ | ||
| skip: 'false', | ||
| 'pending-targets': 'core/1.57' | ||
| }) | ||
| }) | ||
|
|
||
| it('retries a merged target when force_rerun is set', () => { | ||
| expect( | ||
| runFilter({ | ||
| targets: ['core/1.57'], | ||
| prs: [mergedCore157], | ||
| forceRerun: true | ||
| }) | ||
| ).toMatchObject({ skip: 'false', 'pending-targets': 'core/1.57' }) | ||
| }) | ||
|
|
||
| it('still skips a target whose backport PR is open', () => { | ||
| expect( | ||
| runFilter({ | ||
| targets: ['core/1.57'], | ||
| branches: ['backport-21829-to-core-1.57'], | ||
| prs: [{ ...mergedCore157, state: 'open', number: 21881 }] | ||
| }) | ||
| ).toMatchObject({ | ||
| skip: 'true', | ||
| 'all-merged': 'false', | ||
| 'already-exists': 'core/1.57 (PR #21881)' | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.