Skip to content

Commit 21026c0

Browse files
authored
chore(ci): refactor (#315)
1 parent d405234 commit 21026c0

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/main.ts

+35-30
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,52 @@ if (!markdown || !token || !limit_to_pr_opened) {
1212

1313
// Main function
1414
async function action(): Promise<void> {
15-
// If limit_to_pr_opened is true, then verify status (opened, reopened)
16-
const trigger = JSON.stringify(context.payload.action) || ''
15+
// If limit_to_pr_opened is true, verify status (opened, reopened)
16+
const trigger = context.payload.action || ''
1717
const statuses = ['opened', 'reopened']
18-
if (limit_to_pr_opened === 'true' && statuses.includes(trigger)) {
19-
info('PR not opened or reopened with limit_to_pr_opened=true. Exiting.')
18+
if (limit_to_pr_opened === 'true' && !statuses.includes(trigger)) {
19+
info('PR not opened or reopened with limit_to_pr_opened=true. Exiting.')
2020
return
2121
}
2222

23-
// Get PR body from GitHub context (previously using octokit)
24-
const body = context.payload.pull_request?.body || ''
23+
// Ensure pull request exists
24+
if (!context.payload.pull_request) {
25+
error('Error: No pull request found in context. Exiting.')
26+
return
27+
}
2528

26-
// Note: Any of these checks can work
27-
// body.includes(markdown)
28-
// body.endsWith(markdown)
29-
// body.match(new RegExp(markdown))
30-
// !~body.indexOf(markdown)
31-
// !~body.search(markdown)
29+
// Get PR body from GitHub context
30+
const body = context.payload.pull_request?.body || ''
3231

3332
// If message is already present, then return/exit
34-
if (body.endsWith(markdown)) {
35-
info('Markdown message is already present. Exiting.')
33+
if (body.includes(markdown)) {
34+
info('Markdown message is already present. Exiting.')
3635
return
3736
}
3837

39-
// If not present, then append
40-
const octokit = getOctokit(token)
41-
if (!body.endsWith(markdown)) {
42-
info('Description is being updated.')
43-
await octokit.rest.pulls.update({
44-
owner: context.repo.owner,
45-
repo: context.repo.repo,
46-
pull_number: context.payload.number,
47-
// Split out any duplicate messages, has been an issue
48-
body: body.split(markdown)[0].concat(`\n\n${markdown}`)
49-
})
50-
return
51-
}
38+
// Append markdown after removing existing duplicates
39+
const updatedBody = `${body
40+
.split('\n')
41+
.filter(line => line.trim() !== markdown.trim())
42+
.join('\n')
43+
.trim()}\n\n${markdown}`
5244

53-
// If here, kick up an error
54-
error('Unexpected result. Please verify the action has performed correctly.')
45+
// Update PR body
46+
const octokit = getOctokit(token)
47+
info('Description is being updated.')
48+
await octokit.rest.pulls.update({
49+
owner: context.repo.owner,
50+
repo: context.repo.repo,
51+
pull_number: context.payload.pull_request.number,
52+
body: updatedBody
53+
})
5554
}
5655

5756
// Run main function
58-
action()
57+
;(async () => {
58+
try {
59+
await action()
60+
} catch (err) {
61+
error(`Unexpected error: ${err instanceof Error ? err.message : err}`)
62+
}
63+
})()

0 commit comments

Comments
 (0)