Skip to content

Commit 09ce719

Browse files
authored
Merge pull request #4009 from github/mbg/action-state/additions
Add `Api`, `ReadOnlyEnv`, and `Base` state features
2 parents e584241 + 4f688de commit 09ce719

10 files changed

Lines changed: 44 additions & 12 deletions

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"@actions/http-client": "^3.0.0",
3131
"@actions/io": "^2.0.0",
3232
"@actions/tool-cache": "^3.0.1",
33+
"@octokit/core": "^7.0.6",
34+
"@octokit/plugin-paginate-rest": "^14.0.0",
35+
"@octokit/plugin-rest-endpoint-methods": "^17.0.0",
3336
"@octokit/plugin-retry": "^8.1.0",
3437
"archiver": "^8.0.0",
3538
"fast-deep-equal": "^3.1.3",

src/action-common.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as core from "@actions/core";
22

33
import { ActionsEnv, getActionsEnv } from "./actions-util";
4-
import { Env } from "./environment";
5-
import { FeatureEnablement } from "./feature-flags";
4+
import type { ApiClient } from "./api-client";
5+
import { Env, ReadOnlyEnv } from "./environment";
6+
import type { FeatureEnablement } from "./feature-flags";
67
import { getActionsLogger, Logger } from "./logging";
78
import {
89
ActionName,
@@ -11,7 +12,7 @@ import {
1112
} from "./status-report";
1213
import { getEnv, getErrorMessage } from "./util";
1314

14-
/** Common state that is always available in `ActionState`. */
15+
/** Base state that is available to an Action on startup. */
1516
export interface BaseState {
1617
/** The name of the Action. */
1718
name: ActionName;
@@ -21,6 +22,7 @@ export interface BaseState {
2122

2223
/** Describes different state features that an Action may have. */
2324
export interface FeatureState {
25+
Base: BaseState;
2426
Logger: {
2527
/** The logger that is in use. */
2628
logger: Logger;
@@ -29,10 +31,17 @@ export interface FeatureState {
2931
/** Information about environment variables. */
3032
env: Env;
3133
};
34+
ReadOnlyEnv: {
35+
env: ReadOnlyEnv;
36+
};
3237
Actions: {
3338
/** Access to Actions-related functionality. */
3439
actions: ActionsEnv;
3540
};
41+
Api: {
42+
/** A GitHub API client. */
43+
apiClient: ApiClient;
44+
};
3645
FeatureFlags: {
3746
/** Information about enabled feature flags. */
3847
features: FeatureEnablement;
@@ -44,7 +53,7 @@ export type StateFeature = keyof FeatureState;
4453

4554
/** Constructs the intersection of all state types identifies by `Fs`. */
4655
export type FieldsOf<Fs extends readonly StateFeature[]> = Fs extends []
47-
? BaseState
56+
? Record<never, never>
4857
: Fs extends [
4958
infer Head extends StateFeature,
5059
...infer Tail extends readonly StateFeature[],
@@ -60,7 +69,7 @@ export type ActionState<Fs extends readonly StateFeature[]> = FieldsOf<Fs>;
6069
* Each Action can then augment the `state` further if additional features are required.
6170
*/
6271
export type ActionMain = (
63-
state: ActionState<["Logger", "Env", "Actions"]>,
72+
state: ActionState<["Base", "Logger", "Env", "Actions"]>,
6473
) => Promise<void>;
6574

6675
/** A specification for a CodeQL Action step. */

src/analyze-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ async function runAutobuildIfLegacyGoWorkflow(config: Config, logger: Logger) {
212212
await runAutobuild(config, BuiltInLanguage.go, logger);
213213
}
214214

215-
async function run({ startedAt, logger }: ActionState<["Logger"]>) {
215+
async function run({ startedAt, logger }: ActionState<["Base", "Logger"]>) {
216216
// To capture errors appropriately, keep as much code within the try-catch as
217217
// possible, and only use safe functions outside.
218218

src/api-client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as core from "@actions/core";
22
import * as githubUtils from "@actions/github/lib/utils";
3+
import { type Octokit } from "@octokit/core";
4+
import { type PaginateInterface } from "@octokit/plugin-paginate-rest";
5+
import { type Api } from "@octokit/plugin-rest-endpoint-methods";
36
import * as retry from "@octokit/plugin-retry";
47

58
import { getActionVersion, getRequiredInput } from "./actions-util";
@@ -43,10 +46,13 @@ export interface GitHubApiExternalRepoDetails {
4346
apiURL: string | undefined;
4447
}
4548

49+
/** The type of GitHub API client we use. */
50+
export type ApiClient = Octokit & Api & { paginate: PaginateInterface };
51+
4652
function createApiClientWithDetails(
4753
apiDetails: GitHubApiCombinedDetails,
4854
{ allowExternal = false } = {},
49-
) {
55+
): ApiClient {
5056
const auth =
5157
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
5258
const retryingOctokit = githubUtils.GitHub.plugin(retry.retry);

src/autobuild-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async function sendCompletedStatusReport(
6868
}
6969
}
7070

71-
async function run({ startedAt, logger }: ActionState<["Logger"]>) {
71+
async function run({ startedAt, logger }: ActionState<["Base", "Logger"]>) {
7272
// To capture errors appropriately, keep as much code within the try-catch as
7373
// possible, and only use safe functions outside.
7474

src/init-action.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ async function sendCompletedStatusReport(
203203
}
204204
}
205205

206-
async function run(actionState: ActionState<["Logger", "Env", "Actions"]>) {
206+
async function run(
207+
actionState: ActionState<["Base", "Logger", "Env", "Actions"]>,
208+
) {
207209
// To capture errors appropriately, keep as much code within the try-catch as
208210
// possible, and only use safe functions outside.
209211

src/setup-codeql-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async function sendCompletedStatusReport(
9090
async function run({
9191
startedAt,
9292
logger,
93-
}: ActionState<["Logger"]>): Promise<void> {
93+
}: ActionState<["Base", "Logger"]>): Promise<void> {
9494
// To capture errors appropriately, keep as much code within the try-catch as
9595
// possible, and only use safe functions outside.
9696

src/testing-utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,15 @@ export function getTestActionsEnv(): ActionsEnv {
193193
}
194194

195195
/** For testing purposes, we make all available state features accessible in `TestEnv`. */
196-
type AllState = ["Logger", "Env", "Actions", "FeatureFlags"];
196+
type AllState = [
197+
"Base",
198+
"Logger",
199+
"Env",
200+
"ReadOnlyEnv",
201+
"Actions",
202+
"Api",
203+
"FeatureFlags",
204+
];
197205

198206
/** Initialise a fresh `ActionState<AllState>` value. */
199207
export function initAllState(
@@ -205,6 +213,7 @@ export function initAllState(
205213
logger: new RecordingLogger(),
206214
env: getTestEnv(),
207215
actions: getTestActionsEnv(),
216+
apiClient: github.getOctokit("123"),
208217
features: createFeatures([]),
209218
...overrides,
210219
};

src/upload-sarif-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function sendSuccessStatusReport(
5454
}
5555
}
5656

57-
async function run({ startedAt, logger }: ActionState<["Logger"]>) {
57+
async function run({ startedAt, logger }: ActionState<["Base", "Logger"]>) {
5858
// To capture errors appropriately, keep as much code within the try-catch as
5959
// possible, and only use safe functions outside.
6060
try {

0 commit comments

Comments
 (0)