Skip to content

Commit e089ea2

Browse files
committed
chore: fix lint findings
1 parent 457f111 commit e089ea2

File tree

7 files changed

+55
-55
lines changed

7 files changed

+55
-55
lines changed

src/action.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ describe("Action", () => {
2525
workflow_timeout_seconds: "60",
2626
};
2727

28-
vi.spyOn(core, "getInput").mockImplementation((input: string) => {
28+
vi.spyOn(core, "getInput").mockImplementation((input: string): string => {
29+
/* eslint-disable @typescript-eslint/no-unsafe-return */
2930
switch (input) {
3031
case "token":
3132
return mockEnvConfig.token;
@@ -44,6 +45,7 @@ describe("Action", () => {
4445
default:
4546
throw new Error("invalid input requested");
4647
}
48+
/* eslint-enable @typescript-eslint/no-unsafe-return */
4749
});
4850
});
4951

src/action.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ export interface ActionConfig {
4242
workflowTimeoutSeconds: number;
4343
}
4444

45-
interface ActionWorkflowInputs {
46-
[input: string]: string | number | boolean;
47-
}
45+
type ActionWorkflowInputs = Record<string, string | number | boolean>;
4846

4947
export enum ActionOutputs {
5048
runId = "run_id",
@@ -91,32 +89,23 @@ function getWorkflowInputs(
9189
}
9290

9391
try {
94-
const parsedJson = JSON.parse(workflowInputs);
92+
const parsedJson = JSON.parse(workflowInputs) as Record<string, unknown>;
9593
for (const key of Object.keys(parsedJson)) {
9694
const value = parsedJson[key];
97-
const type = (() => {
98-
switch (true) {
99-
case value === null: {
100-
return "null";
101-
}
102-
case Array.isArray(value): {
103-
return "Array";
104-
}
105-
default:
106-
return typeof value;
107-
}
108-
})();
95+
const type =
96+
value === null ? "null" : Array.isArray(value) ? "Array" : typeof value;
97+
10998
if (!["string", "number", "boolean"].includes(type)) {
11099
throw new Error(
111100
`Expected value to be string, number, or boolean. "${key}" value is ${type}`,
112101
);
113102
}
114103
}
115-
return parsedJson;
104+
return parsedJson as ActionWorkflowInputs;
116105
} catch (error) {
117106
core.error("Failed to parse workflow_inputs JSON");
118107
if (error instanceof Error) {
119-
error.stack && core.debug(error.stack);
108+
core.debug(error.stack ?? "");
120109
}
121110
throw error;
122111
}
@@ -125,6 +114,7 @@ function getWorkflowInputs(
125114
function getWorkflowValue(workflowInput: string): string | number {
126115
try {
127116
// We can assume that the string is defined and not empty at this point.
117+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
128118
return getNumberFromValue(workflowInput)!;
129119
} catch {
130120
// Assume using a workflow name instead of an ID.

src/api.spec.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,32 @@ interface MockResponse {
2121
status: number;
2222
}
2323

24-
async function* mockPageIterator<T, P>(apiMethod: (params: P) => T, params: P) {
24+
function* mockPageIterator<T, P>(
25+
apiMethod: (params: P) => T,
26+
params: P,
27+
): Generator<T, void> {
2528
yield apiMethod(params);
2629
}
2730

2831
const mockOctokit = {
2932
rest: {
3033
actions: {
31-
createWorkflowDispatch: async (_req?: any): Promise<MockResponse> => {
34+
createWorkflowDispatch: (_req?: any): Promise<MockResponse> => {
3235
throw new Error("Should be mocked");
3336
},
34-
getWorkflowRun: async (_req?: any): Promise<MockResponse> => {
37+
getWorkflowRun: (_req?: any): Promise<MockResponse> => {
3538
throw new Error("Should be mocked");
3639
},
37-
listRepoWorkflows: async (_req?: any): Promise<MockResponse> => {
40+
listRepoWorkflows: (_req?: any): Promise<MockResponse> => {
3841
throw new Error("Should be mocked");
3942
},
40-
listWorkflowRuns: async (_req?: any): Promise<MockResponse> => {
43+
listWorkflowRuns: (_req?: any): Promise<MockResponse> => {
4144
throw new Error("Should be mocked");
4245
},
43-
downloadWorkflowRunLogs: async (_req?: any): Promise<MockResponse> => {
46+
downloadWorkflowRunLogs: (_req?: any): Promise<MockResponse> => {
4447
throw new Error("Should be mocked");
4548
},
46-
listJobsForWorkflowRun: async (_req?: any): Promise<MockResponse> => {
49+
listJobsForWorkflowRun: (_req?: any): Promise<MockResponse> => {
4750
throw new Error("Should be mocked");
4851
},
4952
},
@@ -75,6 +78,7 @@ describe("API", () => {
7578
return "";
7679
}
7780
});
81+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
7882
vi.spyOn(github, "getOctokit").mockReturnValue(mockOctokit as any);
7983
init();
8084
});
@@ -121,13 +125,13 @@ describe("API", () => {
121125
vi.spyOn(
122126
mockOctokit.rest.actions,
123127
"createWorkflowDispatch",
124-
).mockImplementation(async (req?: any) => {
128+
).mockImplementation((req?: any) => {
125129
dispatchedId = req.inputs.distinct_id;
126130

127-
return {
131+
return Promise.resolve({
128132
data: undefined,
129133
status: 204,
130-
};
134+
});
131135
});
132136

133137
await dispatchWorkflow(distinctId);
@@ -255,7 +259,7 @@ describe("API", () => {
255259
workflowIdCfg.ref = "/refs/heads/master";
256260
let parsedRef!: string;
257261
vi.spyOn(mockOctokit.rest.actions, "listWorkflowRuns").mockImplementation(
258-
async (req: any) => {
262+
(req: any) => {
259263
parsedRef = req.branch;
260264
const mockResponse: MockResponse = {
261265
data: {
@@ -264,7 +268,7 @@ describe("API", () => {
264268
},
265269
status: 200,
266270
};
267-
return mockResponse;
271+
return Promise.resolve(mockResponse);
268272
},
269273
);
270274

@@ -276,7 +280,7 @@ describe("API", () => {
276280
workflowIdCfg.ref = "/refs/tags/1.5.0";
277281
let parsedRef!: string;
278282
vi.spyOn(mockOctokit.rest.actions, "listWorkflowRuns").mockImplementation(
279-
async (req: any) => {
283+
(req: any) => {
280284
parsedRef = req.branch;
281285
const mockResponse: MockResponse = {
282286
data: {
@@ -285,7 +289,7 @@ describe("API", () => {
285289
},
286290
status: 200,
287291
};
288-
return mockResponse;
292+
return Promise.resolve(mockResponse);
289293
},
290294
);
291295

@@ -297,7 +301,7 @@ describe("API", () => {
297301
workflowIdCfg.ref = "/refs/cake";
298302
let parsedRef!: string;
299303
vi.spyOn(mockOctokit.rest.actions, "listWorkflowRuns").mockImplementation(
300-
async (req: any) => {
304+
(req: any) => {
301305
parsedRef = req.branch;
302306
const mockResponse: MockResponse = {
303307
data: {
@@ -306,7 +310,7 @@ describe("API", () => {
306310
},
307311
status: 200,
308312
};
309-
return mockResponse;
313+
return Promise.resolve(mockResponse);
310314
},
311315
);
312316

@@ -433,16 +437,13 @@ describe("API", () => {
433437
});
434438

435439
it("should return a populated array", async () => {
436-
const attempt = async () => {
437-
return [0];
438-
};
439-
440+
const attempt = () => Promise.resolve([0]);
440441
expect(await retryOrDie(attempt, 1000)).toHaveLength(1);
441442
});
442443

443444
it("should throw if the given timeout is exceeded", async () => {
444445
// Never return data.
445-
const attempt = async () => [];
446+
const attempt = () => Promise.resolve([]);
446447

447448
const retryOrDiePromise = retryOrDie(attempt, 1000);
448449
vi.advanceTimersByTime(2000);

src/api.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let config: ActionConfig;
99
let octokit: Octokit;
1010

1111
export function init(cfg?: ActionConfig): void {
12-
config = cfg || getConfig();
12+
config = cfg ?? getConfig();
1313
octokit = github.getOctokit(config.token);
1414
}
1515

@@ -27,6 +27,7 @@ export async function dispatchWorkflow(distinctId: string): Promise<void> {
2727
},
2828
});
2929

30+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
3031
if (response.status !== 204) {
3132
throw new Error(
3233
`Failed to dispatch action, expected 204 but received ${response.status}`,
@@ -48,7 +49,7 @@ export async function dispatchWorkflow(distinctId: string): Promise<void> {
4849
core.error(
4950
`dispatchWorkflow: An unexpected error has occurred: ${error.message}`,
5051
);
51-
error.stack && core.debug(error.stack);
52+
core.debug(error.stack ?? "");
5253
}
5354
throw error;
5455
}
@@ -72,6 +73,7 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
7273
let workflowId: number | undefined;
7374

7475
for await (const response of workflowIterator) {
76+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
7577
if (response.status !== 200) {
7678
throw new Error(
7779
`Failed to get workflows, expected 200 but received ${response.status}`,
@@ -99,7 +101,7 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
99101
core.error(
100102
`getWorkflowId: An unexpected error has occurred: ${error.message}`,
101103
);
102-
error.stack && core.debug(error.stack);
104+
core.debug(error.stack ?? "");
103105
}
104106
throw error;
105107
}
@@ -114,6 +116,7 @@ export async function getWorkflowRunUrl(runId: number): Promise<string> {
114116
run_id: runId,
115117
});
116118

119+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
117120
if (response.status !== 200) {
118121
throw new Error(
119122
`Failed to get Workflow Run state, expected 200 but received ${response.status}`,
@@ -133,7 +136,7 @@ export async function getWorkflowRunUrl(runId: number): Promise<string> {
133136
core.error(
134137
`getWorkflowRunUrl: An unexpected error has occurred: ${error.message}`,
135138
);
136-
error.stack && core.debug(error.stack);
139+
core.debug(error.stack ?? "");
137140
}
138141
throw error;
139142
}
@@ -158,6 +161,7 @@ export async function getWorkflowRunIds(workflowId: number): Promise<number[]> {
158161
}),
159162
});
160163

164+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
161165
if (response.status !== 200) {
162166
throw new Error(
163167
`Failed to get Workflow runs, expected 200 but received ${response.status}`,
@@ -171,9 +175,9 @@ export async function getWorkflowRunIds(workflowId: number): Promise<number[]> {
171175
core.debug(
172176
"Fetched Workflow Runs:\n" +
173177
` Repository: ${config.owner}/${config.repo}\n` +
174-
` Branch: ${branchName || "undefined"}\n` +
178+
` Branch: ${branchName}\n` +
175179
` Workflow ID: ${workflowId}\n` +
176-
` Runs Fetched: [${runIds}]`,
180+
` Runs Fetched: [${runIds.join(", ")}]`,
177181
);
178182

179183
return runIds;
@@ -182,7 +186,7 @@ export async function getWorkflowRunIds(workflowId: number): Promise<number[]> {
182186
core.error(
183187
`getWorkflowRunIds: An unexpected error has occurred: ${error.message}`,
184188
);
185-
error.stack && core.debug(error.stack);
189+
core.debug(error.stack ?? "");
186190
}
187191
throw error;
188192
}
@@ -198,6 +202,7 @@ export async function getWorkflowRunJobSteps(runId: number): Promise<string[]> {
198202
filter: "latest",
199203
});
200204

205+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
201206
if (response.status !== 200) {
202207
throw new Error(
203208
`Failed to get Workflow Run Jobs, expected 200 but received ${response.status}`,
@@ -206,16 +211,16 @@ export async function getWorkflowRunJobSteps(runId: number): Promise<string[]> {
206211

207212
const jobs = response.data.jobs.map((job) => ({
208213
id: job.id,
209-
steps: job.steps?.map((step) => step.name) || [],
214+
steps: job.steps?.map((step) => step.name) ?? [],
210215
}));
211216
const steps = Array.from(new Set(jobs.flatMap((job) => job.steps)));
212217

213218
core.debug(
214219
"Fetched Workflow Run Job Steps:\n" +
215220
` Repository: ${config.owner}/${config.repo}\n` +
216221
` Workflow Run ID: ${runId}\n` +
217-
` Jobs Fetched: [${jobs.map((job) => job.id)}]` +
218-
` Steps Fetched: [${steps}]`,
222+
` Jobs Fetched: [${jobs.map((job) => job.id).join(", ")}]` +
223+
` Steps Fetched: [${steps.join(", ")}]`,
219224
);
220225

221226
return steps;
@@ -224,7 +229,7 @@ export async function getWorkflowRunJobSteps(runId: number): Promise<string[]> {
224229
core.error(
225230
`getWorkflowRunJobs: An unexpected error has occurred: ${error.message}`,
226231
);
227-
error.stack && core.debug(error.stack);
232+
core.debug(error.stack ?? "");
228233
}
229234
throw error;
230235
}

src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function run(): Promise<void> {
4545
);
4646

4747
core.debug(
48-
`Attempting to get step names for Run IDs: [${workflowRunIds}]`,
48+
`Attempting to get step names for Run IDs: [${workflowRunIds.join(", ")}]`,
4949
);
5050

5151
const idRegex = new RegExp(DISTINCT_ID);
@@ -93,10 +93,10 @@ async function run(): Promise<void> {
9393
if (error instanceof Error) {
9494
core.error(`Failed to complete: ${error.message}`);
9595
core.warning("Does the token have the correct permissions?");
96-
error.stack && core.debug(error.stack);
96+
core.debug(error.stack ?? "");
9797
core.setFailed(error.message);
9898
}
9999
}
100100
}
101101

102-
(() => run())();
102+
((): Promise<void> => run())();

src/reset.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Do not add any other lines of code to this file!
2+
import "@total-typescript/ts-reset";

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as core from "@actions/core";
22

33
function getBranchNameFromRef(ref: string): string | undefined {
44
const refItems = ref.split(/\/?refs\/heads\//);
5-
if (refItems.length > 1 && refItems[1]!.length > 0) {
5+
if (refItems.length > 1 && (refItems[1]?.length ?? 0) > 0) {
66
return refItems[1];
77
}
88
}

0 commit comments

Comments
 (0)