@@ -23,6 +23,7 @@ export default eventHandler(async (event) => {
2323 "sb-only-templates" : onlyTemplatesHeader ,
2424 "sb-comment-with-sha" : commentWithShaHeader ,
2525 "sb-comment-with-dev" : commentWithDevHeader ,
26+ "sb-sync-comment-with-issue" : syncCommentWithIssueHeader ,
2627 } = getHeaders ( event ) ;
2728 const compact = compactHeader === "true" ;
2829 const onlyTemplates = onlyTemplatesHeader === "true" ;
@@ -32,6 +33,7 @@ export default eventHandler(async (event) => {
3233 ( packageManagerHeader as PackageManager ) || "npm" ;
3334 const commentWithSha = commentWithShaHeader === "true" ;
3435 const commentWithDev = commentWithDevHeader === "true" ;
36+ const syncCommentWithIssue = syncCommentWithIssueHeader === "true" ;
3537
3638 if ( ! key || ! runIdHeader || ! shasumsHeader ) {
3739 throw createError ( {
@@ -246,7 +248,14 @@ export default eventHandler(async (event) => {
246248 isPullRequest ( workflowData . ref ) &&
247249 ( await getPullRequestState ( installation , workflowData ) ) === "open"
248250 ) {
249- let prevComment : OctokitComponents [ "schemas" ] [ "issue-comment" ] ;
251+ let prevComment :
252+ | OctokitComponents [ "schemas" ] [ "issue-comment" ]
253+ | undefined ;
254+ let prevIssueComment :
255+ | OctokitComponents [ "schemas" ] [ "issue-comment" ]
256+ | undefined ;
257+ let relatedIssueNumber : number | undefined ;
258+ const matchIssueNumber = / ( f i x | c l o s e | r e s o l v e ) \s * ( \d + ) / gi;
250259
251260 await installation . paginate (
252261 "GET /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
@@ -261,12 +270,52 @@ export default eventHandler(async (event) => {
261270 prevComment = c ;
262271 done ( ) ;
263272 break ;
273+ } else {
274+ const body = c . body || "" ;
275+ let match ;
276+ while ( ( match = matchIssueNumber . exec ( body ) ) !== null ) {
277+ const issueNumber = Number ( match [ 2 ] ) ;
278+ if ( ! isNaN ( issueNumber ) ) {
279+ relatedIssueNumber = issueNumber ;
280+ break ;
281+ }
282+ }
283+ }
284+ if ( prevComment ) {
285+ if ( ! syncCommentWithIssue ) {
286+ done ( ) ;
287+ break ;
288+ } else if ( relatedIssueNumber ) {
289+ done ( ) ;
290+ break ;
291+ }
264292 }
265293 }
266294 return [ ] ;
267295 } ,
268296 ) ;
269297
298+ if ( syncCommentWithIssue && relatedIssueNumber ) {
299+ await installation . paginate (
300+ "GET /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
301+ {
302+ owner : workflowData . owner ,
303+ repo : workflowData . repo ,
304+ issue_number : relatedIssueNumber ,
305+ } ,
306+ ( { data } , done ) => {
307+ for ( const c of data ) {
308+ if ( c . performed_via_github_app ?. id === Number ( appId ) ) {
309+ prevIssueComment = c ;
310+ done ( ) ;
311+ break ;
312+ }
313+ }
314+ return [ ] ;
315+ } ,
316+ ) ;
317+ }
318+
270319 if ( comment !== "off" ) {
271320 const {
272321 data : { permissions } ,
@@ -280,49 +329,78 @@ export default eventHandler(async (event) => {
280329
281330 try {
282331 if ( comment === "update" && prevComment ! ) {
332+ const commentBody = generatePullRequestPublishMessage (
333+ origin ,
334+ templatesHtmlMap ,
335+ packagesWithoutPrefix ,
336+ workflowData ,
337+ compact ,
338+ onlyTemplates ,
339+ checkRunUrl ,
340+ packageManager ,
341+ commentWithSha ? "sha" : "ref" ,
342+ bin ,
343+ commentWithDev ,
344+ ) ;
345+
283346 await installation . request (
284347 "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}" ,
285348 {
286349 owner : workflowData . owner ,
287350 repo : workflowData . repo ,
288351 comment_id : prevComment . id ,
289- body : generatePullRequestPublishMessage (
290- origin ,
291- templatesHtmlMap ,
292- packagesWithoutPrefix ,
293- workflowData ,
294- compact ,
295- onlyTemplates ,
296- checkRunUrl ,
297- packageManager ,
298- commentWithSha ? "sha" : "ref" ,
299- bin ,
300- commentWithDev ,
301- ) ,
352+ body : commentBody ,
302353 } ,
303354 ) ;
355+ if (
356+ syncCommentWithIssue &&
357+ relatedIssueNumber &&
358+ prevIssueComment
359+ ) {
360+ await installation . request (
361+ "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}" ,
362+ {
363+ owner : workflowData . owner ,
364+ repo : workflowData . repo ,
365+ comment_id : prevIssueComment . id ,
366+ body : commentBody ,
367+ } ,
368+ ) ;
369+ }
304370 } else {
371+ const commentBody = generatePullRequestPublishMessage (
372+ origin ,
373+ templatesHtmlMap ,
374+ packagesWithoutPrefix ,
375+ workflowData ,
376+ compact ,
377+ onlyTemplates ,
378+ checkRunUrl ,
379+ packageManager ,
380+ comment === "update" ? "ref" : "sha" ,
381+ bin ,
382+ commentWithDev ,
383+ ) ;
305384 await installation . request (
306385 "POST /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
307386 {
308387 owner : workflowData . owner ,
309388 repo : workflowData . repo ,
310389 issue_number : Number ( workflowData . ref ) ,
311- body : generatePullRequestPublishMessage (
312- origin ,
313- templatesHtmlMap ,
314- packagesWithoutPrefix ,
315- workflowData ,
316- compact ,
317- onlyTemplates ,
318- checkRunUrl ,
319- packageManager ,
320- comment === "update" ? "ref" : "sha" ,
321- bin ,
322- commentWithDev ,
323- ) ,
390+ body : commentBody ,
324391 } ,
325392 ) ;
393+ if ( syncCommentWithIssue && relatedIssueNumber ) {
394+ await installation . request (
395+ "POST /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
396+ {
397+ owner : workflowData . owner ,
398+ repo : workflowData . repo ,
399+ issue_number : relatedIssueNumber ,
400+ body : commentBody ,
401+ } ,
402+ ) ;
403+ }
326404 }
327405 } catch ( error ) {
328406 console . error ( "failed to create/update comment" , error , permissions ) ;
0 commit comments