Skip to content

Commit 8bdaf2d

Browse files
authored
feat: try allDiffs api first and fallback to showChanges when unavailable (#210)
1 parent 842110d commit 8bdaf2d

File tree

3 files changed

+39
-48
lines changed

3 files changed

+39
-48
lines changed

.changeset/neat-hats-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"changesets-gitlab": minor
3+
---
4+
5+
feat: try `allDiffs` api first and fallback to `showChanges` when unavailable

src/comment.ts

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import type {
44
ReleasePlan,
55
VersionType,
66
} 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,
1415
} from '@gitbeaker/rest'
1516
import { humanId } from 'human-id'
1617
import { markdownTable } from 'markdown-table'
@@ -20,7 +21,7 @@ import * as context from './context.js'
2021
import { env } from './env.js'
2122
import { getChangedPackages } from './get-changed-packages.js'
2223
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'
2425

2526
const generatedByBotNote = 'Generated By Changesets GitLab Bot'
2627

@@ -207,10 +208,10 @@ async function getNoteInfo(
207208
}
208209

209210
const hasChangesetBeenAdded = async (
210-
changedFilesPromise: Promise<MergeRequestChangesSchema>,
211+
changedFilesPromise: Promise<CommitDiffSchema[] | MergeRequestDiffSchema[]>,
211212
) => {
212213
const changedFiles = await changedFilesPromise
213-
return changedFiles.changes.some(file => {
214+
return changedFiles.some(file => {
214215
return (
215216
file.new_file &&
216217
/^\.changeset\/.+\.md$/.test(file.new_path) &&
@@ -219,35 +220,6 @@ const hasChangesetBeenAdded = async (
219220
})
220221
}
221222

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-
251223
// eslint-disable-next-line sonarjs/cognitive-complexity
252224
export const comment = async () => {
253225
const mrBranch = env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
@@ -271,18 +243,32 @@ export const comment = async () => {
271243
let errFromFetchingChangedFiles = ''
272244
try {
273245
const latestCommitSha = env.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
274-
const changedFilesPromise = api.MergeRequests.showChanges(
246+
247+
const changedFilesPromise = api.MergeRequests.allDiffs(
275248
context.projectId,
276249
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+
})
278264

279265
const [noteInfo, hasChangeset, { changedPackages, releasePlan }] =
280266
await Promise.all([
281267
getNoteInfo(api, mrIid, commentType),
282268
hasChangesetBeenAdded(changedFilesPromise),
283269
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),
286272
),
287273
api,
288274
}).catch((err: unknown) => {
@@ -368,11 +354,9 @@ export const comment = async () => {
368354
)
369355
}
370356
}
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
376360
console.error(description)
377361
try {
378362
console.error('request:', await request.text())

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,5 @@ export const FALSY_VALUES = new Set(['false', '0'])
177177
export const TRUTHY_VALUES = new Set(['true', '1'])
178178

179179
export const GITLAB_MAX_TAGS = 4
180+
181+
export const HTTP_STATUS_NOT_FOUND = 404

0 commit comments

Comments
 (0)