Skip to content

feat: add optional GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE env #196

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

Merged
merged 4 commits into from
Apr 1, 2025
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/swift-berries-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"changesets-gitlab": minor
---

feat: add a new optional `GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE` environment variable to automatically resolve added discussion when changeset is present, if you want to always resolve the discussion, you should actually use `GITLAB_COMMENT_TYPE=note` instead, default `true`
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ GLOBAL_AGENT_NO_PROXY # Like above but for no proxied requests

GITLAB_HOST # optional, if you're using custom GitLab host, will fallback to `CI_SERVER_URL` if not provided

GITLAB_TOKEN # required, token with accessibility to push, package registries, and merge request APIs. Note the CI_JOB_TOKEN does not have sufficient permissons
GITLAB_TOKEN_TYPE # optional, type of the provided token in GITLAB_TOKEN. defaults to personal access token. Can be `oauth` if you use Gitlab Oauth (personal access) token..
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
GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com`
GITLAB_COMMENT_TYPE # optional, type of the comment. defaults to `discussion`. can be set to `note` to not create a discussion instead of a thread
GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI
DEBUG_GITLAB_CREDENTIAL # optional, default `false`
GITLAB_TOKEN # required, token with accessibility to push, package registries, and merge request APIs. Note the CI_JOB_TOKEN does not have sufficient permissions
GITLAB_TOKEN_TYPE # optional, type of the provided token in GITLAB_TOKEN. defaults to personal access token. Can be `oauth` if you use Gitlab Oauth (personal access) token
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
GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com`
GITLAB_COMMENT_TYPE # optional, type of the comment. defaults to `discussion`. can be set to `note` to not create a discussion instead of a thread
GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE # optional, automatically resolve added discussion when changeset is present, if you want to always resolve the discussion, you should actually use `GITLAB_COMMENT_TYPE=note` instead, default `true`
GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI
DEBUG_GITLAB_CREDENTIAL # optional, whether to log when setting remote url with sensitive `token` displayed
```

### Example workflow
Expand Down
37 changes: 23 additions & 14 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as context from './context.js'
import { env } from './env.js'
import { getChangedPackages } from './get-changed-packages.js'
import type { LooseString } from './types.js'
import { getUsername } from './utils.js'
import { getUsername, TRUTHY_VALUES } from './utils.js'

const generatedByBotNote = 'Generated By Changesets GitLab Bot'

Expand Down Expand Up @@ -246,11 +246,8 @@ export const comment = async () => {

const {
CI_MERGE_REQUEST_IID: mrIid,
CI_MERGE_REQUEST_PROJECT_URL,
CI_MERGE_REQUEST_SOURCE_BRANCH_SHA,
CI_MERGE_REQUEST_TITLE,
GITLAB_COMMENT_TYPE,
GITLAB_ADD_CHANGESET_MESSAGE,
GITLAB_COMMENT_TYPE: commentType,
GITLAB_ADD_CHANGESET_MESSAGE: commitMessage,
} = env

if (mrBranch.startsWith('changeset-release')) {
Expand All @@ -261,15 +258,15 @@ export const comment = async () => {

let errFromFetchingChangedFiles = ''
try {
const latestCommitSha = CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
const latestCommitSha = env.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
const changedFilesPromise = api.MergeRequests.showChanges(
context.projectId,
mrIid,
)

const [noteInfo, hasChangeset, { changedPackages, releasePlan }] =
await Promise.all([
getNoteInfo(api, mrIid, GITLAB_COMMENT_TYPE),
getNoteInfo(api, mrIid, commentType),
hasChangesetBeenAdded(changedFilesPromise),
getChangedPackages({
changedFiles: changedFilesPromise.then(x =>
Expand All @@ -289,17 +286,17 @@ export const comment = async () => {
}),
] as const)

const addChangesetUrl = `${CI_MERGE_REQUEST_PROJECT_URL}/-/new/${mrBranch}?file_name=.changeset/${humanId(
const addChangesetUrl = `${env.CI_MERGE_REQUEST_PROJECT_URL}/-/new/${mrBranch}?file_name=.changeset/${humanId(
{
separator: '-',
capitalize: false,
},
)}.md&file=${getNewChangesetTemplate(
changedPackages,
CI_MERGE_REQUEST_TITLE,
env.CI_MERGE_REQUEST_TITLE,
)}${
GITLAB_ADD_CHANGESET_MESSAGE
? '&commit_message=' + encodeURIComponent(GITLAB_ADD_CHANGESET_MESSAGE)
commitMessage
? '&commit_message=' + encodeURIComponent(commitMessage)
: ''
}`

Expand All @@ -309,9 +306,21 @@ export const comment = async () => {
: getAbsentMessage(latestCommitSha, addChangesetUrl, releasePlan)) +
errFromFetchingChangedFiles

switch (GITLAB_COMMENT_TYPE) {
switch (commentType) {
case 'discussion': {
if (noteInfo) {
if (
hasChangeset &&
TRUTHY_VALUES.has(env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE || '1')
) {
await api.MergeRequestDiscussions.resolve(
context.projectId,
mrIid,
noteInfo.discussionId,
true,
)
}

return api.MergeRequestDiscussions.editNote(
context.projectId,
mrIid,
Expand Down Expand Up @@ -343,7 +352,7 @@ export const comment = async () => {
}
default: {
throw new Error(
`Invalid comment type "${GITLAB_COMMENT_TYPE}", should be "discussion" or "note"`,
`Invalid comment type "${commentType}", should be "discussion" or "note"`,
)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const env = {
GITLAB_CI_USER_EMAIL:
process.env.GITLAB_CI_USER_EMAIL || 'gitlab[bot]@users.noreply.gitlab.com',
GITLAB_COMMENT_TYPE: process.env.GITLAB_COMMENT_TYPE ?? 'discussion',
DEBUG_GITLAB_CREDENTIAL: process.env.DEBUG_GITLAB_CREDENTIAL ?? 'false',

// only check for the token if we are explicitly using it

Expand Down
24 changes: 11 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,27 @@ import { setupUser } from './git-utils.js'
import readChangesetState from './read-changeset-state.js'
import { runPublish, runVersion } from './run.js'
import type { MainCommandOptions } from './types.js'
import { execSync, getOptionalInput, getUsername } from './utils.js'
import {
execSync,
getOptionalInput,
getUsername,
TRUTHY_VALUES,
} from './utils.js'

export const main = async ({
published,
onlyChangesets,
}: MainCommandOptions = {}) => {
const {
CI,
GITLAB_HOST,
GITLAB_TOKEN,
HOME,
NPM_TOKEN,
DEBUG_GITLAB_CREDENTIAL = 'false',
} = env
const { GITLAB_TOKEN, NPM_TOKEN } = env

setOutput('published', false)
setOutput('publishedPackages', [])

if (CI) {
if (env.CI) {
console.log('setting git user')
await setupUser()

const url = new URL(GITLAB_HOST)
const url = new URL(env.GITLAB_HOST)

console.log('setting GitLab credentials')
const username = await getUsername(createApi())
Expand All @@ -47,7 +45,7 @@ export const main = async ({
url.host
}${url.pathname.replace(/\/$/, '')}/${env.CI_PROJECT_PATH}.git`,
],
{ silent: !['true', '1'].includes(DEBUG_GITLAB_CREDENTIAL) },
{ silent: !TRUTHY_VALUES.has(env.DEBUG_GITLAB_CREDENTIAL!) },
)
}

Expand All @@ -67,7 +65,7 @@ export const main = async ({
'No changesets found, attempting to publish any unpublished packages to npm',
)

const npmrcPath = `${HOME}/.npmrc`
const npmrcPath = `${env.HOME}/.npmrc`
if (fs.existsSync(npmrcPath)) {
console.log('Found existing .npmrc file')
} else if (NPM_TOKEN) {
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export type Env = GitLabCIPredefinedVariables &
GITLAB_CI_USER_NAME?: string
GITLAB_CI_USER_EMAIL: string
GITLAB_COMMENT_TYPE: LooseString<'discussion' | 'note'>
GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE?: LooseString<'1' | 'true'>
GITLAB_ADD_CHANGESET_MESSAGE?: string
DEBUG_GITLAB_CREDENTIAL: LooseString<'1' | 'true'>
DEBUG_GITLAB_CREDENTIAL?: LooseString<'1' | 'true'>

HOME: string
NPM_TOKEN?: string
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,5 @@ export const getUsername = (api: Gitlab) => {

export const cjsRequire =
typeof require === 'undefined' ? createRequire(import.meta.url) : require

export const TRUTHY_VALUES = new Set(['true', '1'])
Loading