Skip to content

Commit 44d561d

Browse files
committed
fix: gitlab authentication with username and token
1 parent dd28be5 commit 44d561d

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

.changeset/twelve-seas-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'changesets-gitlab': patch
3+
---
4+
5+
fix: gitlab authentication with username and token

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ GitLab CI cli for [changesets](https://github.com/atlassian/changesets) like its
1818
- published - A boolean value to indicate whether a publishing is happened or not
1919
- publishedPackages - A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0"}, {"name": "@xx/xy", "version": "0.8.9"}]`
2020

21+
### Environment Variables
22+
23+
```sh
24+
GLOBAL_AGENT_HTTP_PROXY # optional, if you're using custom GitLab service under proxy
25+
GLOBAL_AGENT_HTTPS_PROXY # As above but for https requests
26+
GLOBAL_AGENT_NO_PROXY # Like above but for no proxied requests
27+
28+
# http_proxy, https_proxy, no_proxy environment variables are supported at the same time
29+
30+
GITLAB_HOST # optional, if you're using custom GitLab host
31+
32+
GITLAB_TOKEN # required, token with accessibility to push
33+
GITLAB_USER_NAME # required, username with accessibility to push, used in pairs of the above token
34+
```
35+
2136
### Example workflow
2237

2338
#### Without Publishing

src/cli.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import './env.js'
44

5+
import { URL } from 'url'
6+
57
import { getInput, setFailed, setOutput } from '@actions/core'
68
import fs from 'fs-extra'
9+
import { exec } from '@actions/exec'
710

811
import { setupUser } from './gitUtils.js'
912
import readChangesetState from './readChangesetState.js'
@@ -12,14 +15,18 @@ import { runPublish, runVersion } from './run.js'
1215
const main = async () => {
1316
const {
1417
CI,
18+
CI_PROJECT_PATH,
1519
GITLAB_HOST = 'https://gitlab.com',
1620
GITLAB_TOKEN,
21+
GITLAB_USER_NAME,
1722
HOME,
1823
NPM_TOKEN,
1924
} = process.env
2025

21-
if (!GITLAB_TOKEN) {
22-
setFailed('Please add the GITLAB_TOKEN to the changesets action')
26+
if (!GITLAB_TOKEN || !GITLAB_USER_NAME) {
27+
setFailed(
28+
'Please add the `GITLAB_TOKEN` and `GITLAB_USER_NAME` to the changesets action',
29+
)
2330
return
2431
}
2532

@@ -30,14 +37,18 @@ const main = async () => {
3037
console.log('setting git user')
3138
await setupUser()
3239

33-
console.log('setting GitHub credentials')
34-
await fs.writeFile(
35-
`${HOME!}/.netrc`,
36-
`machine ${GITLAB_HOST.replace(
37-
/^https?:\/\//,
38-
'',
39-
)}\nlogin gitlab[bot]\npassword ${GITLAB_TOKEN}`,
40-
)
40+
console.log('setting GitLab credentials')
41+
42+
const url = new URL(GITLAB_HOST)
43+
44+
await exec('git', [
45+
'remote',
46+
'set-url',
47+
'origin',
48+
`${url.protocol}//${GITLAB_USER_NAME}:${GITLAB_TOKEN}@${
49+
url.host
50+
}/${CI_PROJECT_PATH!}.git`,
51+
])
4152
}
4253

4354
const { changesets } = await readChangesetState()

src/gitUtils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ import { exec } from '@actions/exec'
33
import { execWithOutput, identify } from './utils.js'
44

55
export const setupUser = async () => {
6-
await exec('git', ['config', '--global', 'user.name', `"gitlab[bot]"`])
6+
await exec('git', [
7+
'config',
8+
'--global',
9+
'user.name',
10+
process.env.GITLAB_USER_NAME!,
11+
])
712
await exec('git', [
813
'config',
914
'--global',
1015
'user.email',
11-
`"gitlab[bot]@users.noreply.gitlab.com"`,
16+
process.env.GITLAB_USER_EMAIL || '"gitlab[bot]@users.noreply.gitlab.com"',
1217
])
1318
}
1419

src/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export async function runVersion({
201201
const { preState } = await readChangesetState(cwd)
202202

203203
await gitUtils.switchToMaybeExistingBranch(versionBranch)
204-
await gitUtils.reset(branch)
204+
await gitUtils.reset(`origin/${branch}`)
205205

206206
const versionsByDirectory = await getVersionsByDirectory(cwd)
207207

0 commit comments

Comments
 (0)