Repository navigation
Add ActionState type and make some initial use of it
#3990
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2399df7
Add `ActionsEnvVars` enum
mbg e190547
Allow abstracting over `process.env`
mbg 9b35ab9
Add initial `ActionState` type
mbg 26e9e9e
Add `TestEnv` class to simplify testing of `ActionState`-dependent fu…
mbg b009eac
Add `ActionsEnv` support to `ActionState`
mbg 9c16c3b
Add common entry point
mbg a3a1cdd
Use the common entry point for `upload-sarif`
mbg 6ab762c
Use the common entry point for `setup-codeql`
mbg 6136b93
Use the common entry point for `init`
mbg a9ceac1
Accept `ActionState` in `getConfigFileInput`
mbg fcd9f6c
Fix `logger` reference
mbg 772e8e9
Use the common entry point for `autobuild`
mbg ceac4af
Use the common entry point for `analyze`
mbg ef29bf2
Improve some comments
mbg eba9d55
Merge remote-tracking branch 'origin/main' into mbg/action-state
mbg 5fa9c0c
Add and use `getDisplayActionName` for log message
mbg e34d7fe
Fix: Use `getTestActionsEnv`
mbg d309c0e
Improve `FieldsOf`
mbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import * as core from "@actions/core"; | ||
|
|
||
| import { ActionsEnv, getActionsEnv } from "./actions-util"; | ||
| import { Env } from "./environment"; | ||
| import { FeatureEnablement } from "./feature-flags"; | ||
| import { getActionsLogger, Logger } from "./logging"; | ||
| import { ActionName, sendUnhandledErrorStatusReport } from "./status-report"; | ||
| import { getEnv, getErrorMessage } from "./util"; | ||
|
|
||
| /** Common state that is always available in `ActionState`. */ | ||
| export interface BaseState { | ||
| /** The name of the Action. */ | ||
| name: ActionName; | ||
| /** When the Action was started. */ | ||
| startedAt: Date; | ||
| } | ||
|
|
||
| /** Describes different state features that an Action may have. */ | ||
| export interface FeatureState { | ||
| Logger: { | ||
| /** The logger that is in use. */ | ||
| logger: Logger; | ||
| }; | ||
| Env: { | ||
| /** Information about environment variables. */ | ||
| env: Env; | ||
| }; | ||
| Actions: { | ||
| /** Access to Actions-related functionality. */ | ||
| actions: ActionsEnv; | ||
| }; | ||
| FeatureFlags: { | ||
| /** Information about enabled feature flags. */ | ||
| features: FeatureEnablement; | ||
| }; | ||
| } | ||
|
|
||
| /** Identifies a type of state an Action may have. */ | ||
| export type StateFeature = keyof FeatureState; | ||
|
|
||
| /** Constructs the union of all state types identifies by `Fs`. */ | ||
| export type FieldsOf<Fs extends readonly StateFeature[]> = Fs extends [] | ||
| ? BaseState | ||
| : Fs extends [ | ||
| infer Head extends StateFeature, | ||
| ...infer Tail extends StateFeature[], | ||
| ] | ||
| ? FeatureState[Head] & FieldsOf<Tail> | ||
| : never; | ||
|
|
||
| /** Describes the state of an Action that has access to the state corresponding to `Fs`. */ | ||
| export type ActionState<Fs extends readonly StateFeature[]> = FieldsOf<Fs>; | ||
|
|
||
| /** The type of an Action's main entry point. This is a function that is provided | ||
| * with a basic `ActionState` object with features that are always available. | ||
| * Each Action can then augment the `state` further if additional features are required. | ||
| */ | ||
| export type ActionMain = ( | ||
| state: ActionState<["Logger", "Env", "Actions"]>, | ||
| ) => Promise<void>; | ||
|
|
||
| /** A specification for a CodeQL Action step. */ | ||
| export interface Action { | ||
| /** The name of the Action. */ | ||
| name: ActionName; | ||
| /** The entry point for the Action. */ | ||
| run: ActionMain; | ||
| } | ||
|
|
||
| /** A generic entry point that sets up the basic environment for the `action` and runs it. */ | ||
| export async function runInActions(action: Action) { | ||
| const startedAt = new Date(); | ||
| const logger = getActionsLogger(); | ||
| const env = getEnv(); | ||
| const actionsEnv = getActionsEnv(); | ||
|
|
||
| try { | ||
| await action.run({ | ||
| name: action.name, | ||
| startedAt, | ||
| logger, | ||
| env, | ||
| actions: actionsEnv, | ||
| }); | ||
| } catch (error) { | ||
| core.setFailed(`${action.name} action failed: ${getErrorMessage(error)}`); | ||
|
mbg marked this conversation as resolved.
Outdated
|
||
| await sendUnhandledErrorStatusReport(action.name, startedAt, error, logger); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.