From 38be40ff253933f7e2ba4f3aa989063e83cf5028 Mon Sep 17 00:00:00 2001 From: Dillon Nys <24740863+dnys1@users.noreply.github.com> Date: Wed, 20 Sep 2023 08:39:47 -0700 Subject: [PATCH] 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. --- actions/lib/bootstrap.mjs | 2 + actions/lib/src/node/actions/github.dart | 218 +++++++++++++++++++++++ actions/package.json | 1 + actions/pnpm-lock.yaml | 156 ++++++++++++++++ 4 files changed, 377 insertions(+) create mode 100644 actions/lib/src/node/actions/github.dart diff --git a/actions/lib/bootstrap.mjs b/actions/lib/bootstrap.mjs index 2551881c4a..8158de96a7 100644 --- a/actions/lib/bootstrap.mjs +++ b/actions/lib/bootstrap.mjs @@ -3,6 +3,7 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; +import * as github from '@actions/github'; import * as httpClient from '@actions/http-client'; import * as toolCache from '@actions/tool-cache'; import * as childProcess from 'node:child_process'; @@ -20,6 +21,7 @@ const require = createRequire(import.meta.url); globalThis.self = globalThis; globalThis.core = core; globalThis.exec = exec; +globalThis.github = github; globalThis.HttpClient = httpClient.HttpClient; globalThis.toolCache = toolCache; globalThis.fs = fs; diff --git a/actions/lib/src/node/actions/github.dart b/actions/lib/src/node/actions/github.dart new file mode 100644 index 0000000000..1bdaf1c27f --- /dev/null +++ b/actions/lib/src/node/actions/github.dart @@ -0,0 +1,218 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import 'dart:js_interop'; + +import 'package:actions/src/node/process.dart'; + +@JS() +external GitHub get github; + +@JS() +@anonymous +extension type GitHub._(JSObject it) { + /// The GitHub context this action is running in. + /// + /// See: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context + external GitHubContext get context; +} + +/// A typed representation of the `github` context object. +/// +/// See also: +/// - https://docs.github.com/en/actions/learn-github-actions/contexts#github-context +/// - https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables +@JS('github.Context') +extension type GitHubContext._(JSObject it) { + /// The webhook payload object that triggered this workflow. + external WebhookPayload get payload; + + /// The name of the event that triggered the workflow run. + /// + /// From the `GITHUB_EVENT_NAME` environment variable. + external String get eventName; + + /// The commit SHA that triggered the workflow. + /// + /// The value of this commit SHA depends on the event that triggered the workflow. + /// For more information, see [Events that trigger workflows](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows). + /// + /// For example, `ffac537e6cbbf934b08745a378932722df287a53`. + /// + /// From the `GITHUB_SHA` environment variable. + external String get sha; + + /// The fully-formed ref of the branch or tag that triggered the workflow run. + /// + /// For workflows triggered by push, this is the branch or tag ref that was pushed. + /// For workflows triggered by pull_request, this is the pull request merge branch. + /// For workflows triggered by release, this is the release tag created. For other + /// triggers, this is the branch or tag ref that triggered the workflow run. + /// + /// This is only set if a branch or tag is available for the event type. The ref given + /// is fully-formed, meaning that for branches the format is `refs/heads/`, + /// for pull requests it is `refs/pull//merge`, and for tags it is + /// `refs/tags/`. + /// + /// For example, `refs/heads/feature-branch-1`. + /// + /// From the `GITHUB_REF` environment variable. + external String get ref; + + /// The name of the workflow. If the workflow file doesn't specify a `name`, the value of + /// this property is the full path of the workflow file in the repository. + /// + /// From the `GITHUB_WORKFLOW` environment variable. + external String get workflow; + + /// The name of the action currently running, or the `id` of a step. + /// + /// GitHub removes special characters, and uses the name `__run` when the current step runs + /// a script without an `id`. If you use the same action more than once in the same job, + /// the name will include a suffix with the sequence number with underscore before it. + /// + /// For example, the first script you run will have the name `__run`, and the second script + /// will be named `__run_2`. Similarly, the second invocation of `actions/checkout` will be + /// `actionscheckout2`. + /// + /// From the `GITHUB_ACTION` environment variable. + external String get action; + + /// The username of the user that triggered the initial workflow run. + /// + /// If the workflow run is a re-run, this value may differ from `github.triggering_actor`. + /// Any workflow re-runs will use the privileges of `github.actor`, even if the actor initiating + /// the re-run (`github.triggering_actor`) has different privileges. + /// + /// From the `GITHUB_ACTOR` environment variable. + external String get actor; + + /// The `job_id` of the current job. + /// + /// **Note:** This context property is set by the Actions runner, and is only available within + /// the execution `steps` of a job. Otherwise, the value of this property will be `null`. + /// + /// From the `GITHUB_JOB` environment variable. + external String get job; + + /// A unique number for each run of a particular workflow in a repository. + /// + /// This number begins at 1 for the workflow's first run, and increments with each new run. This + /// number does not change if you re-run the workflow run. + /// + /// From the `GITHUB_RUN_NUMBER` environment variable. + external int get runNumber; + + /// A unique number for each workflow run within a repository. + /// + /// This number does not change if you re-run the workflow run. + /// + /// From the `GITHUB_RUN_ID` environment variable. + external int get runId; + + /// The URL of the GitHub REST API. + /// + /// From the `GITHUB_API_URL` environment variable. + external String get apiUrl; + + /// The URL of the GitHub server. + /// + /// For example: `https://github.com`. + /// + /// From the `GITHUB_SERVER_URL` environment variable. + external String get serverUrl; + + /// The URL of the GitHub GraphQL API. + /// + /// From the `GITHUB_GRAPHQL_URL` environment variable. + external String get graphqlUrl; + + /// The issue or pull request which triggered this action. + external GitHubIssue get issue; + + /// The repo from which this action was run. + external GitHubRepo get repo; + + /// The name of the base ref or target branch of the pull request in a workflow run. + /// + /// This is only set when the event that triggers a workflow run is either `pull_request` + /// or `pull_request_target`. + /// + /// For example, `main`. + /// + /// From the `GITHUB_BASE_REF` environment variable. + String? get baseRef => process.getEnv('GITHUB_BASE_REF'); + + /// The head ref or source branch of the pull request in a workflow run. + /// + /// This property is only set when the event that triggers a workflow run is either + /// `pull_request` or `pull_request_target`. + /// + /// For example, feature-branch-1. + /// + /// From the `GITHUB_HEAD_REF` environment variable. + String? get headRef => process.getEnv('GITHUB_HEAD_REF'); + + /// The short ref name of the branch or tag that triggered the workflow run. + /// + /// This value matches the branch or tag name shown on GitHub. + /// + /// For example, `feature-branch-1`. + /// + /// From the `GITHUB_REF_NAME` environment variable. + String get refName => process.getEnv('GITHUB_REF_NAME')!; + + /// The type of ref that triggered the workflow run. + /// + /// Valid values are `branch` or `tag`. + /// + /// From the `GITHUB_REF_TYPE` environment variable. + GitHubRefType get refType => GitHubRefType.values.byName( + process.getEnv('GITHUB_REF_TYPE')!, + ); + + /// A unique number for each attempt of a particular workflow run in a repository. + /// + /// This number begins at `1` for the workflow run's first attempt, and increments + /// with each re-run. + /// + /// For example, `3`. + /// + /// From the `GITHUB_RUN_ATTEMPT` environment variable. + int get runAttempt => int.parse(process.getEnv('GITHUB_RUN_ATTEMPT')!); + + /// The URL to this workflow's run. + String get workflowRunUrl => '$serverUrl/${repo.owner}/${repo.repo}/actions/runs/$runId'; +} + +enum GitHubRefType { branch, tag } + +@JS() +@anonymous +extension type GitHubRepo._(JSObject it) { + external String get owner; + external String get repo; +} + +@JS() +@anonymous +extension type GitHubIssue._(JSObject it) implements GitHubRepo { + external int get number; +} + +@JS() +@anonymous +extension type WebhookPayload._(JSObject it) implements JSObject { + @JS('pull_request') + external PullRequest? get pullRequest; +} + +@JS() +@anonymous +extension type PullRequest._(JSObject it) implements JSObject { + external int get number; + + @JS('html_url') + external String? get htmlUrl; + external String? get body; +} diff --git a/actions/package.json b/actions/package.json index 60ddc05b3c..4783821cc9 100644 --- a/actions/package.json +++ b/actions/package.json @@ -12,6 +12,7 @@ "dependencies": { "@actions/core": "^1.10.0", "@actions/exec": "^1.1.1", + "@actions/github": "^5.1.1", "@actions/http-client": "^2.1.1", "@actions/tool-cache": "^2.0.1" }, diff --git a/actions/pnpm-lock.yaml b/actions/pnpm-lock.yaml index 53d1c7a063..ab4f77eb8d 100644 --- a/actions/pnpm-lock.yaml +++ b/actions/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: '@actions/exec': specifier: ^1.1.1 version: 1.1.1 + '@actions/github': + specifier: ^5.1.1 + version: 5.1.1 '@actions/http-client': specifier: ^2.1.1 version: 2.1.1 @@ -38,6 +41,17 @@ packages: '@actions/io': 1.1.3 dev: false + /@actions/github@5.1.1: + resolution: {integrity: sha512-Nk59rMDoJaV+mHCOJPXuvB1zIbomlKS0dmSIqPGxd0enAXBnOfn4VWF+CGtRCwXZG9Epa54tZA7VIRlJDS8A6g==} + dependencies: + '@actions/http-client': 2.1.1 + '@octokit/core': 3.6.0 + '@octokit/plugin-paginate-rest': 2.21.3(@octokit/core@3.6.0) + '@octokit/plugin-rest-endpoint-methods': 5.16.2(@octokit/core@3.6.0) + transitivePeerDependencies: + - encoding + dev: false + /@actions/http-client@2.1.1: resolution: {integrity: sha512-qhrkRMB40bbbLo7gF+0vu+X+UawOvQQqNAA/5Unx774RS8poaOhThDOG6BGmxvAnxhQnDp2BG/ZUm65xZILTpw==} dependencies: @@ -59,21 +73,148 @@ packages: uuid: 3.4.0 dev: false + /@octokit/auth-token@2.5.0: + resolution: {integrity: sha512-r5FVUJCOLl19AxiuZD2VRZ/ORjp/4IN98Of6YJoJOkY75CIBuYfmiNHGrDwXr+aLGG55igl9QrxX3hbiXlLb+g==} + dependencies: + '@octokit/types': 6.41.0 + dev: false + + /@octokit/core@3.6.0: + resolution: {integrity: sha512-7RKRKuA4xTjMhY+eG3jthb3hlZCsOwg3rztWh75Xc+ShDWOfDDATWbeZpAHBNRpm4Tv9WgBMOy1zEJYXG6NJ7Q==} + dependencies: + '@octokit/auth-token': 2.5.0 + '@octokit/graphql': 4.8.0 + '@octokit/request': 5.6.3 + '@octokit/request-error': 2.1.0 + '@octokit/types': 6.41.0 + before-after-hook: 2.2.3 + universal-user-agent: 6.0.0 + transitivePeerDependencies: + - encoding + dev: false + + /@octokit/endpoint@6.0.12: + resolution: {integrity: sha512-lF3puPwkQWGfkMClXb4k/eUT/nZKQfxinRWJrdZaJO85Dqwo/G0yOC434Jr2ojwafWJMYqFGFa5ms4jJUgujdA==} + dependencies: + '@octokit/types': 6.41.0 + is-plain-object: 5.0.0 + universal-user-agent: 6.0.0 + dev: false + + /@octokit/graphql@4.8.0: + resolution: {integrity: sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==} + dependencies: + '@octokit/request': 5.6.3 + '@octokit/types': 6.41.0 + universal-user-agent: 6.0.0 + transitivePeerDependencies: + - encoding + dev: false + + /@octokit/openapi-types@12.11.0: + resolution: {integrity: sha512-VsXyi8peyRq9PqIz/tpqiL2w3w80OgVMwBHltTml3LmVvXiphgeqmY9mvBw9Wu7e0QWk/fqD37ux8yP5uVekyQ==} + dev: false + + /@octokit/plugin-paginate-rest@2.21.3(@octokit/core@3.6.0): + resolution: {integrity: sha512-aCZTEf0y2h3OLbrgKkrfFdjRL6eSOo8komneVQJnYecAxIej7Bafor2xhuDJOIFau4pk0i/P28/XgtbyPF0ZHw==} + peerDependencies: + '@octokit/core': '>=2' + dependencies: + '@octokit/core': 3.6.0 + '@octokit/types': 6.41.0 + dev: false + + /@octokit/plugin-rest-endpoint-methods@5.16.2(@octokit/core@3.6.0): + resolution: {integrity: sha512-8QFz29Fg5jDuTPXVtey05BLm7OB+M8fnvE64RNegzX7U+5NUXcOcnpTIK0YfSHBg8gYd0oxIq3IZTe9SfPZiRw==} + peerDependencies: + '@octokit/core': '>=3' + dependencies: + '@octokit/core': 3.6.0 + '@octokit/types': 6.41.0 + deprecation: 2.3.1 + dev: false + + /@octokit/request-error@2.1.0: + resolution: {integrity: sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==} + dependencies: + '@octokit/types': 6.41.0 + deprecation: 2.3.1 + once: 1.4.0 + dev: false + + /@octokit/request@5.6.3: + resolution: {integrity: sha512-bFJl0I1KVc9jYTe9tdGGpAMPy32dLBXXo1dS/YwSCTL/2nd9XeHsY616RE3HPXDVk+a+dBuzyz5YdlXwcDTr2A==} + dependencies: + '@octokit/endpoint': 6.0.12 + '@octokit/request-error': 2.1.0 + '@octokit/types': 6.41.0 + is-plain-object: 5.0.0 + node-fetch: 2.7.0 + universal-user-agent: 6.0.0 + transitivePeerDependencies: + - encoding + dev: false + + /@octokit/types@6.41.0: + resolution: {integrity: sha512-eJ2jbzjdijiL3B4PrSQaSjuF2sPEQPVCPzBvTHJD9Nz+9dw2SGH4K4xeQJ77YfTq5bRQ+bD8wT11JbeDPmxmGg==} + dependencies: + '@octokit/openapi-types': 12.11.0 + dev: false + /@vercel/ncc@0.36.1: resolution: {integrity: sha512-S4cL7Taa9yb5qbv+6wLgiKVZ03Qfkc4jGRuiUQMQ8HGBD5pcNRnHeYM33zBvJE4/zJGjJJ8GScB+WmTsn9mORw==} hasBin: true dev: true + /before-after-hook@2.2.3: + resolution: {integrity: sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==} + dev: false + + /deprecation@2.3.1: + resolution: {integrity: sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==} + dev: false + + /is-plain-object@5.0.0: + resolution: {integrity: sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==} + engines: {node: '>=0.10.0'} + dev: false + + /node-fetch@2.7.0: + resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==} + engines: {node: 4.x || >=6.0.0} + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + dependencies: + whatwg-url: 5.0.0 + dev: false + + /once@1.4.0: + resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==} + dependencies: + wrappy: 1.0.2 + dev: false + /semver@6.3.1: resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} hasBin: true dev: false + /tr46@0.0.3: + resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==} + dev: false + /tunnel@0.0.6: resolution: {integrity: sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==} engines: {node: '>=0.6.11 <=0.7.0 || >=0.7.3'} dev: false + /universal-user-agent@6.0.0: + resolution: {integrity: sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==} + dev: false + /uuid@3.4.0: resolution: {integrity: sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==} deprecated: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details. @@ -84,3 +225,18 @@ packages: resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==} hasBin: true dev: false + + /webidl-conversions@3.0.1: + resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} + dev: false + + /whatwg-url@5.0.0: + resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==} + dependencies: + tr46: 0.0.3 + webidl-conversions: 3.0.1 + dev: false + + /wrappy@1.0.2: + resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==} + dev: false