Skip to content

Commit e259b23

Browse files
authored
fix(version-dispatch): clear preview comment on every gated early-return (#83)
When an open PR transitions into a gated state mid-flight — someone adds a \`skip-release\` label, retargets the base off the default branch, or the workspace map suddenly returns empty — the action used to return early without touching any prior preview comment, so the stale "Version preview" comment lingered on the PR. Route every preview-mode early-return through the new \`clearVersionPreview\` helper which deletes any prior preview comment by marker. Adds two integration tests covering the \`skip-release\` and non-default-branch transitions. 160/160 tests passing.
1 parent 96c8400 commit e259b23

5 files changed

Lines changed: 100 additions & 3 deletions

File tree

dist/version-dispatch/index.js

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/version-dispatch/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/version-dispatch.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,63 @@ describe('versionDispatch', () => {
355355
expect(client.rest.issues.createComment).toHaveBeenCalledTimes(1)
356356
})
357357

358+
it('clears stale preview comments when skip-release is added mid-flight', async () => {
359+
github.context.payload = {
360+
pull_request: {
361+
title: 'feat: pending',
362+
number: 555,
363+
merged: false,
364+
state: 'open',
365+
user: { login: 'brucewayne' },
366+
base: { ref },
367+
labels: [{ name: 'skip-release' }],
368+
},
369+
}
370+
371+
setupPreviewPaginate(
372+
[{ sha: 'aaa1111', commit: { message: 'feat(atoms)!: drop legacy' } }],
373+
{ aaa1111: [{ filename: 'libraries/atoms/index.ts' }] },
374+
[{ id: 9100, body: `${PREVIEW_MARKER}\nstale preview from before the label was added` }]
375+
)
376+
377+
await versionDispatch({ filesystem: fs as never })
378+
379+
expect(client.rest.issues.deleteComment).toHaveBeenCalledWith({
380+
...repo,
381+
comment_id: 9100,
382+
})
383+
expect(client.rest.issues.createComment).not.toHaveBeenCalled()
384+
expect(client.rest.actions.createWorkflowDispatch).not.toHaveBeenCalled()
385+
})
386+
387+
it('clears stale preview comments when the PR base is retargeted off the default branch', async () => {
388+
github.context.payload = {
389+
pull_request: {
390+
title: 'feat: pending',
391+
number: 555,
392+
merged: false,
393+
state: 'open',
394+
user: { login: 'brucewayne' },
395+
base: { ref: 'wayne-foundation/batmobile-v2' },
396+
labels: [],
397+
},
398+
}
399+
400+
setupPreviewPaginate(
401+
[{ sha: 'aaa1111', commit: { message: 'feat(atoms)!: drop legacy' } }],
402+
{ aaa1111: [{ filename: 'libraries/atoms/index.ts' }] },
403+
[{ id: 9101, body: `${PREVIEW_MARKER}\nstale preview from when base was master` }]
404+
)
405+
406+
await versionDispatch({ filesystem: fs as never })
407+
408+
expect(client.rest.issues.deleteComment).toHaveBeenCalledWith({
409+
...repo,
410+
comment_id: 9101,
411+
})
412+
expect(client.rest.issues.createComment).not.toHaveBeenCalled()
413+
})
414+
358415
it('clears stale comments and posts nothing when no commits bump anything', async () => {
359416
github.context.payload = {
360417
pull_request: {

src/version-dispatch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { VersionDispatchInput as Input } from './constants'
77
import { Filesystem } from './utils/types'
88
import { Bump, BUMP_NONE, bumpFromMessage, maxBump } from './version-dispatch/bumps'
99
import { filesToPackages } from './version-dispatch/files-to-packages'
10-
import { postVersionPreview } from './version-dispatch/preview'
10+
import { clearVersionPreview, postVersionPreview } from './version-dispatch/preview'
1111

1212
type Params = {
1313
filesystem?: Filesystem
@@ -86,6 +86,7 @@ export async function versionDispatch({ filesystem = fs }: Params = {}) {
8686
const excludedLabel = (pr.labels ?? []).find((label) => excludedLabels.has(label.name))
8787
if (excludedLabel) {
8888
core.notice(`Skipped for excluded label "${excludedLabel.name}"`)
89+
if (isPreview) await clearVersionPreview({ client, repo, prNumber: pr.number })
8990
return null
9091
}
9192

@@ -95,12 +96,14 @@ export async function versionDispatch({ filesystem = fs }: Params = {}) {
9596

9697
if (pr.base?.ref && pr.base.ref !== defaultBranch) {
9798
core.notice(`Skipped versioning for PR not targeting ${defaultBranch}`)
99+
if (isPreview) await clearVersionPreview({ client, repo, prNumber: pr.number })
98100
return null
99101
}
100102

101103
const packagePaths = await getPathsByPackageNames({ filesystem })
102104
if (Object.keys(packagePaths).length === 0) {
103105
core.warning('No workspace packages discovered. Aborting.')
106+
if (isPreview) await clearVersionPreview({ client, repo, prNumber: pr.number })
104107
return null
105108
}
106109

src/version-dispatch/preview.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ export function renderPreviewComment(rows: PreviewRow[]): string {
123123
return lines.join('\n')
124124
}
125125

126+
/**
127+
* Delete every prior preview comment on the PR. Used by the action's
128+
* early-return paths (excluded label, wrong base branch, no workspace
129+
* packages, etc.) to make sure a previously-posted preview comment
130+
* does not linger after the PR transitions into a gated state.
131+
*/
132+
export async function clearVersionPreview({
133+
client,
134+
repo,
135+
prNumber,
136+
}: {
137+
client: GithubClient
138+
repo: Repo
139+
prNumber: number
140+
}): Promise<void> {
141+
const deleted = await deleteExistingPreviewComments({ client, repo, prNumber })
142+
if (deleted > 0) core.info(`preview: cleared ${deleted} stale preview comment(s)`)
143+
}
144+
126145
async function deleteExistingPreviewComments({
127146
client,
128147
repo,

0 commit comments

Comments
 (0)