diff --git a/README.md b/README.md index 9f819310..62b19c37 100644 --- a/README.md +++ b/README.md @@ -424,6 +424,24 @@ jobs: await printStuff() ``` +### Use scripts with jsDoc support + +If you want type support for your scripts, you could use the command below to install the +`github-script` type declaration. +```sh +$ npm i -D @types/github-script@github:actions/github-script +``` + +And then add the `jsDoc` declaration to your script like this: +```js +// @ts-check +/** @param {import('@types/github-script').AsyncFunctionArguments} AsyncFunctionArguments */ +export default async ({ core, context }) => { + core.debug("Running something at the moment"); + return context.actor; +}; +``` + ### Use env as input You can set env vars to use them in your script: diff --git a/action.yml b/action.yml index 516ae164..c877b035 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,9 @@ inputs: retry-exempt-status-codes: description: A comma separated list of status codes that will NOT be retried e.g. "400,500". No effect unless `retries` is set default: 400,401,403,404,422 # from https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14 + base-url: + description: An optional GitHub REST API URL to connect to a different GitHub instance. For example, https://my.github-enterprise-server.com/api/v3 + required: false outputs: result: description: The return value of the script, stringified with `JSON.stringify` diff --git a/dist/index.js b/dist/index.js index bf761f68..db5b5fa9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -35500,6 +35500,7 @@ async function main() { const debug = core.getBooleanInput('debug'); const userAgent = core.getInput('user-agent'); const previews = core.getInput('previews'); + const baseUrl = core.getInput('base-url'); const retries = parseInt(core.getInput('retries')); const exemptStatusCodes = parseNumberArray(core.getInput('retry-exempt-status-codes')); const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes, utils.defaults); @@ -35508,7 +35509,8 @@ async function main() { userAgent: userAgent || undefined, previews: previews ? previews.split(',') : undefined, retry: retryOpts, - request: requestOpts + request: requestOpts, + baseUrl: baseUrl || undefined }; const github = (0,lib_github.getOctokit)(token, opts, plugin_retry_dist_node.retry, dist_node.requestLog); const script = core.getInput('script', { required: true }); diff --git a/package.json b/package.json index 5ab40369..86f13f7f 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,14 @@ "author": "GitHub", "license": "MIT", "main": "dist/index.js", + "types": "types/async-function.d.ts", "private": true, "engines": { "node": ">=20.0.0 <21.0.0" }, "scripts": { - "build": "ncc build src/main.ts", + "build": "npm run build:types && ncc build src/main.ts", + "build:types": "tsc src/async-function.ts -t es5 --declaration --allowJs --emitDeclarationOnly --outDir types", "format:check": "prettier --check src __test__", "format:write": "prettier --write src __test__", "lint": "eslint src __test__", diff --git a/src/async-function.ts b/src/async-function.ts index ba8e7496..a60af875 100644 --- a/src/async-function.ts +++ b/src/async-function.ts @@ -7,7 +7,7 @@ import * as io from '@actions/io' const AsyncFunction = Object.getPrototypeOf(async () => null).constructor -type AsyncFunctionArguments = { +export declare type AsyncFunctionArguments = { context: Context core: typeof core github: InstanceType diff --git a/src/main.ts b/src/main.ts index 44b1270c..96551515 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,6 +17,7 @@ main().catch(handleError) type Options = { log?: Console userAgent?: string + baseUrl?: string previews?: string[] retry?: RetryOptions request?: RequestRequestOptions @@ -27,6 +28,7 @@ async function main(): Promise { const debug = core.getBooleanInput('debug') const userAgent = core.getInput('user-agent') const previews = core.getInput('previews') + const baseUrl = core.getInput('base-url') const retries = parseInt(core.getInput('retries')) const exemptStatusCodes = parseNumberArray( core.getInput('retry-exempt-status-codes') @@ -42,7 +44,8 @@ async function main(): Promise { userAgent: userAgent || undefined, previews: previews ? previews.split(',') : undefined, retry: retryOpts, - request: requestOpts + request: requestOpts, + baseUrl: baseUrl || undefined } const github = getOctokit(token, opts, retry, requestLog) diff --git a/types/async-function.d.ts b/types/async-function.d.ts new file mode 100644 index 00000000..b4d6a8e8 --- /dev/null +++ b/types/async-function.d.ts @@ -0,0 +1,20 @@ +/// +import * as core from '@actions/core'; +import * as exec from '@actions/exec'; +import { Context } from '@actions/github/lib/context'; +import { GitHub } from '@actions/github/lib/utils'; +import * as glob from '@actions/glob'; +import * as io from '@actions/io'; +import fetch from 'node-fetch'; +export declare type AsyncFunctionArguments = { + context: Context; + core: typeof core; + github: InstanceType; + exec: typeof exec; + glob: typeof glob; + io: typeof io; + fetch: typeof fetch; + require: NodeRequire; + __original_require__: NodeRequire; +}; +export declare function callAsyncFunction(args: AsyncFunctionArguments, source: string): Promise;