@@ -129,14 +129,9 @@ ${tag}`
129
129
}
130
130
}
131
131
132
- private reviewCommentsBuffer : {
133
- path : string
134
- start_line : number
135
- end_line : number
136
- message : string
137
- } [ ] = [ ]
138
-
139
- async buffer_review_comment (
132
+ async review_comment (
133
+ pull_number : number ,
134
+ commit_id : string ,
140
135
path : string ,
141
136
start_line : number ,
142
137
end_line : number ,
@@ -148,36 +143,66 @@ ${tag}`
148
143
${ message }
149
144
150
145
${ tag } `
151
-
152
- this . reviewCommentsBuffer . push ( {
153
- path,
154
- start_line,
155
- end_line,
156
- message
157
- } )
158
- }
159
-
160
- async submit_review ( pull_number : number , commit_id : string ) {
146
+ // replace comment made by this action
161
147
try {
162
- if ( this . reviewCommentsBuffer . length > 0 ) {
163
- await octokit . pulls . createReview ( {
164
- owner : repo . owner ,
165
- repo : repo . repo ,
166
- pull_number,
167
- commit_id,
168
- event : 'COMMENT' ,
169
- comments : this . reviewCommentsBuffer . map ( comment => ( {
170
- path : comment . path ,
171
- body : comment . message ,
172
- line : comment . end_line ,
173
- start_line : comment . start_line ,
148
+ let found = false
149
+ const comments = await this . get_comments_at_range (
150
+ pull_number ,
151
+ path ,
152
+ start_line ,
153
+ end_line
154
+ )
155
+ for ( const comment of comments ) {
156
+ if ( comment . body . includes ( tag ) ) {
157
+ core . info (
158
+ `Updating review comment for ${ path } :${ start_line } -${ end_line } : ${ message } `
159
+ )
160
+ await octokit . pulls . updateReviewComment ( {
161
+ owner : repo . owner ,
162
+ repo : repo . repo ,
163
+ comment_id : comment . id ,
164
+ body : message
165
+ } )
166
+ found = true
167
+ break
168
+ }
169
+ }
170
+
171
+ if ( ! found ) {
172
+ core . info (
173
+ `Creating new review comment for ${ path } :${ start_line } -${ end_line } : ${ message } `
174
+ )
175
+ // if start_line is same as end_line, it's a single line comment
176
+ // otherwise it's a multi-line comment
177
+ if ( start_line === end_line ) {
178
+ await octokit . pulls . createReviewComment ( {
179
+ owner : repo . owner ,
180
+ repo : repo . repo ,
181
+ pull_number,
182
+ body : message ,
183
+ commit_id,
184
+ path,
185
+ line : end_line
186
+ } )
187
+ } else {
188
+ await octokit . pulls . createReviewComment ( {
189
+ owner : repo . owner ,
190
+ repo : repo . repo ,
191
+ pull_number,
192
+ body : message ,
193
+ commit_id,
194
+ path,
195
+ line : end_line ,
196
+ start_line,
174
197
start_side : 'RIGHT'
175
- } ) )
176
- } )
177
- this . reviewCommentsBuffer = [ ]
198
+ } )
199
+ }
178
200
}
179
201
} catch ( e ) {
180
- core . warning ( `Failed to submit review: ${ e } ` )
202
+ core . warning (
203
+ `Failed to post review comment, for ${ path } :${ start_line } -${ end_line } : ${ e } `
204
+ )
205
+ // throw error
181
206
throw e
182
207
}
183
208
}
0 commit comments