@@ -4,13 +4,14 @@ import type {
4
4
ReleasePlan ,
5
5
VersionType ,
6
6
} from '@changesets/types'
7
- import type { Gitlab } from '@gitbeaker/core'
8
- import type {
9
- DiscussionNoteSchema ,
10
- DiscussionSchema ,
11
- MergeRequestChangesSchema ,
12
- MergeRequestNoteSchema ,
13
- NoteSchema ,
7
+ import type { CommitDiffSchema , Gitlab } from '@gitbeaker/core'
8
+ import {
9
+ GitbeakerRequestError ,
10
+ type DiscussionNoteSchema ,
11
+ type DiscussionSchema ,
12
+ type MergeRequestDiffSchema ,
13
+ type MergeRequestNoteSchema ,
14
+ type NoteSchema ,
14
15
} from '@gitbeaker/rest'
15
16
import { humanId } from 'human-id'
16
17
import { markdownTable } from 'markdown-table'
@@ -20,7 +21,7 @@ import * as context from './context.js'
20
21
import { env } from './env.js'
21
22
import { getChangedPackages } from './get-changed-packages.js'
22
23
import type { LooseString } from './types.js'
23
- import { getUsername , TRUTHY_VALUES } from './utils.js'
24
+ import { getUsername , HTTP_STATUS_NOT_FOUND , TRUTHY_VALUES } from './utils.js'
24
25
25
26
const generatedByBotNote = 'Generated By Changesets GitLab Bot'
26
27
@@ -207,10 +208,10 @@ async function getNoteInfo(
207
208
}
208
209
209
210
const hasChangesetBeenAdded = async (
210
- changedFilesPromise : Promise < MergeRequestChangesSchema > ,
211
+ changedFilesPromise : Promise < CommitDiffSchema [ ] | MergeRequestDiffSchema [ ] > ,
211
212
) => {
212
213
const changedFiles = await changedFilesPromise
213
- return changedFiles . changes . some ( file => {
214
+ return changedFiles . some ( file => {
214
215
return (
215
216
file . new_file &&
216
217
/ ^ \. c h a n g e s e t \/ .+ \. m d $ / . test ( file . new_path ) &&
@@ -219,35 +220,6 @@ const hasChangesetBeenAdded = async (
219
220
} )
220
221
}
221
222
222
- /**
223
- * @see https://github.com/jdalrymple/gitbeaker/blob/52ef0e622de304d98afb811f4937560edefd8889/packages/rest/src/Requester.ts#L79-L86
224
- */
225
- export interface GitLabAPIError extends Error {
226
- cause : {
227
- description : string
228
- request : Request
229
- response : Response
230
- }
231
- }
232
-
233
- const GITLAB_API_ERROR_CAUSE_KEYS = new Set ( [
234
- 'description' ,
235
- 'request' ,
236
- 'response' ,
237
- ] )
238
-
239
- // type-coverage:ignore-next-line -- https://github.com/plantain-00/type-coverage/issues/133
240
- const { toString } = Object . prototype // eslint-disable-line @typescript-eslint/unbound-method
241
-
242
- const isError = ( value : unknown ) : value is Error =>
243
- toString . call ( value ) === '[object Error]'
244
-
245
- const isGitLabAPIError = ( error : unknown ) : error is GitLabAPIError =>
246
- isError ( error ) &&
247
- ! ! error . cause &&
248
- typeof error . cause === 'object' &&
249
- Object . keys ( error . cause ) . every ( key => GITLAB_API_ERROR_CAUSE_KEYS . has ( key ) )
250
-
251
223
// eslint-disable-next-line sonarjs/cognitive-complexity
252
224
export const comment = async ( ) => {
253
225
const mrBranch = env . CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
@@ -271,18 +243,32 @@ export const comment = async () => {
271
243
let errFromFetchingChangedFiles = ''
272
244
try {
273
245
const latestCommitSha = env . CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
274
- const changedFilesPromise = api . MergeRequests . showChanges (
246
+
247
+ const changedFilesPromise = api . MergeRequests . allDiffs (
275
248
context . projectId ,
276
249
mrIid ,
277
- )
250
+ ) . catch ( async ( err : unknown ) => {
251
+ if (
252
+ ! ( err instanceof GitbeakerRequestError ) ||
253
+ err . cause ?. response . status !== HTTP_STATUS_NOT_FOUND
254
+ ) {
255
+ throw err
256
+ }
257
+
258
+ const { changes } = await api . MergeRequests . showChanges (
259
+ context . projectId ,
260
+ mrIid ,
261
+ )
262
+ return changes
263
+ } )
278
264
279
265
const [ noteInfo , hasChangeset , { changedPackages, releasePlan } ] =
280
266
await Promise . all ( [
281
267
getNoteInfo ( api , mrIid , commentType ) ,
282
268
hasChangesetBeenAdded ( changedFilesPromise ) ,
283
269
getChangedPackages ( {
284
- changedFiles : changedFilesPromise . then ( x =>
285
- x . changes . map ( x => x . new_path ) ,
270
+ changedFiles : changedFilesPromise . then ( changedFiles =>
271
+ changedFiles . map ( ( { new_path } ) => new_path ) ,
286
272
) ,
287
273
api,
288
274
} ) . catch ( ( err : unknown ) => {
@@ -368,11 +354,9 @@ export const comment = async () => {
368
354
)
369
355
}
370
356
}
371
- } catch ( err : unknown ) {
372
- if ( isGitLabAPIError ( err ) ) {
373
- const {
374
- cause : { description, request, response } ,
375
- } = err
357
+ } catch ( err ) {
358
+ if ( err instanceof GitbeakerRequestError && err . cause ) {
359
+ const { description, request, response } = err . cause
376
360
console . error ( description )
377
361
try {
378
362
console . error ( 'request:' , await request . text ( ) )
0 commit comments