Skip to content

Commit

Permalink
Added pullRequestNumber to the action's outputs (#167)
Browse files Browse the repository at this point in the history
* feat: set pullRequesstNumber as action output

* chore: add changeset

* fix: feedback from pr review

* Update .changeset/long-mayflies-yell.md

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
dmregister and Andarist authored Apr 7, 2022
1 parent c7c59f0 commit 993a0a0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-mayflies-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": minor
---

Added `pullRequestNumber` to the action's outputs
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ outputs:
A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0"}, {"name": "@xx/xy", "version": "0.8.9"}]`
hasChangesets:
description: A boolean about whether there were changesets. Useful if you want to create your own publishing functionality.
pullRequestNumber:
description: The pull request number that was created or updated
branding:
icon: "package"
color: "blue"
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
return;
}
case hasChangesets:
await runVersion({
const { pullRequestNumber } = await runVersion({
script: getOptionalInput("version"),
githubToken,
prTitle: getOptionalInput("title"),
commitMessage: getOptionalInput("commit"),
hasPublishScript,
});

core.setOutput("pullRequestNumber", String(pullRequestNumber));

return;
}
})().catch((err) => {
Expand Down
12 changes: 12 additions & 0 deletions src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ describe("version", () => {
() => ({ data: { items: [] } })
);

mockedGithubMethods.pulls.create.mockImplementationOnce(
() => ({ data: { number: 123 } })
);

await writeChangesets(
[
{
Expand Down Expand Up @@ -92,6 +96,10 @@ describe("version", () => {
() => ({ data: { items: [] } })
);

mockedGithubMethods.pulls.create.mockImplementationOnce(
() => ({ data: { number: 123 } })
);

await writeChangesets(
[
{
Expand Down Expand Up @@ -123,6 +131,10 @@ describe("version", () => {
() => ({ data: { items: [] } })
);

mockedGithubMethods.pulls.create.mockImplementationOnce(
() => ({ data: { number: 123 } })
);

await writeChangesets(
[
{
Expand Down
20 changes: 17 additions & 3 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,18 @@ type VersionOptions = {
hasPublishScript?: boolean;
};

type RunVersionResult = {
pullRequestNumber: number;
};

export async function runVersion({
script,
githubToken,
cwd = process.cwd(),
prTitle = "Version Packages",
commitMessage = "Version Packages",
hasPublishScript = false,
}: VersionOptions) {
}: VersionOptions): Promise<RunVersionResult> {
let repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
let branch = github.context.ref.replace("refs/heads/", "");
let versionBranch = `changeset-release/${branch}`;
Expand Down Expand Up @@ -280,20 +284,30 @@ ${
console.log(JSON.stringify(searchResult.data, null, 2));
if (searchResult.data.items.length === 0) {
console.log("creating pull request");
await octokit.pulls.create({
const {
data: { number },
} = await octokit.pulls.create({
base: branch,
head: versionBranch,
title: finalPrTitle,
body: await prBodyPromise,
...github.context.repo,
});

return {
pullRequestNumber: number,
};
} else {
octokit.pulls.update({
await octokit.pulls.update({
pull_number: searchResult.data.items[0].number,
title: finalPrTitle,
body: await prBodyPromise,
...github.context.repo,
});
console.log("pull request found");

return {
pullRequestNumber: searchResult.data.items[0].number,
};
}
}

0 comments on commit 993a0a0

Please sign in to comment.