Skip to content

Commit

Permalink
Fix undefined steps for partial workflow (#5)
Browse files Browse the repository at this point in the history
Fix type error for `Error: Cannot read properties of undefined
(reading 'status')`. I believe it's from the `steps[key].status ==
Status.Failed` if the value is in the object but undefined. Removed the
class for an object that is allowed to be partial and added more `?.`
guards for field access.
  • Loading branch information
emcfarlane authored May 30, 2024
1 parent 65b95de commit fc0b591
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permissions:
contents: read
pull-requests: write
env:
BUF_VERSION: "1.32.1"
BUF_VERSION: "1.32.2"
jobs:
build:
runs-on: ubuntu-latest
Expand Down
35 changes: 10 additions & 25 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37788,7 +37788,6 @@ async function commentOnPR(context, github, comment) {

async function main() {
const inputs = getInputs();
core.debug(`inputs: ${JSON.stringify(inputs, null, 2)}`);
const github = (0,lib_github.getOctokit)(inputs.github_token);
const bufPath = await installBuf(github, inputs.version);
await login(bufPath, inputs);
Expand Down Expand Up @@ -37819,47 +37818,33 @@ async function main() {
// NB: Write empties the buffer must be after the comment.
await summary.write();
// Finally, set the status of the action.
for (const key of Object.keys(steps)) {
if (steps[key].status == Status.Failed) {
for (const [key, value] of Object.entries(steps)) {
if (value?.status == Status.Failed) {
core.setFailed(`Failed ${key}`);
}
}
}
main()
.catch((err) => core.setFailed(err.message))
.then(() => core.debug(`done in ${process.uptime()} s`));
class Steps {
build;
lint;
format;
breaking;
push;
archive;
}
// runWorkflow runs the buf workflow. It returns the results of each step.
// First, it builds the input. If the build fails, the workflow stops.
// Next, it runs lint, format, and breaking checks. If any of these fail, the workflow stops.
// Finally, it pushes or archives the label to the registry.
async function runWorkflow(bufPath, inputs) {
const steps = new Steps();
const steps = {};
steps.build = await build(bufPath, inputs);
if (steps.build.status == Status.Failed) {
return steps;
}
const checks = await Promise.all([
lint(bufPath, inputs).then((result) => {
steps.lint = result;
return result;
}),
format(bufPath, inputs).then((result) => {
steps.format = result;
return result;
}),
breaking(bufPath, inputs).then((result) => {
steps.breaking = result;
return result;
}),
lint(bufPath, inputs),
format(bufPath, inputs),
breaking(bufPath, inputs),
]);
steps.lint = checks[0];
steps.format = checks[1];
steps.breaking = checks[2];
if (checks.some((result) => result.status == Status.Failed)) {
return steps;
}
Expand Down Expand Up @@ -37908,7 +37893,7 @@ async function build(bufPath, inputs) {
async function lint(bufPath, inputs) {
if (!inputs.lint) {
core.info("Skipping lint");
return Promise.resolve(skip());
return skip();
}
const args = ["lint", "--error-format", "github-actions"];
if (inputs.input) {
Expand Down
44 changes: 20 additions & 24 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { commentOnPR } from "./comment";

async function main() {
const inputs = getInputs();
core.debug(`inputs: ${JSON.stringify(inputs, null, 2)}`);
const github = getOctokit(inputs.github_token);
const bufPath = await installBuf(github, inputs.version);
await login(bufPath, inputs);
Expand Down Expand Up @@ -55,8 +54,11 @@ async function main() {
// NB: Write empties the buffer must be after the comment.
await summary.write();
// Finally, set the status of the action.
for (const key of Object.keys(steps)) {
if (steps[key].status == Status.Failed) {
for (const [key, value] of Object.entries(steps) as [
keyof Steps,
Result | undefined,
][]) {
if (value?.status == Status.Failed) {
core.setFailed(`Failed ${key}`);
}
}
Expand All @@ -66,39 +68,33 @@ main()
.catch((err) => core.setFailed(err.message))
.then(() => core.debug(`done in ${process.uptime()} s`));

class Steps {
build: Result;
lint: Result;
format: Result;
breaking: Result;
push: Result;
archive: Result;
interface Steps {
build?: Result;
lint?: Result;
format?: Result;
breaking?: Result;
push?: Result;
archive?: Result;
}

// runWorkflow runs the buf workflow. It returns the results of each step.
// First, it builds the input. If the build fails, the workflow stops.
// Next, it runs lint, format, and breaking checks. If any of these fail, the workflow stops.
// Finally, it pushes or archives the label to the registry.
async function runWorkflow(bufPath: string, inputs: Inputs): Promise<Steps> {
const steps = new Steps();
const steps: Steps = {};
steps.build = await build(bufPath, inputs);
if (steps.build.status == Status.Failed) {
return steps;
}
const checks = await Promise.all([
lint(bufPath, inputs).then((result) => {
steps.lint = result;
return result;
}),
format(bufPath, inputs).then((result) => {
steps.format = result;
return result;
}),
breaking(bufPath, inputs).then((result) => {
steps.breaking = result;
return result;
}),
lint(bufPath, inputs),
format(bufPath, inputs),
breaking(bufPath, inputs),
]);
steps.lint = checks[0];
steps.format = checks[1];
steps.breaking = checks[2];
if (checks.some((result) => result.status == Status.Failed)) {
return steps;
}
Expand Down Expand Up @@ -154,7 +150,7 @@ async function build(bufPath: string, inputs: Inputs): Promise<Result> {
async function lint(bufPath: string, inputs: Inputs): Promise<Result> {
if (!inputs.lint) {
core.info("Skipping lint");
return Promise.resolve(skip());
return skip();
}
const args = ["lint", "--error-format", "github-actions"];
if (inputs.input) {
Expand Down

0 comments on commit fc0b591

Please sign in to comment.