From cc685dce52aca634bc157fee2f6d4f91f7a8070e Mon Sep 17 00:00:00 2001 From: Salman Chishti Date: Mon, 9 Mar 2026 04:44:59 -0700 Subject: [PATCH] Add docs and tests for getOctokit script context --- README.md | 11 +++++++++++ __test__/async-function.test.ts | 12 ++++++++++++ dist/index.js | 1 + 3 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 3dfe48d..0cef83c 100644 --- a/README.md +++ b/README.md @@ -504,6 +504,8 @@ The `GITHUB_TOKEN` used by default is scoped to the current repository, see [Aut If you need access to a different repository or an API that the `GITHUB_TOKEN` doesn't have permissions to, you can provide your own [PAT](https://help.github.com/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) as a secret using the `github-token` input. +If you need to use multiple tokens in the same script, `getOctokit` is also available in the script context so you can create additional authenticated clients without using `require('@actions/github')`. + [Learn more about creating and using encrypted secrets](https://docs.github.com/actions/reference/encrypted-secrets) ```yaml @@ -516,6 +518,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/github-script@v8 + env: + APP_TOKEN: ${{ secrets.MY_OTHER_PAT }} with: github-token: ${{ secrets.MY_PAT }} script: | @@ -525,6 +529,13 @@ jobs: repo: context.repo.repo, labels: ['Triage'] }) + + const appOctokit = getOctokit(process.env.APP_TOKEN) + await appOctokit.rest.repos.createDispatchEvent({ + owner: 'my-org', + repo: 'another-repo', + event_type: 'trigger-deploy' + }) ``` ### Using exec package diff --git a/__test__/async-function.test.ts b/__test__/async-function.test.ts index d68b302..c37ca52 100644 --- a/__test__/async-function.test.ts +++ b/__test__/async-function.test.ts @@ -8,6 +8,18 @@ describe('callAsyncFunction', () => { expect(result).toEqual('bar') }) + test('passes getOctokit through the script context', async () => { + const getOctokit = jest.fn().mockReturnValue('secondary-client') + + const result = await callAsyncFunction( + {getOctokit} as any, + "return getOctokit('token')" + ) + + expect(getOctokit).toHaveBeenCalledWith('token') + expect(result).toEqual('secondary-client') + }) + test('throws on ReferenceError', async () => { expect.assertions(1) diff --git a/dist/index.js b/dist/index.js index 19ad994..4dd0539 100644 --- a/dist/index.js +++ b/dist/index.js @@ -36289,6 +36289,7 @@ async function main() { __original_require__: require, github, octokit: github, + getOctokit: lib_github.getOctokit, context: lib_github.context, core: core, exec: exec,