Skip to content

Commit f819185

Browse files
authored
feat: use gitlab api to get username (#76)
Co-authored-by: Daniel Kopp <daniel.kopp@aoe.com> Resolves #63
1 parent a89dc49 commit f819185

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

.changeset/two-sheep-itch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'changesets-gitlab': minor
3+
---
4+
5+
get username from api instead usage of env variable

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ GITLAB_HOST # optional, if you're using custom GitLab host
4848

4949
GITLAB_TOKEN # required, token with accessibility to push
5050
GITLAB_TOKEN_TYPE # optional, type of the provided token in GITLAB_TOKEN. defaults to personal access token. can be `job` if you provide the Gitlab CI_JOB_TOKEN or `oauth` if you use Gitlab Oauth token
51-
GITLAB_CI_USER_NAME # required, username with accessibility to push, used in pairs of the above token (if it was personal access token)
51+
GITLAB_CI_USER_NAME # optional, username with accessibility to push, used in pairs of the above token (if it was personal access token). If not set read it from the Gitlab API
5252
GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com`
5353
DEBUG_GITLAB_CREDENTIAL # optional, default `false`
5454
```

src/cli.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ const cjsRequire =
1515
typeof require === 'undefined' ? _.createRequire(import.meta.url) : require
1616

1717
const run = async () => {
18-
const { GITLAB_CI_USER_NAME, GITLAB_TOKEN } = process.env
18+
const { GITLAB_TOKEN } = process.env
1919

20-
if (!GITLAB_TOKEN || !GITLAB_CI_USER_NAME) {
21-
setFailed(
22-
'Please add the `GITLAB_TOKEN` and `GITLAB_CI_USER_NAME` to the changesets action',
23-
)
20+
if (!GITLAB_TOKEN) {
21+
setFailed('Please add the `GITLAB_TOKEN` to the changesets action')
2422
return
2523
}
2624

src/comment.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ValidationError } from '@changesets/errors'
22
import type {
3-
ReleasePlan,
43
ComprehensiveRelease,
4+
ReleasePlan,
55
VersionType,
66
} from '@changesets/types'
77
import type { Gitlab, MergeRequests } from '@gitbeaker/core'
@@ -11,9 +11,12 @@ import { markdownTable } from 'markdown-table'
1111

1212
import * as context from './context.js'
1313
import { getChangedPackages } from './get-changed-packages.js'
14+
import { getUsername } from './utils.js'
1415

1516
import { createApi } from './index.js'
1617

18+
const generatedByBotNote = 'Generated By Changesets GitLab Bot'
19+
1720
const getReleasePlanMessage = (releasePlan: ReleasePlan | null) => {
1821
if (!releasePlan) return ''
1922

@@ -69,7 +72,7 @@ ${getReleasePlanMessage(releasePlan)}
6972
7073
[Click here if you're a maintainer who wants to add a changeset to this MR](${addChangesetUrl})
7174
72-
__Generated By Changesets GitLab Bot__
75+
__${generatedByBotNote}__
7376
`
7477

7578
const getApproveMessage = (
@@ -88,7 +91,7 @@ Not sure what this means? [Click here to learn what changesets are](https://git
8891
8992
[Click here if you're a maintainer who wants to add another changeset to this MR](${addChangesetUrl})
9093
91-
__Generated By Changesets GitLab Bot__
94+
__${generatedByBotNote}__
9295
`
9396

9497
const getNewChangesetTemplate = (changedPackages: string[], title: string) =>
@@ -101,18 +104,19 @@ ${title}
101104

102105
const getNoteInfo = (api: Gitlab, mrIid: number | string) =>
103106
api.MergeRequestDiscussions.all(context.projectId, mrIid).then(
104-
discussions => {
107+
async discussions => {
105108
for (const discussion of discussions) {
106109
if (!discussion.notes) {
107110
continue
108111
}
109112

113+
const username = await getUsername(api)
110114
const changesetBotNote = discussion.notes.find(
111115
note =>
112-
note.author.username === process.env.GITLAB_CI_USER_NAME &&
113-
// We need to ensure the note is generated by us but we don't have a app bot like GitHub
116+
note.author.username === username &&
117+
// We need to ensure the note is generated by us, but we don't have an app bot like GitHub
114118
// @see https://github.com/apps/changeset-bot
115-
note.body.includes('Generated By Changesets GitLab Bot'),
119+
note.body.includes(generatedByBotNote),
116120
)
117121

118122
if (changesetBotNote) {

src/main.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { setupUser } from './gitUtils.js'
88
import readChangesetState from './readChangesetState.js'
99
import { runPublish, runVersion } from './run.js'
1010
import type { MainCommandOptions } from './types.js'
11-
import { execSync, getOptionalInput } from './utils.js'
11+
import { execSync, getOptionalInput, getUsername } from './utils.js'
12+
13+
import { createApi } from './index.js'
1214

1315
export const main = async ({
1416
published,
@@ -18,7 +20,6 @@ export const main = async ({
1820
CI,
1921
CI_PROJECT_PATH,
2022
GITLAB_HOST = 'https://gitlab.com',
21-
GITLAB_CI_USER_NAME,
2223
GITLAB_TOKEN,
2324
HOME,
2425
NPM_TOKEN,
@@ -32,17 +33,18 @@ export const main = async ({
3233
console.log('setting git user')
3334
await setupUser()
3435

35-
console.log('setting GitLab credentials')
36-
3736
const url = new URL(GITLAB_HOST)
3837

38+
console.log('setting GitLab credentials')
39+
const username = await getUsername(createApi())
40+
3941
await exec(
4042
'git',
4143
[
4244
'remote',
4345
'set-url',
4446
'origin',
45-
`${url.protocol}//${GITLAB_CI_USER_NAME!}:${GITLAB_TOKEN!}@${
47+
`${url.protocol}//${username}:${GITLAB_TOKEN!}@${
4648
url.host
4749
}/${CI_PROJECT_PATH!}.git`,
4850
],

src/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'node:path'
44

55
import { getInput } from '@actions/core'
66
import { exec } from '@actions/exec'
7+
import type { Gitlab } from '@gitbeaker/core'
78
import type { Package } from '@manypkg/get-packages'
89
import { getPackages } from '@manypkg/get-packages'
910
import { toString as mdastToString } from 'mdast-util-to-string'
@@ -152,3 +153,9 @@ export const execSync = (command: string) =>
152153
})
153154

154155
export const getOptionalInput = (name: string) => getInput(name) || undefined
156+
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
161+
}

0 commit comments

Comments
 (0)