Skip to content

Commit 38be40f

Browse files
authored
feat(actions): Add GitHub interop (#3767)
Adds interop code for the `@actions/github` package which contains helpers for getting the context of the current action.
1 parent a109d3c commit 38be40f

File tree

4 files changed

+377
-0
lines changed

4 files changed

+377
-0
lines changed

actions/lib/bootstrap.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import * as core from '@actions/core';
55
import * as exec from '@actions/exec';
6+
import * as github from '@actions/github';
67
import * as httpClient from '@actions/http-client';
78
import * as toolCache from '@actions/tool-cache';
89
import * as childProcess from 'node:child_process';
@@ -20,6 +21,7 @@ const require = createRequire(import.meta.url);
2021
globalThis.self = globalThis;
2122
globalThis.core = core;
2223
globalThis.exec = exec;
24+
globalThis.github = github;
2325
globalThis.HttpClient = httpClient.HttpClient;
2426
globalThis.toolCache = toolCache;
2527
globalThis.fs = fs;
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'dart:js_interop';
5+
6+
import 'package:actions/src/node/process.dart';
7+
8+
@JS()
9+
external GitHub get github;
10+
11+
@JS()
12+
@anonymous
13+
extension type GitHub._(JSObject it) {
14+
/// The GitHub context this action is running in.
15+
///
16+
/// See: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
17+
external GitHubContext get context;
18+
}
19+
20+
/// A typed representation of the `github` context object.
21+
///
22+
/// See also:
23+
/// - https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
24+
/// - https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
25+
@JS('github.Context')
26+
extension type GitHubContext._(JSObject it) {
27+
/// The webhook payload object that triggered this workflow.
28+
external WebhookPayload get payload;
29+
30+
/// The name of the event that triggered the workflow run.
31+
///
32+
/// From the `GITHUB_EVENT_NAME` environment variable.
33+
external String get eventName;
34+
35+
/// The commit SHA that triggered the workflow.
36+
///
37+
/// The value of this commit SHA depends on the event that triggered the workflow.
38+
/// For more information, see [Events that trigger workflows](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows).
39+
///
40+
/// For example, `ffac537e6cbbf934b08745a378932722df287a53`.
41+
///
42+
/// From the `GITHUB_SHA` environment variable.
43+
external String get sha;
44+
45+
/// The fully-formed ref of the branch or tag that triggered the workflow run.
46+
///
47+
/// For workflows triggered by push, this is the branch or tag ref that was pushed.
48+
/// For workflows triggered by pull_request, this is the pull request merge branch.
49+
/// For workflows triggered by release, this is the release tag created. For other
50+
/// triggers, this is the branch or tag ref that triggered the workflow run.
51+
///
52+
/// This is only set if a branch or tag is available for the event type. The ref given
53+
/// is fully-formed, meaning that for branches the format is `refs/heads/<branch_name>`,
54+
/// for pull requests it is `refs/pull/<pr_number>/merge`, and for tags it is
55+
/// `refs/tags/<tag_name>`.
56+
///
57+
/// For example, `refs/heads/feature-branch-1`.
58+
///
59+
/// From the `GITHUB_REF` environment variable.
60+
external String get ref;
61+
62+
/// The name of the workflow. If the workflow file doesn't specify a `name`, the value of
63+
/// this property is the full path of the workflow file in the repository.
64+
///
65+
/// From the `GITHUB_WORKFLOW` environment variable.
66+
external String get workflow;
67+
68+
/// The name of the action currently running, or the `id` of a step.
69+
///
70+
/// GitHub removes special characters, and uses the name `__run` when the current step runs
71+
/// a script without an `id`. If you use the same action more than once in the same job,
72+
/// the name will include a suffix with the sequence number with underscore before it.
73+
///
74+
/// For example, the first script you run will have the name `__run`, and the second script
75+
/// will be named `__run_2`. Similarly, the second invocation of `actions/checkout` will be
76+
/// `actionscheckout2`.
77+
///
78+
/// From the `GITHUB_ACTION` environment variable.
79+
external String get action;
80+
81+
/// The username of the user that triggered the initial workflow run.
82+
///
83+
/// If the workflow run is a re-run, this value may differ from `github.triggering_actor`.
84+
/// Any workflow re-runs will use the privileges of `github.actor`, even if the actor initiating
85+
/// the re-run (`github.triggering_actor`) has different privileges.
86+
///
87+
/// From the `GITHUB_ACTOR` environment variable.
88+
external String get actor;
89+
90+
/// The `job_id` of the current job.
91+
///
92+
/// **Note:** This context property is set by the Actions runner, and is only available within
93+
/// the execution `steps` of a job. Otherwise, the value of this property will be `null`.
94+
///
95+
/// From the `GITHUB_JOB` environment variable.
96+
external String get job;
97+
98+
/// A unique number for each run of a particular workflow in a repository.
99+
///
100+
/// This number begins at 1 for the workflow's first run, and increments with each new run. This
101+
/// number does not change if you re-run the workflow run.
102+
///
103+
/// From the `GITHUB_RUN_NUMBER` environment variable.
104+
external int get runNumber;
105+
106+
/// A unique number for each workflow run within a repository.
107+
///
108+
/// This number does not change if you re-run the workflow run.
109+
///
110+
/// From the `GITHUB_RUN_ID` environment variable.
111+
external int get runId;
112+
113+
/// The URL of the GitHub REST API.
114+
///
115+
/// From the `GITHUB_API_URL` environment variable.
116+
external String get apiUrl;
117+
118+
/// The URL of the GitHub server.
119+
///
120+
/// For example: `https://github.com`.
121+
///
122+
/// From the `GITHUB_SERVER_URL` environment variable.
123+
external String get serverUrl;
124+
125+
/// The URL of the GitHub GraphQL API.
126+
///
127+
/// From the `GITHUB_GRAPHQL_URL` environment variable.
128+
external String get graphqlUrl;
129+
130+
/// The issue or pull request which triggered this action.
131+
external GitHubIssue get issue;
132+
133+
/// The repo from which this action was run.
134+
external GitHubRepo get repo;
135+
136+
/// The name of the base ref or target branch of the pull request in a workflow run.
137+
///
138+
/// This is only set when the event that triggers a workflow run is either `pull_request`
139+
/// or `pull_request_target`.
140+
///
141+
/// For example, `main`.
142+
///
143+
/// From the `GITHUB_BASE_REF` environment variable.
144+
String? get baseRef => process.getEnv('GITHUB_BASE_REF');
145+
146+
/// The head ref or source branch of the pull request in a workflow run.
147+
///
148+
/// This property is only set when the event that triggers a workflow run is either
149+
/// `pull_request` or `pull_request_target`.
150+
///
151+
/// For example, feature-branch-1.
152+
///
153+
/// From the `GITHUB_HEAD_REF` environment variable.
154+
String? get headRef => process.getEnv('GITHUB_HEAD_REF');
155+
156+
/// The short ref name of the branch or tag that triggered the workflow run.
157+
///
158+
/// This value matches the branch or tag name shown on GitHub.
159+
///
160+
/// For example, `feature-branch-1`.
161+
///
162+
/// From the `GITHUB_REF_NAME` environment variable.
163+
String get refName => process.getEnv('GITHUB_REF_NAME')!;
164+
165+
/// The type of ref that triggered the workflow run.
166+
///
167+
/// Valid values are `branch` or `tag`.
168+
///
169+
/// From the `GITHUB_REF_TYPE` environment variable.
170+
GitHubRefType get refType => GitHubRefType.values.byName(
171+
process.getEnv('GITHUB_REF_TYPE')!,
172+
);
173+
174+
/// A unique number for each attempt of a particular workflow run in a repository.
175+
///
176+
/// This number begins at `1` for the workflow run's first attempt, and increments
177+
/// with each re-run.
178+
///
179+
/// For example, `3`.
180+
///
181+
/// From the `GITHUB_RUN_ATTEMPT` environment variable.
182+
int get runAttempt => int.parse(process.getEnv('GITHUB_RUN_ATTEMPT')!);
183+
184+
/// The URL to this workflow's run.
185+
String get workflowRunUrl => '$serverUrl/${repo.owner}/${repo.repo}/actions/runs/$runId';
186+
}
187+
188+
enum GitHubRefType { branch, tag }
189+
190+
@JS()
191+
@anonymous
192+
extension type GitHubRepo._(JSObject it) {
193+
external String get owner;
194+
external String get repo;
195+
}
196+
197+
@JS()
198+
@anonymous
199+
extension type GitHubIssue._(JSObject it) implements GitHubRepo {
200+
external int get number;
201+
}
202+
203+
@JS()
204+
@anonymous
205+
extension type WebhookPayload._(JSObject it) implements JSObject {
206+
@JS('pull_request')
207+
external PullRequest? get pullRequest;
208+
}
209+
210+
@JS()
211+
@anonymous
212+
extension type PullRequest._(JSObject it) implements JSObject {
213+
external int get number;
214+
215+
@JS('html_url')
216+
external String? get htmlUrl;
217+
external String? get body;
218+
}

actions/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@actions/core": "^1.10.0",
1414
"@actions/exec": "^1.1.1",
15+
"@actions/github": "^5.1.1",
1516
"@actions/http-client": "^2.1.1",
1617
"@actions/tool-cache": "^2.0.1"
1718
},

0 commit comments

Comments
 (0)