Skip to content

Commit b681513

Browse files
crysadrakJiří FenclJounQin
authored
feat: add optional GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE env (#196)
Co-authored-by: Jiří Fencl <jiri.fencl@firma.seznam.cz> Co-authored-by: JounQin <admin@1stg.me>
1 parent cac8607 commit b681513

File tree

7 files changed

+51
-36
lines changed

7 files changed

+51
-36
lines changed

.changeset/swift-berries-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"changesets-gitlab": minor
3+
---
4+
5+
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`

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ GLOBAL_AGENT_NO_PROXY # Like above but for no proxied requests
4848

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

51-
GITLAB_TOKEN # required, token with accessibility to push, package registries, and merge request APIs. Note the CI_JOB_TOKEN does not have sufficient permissons
52-
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..
53-
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
54-
GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com`
55-
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
56-
GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI
57-
DEBUG_GITLAB_CREDENTIAL # optional, default `false`
51+
GITLAB_TOKEN # required, token with accessibility to push, package registries, and merge request APIs. Note the CI_JOB_TOKEN does not have sufficient permissions
52+
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
53+
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
54+
GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com`
55+
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
56+
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`
57+
GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI
58+
DEBUG_GITLAB_CREDENTIAL # optional, whether to log when setting remote url with sensitive `token` displayed
5859
```
5960

6061
### Example workflow

src/comment.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as context from './context.js'
2020
import { env } from './env.js'
2121
import { getChangedPackages } from './get-changed-packages.js'
2222
import type { LooseString } from './types.js'
23-
import { getUsername } from './utils.js'
23+
import { getUsername, TRUTHY_VALUES } from './utils.js'
2424

2525
const generatedByBotNote = 'Generated By Changesets GitLab Bot'
2626

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

247247
const {
248248
CI_MERGE_REQUEST_IID: mrIid,
249-
CI_MERGE_REQUEST_PROJECT_URL,
250-
CI_MERGE_REQUEST_SOURCE_BRANCH_SHA,
251-
CI_MERGE_REQUEST_TITLE,
252-
GITLAB_COMMENT_TYPE,
253-
GITLAB_ADD_CHANGESET_MESSAGE,
249+
GITLAB_COMMENT_TYPE: commentType,
250+
GITLAB_ADD_CHANGESET_MESSAGE: commitMessage,
254251
} = env
255252

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

262259
let errFromFetchingChangedFiles = ''
263260
try {
264-
const latestCommitSha = CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
261+
const latestCommitSha = env.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
265262
const changedFilesPromise = api.MergeRequests.showChanges(
266263
context.projectId,
267264
mrIid,
268265
)
269266

270267
const [noteInfo, hasChangeset, { changedPackages, releasePlan }] =
271268
await Promise.all([
272-
getNoteInfo(api, mrIid, GITLAB_COMMENT_TYPE),
269+
getNoteInfo(api, mrIid, commentType),
273270
hasChangesetBeenAdded(changedFilesPromise),
274271
getChangedPackages({
275272
changedFiles: changedFilesPromise.then(x =>
@@ -289,17 +286,17 @@ export const comment = async () => {
289286
}),
290287
] as const)
291288

292-
const addChangesetUrl = `${CI_MERGE_REQUEST_PROJECT_URL}/-/new/${mrBranch}?file_name=.changeset/${humanId(
289+
const addChangesetUrl = `${env.CI_MERGE_REQUEST_PROJECT_URL}/-/new/${mrBranch}?file_name=.changeset/${humanId(
293290
{
294291
separator: '-',
295292
capitalize: false,
296293
},
297294
)}.md&file=${getNewChangesetTemplate(
298295
changedPackages,
299-
CI_MERGE_REQUEST_TITLE,
296+
env.CI_MERGE_REQUEST_TITLE,
300297
)}${
301-
GITLAB_ADD_CHANGESET_MESSAGE
302-
? '&commit_message=' + encodeURIComponent(GITLAB_ADD_CHANGESET_MESSAGE)
298+
commitMessage
299+
? '&commit_message=' + encodeURIComponent(commitMessage)
303300
: ''
304301
}`
305302

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

312-
switch (GITLAB_COMMENT_TYPE) {
309+
switch (commentType) {
313310
case 'discussion': {
314311
if (noteInfo) {
312+
if (
313+
hasChangeset &&
314+
TRUTHY_VALUES.has(env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE || '1')
315+
) {
316+
await api.MergeRequestDiscussions.resolve(
317+
context.projectId,
318+
mrIid,
319+
noteInfo.discussionId,
320+
true,
321+
)
322+
}
323+
315324
return api.MergeRequestDiscussions.editNote(
316325
context.projectId,
317326
mrIid,
@@ -343,7 +352,7 @@ export const comment = async () => {
343352
}
344353
default: {
345354
throw new Error(
346-
`Invalid comment type "${GITLAB_COMMENT_TYPE}", should be "discussion" or "note"`,
355+
`Invalid comment type "${commentType}", should be "discussion" or "note"`,
347356
)
348357
}
349358
}

src/env.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const env = {
1818
GITLAB_CI_USER_EMAIL:
1919
process.env.GITLAB_CI_USER_EMAIL || 'gitlab[bot]@users.noreply.gitlab.com',
2020
GITLAB_COMMENT_TYPE: process.env.GITLAB_COMMENT_TYPE ?? 'discussion',
21-
DEBUG_GITLAB_CREDENTIAL: process.env.DEBUG_GITLAB_CREDENTIAL ?? 'false',
2221

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

src/main.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,27 @@ import { setupUser } from './git-utils.js'
1010
import readChangesetState from './read-changeset-state.js'
1111
import { runPublish, runVersion } from './run.js'
1212
import type { MainCommandOptions } from './types.js'
13-
import { execSync, getOptionalInput, getUsername } from './utils.js'
13+
import {
14+
execSync,
15+
getOptionalInput,
16+
getUsername,
17+
TRUTHY_VALUES,
18+
} from './utils.js'
1419

1520
export const main = async ({
1621
published,
1722
onlyChangesets,
1823
}: MainCommandOptions = {}) => {
19-
const {
20-
CI,
21-
GITLAB_HOST,
22-
GITLAB_TOKEN,
23-
HOME,
24-
NPM_TOKEN,
25-
DEBUG_GITLAB_CREDENTIAL = 'false',
26-
} = env
24+
const { GITLAB_TOKEN, NPM_TOKEN } = env
2725

2826
setOutput('published', false)
2927
setOutput('publishedPackages', [])
3028

31-
if (CI) {
29+
if (env.CI) {
3230
console.log('setting git user')
3331
await setupUser()
3432

35-
const url = new URL(GITLAB_HOST)
33+
const url = new URL(env.GITLAB_HOST)
3634

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

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

70-
const npmrcPath = `${HOME}/.npmrc`
68+
const npmrcPath = `${env.HOME}/.npmrc`
7169
if (fs.existsSync(npmrcPath)) {
7270
console.log('Found existing .npmrc file')
7371
} else if (NPM_TOKEN) {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export type Env = GitLabCIPredefinedVariables &
2020
GITLAB_CI_USER_NAME?: string
2121
GITLAB_CI_USER_EMAIL: string
2222
GITLAB_COMMENT_TYPE: LooseString<'discussion' | 'note'>
23+
GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE?: LooseString<'1' | 'true'>
2324
GITLAB_ADD_CHANGESET_MESSAGE?: string
24-
DEBUG_GITLAB_CREDENTIAL: LooseString<'1' | 'true'>
25+
DEBUG_GITLAB_CREDENTIAL?: LooseString<'1' | 'true'>
2526

2627
HOME: string
2728
NPM_TOKEN?: string

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,5 @@ export const getUsername = (api: Gitlab) => {
171171

172172
export const cjsRequire =
173173
typeof require === 'undefined' ? createRequire(import.meta.url) : require
174+
175+
export const TRUTHY_VALUES = new Set(['true', '1'])

0 commit comments

Comments
 (0)