@@ -5,10 +5,10 @@ const minimatch = require("minimatch");
55const { readFileSync } = require ( "fs" ) ;
66
77const header = core . getInput ( "comment-header" ) ;
8- const footer = core . getInput ( "comment-footer" )
8+ const footer = core . getInput ( "comment-footer" ) ;
99
1010const minimatchOptions = {
11- dot : core . getInput ( ' include-hidden-files' ) === ' true'
11+ dot : core . getInput ( " include-hidden-files" ) === " true" ,
1212} ;
1313
1414function getChecklistPaths ( ) : Record < string , string [ ] > {
@@ -17,22 +17,56 @@ function getChecklistPaths(): Record<string, string[]> {
1717 return parsedFile . paths ;
1818}
1919
20- function formatItemsForPath ( [ path , items ] ) : string {
21- const showPaths = core . getInput ( "show-paths" ) === 'true' ;
20+ function formatItemsForPath ( previousComment , [ path , items ] ) {
21+ const showPaths = core . getInput ( "show-paths" ) === "true" ;
22+ const mergeComment = core . getInput ( "merge-comment" ) === "true" ;
23+
24+ if ( ! ! previousComment && mergeComment ) {
25+ const existingCheckedItems = previousComment
26+ . split ( "\n" )
27+ . filter ( ( line ) => line !== "" && line . startsWith ( "- [x]" ) )
28+ . map ( ( line ) => line . substring ( 5 ) . trim ( ) ) ;
29+ const preservedItems = items . filter ( ( item ) => {
30+ return ! ! existingCheckedItems . find ( ( existingItem ) =>
31+ existingItem . includes ( item )
32+ ) ;
33+ } ) ;
34+ const newItems = items . filter ( ( item ) => {
35+ return ! existingCheckedItems . find ( ( existingItem ) =>
36+ existingItem . includes ( item )
37+ ) ;
38+ } ) ;
39+
40+ return showPaths
41+ ? [
42+ `__Files matching \`${ path } \`:__\n` ,
43+ ...preservedItems . map ( ( item ) => `- [x] ${ item } \n` ) ,
44+ ...newItems . map ( ( item ) => `- [ ] ${ item } \n` ) ,
45+ "\n" ,
46+ ] . join ( "" )
47+ : [
48+ ...preservedItems . map ( ( item ) => `- [x] ${ item } \n` ) ,
49+ ...newItems . map ( ( item ) => `- [ ] ${ item } \n` ) ,
50+ ] . join ( "" ) ;
51+ }
2252
2353 return showPaths
24- ? [
25- `__Files matching \`${ path } \`:__\n` ,
26- ...items . map ( ( item ) => `- [ ] ${ item } \n` ) ,
27- "\n" ,
28- ] . join ( "" )
29- : [ ...items . map ( ( item ) => `- [ ] ${ item } \n` ) ] . join ( "" ) ;
54+ ? [
55+ `__Files matching \`${ path } \`:__\n` ,
56+ ...items . map ( ( item ) => `- [ ] ${ item } \n` ) ,
57+ "\n" ,
58+ ] . join ( "" )
59+ : [ ...items . map ( ( item ) => `- [ ] ${ item } \n` ) ] . join ( "" ) ;
3060}
3161
3262async function run ( ) {
3363 const context = github . context ;
3464 const { owner, repo } = context . repo ;
35- const number = ( context . payload . issue ?? context . payload . pull_request ?? context . payload ) . number ;
65+ const number = (
66+ context . payload . issue ??
67+ context . payload . pull_request ??
68+ context . payload
69+ ) . number ;
3670
3771 const ghToken = core . getInput ( "gh-token" ) ;
3872 const client = github . getOctokit ( ghToken ) ;
@@ -42,9 +76,9 @@ async function run() {
4276 await client . rest . pulls . listFiles ( {
4377 owner : owner ,
4478 repo : repo ,
45- pull_number : number
79+ pull_number : number ,
4680 } )
47- ) . data . map ( file => file . filename ) ;
81+ ) . data . map ( ( file ) => file . filename ) ;
4882
4983 const applicableChecklistPaths = Object . entries ( checklistPaths ) . filter (
5084 ( [ key , _ ] ) => {
@@ -61,42 +95,44 @@ async function run() {
6195 await client . rest . issues . listComments ( {
6296 owner : owner ,
6397 repo : repo ,
64- issue_number : number
98+ issue_number : number ,
6599 } )
66- ) . data . find ( comment => comment . body . includes ( footer ) ) ;
100+ ) . data . find ( ( comment ) => comment . body . includes ( footer ) ) ;
67101
68102 if ( applicableChecklistPaths . length > 0 ) {
69103 const body = [
70104 `${ header } \n\n` ,
71- ...applicableChecklistPaths . map ( formatItemsForPath ) ,
72- `\n${ footer } `
105+ ...applicableChecklistPaths . map ( ( [ path , items ] ) =>
106+ formatItemsForPath ( existingComment . body , [ path , items ] )
107+ ) ,
108+ `\n${ footer } ` ,
73109 ] . join ( "" ) ;
74110
75111 if ( existingComment ) {
76112 await client . rest . issues . updateComment ( {
77113 owner : owner ,
78114 repo : repo ,
79115 comment_id : existingComment . id ,
80- body
116+ body,
81117 } ) ;
82118 } else {
83119 await client . rest . issues . createComment ( {
84120 owner : owner ,
85121 repo : repo ,
86122 issue_number : number ,
87- body
123+ body,
88124 } ) ;
89125 }
90126 } else {
91127 if ( existingComment ) {
92128 await client . rest . issues . deleteComment ( {
93129 owner : owner ,
94130 repo : repo ,
95- comment_id : existingComment . id
131+ comment_id : existingComment . id ,
96132 } ) ;
97133 }
98134 console . log ( "No paths were modified that match checklist paths" ) ;
99135 }
100136}
101137
102- run ( ) . catch ( err => core . setFailed ( err . message ) ) ;
138+ run ( ) . catch ( ( err ) => core . setFailed ( err . message ) ) ;
0 commit comments