Fix the /version subaction to not crash on missing pr-base-branch input (#666)

* use `getOptionalInput` consistently

* introduce `getRequiredInput`

* add changeset
This commit is contained in:
Mateusz Burzyński
2026-06-23 11:26:43 +00:00
committed by GitHub
parent ccb38113ea
commit dc29b738b5
7 changed files with 44 additions and 20 deletions
+5
View File
@@ -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.
+1 -4
View File
@@ -1,13 +1,10 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import path from "node:path";
import * as core from "@actions/core"; import * as core from "@actions/core";
import { Git } from "./git.ts"; import { Git } from "./git.ts";
import { setupOctokit } from "./octokit.ts"; import { setupOctokit } from "./octokit.ts";
import readChangesetState from "./readChangesetState.ts"; import readChangesetState from "./readChangesetState.ts";
import { runPublish, runVersion } from "./run.ts"; import { runPublish, runVersion } from "./run.ts";
import { fileExists } from "./utils.ts"; import { fileExists, getOptionalInput } from "./utils.ts";
const getOptionalInput = (name: string) => core.getInput(name) || undefined;
(async () => { (async () => {
// to maintain compatibility with workflows created before github-token input was introduced // to maintain compatibility with workflows created before github-token input was introduced
+6 -2
View File
@@ -3,7 +3,11 @@ import os from "node:os";
import path from "node:path"; import path from "node:path";
import artifact from "@actions/artifact"; import artifact from "@actions/artifact";
import * as core from "@actions/core"; import * as core from "@actions/core";
import { downloadArtifact, execChangesetsCli } from "../utils.ts"; import {
downloadArtifact,
execChangesetsCli,
getOptionalInput,
} from "../utils.ts";
try { try {
await main(); await main();
@@ -12,7 +16,7 @@ try {
} }
async function main() { 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 // If the user needs to change the cwd, set `working-directory` in the step instead
const cwd = process.cwd(); const cwd = process.cwd();
+4 -3
View File
@@ -1,5 +1,6 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import * as github from "@actions/github"; import * as github from "@actions/github";
import { getOptionalInput, getRequiredInput } from "../utils.ts";
type Octokit = ReturnType<typeof github.getOctokit>; type Octokit = ReturnType<typeof github.getOctokit>;
type CreateCommentParams = NonNullable< type CreateCommentParams = NonNullable<
@@ -23,9 +24,9 @@ async function main() {
); );
} }
const githubToken = core.getInput("github-token", { required: true }); const githubToken = getRequiredInput("github-token");
const body = core.getInput("body", { required: true }); const body = getRequiredInput("body");
const updateId = core.getInput("update-id"); const updateId = getOptionalInput("update-id");
const commentMarker = updateId ? getCommentMarker(updateId) : null; const commentMarker = updateId ? getCommentMarker(updateId) : null;
const commentBody = commentMarker ? `${commentMarker}\n\n${body}` : body; const commentBody = commentMarker ? `${commentMarker}\n\n${body}` : body;
+8 -4
View File
@@ -4,7 +4,11 @@ import * as core from "@actions/core";
import { Git } from "../git.ts"; import { Git } from "../git.ts";
import { setupOctokit } from "../octokit.ts"; import { setupOctokit } from "../octokit.ts";
import { runPublish } from "../run.ts"; import { runPublish } from "../run.ts";
import { downloadArtifact } from "../utils.ts"; import {
downloadArtifact,
getOptionalInput,
getRequiredInput,
} from "../utils.ts";
try { try {
await main(); await main();
@@ -13,9 +17,9 @@ try {
} }
async function main() { async function main() {
const githubToken = core.getInput("github-token", { required: true }); const githubToken = getRequiredInput("github-token");
const script = core.getInput("script"); const script = getOptionalInput("script");
const packDirArtifactId = core.getInput("pack-dir-artifact-id"); const packDirArtifactId = getOptionalInput("pack-dir-artifact-id");
const createGithubReleases = core.getBooleanInput("create-github-releases"); const createGithubReleases = core.getBooleanInput("create-github-releases");
// If the user needs to change the cwd, set `working-directory` in the step instead // If the user needs to change the cwd, set `working-directory` in the step instead
+12
View File
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import { createRequire } from "node:module"; import { createRequire } from "node:module";
import path from "node:path"; import path from "node:path";
import artifact from "@actions/artifact"; import artifact from "@actions/artifact";
import * as core from "@actions/core";
import { import {
exec, exec,
getExecOutput, 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) { function resolveChangesetsCli(cwd: string) {
return require.resolve("@changesets/cli/bin.js", { return require.resolve("@changesets/cli/bin.js", {
paths: [cwd], paths: [cwd],
+8 -7
View File
@@ -2,6 +2,7 @@ import * as core from "@actions/core";
import { Git } from "../git.ts"; import { Git } from "../git.ts";
import { setupOctokit } from "../octokit.ts"; import { setupOctokit } from "../octokit.ts";
import { runVersion } from "../run.ts"; import { runVersion } from "../run.ts";
import { getOptionalInput, getRequiredInput } from "../utils.ts";
try { try {
await main(); await main();
@@ -10,13 +11,13 @@ try {
} }
async function main() { async function main() {
const githubToken = core.getInput("github-token", { required: true }); const githubToken = getRequiredInput("github-token");
const script = core.getInput("script"); const script = getOptionalInput("script");
const commitMessage = core.getInput("commit-message", { required: true }); const commitMessage = getRequiredInput("commit-message");
const prTitle = core.getInput("pr-title", { required: true }); const prTitle = getRequiredInput("pr-title");
const prDraft = core.getInput("pr-draft") || undefined; const prDraft = getOptionalInput("pr-draft");
const prBaseBranch = core.getInput("pr-base-branch"); const prBaseBranch = getOptionalInput("pr-base-branch");
const commitMode = core.getInput("commit-mode") || "git-cli"; const commitMode = getOptionalInput("commit-mode") ?? "git-cli";
const setupGitUser = core.getBooleanInput("setup-git-user"); const setupGitUser = core.getBooleanInput("setup-git-user");
// Validations // Validations