|
1 | 1 | import { expect, test, vi, describe } from 'vitest'; |
2 | 2 | import { Gitlab } from '@gitbeaker/rest'; |
3 | 3 | import { gitlabPushMrReviews } from './gitlabPushMrReviews'; |
4 | | -import { sourcebot_pr_payload, sourcebot_file_diff_review } from '../types'; |
| 4 | +import { sourcebot_pr_payload, sourcebot_file_diff_review, sourcebot_diff_refs } from '../types'; |
5 | 5 |
|
6 | 6 | type GitlabClient = InstanceType<typeof Gitlab>; |
7 | 7 |
|
@@ -67,6 +67,7 @@ describe('gitlabPushMrReviews', () => { |
67 | 67 | baseSha: 'base_sha_value', |
68 | 68 | headSha: 'head_sha_value', |
69 | 69 | startSha: 'start_sha_value', |
| 70 | + oldPath: 'src/foo.ts', |
70 | 71 | newPath: 'src/foo.ts', |
71 | 72 | newLine: '5', |
72 | 73 | }), |
@@ -187,4 +188,142 @@ describe('gitlabPushMrReviews', () => { |
187 | 188 | gitlabPushMrReviews(client, 101, MOCK_PAYLOAD, SINGLE_REVIEW), |
188 | 189 | ).resolves.not.toThrow(); |
189 | 190 | }); |
| 191 | + |
| 192 | + test('uses oldFilename as oldPath when file was renamed', async () => { |
| 193 | + const renamedReview: sourcebot_file_diff_review[] = [ |
| 194 | + { |
| 195 | + filename: 'src/new-name.ts', |
| 196 | + oldFilename: 'src/old-name.ts', |
| 197 | + reviews: [{ line_start: 1, line_end: 1, review: 'Comment on renamed file' }], |
| 198 | + }, |
| 199 | + ]; |
| 200 | + const client = makeMockClient(); |
| 201 | + |
| 202 | + await gitlabPushMrReviews(client, 101, MOCK_PAYLOAD, renamedReview); |
| 203 | + |
| 204 | + expect(client.MergeRequestDiscussions.create).toHaveBeenCalledWith( |
| 205 | + 101, |
| 206 | + 42, |
| 207 | + 'Comment on renamed file', |
| 208 | + expect.objectContaining({ |
| 209 | + position: expect.objectContaining({ |
| 210 | + oldPath: 'src/old-name.ts', |
| 211 | + newPath: 'src/new-name.ts', |
| 212 | + }), |
| 213 | + }), |
| 214 | + ); |
| 215 | + }); |
| 216 | + |
| 217 | + test('passes oldLine for context lines', async () => { |
| 218 | + // old line 47 = new line 48 (a line was added at new line 47 before it) |
| 219 | + const payloadWithDiffs: sourcebot_pr_payload = { |
| 220 | + ...MOCK_PAYLOAD, |
| 221 | + file_diffs: [{ |
| 222 | + from: 'src/foo.ts', |
| 223 | + to: 'src/foo.ts', |
| 224 | + diffs: [{ |
| 225 | + oldSnippet: '@@ -47,1 +48,1 @@\n47: context line\n', |
| 226 | + newSnippet: '@@ -47,1 +48,1 @@\n48: context line\n', |
| 227 | + }], |
| 228 | + }], |
| 229 | + }; |
| 230 | + const contextReview: sourcebot_file_diff_review[] = [ |
| 231 | + { |
| 232 | + filename: 'src/foo.ts', |
| 233 | + reviews: [{ line_start: 48, line_end: 48, review: 'Context line comment' }], |
| 234 | + }, |
| 235 | + ]; |
| 236 | + const client = makeMockClient(); |
| 237 | + |
| 238 | + await gitlabPushMrReviews(client, 101, payloadWithDiffs, contextReview); |
| 239 | + |
| 240 | + expect(client.MergeRequestDiscussions.create).toHaveBeenCalledWith( |
| 241 | + 101, |
| 242 | + 42, |
| 243 | + 'Context line comment', |
| 244 | + expect.objectContaining({ |
| 245 | + position: expect.objectContaining({ |
| 246 | + newLine: '48', |
| 247 | + oldLine: '47', |
| 248 | + }), |
| 249 | + }), |
| 250 | + ); |
| 251 | + }); |
| 252 | + |
| 253 | + test('does not pass oldLine for added lines', async () => { |
| 254 | + const payloadWithDiffs: sourcebot_pr_payload = { |
| 255 | + ...MOCK_PAYLOAD, |
| 256 | + file_diffs: [{ |
| 257 | + from: 'src/foo.ts', |
| 258 | + to: 'src/foo.ts', |
| 259 | + diffs: [{ |
| 260 | + oldSnippet: '@@ -1,1 +1,2 @@\n1: existing line\n', |
| 261 | + newSnippet: '@@ -1,1 +1,2 @@\n1: existing line\n2:+added line\n', |
| 262 | + }], |
| 263 | + }], |
| 264 | + }; |
| 265 | + const addedLineReview: sourcebot_file_diff_review[] = [ |
| 266 | + { |
| 267 | + filename: 'src/foo.ts', |
| 268 | + reviews: [{ line_start: 2, line_end: 2, review: 'Comment on added line' }], |
| 269 | + }, |
| 270 | + ]; |
| 271 | + const client = makeMockClient(); |
| 272 | + |
| 273 | + await gitlabPushMrReviews(client, 101, payloadWithDiffs, addedLineReview); |
| 274 | + |
| 275 | + const call = client.MergeRequestDiscussions.create.mock.calls[0][3]; |
| 276 | + expect(call.position).not.toHaveProperty('oldLine'); |
| 277 | + expect(call.position.newLine).toBe('2'); |
| 278 | + }); |
| 279 | + |
| 280 | + test('uses new path for both oldPath and newPath when old path is /dev/null (added file)', async () => { |
| 281 | + const addedFileReview: sourcebot_file_diff_review[] = [ |
| 282 | + { |
| 283 | + filename: 'src/new-file.ts', |
| 284 | + oldFilename: '/dev/null', |
| 285 | + reviews: [{ line_start: 1, line_end: 1, review: 'Comment on new file' }], |
| 286 | + }, |
| 287 | + ]; |
| 288 | + const client = makeMockClient(); |
| 289 | + |
| 290 | + await gitlabPushMrReviews(client, 101, MOCK_PAYLOAD, addedFileReview); |
| 291 | + |
| 292 | + expect(client.MergeRequestDiscussions.create).toHaveBeenCalledWith( |
| 293 | + 101, |
| 294 | + 42, |
| 295 | + 'Comment on new file', |
| 296 | + expect.objectContaining({ |
| 297 | + position: expect.objectContaining({ |
| 298 | + oldPath: 'src/new-file.ts', |
| 299 | + newPath: 'src/new-file.ts', |
| 300 | + }), |
| 301 | + }), |
| 302 | + ); |
| 303 | + }); |
| 304 | + |
| 305 | + test('uses old path for both oldPath and newPath when new path is /dev/null (deleted file)', async () => { |
| 306 | + const deletedFileReview: sourcebot_file_diff_review[] = [ |
| 307 | + { |
| 308 | + filename: '/dev/null', |
| 309 | + oldFilename: 'src/deleted-file.ts', |
| 310 | + reviews: [{ line_start: 1, line_end: 1, review: 'Comment on deleted file' }], |
| 311 | + }, |
| 312 | + ]; |
| 313 | + const client = makeMockClient(); |
| 314 | + |
| 315 | + await gitlabPushMrReviews(client, 101, MOCK_PAYLOAD, deletedFileReview); |
| 316 | + |
| 317 | + expect(client.MergeRequestDiscussions.create).toHaveBeenCalledWith( |
| 318 | + 101, |
| 319 | + 42, |
| 320 | + 'Comment on deleted file', |
| 321 | + expect.objectContaining({ |
| 322 | + position: expect.objectContaining({ |
| 323 | + oldPath: 'src/deleted-file.ts', |
| 324 | + newPath: 'src/deleted-file.ts', |
| 325 | + }), |
| 326 | + }), |
| 327 | + ); |
| 328 | + }); |
190 | 329 | }); |
0 commit comments