Skip to content

Commit be50e4a

Browse files
alan910127JounQin
andauthored
fix(deps): migrate to @gitbeaker/rest (#138)
Co-authored-by: JounQin <admin@1stg.me>
1 parent a30d2ba commit be50e4a

File tree

10 files changed

+84
-321
lines changed

10 files changed

+84
-321
lines changed

.changeset/eleven-kids-happen.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+
Fix deprecation warning of using @gitbeaker/node package

.codesandbox/ci.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"node": "16",
2+
"node": "18",
33
"sandboxes": []
44
}

.github/workflows/ci.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ jobs:
1010
strategy:
1111
matrix:
1212
node:
13-
- 14
14-
- 16
1513
- 18
14+
- 20
1615
runs-on: ubuntu-latest
1716
steps:
1817
- name: Checkout Repo
1918
uses: actions/checkout@v3
2019

2120
- name: Setup Node.js ${{ matrix.node }}
22-
uses: actions/setup-node@v3
21+
uses: actions/setup-node@v4
2322
with:
2423
node-version: ${{ matrix.node }}
2524
cache: yarn
2625

2726
- name: Install Dependencies
28-
run: yarn --frozen-lockfile
27+
run: yarn --frozen-lockfile --ignore-engines
2928

3029
- name: Build, Lint and Test
3130
run: |

.github/workflows/release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
1717
fetch-depth: 0
1818

19-
- name: Setup Node.js 16
20-
uses: actions/setup-node@v3
19+
- name: Setup Node.js LTS
20+
uses: actions/setup-node@v4
2121
with:
22-
node-version: 16
22+
node-version: lts/*
2323
cache: yarn
2424

2525
- name: Install Dependencies

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"@changesets/parse": "^0.3.16",
7878
"@changesets/pre": "^1.0.14",
7979
"@changesets/read": "^0.5.9",
80-
"@gitbeaker/node": "^35.7.0",
80+
"@gitbeaker/rest": "^39.23.0",
8181
"@manypkg/get-packages": "^1.1.3",
8282
"@sentry/node": "^7.6.0",
8383
"commander": "^9.3.0",

src/comment.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,20 @@ const getNoteInfo = (api: Gitlab, mrIid: number | string) =>
129129
},
130130
)
131131

132-
const hasChangesetBeenAdded = (
133-
changedFilesPromise: ReturnType<MergeRequests['changes']>,
134-
) =>
135-
changedFilesPromise.then(files =>
136-
files.changes!.some(
137-
file =>
138-
file.new_file &&
139-
/^\.changeset\/.+\.md$/.test(file.new_path) &&
140-
file.new_path !== '.changeset/README.md',
141-
),
142-
)
132+
const hasChangesetBeenAdded = async (
133+
changedFilesPromise: ReturnType<MergeRequests['showChanges']>,
134+
) => {
135+
const changedFiles = await changedFilesPromise
136+
const changes =
137+
'changes' in changedFiles ? changedFiles.changes : changedFiles.data.changes
138+
return changes.some(file => {
139+
return (
140+
file.new_file &&
141+
/^\.changeset\/.+\.md$/.test(file.new_path) &&
142+
file.new_path !== '.changeset/README.md'
143+
)
144+
})
145+
}
143146

144147
export const comment = async () => {
145148
const {
@@ -168,7 +171,7 @@ export const comment = async () => {
168171

169172
try {
170173
const latestCommitSha = CI_MERGE_REQUEST_SOURCE_BRANCH_SHA!
171-
const changedFilesPromise = api.MergeRequests.changes(
174+
const changedFilesPromise = api.MergeRequests.showChanges(
172175
context.projectId,
173176
mrIid,
174177
)
@@ -179,7 +182,7 @@ export const comment = async () => {
179182
hasChangesetBeenAdded(changedFilesPromise),
180183
getChangedPackages({
181184
changedFiles: changedFilesPromise.then(x =>
182-
x.changes!.map(x => x.new_path),
185+
x.changes.map(x => x.new_path),
183186
),
184187
api,
185188
}).catch((err: unknown) => {
@@ -217,7 +220,6 @@ export const comment = async () => {
217220
return api.MergeRequestDiscussions.editNote(
218221
context.projectId,
219222
mrIid,
220-
// @ts-expect-error - https://github.com/jdalrymple/gitbeaker/pull/523#issuecomment-975276068
221223
noteInfo.discussionId,
222224
noteInfo.noteId,
223225
{
@@ -239,7 +241,7 @@ export const comment = async () => {
239241
context.projectId,
240242
mrIid,
241243
noteInfo.noteId,
242-
prComment,
244+
{ body: prComment },
243245
)
244246
}
245247

src/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Gitlab } from '@gitbeaker/node'
1+
import { Gitlab } from '@gitbeaker/rest'
22
import type { ProxyAgentConfigurationType } from 'global-agent'
33
import { bootstrap } from 'global-agent'
44

@@ -23,21 +23,26 @@ export const createApi = (gitlabToken?: string) => {
2323
}
2424
}
2525

26-
const token = gitlabToken || process.env.GITLAB_TOKEN
27-
28-
let tokenType: 'jobToken' | 'oauthToken' | 'token' = 'token'
26+
const token = gitlabToken || process.env.GITLAB_TOKEN!
27+
const host = process.env.GITLAB_HOST ?? process.env.CI_SERVER_URL
2928

29+
// we cannot use { [tokenType]: token } now
30+
// because it will break the type of the Gitlab constructor
3031
switch (process.env.GITLAB_TOKEN_TYPE) {
3132
case 'job':
32-
tokenType = 'jobToken'
33-
break
33+
return new Gitlab({
34+
host,
35+
jobToken: token,
36+
})
3437
case 'oauth':
35-
tokenType = 'oauthToken'
36-
break
38+
return new Gitlab({
39+
host,
40+
oauthToken: token,
41+
})
42+
default:
43+
return new Gitlab({
44+
host,
45+
token,
46+
})
3747
}
38-
39-
return new Gitlab({
40-
host: process.env.GITLAB_HOST ?? process.env.CI_SERVER_URL,
41-
[tokenType]: token,
42-
})
4348
}

src/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const createRelease = async (
4141
)
4242
}
4343

44-
await api.Releases.create(context.projectId, {
44+
await api.ProjectReleases.create(context.projectId, {
4545
name: tagName,
4646
tag_name: tagName,
4747
description: changelogEntry.content,

src/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ export const execSync = (command: string) =>
154154

155155
export const getOptionalInput = (name: string) => getInput(name) || undefined
156156

157-
export const getUsername = async (api: Gitlab) => {
158-
return process.env.GITLAB_CI_USER_NAME == null
159-
? await api.Users.current().then(currentUser => currentUser.username)
160-
: process.env.GITLAB_CI_USER_NAME
157+
export const getUsername = (api: Gitlab) => {
158+
return (
159+
process.env.GITLAB_CI_USER_NAME ??
160+
api.Users.showCurrentUser().then(currentUser => currentUser.username)
161+
)
161162
}

0 commit comments

Comments
 (0)