@@ -12,47 +12,52 @@ if (!markdown || !token || !limit_to_pr_opened) {
12
12
13
13
// Main function
14
14
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 || ''
17
17
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.' )
20
20
return
21
21
}
22
22
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
+ }
25
28
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 || ''
32
31
33
32
// 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.' )
36
35
return
37
36
}
38
37
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 } `
52
44
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
+ } )
55
54
}
56
55
57
56
// 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