diff --git a/.changeset/slow-actors-exercise.md b/.changeset/slow-actors-exercise.md new file mode 100644 index 00000000..7eb07108 --- /dev/null +++ b/.changeset/slow-actors-exercise.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": minor +--- + +Add `labels` input to configure diff --git a/action.yml b/action.yml index c9ca1b68..288cbc98 100644 --- a/action.yml +++ b/action.yml @@ -28,6 +28,9 @@ inputs: description: "A boolean value to indicate whether to create Github releases after `publish` or not" required: false default: true + labels: + description: The pull request labels that comma separated. Default is empty + required: false outputs: published: description: A boolean value to indicate whether a publishing is happened or not diff --git a/src/__snapshots__/run.test.ts.snap b/src/__snapshots__/run.test.ts.snap index f0367903..a237bea7 100644 --- a/src/__snapshots__/run.test.ts.snap +++ b/src/__snapshots__/run.test.ts.snap @@ -33,6 +33,53 @@ Array [ ] `; +exports[`version creates simple PR with labels 1`] = ` +Array [ + Object { + "base": "some-branch", + "body": "This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing). If you're not ready to do a release yet, that's fine, whenever you add more changesets to some-branch, this PR will be updated. + + +# Releases +## simple-project-pkg-a@1.1.0 + +### Minor Changes + +- Awesome feature + +### Patch Changes + +- Updated dependencies + - simple-project-pkg-b@1.1.0 + +## simple-project-pkg-b@1.1.0 + +### Minor Changes + +- Awesome feature +", + "head": "changeset-release/some-branch", + "owner": "changesets", + "repo": "action", + "title": "Version Packages", + }, +] +`; + +exports[`version creates simple PR with labels 2`] = ` +Array [ + Object { + "issue_number": 123, + "labels": Array [ + "release", + "changesets", + ], + "owner": "changesets", + "repo": "action", + }, +] +`; + exports[`version does not include any release information if a message with simplified release info exceeds size limit 1`] = ` Array [ Object { @@ -116,3 +163,49 @@ Array [ }, ] `; + +exports[`version updates simple PR with adding labels 1`] = ` +Array [ + Object { + "body": "This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing). If you're not ready to do a release yet, that's fine, whenever you add more changesets to some-branch, this PR will be updated. + + +# Releases +## simple-project-pkg-a@1.1.0 + +### Minor Changes + +- Awesome feature + +### Patch Changes + +- Updated dependencies + - simple-project-pkg-b@1.1.0 + +## simple-project-pkg-b@1.1.0 + +### Minor Changes + +- Awesome feature +", + "owner": "changesets", + "pull_number": 123, + "repo": "action", + "title": "Version Packages", + }, +] +`; + +exports[`version updates simple PR with adding labels 2`] = ` +Array [ + Object { + "issue_number": 123, + "labels": Array [ + "release", + "changesets", + ], + "owner": "changesets", + "repo": "action", + }, +] +`; diff --git a/src/index.ts b/src/index.ts index 087eccc5..b91ba29a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -101,6 +101,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; script: getOptionalInput("version"), githubToken, prTitle: getOptionalInput("title"), + prLabels: (getOptionalInput("labels") || "").split(","), commitMessage: getOptionalInput("commit"), hasPublishScript, }); diff --git a/src/run.test.ts b/src/run.test.ts index c3518510..ad8cde84 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -25,10 +25,14 @@ let mockedGithubMethods = { }, pulls: { create: jest.fn(), + update: jest.fn(), }, repos: { createRelease: jest.fn(), }, + issues: { + addLabels: jest.fn(), + } }; (github.getOctokit as any).mockImplementation(() => mockedGithubMethods); @@ -275,4 +279,84 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis. /All release information have been omitted from this message, as the content exceeds the size limit/ ); }); + + it("creates simple PR with labels", async () => { + let cwd = f.copy("simple-project"); + linkNodeModules(cwd); + + mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce( + () => ({ data: { items: [] } }) + ); + + mockedGithubMethods.pulls.create.mockImplementationOnce( + () => ({ data: { number: 123 } }) + ); + + await writeChangesets( + [ + { + releases: [ + { + name: "simple-project-pkg-a", + type: "minor", + }, + { + name: "simple-project-pkg-b", + type: "minor", + }, + ], + summary: "Awesome feature", + }, + ], + cwd + ); + + await runVersion({ + githubToken: "@@GITHUB_TOKEN", + cwd, + prLabels: ["release", "changesets"], + }); + + expect(mockedGithubMethods.pulls.update).toHaveBeenCalledTimes(0); + expect(mockedGithubMethods.pulls.create.mock.calls[0]).toMatchSnapshot(); + expect(mockedGithubMethods.issues.addLabels.mock.calls[0]).toMatchSnapshot(); + }); + + it("updates simple PR with adding labels", async () => { + let cwd = f.copy("simple-project"); + linkNodeModules(cwd); + + mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce( + () => ({ data: { items: [{ number: 123, labels: [{ id: "label_42", name: "release" }] }] } }) + ); + + await writeChangesets( + [ + { + releases: [ + { + name: "simple-project-pkg-a", + type: "minor", + }, + { + name: "simple-project-pkg-b", + type: "minor", + }, + ], + summary: "Awesome feature", + }, + ], + cwd + ); + + await runVersion({ + githubToken: "@@GITHUB_TOKEN", + cwd, + prLabels: ["release", "changesets"], + }); + + expect(mockedGithubMethods.pulls.create).toHaveBeenCalledTimes(0); + expect(mockedGithubMethods.pulls.update.mock.calls[0]).toMatchSnapshot(); + expect(mockedGithubMethods.issues.addLabels.mock.calls[0]).toMatchSnapshot(); + }); }); diff --git a/src/run.ts b/src/run.ts index 2612393f..c87e2da1 100644 --- a/src/run.ts +++ b/src/run.ts @@ -249,6 +249,7 @@ type VersionOptions = { githubToken: string; cwd?: string; prTitle?: string; + prLabels?: string[]; commitMessage?: string; hasPublishScript?: boolean; prBodyMaxCharacters?: number; @@ -263,6 +264,7 @@ export async function runVersion({ githubToken, cwd = process.cwd(), prTitle = "Version Packages", + prLabels = [], commitMessage = "Version Packages", hasPublishScript = false, prBodyMaxCharacters = MAX_CHARACTERS_PER_MESSAGE, @@ -350,6 +352,15 @@ export async function runVersion({ ...github.context.repo, }); + if (prLabels.length > 0) { + console.log("adding labels to the pull request"); + await octokit.issues.addLabels({ + issue_number: newPullRequest.number, + labels: prLabels, + ...github.context.repo, + }); + } + return { pullRequestNumber: newPullRequest.number, }; @@ -364,6 +375,18 @@ export async function runVersion({ ...github.context.repo, }); + if (prLabels.length > 0) { + const existingLabels = pullRequest.labels.map((l) => l.name); + if (prLabels.some((l) => !existingLabels.includes(l))) { + console.log("adding labels to the pull request"); + await octokit.issues.addLabels({ + issue_number: pullRequest.number, + labels: prLabels, + ...github.context.repo, + }); + } + } + return { pullRequestNumber: pullRequest.number, };