Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: inputs as class, graceful validation errors #50

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 85 additions & 50 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

50 changes: 32 additions & 18 deletions src/fetch-check-runs.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import * as github from "@actions/github";
import type { components } from "@octokit/openapi-types";
import { inputs } from "./inputs";
import { RelevantCheckRuns } from "./relevant-check-runs";
import { GitHub } from "@actions/github/lib/utils";

export type CheckRun = components["schemas"]["check-run"];

const octokit = github.getOctokit(inputs.token);
export class CheckRunFetcher {
private octokit: InstanceType<typeof GitHub> | null = null;

export const fetchCheckRuns = async (): Promise<RelevantCheckRuns> => {
const iterator = octokit.paginate.iterator(octokit.rest.checks.listForRef, {
...github.context.repo,
ref: inputs.ref,
per_page: 100,
});
constructor(
private token: string,
private ref: string,
private ownName: string,
private ignoredChecks: Set<string>,
) {}

let runs: CheckRun[] = [];
async fetch(): Promise<RelevantCheckRuns> {
this.octokit ??= github.getOctokit(this.token);

for await (const { data } of iterator) {
runs = runs.concat(data);
}
const iterator = this.octokit.paginate.iterator(
this.octokit.rest.checks.listForRef,
{
...github.context.repo,
ref: this.ref,
per_page: 100,
},
);

let runs: CheckRun[] = [];

return new RelevantCheckRuns(
runs.filter(
(run) => run.name !== inputs.name && !inputs.ignored.has(run.name),
),
);
};
for await (const { data } of iterator) {
runs = runs.concat(data);
}

return new RelevantCheckRuns(
runs.filter(
(run) => run.name !== this.ownName && !this.ignoredChecks.has(run.name),
),
);
}
}
45 changes: 32 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import * as core from "@actions/core";
import { delay } from "./delay";
import { fetchCheckRuns } from "./fetch-check-runs";
import { inputs } from "./inputs";
import { CheckRunFetcher } from "./fetch-check-runs";
import { Inputs } from "./inputs";
import { Display } from "./display";

const startTime = new Date();
const run = async (): Promise<void> => {
const startTime = new Date();
let inputs: Inputs;

const shouldTimeOut = (): boolean => {
const executionTime = Math.round(
(new Date().getTime() - startTime.getTime()) / 1000,
);
return executionTime > inputs.timeout;
};
try {
inputs = new Inputs();
} catch (error) {
if (error instanceof Error) {
core.setFailed(error);
return;
} else {
throw error;
}
}

const shouldTimeOut = (): boolean => {
const executionTime = Math.round(
(new Date().getTime() - startTime.getTime()) / 1000,
);
return executionTime > inputs.timeout;
};

Display.ignoredCheckNames(inputs.ignored);
Display.ignoredCheckNames(inputs.ignored);

const waitForCheckRuns = async (): Promise<void> => {
try {
const checkRunFetcher = new CheckRunFetcher(
inputs.token,
inputs.ref,
inputs.name,
inputs.ignored,
);

while (!shouldTimeOut()) {
Display.startingIteration();

const checkRuns = await fetchCheckRuns();
const checkRuns = await checkRunFetcher.fetch();

if (checkRuns.total() === 0) {
Display.delaying(inputs.interval);
Expand Down Expand Up @@ -57,4 +76,4 @@ const waitForCheckRuns = async (): Promise<void> => {
}
};

waitForCheckRuns();
run();
51 changes: 30 additions & 21 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ import * as core from "@actions/core";

const interval = Number(core.getInput("interval"));

if (!Number.isInteger(interval)) {
throw new Error("Invalid interval");
}
export class Inputs {
readonly token: string;
readonly name: string;
readonly interval: number;
readonly timeout: number;
readonly ref: string;
readonly ignored: Set<string>;

if (interval < 1) {
throw new Error("Interval must be greater than 0");
}
constructor() {
if (!Number.isInteger(interval)) {
throw new Error("Invalid interval");
}

const timeout = Number(core.getInput("timeout"));
if (interval < 1) {
throw new Error("Interval must be greater than 0");
}

if (!Number.isInteger(timeout)) {
throw new Error("Invalid timeout");
}
const timeout = Number(core.getInput("timeout"));

if (timeout < 1) {
throw new Error("Timeout must be greater than 0");
}
if (!Number.isInteger(timeout)) {
throw new Error("Invalid timeout");
}

if (timeout < 1) {
throw new Error("Timeout must be greater than 0");
}

export const inputs = {
token: core.getInput("token", { required: true }),
name: core.getInput("name"),
interval,
timeout,
ref: core.getInput("ref"),
ignored: new Set(core.getMultilineInput("ignored")),
} as const;
this.token = core.getInput("token", { required: true });
this.name = core.getInput("name");
this.interval = interval;
this.timeout = timeout;
this.ref = core.getInput("ref");
this.ignored = new Set(core.getMultilineInput("ignored"));
}
}
Loading