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 known issues, add other token types support #23

Merged
merged 11 commits into from
Nov 22, 2021
5 changes: 5 additions & 0 deletions .changeset/add-missing-deps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'changesets-gitlab': patch
---

Add missing dependencies to the package.json
5 changes: 5 additions & 0 deletions .changeset/add-token-type-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'changesets-gitlab': minor
---

Add support for job tokens and oauth tokens (via GITLAB_TOKEN_TYPE config), The Gitlab job tokens have limited access only to Gitlab package registry so we cannot use it until [this Epic](https://gitlab.com/groups/gitlab-org/-/epics/3559) gets implemented.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lib
node_modules
.eslintcache
.env
.type-coverage
*.log
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ GLOBAL_AGENT_NO_PROXY # Like above but for no proxied requests
GITLAB_HOST # optional, if you're using custom GitLab host

GITLAB_TOKEN # required, token with accessibility to push
GITLAB_CI_USER_NAME # required, username with accessibility to push, used in pairs of the above token
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
GITLAB_CI_USER_NAME # required, username with accessibility to push, used in pairs of the above token (if it was personal access token)
JounQin marked this conversation as resolved.
Show resolved Hide resolved
GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com`
```

Expand Down Expand Up @@ -174,3 +175,23 @@ release:
variables:
INPUT_VERSION: yarn changeset version
```

You may also want to run `yarn install` after the `changeset verion` command to update the `yarn.lock` in the version MR. You need to disable immutable lock file setting using an env variable:

```yml
release:
image: node:lts-alpine
only: main
script: yarn changesets-gitlab
variables:
YARN_ENABLE_IMMUTABLE_INSTALLS: 'false'
INPUT_VERSION: yarn update-versions
```

And your `update-versions` script would be:

```json
{
"update-versions": "changeset version && yarn install"
}
```
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
"@actions/exec": "^1.1.0",
"@changesets/assemble-release-plan": "^5.0.2",
"@changesets/config": "^1.6.2",
"@changesets/errors": "^0.1.4",
"@changesets/parse": "^0.3.9",
"@changesets/pre": "^1.0.7",
"@changesets/read": "^0.5.0",
"@changesets/types": "^4.0.0",
"@gitbeaker/node": "^34.6.0",
"@manypkg/get-packages": "^1.1.3",
Expand Down
8 changes: 7 additions & 1 deletion src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ ${getReleasePlanMessage(releasePlan)}

[Click here if you're a maintainer who wants to add a changeset to this MR](${addChangesetUrl})

__Generated By Changesets GitLab Bot__
`

const getApproveMessage = (
Expand All @@ -87,6 +88,7 @@ Not sure what this means? [Click here to learn what changesets are](https://git

[Click here if you're a maintainer who wants to add another changeset to this MR](${addChangesetUrl})

__Generated By Changesets GitLab Bot__
`

const getNewChangesetTemplate = (changedPackages: string[], title: string) =>
Expand All @@ -100,7 +102,11 @@ ${title}
const getCommentId = (api: Gitlab, mrIid: number | string) =>
api.MergeRequestNotes.all(context.projectId, mrIid).then(comments => {
const changesetBotComment = comments.find(
comment => comment.author.username === process.env.GITLAB_CI_USER_NAME,
comment =>
comment.author.username === process.env.GITLAB_CI_USER_NAME &&
// We need to ensure the comment is generated by us but we don't have a app bot like GitHub
// @see https://github.com/apps/changeset-bot
comment.body.includes('Generated By Changesets GitLab Bot'),
)
return changesetBotComment ? changesetBotComment.id : null
})
Expand Down
4 changes: 1 addition & 3 deletions src/gitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export const switchToMaybeExistingBranch = async (branch: string) => {
const { stderr } = await execWithOutput('git', ['checkout', branch], {
ignoreReturnCode: true,
})
const isCreatingBranch = !stderr
.toString()
.includes(`Switched to a new branch '${branch}'`)
const isCreatingBranch = !stderr.includes(`Switched to branch '${branch}'`)
if (isCreatingBranch) {
await exec('git', ['checkout', '-b', branch])
}
Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,21 @@ export const createApi = (gitlabToken?: string) => {
}
}

const token = gitlabToken || process.env.GITLAB_TOKEN

let tokenType: 'jobToken' | 'oauthToken' | 'token' = 'token'

switch (process.env.GITLAB_TOKEN_TYPE) {
case 'job':
tokenType = 'jobToken'
break
case 'oauth':
tokenType = 'oauthToken'
break
}

return new Gitlab({
host: process.env.GITLAB_HOST,
token: gitlabToken || process.env.GITLAB_TOKEN,
[tokenType]: token,
})
}
22 changes: 11 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
dependencies:
"@babel/types" "^7.16.0"

"@babel/helper-hoist-variables@^7.16.0":
"@babel/helper-hoist-variables@^7.14.5", "@babel/helper-hoist-variables@^7.16.0":
version "7.16.0"
resolved "https://registry.yarnpkg.com/@babel/helper-hoist-variables/-/helper-hoist-variables-7.16.0.tgz#4c9023c2f1def7e28ff46fc1dbcd36a39beaa81a"
integrity sha512-1AZlpazjUR0EQZQv3sgRNfM9mEVWPK3M6vlalczA+EECcPz3XPh6VplbErL5UoMpChhSck5wAJHthlj1bYpcmg==
Expand Down Expand Up @@ -554,13 +554,13 @@
"@babel/plugin-proposal-optional-chaining" "^7.16.0"

"@babel/plugin-proposal-async-do-expressions@^7.14.0":
version "7.16.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-do-expressions/-/plugin-proposal-async-do-expressions-7.16.0.tgz#c747dc2696cf4018578ee441e2cd60d6585d4e33"
integrity sha512-nrAVI0MW6N+voMBE0MbfscU/vnIGxdhaLU0KgkuGA26hLi+rmrEAdJ/HAXVCx0WOmBXTfhbSP1FlL6lZSIWrTg==
version "7.14.5"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-do-expressions/-/plugin-proposal-async-do-expressions-7.14.5.tgz#0aadb8806809246d64682be1b82a6956015e2077"
integrity sha512-AP38yW7vyOzBY/SS0477k/ddmwpLO3x5rYu5bc+wLpLhfjr+lISt0woizugqQvHdzY4K50rBE+h6JXFQh41TGw==
dependencies:
"@babel/helper-hoist-variables" "^7.16.0"
"@babel/helper-hoist-variables" "^7.14.5"
"@babel/helper-plugin-utils" "^7.14.5"
"@babel/plugin-syntax-async-do-expressions" "^7.16.0"
"@babel/plugin-syntax-async-do-expressions" "^7.14.5"

"@babel/plugin-proposal-async-generator-functions@^7.16.4":
version "7.16.4"
Expand Down Expand Up @@ -767,7 +767,7 @@
"@babel/helper-create-regexp-features-plugin" "^7.16.0"
"@babel/helper-plugin-utils" "^7.14.5"

"@babel/plugin-syntax-async-do-expressions@^7.16.0":
"@babel/plugin-syntax-async-do-expressions@^7.14.5":
version "7.16.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-do-expressions/-/plugin-syntax-async-do-expressions-7.16.0.tgz#5fc7e4bca519979a92fd9596d07baee681e37a01"
integrity sha512-3p9KuwOHUXSx/a52S349DKjnfYWtyims8d8kT7MsTI59HmnTaO4MWT159Nc0e1m8773h/8cG44nVtH2V8dDtJg==
Expand Down Expand Up @@ -1593,7 +1593,7 @@
"@manypkg/get-packages" "^1.0.1"
fs-extra "^7.0.1"

"@changesets/read@^0.5.1":
"@changesets/read@^0.5.0", "@changesets/read@^0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@changesets/read/-/read-0.5.1.tgz#89635bf2b84e5fd5d49e1d685adb9c47b5e679a1"
integrity sha512-QJ3rVS+L0Y3yLk3cAOglNh4tuMUfQl8cJjyAnNnJHS9nCXZUiZjYiJc+34XpZT5vUb+4+0FY1wWtzlkAKuLR2g==
Expand Down Expand Up @@ -3057,9 +3057,9 @@ acorn@^7.1.1, acorn@^7.4.0:
integrity sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==

acorn@^8.2.4, acorn@^8.4.1:
version "8.6.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.6.0.tgz#e3692ba0eb1a0c83eaa4f37f5fa7368dd7142895"
integrity sha512-U1riIR+lBSNi3IbxtaHOIKdH8sLFv3NYfNv8sg7ZsNhcfl4HF2++BfqqrNAxoCLQW1iiylOj76ecnaUxz+z9yw==
version "8.5.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.5.0.tgz#4512ccb99b3698c752591e9bb4472e38ad43cee2"
integrity sha512-yXbYeFy+jUuYd3/CDcg2NkIYE991XYX/bje7LmjJigUciaeO1JR4XxXgCIV1/Zc/dRuFEyw1L0pbA+qynJkW5Q==

agent-base@6:
version "6.0.2"
Expand Down