Skip to content

Commit 9c16c3b

Browse files
committed
Add common entry point
1 parent b009eac commit 9c16c3b

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

src/action-common.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
import { ActionsEnv } from "./actions-util";
1+
import * as core from "@actions/core";
2+
3+
import { ActionsEnv, getActionsEnv } from "./actions-util";
24
import { Env } from "./environment";
35
import { FeatureEnablement } from "./feature-flags";
4-
import { Logger } from "./logging";
6+
import { getActionsLogger, Logger } from "./logging";
7+
import { ActionName, sendUnhandledErrorStatusReport } from "./status-report";
8+
import { getEnv, getErrorMessage } from "./util";
9+
10+
/** Common state that is always available in `ActionState`. */
11+
export interface BaseState {
12+
/** The name of the Action. */
13+
name: ActionName;
14+
/** When the Action was started. */
15+
startedAt: Date;
16+
}
517

618
/** Describes different state features that an Action may have. */
719
export interface FeatureState {
@@ -27,10 +39,8 @@ export interface FeatureState {
2739
export type StateFeature = keyof FeatureState;
2840

2941
/** Constructs the union of all state types identifies by `Fs`. */
30-
export type FieldsOf<Fs extends readonly StateFeature[]> = Fs extends [
31-
infer Head extends StateFeature,
32-
]
33-
? FeatureState[Head]
42+
export type FieldsOf<Fs extends readonly StateFeature[]> = Fs extends []
43+
? BaseState
3444
: Fs extends [
3545
infer Head extends StateFeature,
3646
...infer Tail extends StateFeature[],
@@ -40,3 +50,37 @@ export type FieldsOf<Fs extends readonly StateFeature[]> = Fs extends [
4050

4151
/** Describes the state of an Action that has access to the state corresponding to `Fs`. */
4252
export type ActionState<Fs extends readonly StateFeature[]> = FieldsOf<Fs>;
53+
54+
/** The type of an Action's main entry point. */
55+
export type ActionMain = (
56+
state: ActionState<["Logger", "Env", "Actions"]>,
57+
) => Promise<void>;
58+
59+
/** A specification for a CodeQL Action step. */
60+
export interface Action {
61+
/** The name of the Action. */
62+
name: ActionName;
63+
/** The entry point for the Action. */
64+
run: ActionMain;
65+
}
66+
67+
/** A generic entry point that sets up the environment for the `action` and runs it. */
68+
export async function runInActions(action: Action) {
69+
const startedAt = new Date();
70+
const logger = getActionsLogger();
71+
const env = getEnv();
72+
const actionsEnv = getActionsEnv();
73+
74+
try {
75+
await action.run({
76+
name: action.name,
77+
startedAt,
78+
logger,
79+
env,
80+
actions: actionsEnv,
81+
});
82+
} catch (error) {
83+
core.setFailed(`${action.name} action failed: ${getErrorMessage(error)}`);
84+
await sendUnhandledErrorStatusReport(action.name, startedAt, error, logger);
85+
}
86+
}

src/testing-utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
} from "./feature-flags";
3434
import { Logger } from "./logging";
3535
import { OverlayDatabaseMode } from "./overlay/overlay-database-mode";
36+
import { ActionName } from "./status-report";
3637
import {
3738
DEFAULT_DEBUG_ARTIFACT_NAME,
3839
DEFAULT_DEBUG_DATABASE_NAME,
@@ -221,6 +222,8 @@ export class TestEnv<
221222
cloneFrom !== undefined
222223
? { ...cloneFrom.state }
223224
: {
225+
name: ActionName.Init,
226+
startedAt: new Date(),
224227
logger: new RecordingLogger(),
225228
env: getTestEnv(),
226229
actions: getActionsEnv(),
@@ -272,7 +275,7 @@ export class TestEnv<
272275
if (!this.args) {
273276
throw new Error("Trying to call function in TestEnv without arguments.");
274277
}
275-
return this.fn(this.state as ActionState<Fs>, ...this.args);
278+
return this.fn(this.state as unknown as ActionState<Fs>, ...this.args);
276279
}
277280

278281
public passes<T>(

0 commit comments

Comments
 (0)