Skip to content
Open
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/slow-actors-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": minor
---

Add `labels` input to configure
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions src/__snapshots__/run.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
},
]
`;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
84 changes: 84 additions & 0 deletions src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
});
});
23 changes: 23 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ type VersionOptions = {
githubToken: string;
cwd?: string;
prTitle?: string;
prLabels?: string[];
commitMessage?: string;
hasPublishScript?: boolean;
prBodyMaxCharacters?: number;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
};
Expand All @@ -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,
};
Expand Down