Skip to content

Commit

Permalink
Skip creating a PR when all existing changesets are empty (changesets…
Browse files Browse the repository at this point in the history
…#206)

* Don't create a PR if all changesets are empty

Fixes changesets#205.

Note that this maintains the previous behavior that if there are any
changesets (even empty ones), the action does not publish packages.

* Create three-needles-protect.md

* Update .changeset/three-needles-protect.md

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
2 people authored and cometkim committed Apr 11, 2023
1 parent e7f1403 commit 9a81bfd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
25 changes: 18 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,29 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
let dedupe = core.getBooleanInput("dedupe");
let hasChangesets = changesets.length !== 0;

const hasNonEmptyChangesets = changesets.some(
(changeset) => changeset.releases.length > 0
);

core.setOutput("published", "false");
core.setOutput("publishedPackages", "[]");
core.setOutput("hasChangesets", String(hasChangesets));

if (hasChangesets) {
await runVersion({
githubToken,
prTitle: getOptionalInput("title"),
commitMessage: getOptionalInput("commit"),
autoPublish,
dedupe,
});
if (hasNonEmptyChangesets) {
const { pullRequestNumber } = await runVersion({
githubToken,
prTitle: getOptionalInput("title"),
commitMessage: getOptionalInput("commit"),
autoPublish,
dedupe,
});
core.setOutput("pullRequestNumber", String(pullRequestNumber));
return;
} else {
console.log("All changesets are empty; not creating PR");
return;
}
} else {
console.log("No changesets found");

Expand Down
24 changes: 20 additions & 4 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,17 @@ type VersionOptions = {
dedupe?: boolean;
};

type RunVersionResult = {
pullRequestNumber: number;
};

export async function runVersion({
githubToken,
prTitle = "Version Packages",
commitMessage = "Version Packages",
autoPublish = false,
dedupe = false,
}: VersionOptions) {
}: VersionOptions): Promise<RunVersionResult> {
let cwd = process.cwd();

let repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
Expand Down Expand Up @@ -270,20 +274,32 @@ ${
console.log(JSON.stringify(searchResult.data, null, 2));
if (searchResult.data.items.length === 0) {
console.log("creating pull request");
await octokit.rest.pulls.create({

const { data: pullRequest } = await octokit.rest.pulls.create({
base: branch,
head: versionBranch,
title: finalPrTitle,
body: await prBodyPromise,
...github.context.repo,
});

return {
pullRequestNumber: pullRequest.number,
};

} else {
octokit.rest.pulls.update({
const [pullRequest] = searchResult.data.items;
console.log(`updating found pull request #${pullRequest.number}`);

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

return {
pullRequestNumber: pullRequest.number,
};
}
}

0 comments on commit 9a81bfd

Please sign in to comment.