Skip to content

Commit 74f1c72

Browse files
authored
feat: support command on published/only-changesets (#7)
1 parent d8c5c78 commit 74f1c72

File tree

11 files changed

+105
-39
lines changed

11 files changed

+105
-39
lines changed

.changeset/nice-ads-cheer.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: support command on published/only-changesets

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ GitLab CI cli for [changesets](https://github.com/atlassian/changesets) like its
2828
- commit - The commit message to use. Default to `Version Packages`
2929
- title - The merge request title. Default to `Version Packages`
3030

31+
#### Only available in `changesets-gitlab`
32+
33+
- published - Command executed after published
34+
- only_changesets - Command executed on only changesets detected
35+
3136
### Outputs
3237

3338
- published - A boolean value to indicate whether a publishing is happened or not
@@ -66,7 +71,7 @@ comment:
6671
image: node:lts-alpine
6772
stage: comment
6873
only: merge_requests
69-
script: yarn changesets-gitlab -c # comment automatically like https://github.com/changesets/bot
74+
script: yarn changesets-gitlab comment # comment automatically like https://github.com/changesets/bot
7075

7176
release:
7277
image: node:lts-alpine
@@ -89,7 +94,7 @@ comment:
8994
image: node:lts-alpine
9095
stage: comment
9196
only: merge_requests
92-
script: yarn changesets-gitlab -c
97+
script: yarn changesets-gitlab comment
9398
9499
release:
95100
image: node:lts-alpine
@@ -134,7 +139,7 @@ comment:
134139
stage: comment
135140
only:
136141
- merge_requests
137-
script: yarn changesets-gitlab -c
142+
script: yarn changesets-gitlab comment
138143
139144
release:
140145
image: node:lts-alpine
@@ -160,7 +165,7 @@ comment:
160165
stage: comment
161166
only:
162167
- merge_requests
163-
script: yarn changesets-gitlab -c
168+
script: yarn changesets-gitlab comment
164169
165170
release:
166171
image: node:lts-alpine

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@gitbeaker/node": "^32.1.2",
4343
"@manypkg/get-packages": "^1.1.1",
4444
"@sentry/node": "^6.11.0",
45+
"commander": "^8.1.0",
4546
"dotenv": "^10.0.0",
4647
"fs-extra": "^10.0.0",
4748
"global-agent": "^3.0.0",

src/cli.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
import './env.js'
44

5+
import _ from 'module'
6+
57
import { setFailed } from '@actions/core'
8+
import { program } from 'commander'
69

710
import { comment } from './comment.js'
811
import { main } from './main.js'
12+
import { getOptionalInput } from './utils.js'
13+
14+
const cjsRequire =
15+
typeof require === 'undefined' ? _.createRequire(import.meta.url) : require
916

10-
const cli = async () => {
17+
const run = async () => {
1118
const { GITLAB_CI_USER_NAME, GITLAB_TOKEN } = process.env
1219

1320
if (!GITLAB_TOKEN || !GITLAB_CI_USER_NAME) {
@@ -17,10 +24,29 @@ const cli = async () => {
1724
return
1825
}
1926

20-
return ['-c', '--comment'].includes(process.argv[2]) ? comment() : main()
27+
program.version(
28+
(cjsRequire('../package.json') as { version: string }).version,
29+
)
30+
31+
program.command('comment').action(async () => {
32+
await comment()
33+
})
34+
35+
program
36+
.command('main', {
37+
isDefault: true,
38+
})
39+
.action(() =>
40+
main({
41+
published: getOptionalInput('published'),
42+
onlyChangesets: getOptionalInput('only_changesets'),
43+
}),
44+
)
45+
46+
return program.showHelpAfterError().parseAsync()
2147
}
2248

23-
cli().catch((err: Error) => {
49+
run().catch((err: Error) => {
2450
console.error(err)
2551
process.exit(1)
2652
})

src/comment.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const getReleasePlanMessage = (releasePlan: ReleasePlan | null) => {
3434
]),
3535
])
3636

37-
return `<details><summary>This PR includes ${
37+
return `<details><summary>This MR includes ${
3838
releasePlan.changesets.length > 0
3939
? `changesets to release ${
4040
publishableReleases.length === 1
@@ -47,7 +47,7 @@ const getReleasePlanMessage = (releasePlan: ReleasePlan | null) => {
4747
${
4848
publishableReleases.length > 0
4949
? table
50-
: "When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types"
50+
: "When changesets are added to this MR, you'll see the packages that this MR includes changesets for and the associated semver types"
5151
}
5252
5353
</details>`
@@ -61,13 +61,13 @@ const getAbsentMessage = (
6161
6262
Latest commit: ${commitSha}
6363
64-
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**
64+
Merging this MR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**
6565
6666
${getReleasePlanMessage(releasePlan)}
6767
6868
[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).
6969
70-
[Click here if you're a maintainer who wants to add a changeset to this PR](${addChangesetUrl})
70+
[Click here if you're a maintainer who wants to add a changeset to this MR](${addChangesetUrl})
7171
7272
`
7373

@@ -79,13 +79,13 @@ const getApproveMessage = (
7979
8080
Latest commit: ${commitSha}
8181
82-
**The changes in this PR will be included in the next version bump.**
82+
**The changes in this MR will be included in the next version bump.**
8383
8484
${getReleasePlanMessage(releasePlan)}
8585
8686
Not sure what this means? [Click here to learn what changesets are](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).
8787
88-
[Click here if you're a maintainer who wants to add another changeset to this PR](${addChangesetUrl})
88+
[Click here if you're a maintainer who wants to add another changeset to this MR](${addChangesetUrl})
8989
9090
`
9191

@@ -159,7 +159,7 @@ export const comment = async () => {
159159
api,
160160
}).catch((err: unknown) => {
161161
if (err instanceof ValidationError) {
162-
errFromFetchingChangedFiles = `<details><summary>💥 An error occurred when fetching the changed packages and changesets in this PR</summary>\n\n\`\`\`\n${err.message}\n\`\`\`\n\n</details>\n`
162+
errFromFetchingChangedFiles = `<details><summary>💥 An error occurred when fetching the changed packages and changesets in this MR</summary>\n\n\`\`\`\n${err.message}\n\`\`\`\n\n</details>\n`
163163
} else {
164164
console.error(err)
165165
captureException(err)

src/main.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import { exec } from '@actions/exec'
77
import { setupUser } from './gitUtils.js'
88
import readChangesetState from './readChangesetState.js'
99
import { runPublish, runVersion } from './run.js'
10+
import { MainCommandOptions } from './types.js'
11+
import { execSync, getOptionalInput } from './utils.js'
1012

11-
export const main = async () => {
13+
export const main = async ({
14+
published,
15+
onlyChangesets,
16+
}: MainCommandOptions = {}) => {
1217
const {
1318
CI,
1419
CI_PROJECT_PATH,
@@ -47,9 +52,10 @@ export const main = async () => {
4752
const hasPublishScript = !!publishScript
4853

4954
switch (true) {
50-
case !hasChangesets && !hasPublishScript:
55+
case !hasChangesets && !hasPublishScript: {
5156
console.log('No changesets found')
5257
return
58+
}
5359
case !hasChangesets && hasPublishScript: {
5460
console.log(
5561
'No changesets found, attempting to publish any unpublished packages to npm',
@@ -79,16 +85,23 @@ export const main = async () => {
7985
if (result.published) {
8086
setOutput('published', true)
8187
setOutput('publishedPackages', result.publishedPackages)
88+
if (published) {
89+
execSync(published)
90+
}
8291
}
8392
return
8493
}
85-
case hasChangesets:
94+
case hasChangesets: {
8695
await runVersion({
87-
script: getInput('version'),
96+
script: getOptionalInput('version'),
8897
gitlabToken: GITLAB_TOKEN!,
89-
prTitle: getInput('title'),
90-
commitMessage: getInput('commit'),
98+
mrTitle: getOptionalInput('title'),
99+
commitMessage: getOptionalInput('commit'),
91100
hasPublishScript,
92101
})
102+
if (onlyChangesets) {
103+
execSync(onlyChangesets)
104+
}
105+
}
93106
}
94107
}

src/run.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ interface VersionOptions {
182182
script?: string
183183
gitlabToken: string
184184
cwd?: string
185-
prTitle?: string
185+
mrTitle?: string
186186
commitMessage?: string
187187
hasPublishScript?: boolean
188188
}
@@ -191,7 +191,7 @@ export async function runVersion({
191191
script,
192192
gitlabToken,
193193
cwd = process.cwd(),
194-
prTitle = 'Version Packages',
194+
mrTitle = 'Version Packages',
195195
commitMessage = 'Version Packages',
196196
hasPublishScript = false,
197197
}: VersionOptions) {
@@ -201,6 +201,7 @@ export async function runVersion({
201201
const { preState } = await readChangesetState(cwd)
202202

203203
await gitUtils.switchToMaybeExistingBranch(versionBranch)
204+
await exec('git', ['fetch', 'origin', branch])
204205
await gitUtils.reset(`origin/${branch}`)
205206

206207
const versionsByDirectory = await getVersionsByDirectory(cwd)
@@ -220,12 +221,12 @@ export async function runVersion({
220221

221222
const changedPackages = await getChangedPackages(cwd, versionsByDirectory)
222223

223-
const prBodyPromise = (async () =>
224-
`This PR was opened by the [changesets-gitlab](https://github.com/rx-ts/changesets-gitlab) GitLab CI script. When you're ready to do a release, you can merge this and ${
224+
const mrBodyPromise = (async () =>
225+
`This MR was opened by the [changesets-gitlab](https://github.com/rx-ts/changesets-gitlab) GitLab CI script. When you're ready to do a release, you can merge this and ${
225226
hasPublishScript
226227
? 'the packages will be published to npm automatically'
227228
: 'publish to npm yourself or [setup this action to publish automatically](https://github.com/rx-ts/changesets-gitlab#with-publishing)'
228-
}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${branch}, this PR will be updated.
229+
}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${branch}, this MR will be updated.
229230
${
230231
preState
231232
? `
@@ -267,7 +268,7 @@ ${
267268
.map(x => x.content)
268269
.join('\n '))()
269270

270-
const finalPrTitle = `${prTitle}${preState ? ` (${preState.tag})` : ''}`
271+
const finalMrTitle = `${mrTitle}${preState ? ` (${preState.tag})` : ''}`
271272

272273
// project with `commit: true` setting could have already committed files
273274
if (!(await gitUtils.checkIfClean())) {
@@ -289,21 +290,21 @@ ${
289290
})
290291
console.log(JSON.stringify(searchResult, null, 2))
291292
if (searchResult.length === 0) {
292-
console.log('creating pull request')
293+
console.log('creating merge request')
293294
await api.MergeRequests.create(
294295
context.projectId,
295296
versionBranch,
296297
branch,
297-
finalPrTitle,
298+
finalMrTitle,
298299
{
299-
description: await prBodyPromise,
300+
description: await mrBodyPromise,
300301
},
301302
)
302303
} else {
303304
await api.MergeRequests.edit(context.projectId, searchResult[0].iid, {
304-
title: finalPrTitle,
305-
description: await prBodyPromise,
305+
title: finalMrTitle,
306+
description: await mrBodyPromise,
306307
})
307-
console.log('pull request found')
308+
console.log('merge request found')
308309
}
309310
}

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface MainCommandOptions {
2+
published?: string
3+
onlyChangesets?: string
4+
}

src/utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { execSync as _execSync } from 'child_process'
12
import fs from 'fs'
23
import path from 'path'
34

4-
import { unified } from 'unified'
5-
import remarkParse from 'remark-parse'
6-
import remarkStringify from 'remark-stringify'
7-
import { toString as mdastToString } from 'mdast-util-to-string'
5+
import { getInput } from '@actions/core'
86
import { exec } from '@actions/exec'
97
import { getPackages, Package } from '@manypkg/get-packages'
8+
import { toString as mdastToString } from 'mdast-util-to-string'
9+
import remarkParse from 'remark-parse'
10+
import remarkStringify from 'remark-stringify'
11+
import { unified } from 'unified'
1012

1113
export const BumpLevels = {
1214
dep: 0,
@@ -142,3 +144,10 @@ export async function getAllFiles(dir: string, base = dir): Promise<string[]> {
142144
)
143145
return files.flat()
144146
}
147+
148+
export const execSync = (command: string) =>
149+
_execSync(command, {
150+
stdio: 'inherit',
151+
})
152+
153+
export const getOptionalInput = (name: string) => getInput(name) || undefined

test/__snapshots__/utils.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`utils 1`] = `
3+
exports[`utils getAllFiles 1`] = `
44
Array [
55
".gitkeep",
66
"dir/.gitkeep",

0 commit comments

Comments
 (0)