Skip to content

Commit c5538c2

Browse files
Refactor dry-run to else block; update in-memory issue state for accurate preview
1 parent 0459d86 commit c5538c2

2 files changed

Lines changed: 74 additions & 39 deletions

File tree

.github/actions/file/src/index.ts

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,58 +71,61 @@ export default async function () {
7171
if (dryRun) {
7272
if (isResolvedFiling(filing)) {
7373
dryRunCounts.close++
74+
filing.issue.state = 'closed'
7475
core.info(`[dry run] Would CLOSE issue: ${filing.issue.url}`)
7576
} else if (isNewFiling(filing)) {
7677
dryRunCounts.open++
78+
;(filing as Filing).issue = {state: 'open'} as Issue
7779
core.info(
7880
`[dry run] Would OPEN a new issue for: ${filing.findings[0].problemShort} (${filing.findings[0].url})`,
7981
)
8082
} else if (isRepeatedFiling(filing)) {
8183
dryRunCounts.reopen++
84+
filing.issue.state = 'reopened'
8285
core.info(`[dry run] Would REOPEN issue: ${filing.issue.url}`)
8386
}
84-
continue
85-
}
86-
if (isResolvedFiling(filing)) {
87-
// Close the filing’s issue (if necessary)
88-
response = await closeIssue(octokit, new Issue(filing.issue))
89-
filing.issue.state = 'closed'
90-
} else if (isNewFiling(filing)) {
91-
// Open a new issue for the filing
92-
response = await openIssue(octokit, repoWithOwner, filing.findings[0], screenshotRepo)
93-
;(filing as Filing).issue = {state: 'open'} as Issue
87+
} else {
88+
if (isResolvedFiling(filing)) {
89+
// Close the filing's issue (if necessary)
90+
response = await closeIssue(octokit, new Issue(filing.issue))
91+
filing.issue.state = 'closed'
92+
} else if (isNewFiling(filing)) {
93+
// Open a new issue for the filing
94+
response = await openIssue(octokit, repoWithOwner, filing.findings[0], screenshotRepo)
95+
;(filing as Filing).issue = {state: 'open'} as Issue
9496

95-
// Track for grouping
96-
if (shouldOpenGroupedIssues) {
97-
const problemShort: string = filing.findings[0].problemShort
98-
if (!newIssuesByProblemShort[problemShort]) {
99-
newIssuesByProblemShort[problemShort] = []
97+
// Track for grouping
98+
if (shouldOpenGroupedIssues) {
99+
const problemShort: string = filing.findings[0].problemShort
100+
if (!newIssuesByProblemShort[problemShort]) {
101+
newIssuesByProblemShort[problemShort] = []
102+
}
103+
newIssuesByProblemShort[problemShort].push({
104+
url: response.data.html_url,
105+
id: response.data.number,
106+
})
100107
}
101-
newIssuesByProblemShort[problemShort].push({
102-
url: response.data.html_url,
103-
id: response.data.number,
104-
})
108+
} else if (isRepeatedFiling(filing)) {
109+
// Reopen the filing's issue (if necessary) and update the body with the latest finding
110+
response = await reopenIssue(
111+
octokit,
112+
new Issue(filing.issue),
113+
filing.findings[0],
114+
repoWithOwner,
115+
screenshotRepo,
116+
)
117+
filing.issue.state = 'reopened'
118+
}
119+
if (response?.data && filing.issue) {
120+
// Update the filing with the latest issue data
121+
filing.issue.id = response.data.id
122+
filing.issue.nodeId = response.data.node_id
123+
filing.issue.url = response.data.html_url
124+
filing.issue.title = response.data.title
125+
core.info(
126+
`Set issue ${response.data.title} (${repoWithOwner}#${response.data.number}) state to ${filing.issue.state}`,
127+
)
105128
}
106-
} else if (isRepeatedFiling(filing)) {
107-
// Reopen the filing's issue (if necessary) and update the body with the latest finding
108-
response = await reopenIssue(
109-
octokit,
110-
new Issue(filing.issue),
111-
filing.findings[0],
112-
repoWithOwner,
113-
screenshotRepo,
114-
)
115-
filing.issue.state = 'reopened'
116-
}
117-
if (response?.data && filing.issue) {
118-
// Update the filing with the latest issue data
119-
filing.issue.id = response.data.id
120-
filing.issue.nodeId = response.data.node_id
121-
filing.issue.url = response.data.html_url
122-
filing.issue.title = response.data.title
123-
core.info(
124-
`Set issue ${response.data.title} (${repoWithOwner}#${response.data.number}) state to ${filing.issue.state}`,
125-
)
126129
}
127130
} catch (error) {
128131
core.setFailed(`Failed on filing: ${JSON.stringify(filing, null, 2)}\n${error}`)

.github/actions/file/tests/dryRun.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,38 @@ describe('file action — dry_run', () => {
145145
expect(outputs.filings_file).toBeDefined()
146146
})
147147

148+
it('updates in-memory issue state for an accurate preview without mutating remotely', async () => {
149+
setup()
150+
inputs.dry_run = 'true'
151+
152+
await runFileAction()
153+
154+
// The path written is `${RUNNER_TEMP||'/tmp'}/filings-<uuid>.json`; grab it from the output.
155+
const writtenPath = outputs.filings_file
156+
const writtenFilings = JSON.parse(files[writtenPath])
157+
158+
// Resolved cached filing (issues/2) -> would be CLOSED
159+
const resolved = writtenFilings.find(
160+
(f: {issue?: {url?: string}}) => f.issue?.url === 'https://github.com/org/repo/issues/2',
161+
)
162+
expect(resolved?.issue.state).toBe('closed')
163+
164+
// Repeated cached filing (issues/1) -> would be REOPENED
165+
const repeated = writtenFilings.find(
166+
(f: {issue?: {url?: string}}) => f.issue?.url === 'https://github.com/org/repo/issues/1',
167+
)
168+
expect(repeated?.issue.state).toBe('reopened')
169+
170+
// New filing -> issue object created with state 'open'
171+
const opened = writtenFilings.find((f: {issue?: {state?: string}}) => f.issue?.state === 'open')
172+
expect(opened).toBeDefined()
173+
174+
// And confirm we still didn't actually mutate anything remotely
175+
expect(openIssue).not.toHaveBeenCalled()
176+
expect(reopenIssue).not.toHaveBeenCalled()
177+
expect(closeIssue).not.toHaveBeenCalled()
178+
})
179+
148180
it('does call the mutating helpers when dry_run is false (regression guard)', async () => {
149181
setup()
150182
inputs.dry_run = 'false'

0 commit comments

Comments
 (0)