1- import { ActionsEnv } from "./actions-util" ;
1+ import * as core from "@actions/core" ;
2+
3+ import { ActionsEnv , getActionsEnv } from "./actions-util" ;
24import { Env } from "./environment" ;
35import { 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. */
719export interface FeatureState {
@@ -27,10 +39,8 @@ export interface FeatureState {
2739export 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`. */
4252export 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+ }
0 commit comments