@@ -7,7 +7,8 @@ Update the content of the existing comment.
77
88https://octokit.github.io/rest.js/v19
99*/
10- module . exports = async ( { octokit_rest, context, process} ) => {
10+ module . exports = async ( { github, context, process} ) => {
11+ var octokit_rest = github
1112 if ( context . eventName != "pull_request" ) {
1213 // Only PR are supported.
1314 return
@@ -24,23 +25,49 @@ module.exports = async ({octokit_rest, context, process}) => {
2425 */
2526 var doAction = async function ( ) {
2627
28+ console . log ( context )
29+
2730 fs = require ( 'fs' ) ;
28- var body = fs . readFile (
29- process . env . GITHUB_WORKSPACE + "/" + process . env . COMMENT_BODY )
3031
31- var comments = await octokit . rest . issues . listComments ( {
32+ const body = fs . readFileSync (
33+ process . env . GITHUB_WORKSPACE + "/" + process . env . COMMENT_BODY , 'utf8' ) ;
34+ var comment_id = null
35+ var comment_marker = '\n' + process . env . COMMENT_MARKER
36+ var comment_body = body + comment_marker
37+
38+ var comments = await octokit_rest . issues . listComments ( {
3239 owner : context . repo . owner ,
3340 repo : context . repo . repo ,
34- issue_number : context . event . number ,
41+ issue_number : context . payload . number ,
3542 } )
43+
3644 console . log ( comments )
3745
46+ comments . data . forEach ( comment => {
47+ if ( comment . body . endsWith ( comment_marker ) ) {
48+ comment_id = comment . id
49+ }
50+ } )
51+
52+ if ( comment_id ) {
53+ // We have an existing comment.
54+ // update the content.
55+ await octokit_rest . issues . updateComment ( {
56+ owner : context . repo . owner ,
57+ repo : context . repo . repo ,
58+ comment_id : comment_id ,
59+ body : comment_body ,
60+ } )
61+ return
62+ }
63+
64+ // Create a new comment.
3865 await octokit_rest . issues . createComment ( {
3966 owner : context . repo . owner ,
4067 repo : context . repo . repo ,
41- issue_number : context . event . number ,
42- body : body ,
43- } )
68+ issue_number : context . payload . number ,
69+ body : comment_body ,
70+ } )
4471
4572 }
4673
0 commit comments