Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): migrate to @gitbeaker/rest #138

Merged
merged 8 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-kids-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'changesets-gitlab': minor
---

Fix deprecation warning of using @gitbeaker/node package
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"node": "16",
"node": "18",
"sandboxes": []
}
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ jobs:
strategy:
matrix:
node:
- 14
- 16
- 18
- 20
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn

- name: Install Dependencies
run: yarn --frozen-lockfile
run: yarn --frozen-lockfile --ignore-engines

- name: Build, Lint and Test
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0

- name: Setup Node.js 16
uses: actions/setup-node@v3
- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 16
node-version: lts/*
cache: yarn

- name: Install Dependencies
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@changesets/parse": "^0.3.16",
"@changesets/pre": "^1.0.14",
"@changesets/read": "^0.5.9",
"@gitbeaker/node": "^35.7.0",
"@gitbeaker/rest": "^39.23.0",
JounQin marked this conversation as resolved.
Show resolved Hide resolved
"@manypkg/get-packages": "^1.1.3",
"@sentry/node": "^7.6.0",
"commander": "^9.3.0",
Expand Down
32 changes: 17 additions & 15 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,20 @@ const getNoteInfo = (api: Gitlab, mrIid: number | string) =>
},
)

const hasChangesetBeenAdded = (
changedFilesPromise: ReturnType<MergeRequests['changes']>,
) =>
changedFilesPromise.then(files =>
files.changes!.some(
file =>
file.new_file &&
/^\.changeset\/.+\.md$/.test(file.new_path) &&
file.new_path !== '.changeset/README.md',
),
)
const hasChangesetBeenAdded = async (
changedFilesPromise: ReturnType<MergeRequests['showChanges']>,
) => {
const changedFiles = await changedFilesPromise
const changes =
'changes' in changedFiles ? changedFiles.changes : changedFiles.data.changes
JounQin marked this conversation as resolved.
Show resolved Hide resolved
return changes.some(file => {
return (
file.new_file &&
/^\.changeset\/.+\.md$/.test(file.new_path) &&
file.new_path !== '.changeset/README.md'
)
})
}

export const comment = async () => {
const {
Expand Down Expand Up @@ -168,7 +171,7 @@ export const comment = async () => {

try {
const latestCommitSha = CI_MERGE_REQUEST_SOURCE_BRANCH_SHA!
const changedFilesPromise = api.MergeRequests.changes(
const changedFilesPromise = api.MergeRequests.showChanges(
context.projectId,
mrIid,
)
Expand All @@ -179,7 +182,7 @@ export const comment = async () => {
hasChangesetBeenAdded(changedFilesPromise),
getChangedPackages({
changedFiles: changedFilesPromise.then(x =>
x.changes!.map(x => x.new_path),
x.changes.map(x => x.new_path),
),
api,
}).catch((err: unknown) => {
Expand Down Expand Up @@ -217,7 +220,6 @@ export const comment = async () => {
return api.MergeRequestDiscussions.editNote(
context.projectId,
mrIid,
// @ts-expect-error - https://github.com/jdalrymple/gitbeaker/pull/523#issuecomment-975276068
noteInfo.discussionId,
noteInfo.noteId,
{
Expand All @@ -239,7 +241,7 @@ export const comment = async () => {
context.projectId,
mrIid,
noteInfo.noteId,
prComment,
{ body: prComment },
)
}

Expand Down
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Gitlab } from '@gitbeaker/node'
import { Gitlab } from '@gitbeaker/rest'
import type { ProxyAgentConfigurationType } from 'global-agent'
import { bootstrap } from 'global-agent'

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

const token = gitlabToken || process.env.GITLAB_TOKEN

let tokenType: 'jobToken' | 'oauthToken' | 'token' = 'token'
const token = gitlabToken || process.env.GITLAB_TOKEN!
const host = process.env.GITLAB_HOST ?? process.env.CI_SERVER_URL

// we cannot use { [tokenType]: token } now
JounQin marked this conversation as resolved.
Show resolved Hide resolved
// because it will break the type of the Gitlab constructor
switch (process.env.GITLAB_TOKEN_TYPE) {
case 'job':
tokenType = 'jobToken'
break
return new Gitlab({
host,
jobToken: token,
})
case 'oauth':
tokenType = 'oauthToken'
break
return new Gitlab({
host,
oauthToken: token,
})
default:
return new Gitlab({
host,
token,
})
}

return new Gitlab({
host: process.env.GITLAB_HOST ?? process.env.CI_SERVER_URL,
[tokenType]: token,
})
}
2 changes: 1 addition & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const createRelease = async (
)
}

await api.Releases.create(context.projectId, {
await api.ProjectReleases.create(context.projectId, {
name: tagName,
tag_name: tagName,
description: changelogEntry.content,
Expand Down
9 changes: 5 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ export const execSync = (command: string) =>

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

export const getUsername = async (api: Gitlab) => {
return process.env.GITLAB_CI_USER_NAME == null
? await api.Users.current().then(currentUser => currentUser.username)
: process.env.GITLAB_CI_USER_NAME
export const getUsername = (api: Gitlab) => {
return (
process.env.GITLAB_CI_USER_NAME ??
api.Users.showCurrentUser().then(currentUser => currentUser.username)
)
}
Loading