Skip to content

Commit 835ec54

Browse files
committed
Expose async-function argument type
We are exposing the async-function argument type for jsDoc type declaration support. This means that we now could do: "npm i -D @types/github-script@github:actions/github-script" and the add: "@param {import('@types/github-script').AsyncFunctionArguments} AsyncFunctionArguments". This could obviously be done in other ways too, like using "@typed-actions/github-script" instead. But it seems better to use the actual source repository instead of a third-party library to import the type declaration.
1 parent 6f00a0b commit 835ec54

File tree

5 files changed

+197
-2
lines changed

5 files changed

+197
-2
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,26 @@ jobs:
419419
await printStuff()
420420
```
421421

422+
### Use scripts with jsDoc support
423+
424+
If you want type support for your scripts, you could use the the command below to install the
425+
`github-script` type declaration.
426+
```sh
427+
$ npm i -D @types/github-script@github:actions/github-script
428+
```
429+
430+
And then add the `jsDoc` declaration to you script like this:
431+
```js
432+
// @ts-check
433+
/** @param {import('@types/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
434+
export default async ({ core, context }) => {
435+
core.debug("Running something at the moment");
436+
return context.actor;
437+
};
438+
```
439+
For an alternative setup, please read (alternative-setup)[./docs/alternative-setup.md].
440+
441+
422442
### Use env as input
423443

424444
You can set env vars to use them in your script:

docs/alternative-setup.md

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
## Alternative setup
2+
3+
### Example repository structure
4+
In this example we're using the repo structure below, but you are free
5+
to structure it how ever you like.
6+
```
7+
root # Your repository
8+
├── .github
9+
│ ├── ...
10+
│ └── workflows
11+
│ ├── ...
12+
│ └── ci-workflow.yml
13+
├── ...
14+
├── actions
15+
│ ├── action.yml (optional)
16+
│ └── ci-test.js
17+
├── ...
18+
└── package.json
19+
```
20+
21+
### 1. Install the github-script type
22+
```sh
23+
$ npm i -D @types/github-script@github:actions/github-script
24+
```
25+
26+
27+
### 2. Create `ci-test.js` file
28+
29+
```js
30+
// @ts-check
31+
/** @param {import('@types/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
32+
export default async ({ core, context }) => {
33+
core.debug("Running something at the moment");
34+
return context.actor;
35+
};
36+
```
37+
38+
### 3. Create `ci-workflow.yml` file
39+
```yml
40+
on: push
41+
42+
permissions:
43+
pull-requests: read
44+
contents: read
45+
46+
jobs:
47+
example:
48+
runs-on: ubuntu-latest
49+
steps:
50+
- uses: actions/checkout@v3
51+
- uses: actions/setup-node@v3
52+
with:
53+
node-version: 16
54+
55+
- run: npm ci
56+
- uses: actions/github-script@v6
57+
with:
58+
github-token: ${{ secrets.GITHUB_TOKEN }}
59+
result-encoding: string
60+
script: |
61+
const { default: script } = await import('${{ github.workspace }}/actions/ci-test.js');
62+
return await script({ github, context, core, exec, glob, io, fetch, __original_require__ });
63+
```
64+
65+
66+
## Cleaner setup (Optional)
67+
68+
Note that the `ci-workflow.yml` example above can be kind of tedious once you add more of them. So
69+
to address this, one could instead use `composite` actions.
70+
71+
### The `action.yml` file
72+
```yml
73+
name: Typed GitHub Script
74+
author: GitHub
75+
description: Run simple scripts using the GitHub client
76+
branding:
77+
color: blue
78+
icon: code
79+
inputs:
80+
script:
81+
description: The path to script (e.g actions/ci-test.js)
82+
required: true
83+
github-token:
84+
description: The GitHub token used to create an authenticated client
85+
default: ${{ github.token }}
86+
required: false
87+
debug:
88+
description: Whether to tell the GitHub client to log details of its requests. true or false. Default is to run in debug mode when the GitHub Actions step debug logging is turned on.
89+
default: ${{ runner.debug == '1' }}
90+
user-agent:
91+
description: An optional user-agent string
92+
default: actions/github-script
93+
previews:
94+
description: A comma-separated list of API previews to accept
95+
result-encoding:
96+
description: Either "string" or "json" (default "json")—how the result will be encoded
97+
default: json
98+
retries:
99+
description: The number of times to retry a request
100+
default: "0"
101+
retry-exempt-status-codes:
102+
description: A comma separated list of status codes that will NOT be retried e.g. "400,500". No effect unless `retries` is set
103+
default: 400,401,403,404,422 # from https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14
104+
105+
outputs:
106+
result:
107+
description: The return value of the script, stringified with `JSON.stringify`
108+
value: ${{ steps.github-script-result.outputs.result }}
109+
110+
runs:
111+
using: "composite"
112+
steps:
113+
- uses: actions/github-script@v6
114+
id: github-script-result
115+
with:
116+
github-token: ${{ inputs.github-token }}
117+
result-encoding: ${{ inputs.result-encoding }}
118+
debug: ${{ inputs.debug }}
119+
user-agent: ${{ inputs.script }}
120+
previews: ${{ inputs.previews }}
121+
retries: ${{ inputs.retries }}
122+
retry-exempt-status-codes: ${{ inputs.retry-exempt-status-codes }}
123+
script: |
124+
const { default: script } = await import('${{ github.workspace }}/${{ inputs.script }}');
125+
return await script({ github, context, core, exec, glob, io, fetch, __original_require__ });
126+
```
127+
128+
129+
### The `ci-workflow.yml` file
130+
```yml
131+
on: push
132+
133+
permissions:
134+
pull-requests: read
135+
contents: read
136+
137+
jobs:
138+
example:
139+
runs-on: ubuntu-latest
140+
steps:
141+
- uses: actions/checkout@v3
142+
- uses: actions/setup-node@v3
143+
with:
144+
node-version: 16
145+
146+
- run: npm ci
147+
- uses: ./actions
148+
with:
149+
github-token: ${{ secrets.GITHUB_TOKEN }}
150+
result-encoding: string
151+
script: actions/ci-test.js
152+
```
153+

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"author": "GitHub",
66
"license": "MIT",
77
"main": "dist/index.js",
8+
"types": "types/async-function.d.ts",
89
"private": true,
910
"scripts": {
10-
"build": "ncc build src/main.ts",
11+
"build": "npm run build:types && ncc build src/main.ts",
12+
"build:types": "tsc src/async-function.ts -t es5 --declaration --allowJs --emitDeclarationOnly --outDir types",
1113
"format:check": "prettier --check src __test__",
1214
"format:write": "prettier --write src __test__",
1315
"lint": "eslint src __test__",

src/async-function.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import fetch from 'node-fetch'
88

99
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor
1010

11-
type AsyncFunctionArguments = {
11+
export declare type AsyncFunctionArguments = {
1212
context: Context
1313
core: typeof core
1414
github: InstanceType<typeof GitHub>

types/async-function.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference types="node" />
2+
import * as core from '@actions/core';
3+
import * as exec from '@actions/exec';
4+
import { Context } from '@actions/github/lib/context';
5+
import { GitHub } from '@actions/github/lib/utils';
6+
import * as glob from '@actions/glob';
7+
import * as io from '@actions/io';
8+
import fetch from 'node-fetch';
9+
export declare type AsyncFunctionArguments = {
10+
context: Context;
11+
core: typeof core;
12+
github: InstanceType<typeof GitHub>;
13+
exec: typeof exec;
14+
glob: typeof glob;
15+
io: typeof io;
16+
fetch: typeof fetch;
17+
require: NodeRequire;
18+
__original_require__: NodeRequire;
19+
};
20+
export declare function callAsyncFunction<T>(args: AsyncFunctionArguments, source: string): Promise<T>;

0 commit comments

Comments
 (0)