diff --git a/.changeset/silent-tips-dance.md b/.changeset/silent-tips-dance.md new file mode 100644 index 0000000..24361f1 --- /dev/null +++ b/.changeset/silent-tips-dance.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": patch +--- + +Fix the `/version` subaction to not crash on missing `pr-base-branch` input. This input is meant to be optional. diff --git a/src/index.ts b/src/index.ts index 247e292..99e5e49 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,10 @@ import fs from "node:fs/promises"; -import path from "node:path"; import * as core from "@actions/core"; import { Git } from "./git.ts"; import { setupOctokit } from "./octokit.ts"; import readChangesetState from "./readChangesetState.ts"; import { runPublish, runVersion } from "./run.ts"; -import { fileExists } from "./utils.ts"; - -const getOptionalInput = (name: string) => core.getInput(name) || undefined; +import { fileExists, getOptionalInput } from "./utils.ts"; (async () => { // to maintain compatibility with workflows created before github-token input was introduced diff --git a/src/pack/index.ts b/src/pack/index.ts index e8c5b44..f52f5e8 100644 --- a/src/pack/index.ts +++ b/src/pack/index.ts @@ -3,7 +3,11 @@ import os from "node:os"; import path from "node:path"; import artifact from "@actions/artifact"; import * as core from "@actions/core"; -import { downloadArtifact, execChangesetsCli } from "../utils.ts"; +import { + downloadArtifact, + execChangesetsCli, + getOptionalInput, +} from "../utils.ts"; try { await main(); @@ -12,7 +16,7 @@ try { } async function main() { - const publishPlanArtifactId = core.getInput("publish-plan-artifact-id"); + const publishPlanArtifactId = getOptionalInput("publish-plan-artifact-id"); // If the user needs to change the cwd, set `working-directory` in the step instead const cwd = process.cwd(); diff --git a/src/pr-comment/index.ts b/src/pr-comment/index.ts index f280332..b54eb96 100644 --- a/src/pr-comment/index.ts +++ b/src/pr-comment/index.ts @@ -1,5 +1,6 @@ import * as core from "@actions/core"; import * as github from "@actions/github"; +import { getOptionalInput, getRequiredInput } from "../utils.ts"; type Octokit = ReturnType; type CreateCommentParams = NonNullable< @@ -23,9 +24,9 @@ async function main() { ); } - const githubToken = core.getInput("github-token", { required: true }); - const body = core.getInput("body", { required: true }); - const updateId = core.getInput("update-id"); + const githubToken = getRequiredInput("github-token"); + const body = getRequiredInput("body"); + const updateId = getOptionalInput("update-id"); const commentMarker = updateId ? getCommentMarker(updateId) : null; const commentBody = commentMarker ? `${commentMarker}\n\n${body}` : body; diff --git a/src/publish/index.ts b/src/publish/index.ts index 6ed399e..d9653c0 100644 --- a/src/publish/index.ts +++ b/src/publish/index.ts @@ -4,7 +4,11 @@ import * as core from "@actions/core"; import { Git } from "../git.ts"; import { setupOctokit } from "../octokit.ts"; import { runPublish } from "../run.ts"; -import { downloadArtifact } from "../utils.ts"; +import { + downloadArtifact, + getOptionalInput, + getRequiredInput, +} from "../utils.ts"; try { await main(); @@ -13,9 +17,9 @@ try { } async function main() { - const githubToken = core.getInput("github-token", { required: true }); - const script = core.getInput("script"); - const packDirArtifactId = core.getInput("pack-dir-artifact-id"); + const githubToken = getRequiredInput("github-token"); + const script = getOptionalInput("script"); + const packDirArtifactId = getOptionalInput("pack-dir-artifact-id"); const createGithubReleases = core.getBooleanInput("create-github-releases"); // If the user needs to change the cwd, set `working-directory` in the step instead diff --git a/src/utils.ts b/src/utils.ts index e21b237..205eb5c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,7 @@ import fs from "node:fs/promises"; import { createRequire } from "node:module"; import path from "node:path"; import artifact from "@actions/artifact"; +import * as core from "@actions/core"; import { exec, getExecOutput, @@ -123,6 +124,17 @@ export function fileExists(filePath: string) { ); } +export function getOptionalInput(name: string) { + // normalize empty string default return value of `core.getInput` to undefined + return core.getInput(name) || undefined; +} + +export function getRequiredInput(name: string) { + // it's just a small utility wrapper, mainly introduced for usage parity with our custom `getOptionalInput` + // note: `core.getBooleanInput` gets used directly as it already normalizes the return value nicely + return core.getInput(name, { required: true }); +} + function resolveChangesetsCli(cwd: string) { return require.resolve("@changesets/cli/bin.js", { paths: [cwd], diff --git a/src/version/index.ts b/src/version/index.ts index 1cf8f55..59f749c 100644 --- a/src/version/index.ts +++ b/src/version/index.ts @@ -2,6 +2,7 @@ import * as core from "@actions/core"; import { Git } from "../git.ts"; import { setupOctokit } from "../octokit.ts"; import { runVersion } from "../run.ts"; +import { getOptionalInput, getRequiredInput } from "../utils.ts"; try { await main(); @@ -10,13 +11,13 @@ try { } async function main() { - const githubToken = core.getInput("github-token", { required: true }); - const script = core.getInput("script"); - const commitMessage = core.getInput("commit-message", { required: true }); - const prTitle = core.getInput("pr-title", { required: true }); - const prDraft = core.getInput("pr-draft") || undefined; - const prBaseBranch = core.getInput("pr-base-branch"); - const commitMode = core.getInput("commit-mode") || "git-cli"; + const githubToken = getRequiredInput("github-token"); + const script = getOptionalInput("script"); + const commitMessage = getRequiredInput("commit-message"); + const prTitle = getRequiredInput("pr-title"); + const prDraft = getOptionalInput("pr-draft"); + const prBaseBranch = getOptionalInput("pr-base-branch"); + const commitMode = getOptionalInput("commit-mode") ?? "git-cli"; const setupGitUser = core.getBooleanInput("setup-git-user"); // Validations