Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
94 lines (89 loc) · 3.3 KB
/
Copy pathmain.ts
File metadata and controls
94 lines (89 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { debug, info, setFailed, setOutput } from '@actions/core';
import { env } from 'process';
import { ActionDeadline, DeadlineReached, handleDeadline } from './deadline';
import { OctokitGitHub, type GitHubRequestOptions } from './github';
import { parseInput, type Input } from './input';
import { Waiter, type Wait, type WaiterGitHubClient } from './wait';
import { findWorkflowId, type Workflow } from './workflow';
export interface ActionGitHubClient extends WaiterGitHubClient {
workflows(
owner: string,
repo: string,
requestOptions?: GitHubRequestOptions,
): Promise<Workflow[]>;
}
export type GitHubClientFactory = (githubToken: string, retries: number) => ActionGitHubClient;
export type WaiterFactory = (
workflowId: number,
github: WaiterGitHubClient,
input: Input,
deadline: ActionDeadline,
) => Wait;
export type DeadlineFactory = (input: Input) => ActionDeadline;
const createGitHubClient: GitHubClientFactory = (githubToken, retries) =>
new OctokitGitHub(githubToken, retries);
const createWaiter: WaiterFactory = (workflowId, github, input, deadline) =>
new Waiter(workflowId, github, input, info, debug, undefined, deadline);
const createDeadline: DeadlineFactory = (input) => ActionDeadline.fromInput(input);
const errorMessage = (error: unknown): string =>
error instanceof Error ? error.message : String(error);
export async function run(
environment: Record<string, string | undefined> = env,
githubFactory: GitHubClientFactory = createGitHubClient,
waiterFactory: WaiterFactory = createWaiter,
deadlineFactory: DeadlineFactory = createDeadline,
) {
let deadline: ActionDeadline | undefined;
try {
const input = parseInput(environment);
const actionDeadline = deadlineFactory(input);
deadline = actionDeadline;
debug(
`Parsed inputs (w/o token): ${(({ githubToken, ...inputs }) => JSON.stringify(inputs))(
input,
)}`,
);
actionDeadline.throwIfReached();
const github = githubFactory(input.githubToken, input.retries);
debug(`Fetching workflows for ${input.owner}/${input.repo}...`);
const workflows = await actionDeadline.race((signal) =>
github.workflows(input.owner, input.repo, {
signal,
checkDeadline: actionDeadline.throwIfReached,
}),
);
debug(`Found ${workflows.length} workflows in ${input.owner}/${input.repo}`);
actionDeadline.throwIfReached();
const workflowId = findWorkflowId(workflows, input);
if (workflowId !== undefined) {
const result = await waiterFactory(workflowId, github, input, actionDeadline).wait();
if (result === undefined) {
actionDeadline.throwIfReached();
}
} else {
actionDeadline.throwIfReached();
setFailed(
`No workflow found matching workflow path or name: ${input.workflowPath || input.workflowName}`,
);
}
} catch (error: unknown) {
if (error instanceof DeadlineReached) {
try {
handleDeadline(error, info, () => {
setOutput('previous_run_id', '');
setOutput('previous_run_url', '');
});
} catch (deadlineError: unknown) {
setFailed(errorMessage(deadlineError));
}
} else {
deadline?.cancel(error);
setFailed(errorMessage(error));
}
} finally {
deadline?.dispose();
}
}
if (require.main === module) {
run();
}