Skip to content

Commit cc685dc

Browse files
authored
Add docs and tests for getOctokit script context
1 parent a7dc0e4 commit cc685dc

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ The `GITHUB_TOKEN` used by default is scoped to the current repository, see [Aut
504504

505505
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.
506506

507+
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')`.
508+
507509
[Learn more about creating and using encrypted secrets](https://docs.github.com/actions/reference/encrypted-secrets)
508510

509511
```yaml
@@ -516,6 +518,8 @@ jobs:
516518
runs-on: ubuntu-latest
517519
steps:
518520
- uses: actions/github-script@v8
521+
env:
522+
APP_TOKEN: ${{ secrets.MY_OTHER_PAT }}
519523
with:
520524
github-token: ${{ secrets.MY_PAT }}
521525
script: |
@@ -525,6 +529,13 @@ jobs:
525529
repo: context.repo.repo,
526530
labels: ['Triage']
527531
})
532+
533+
const appOctokit = getOctokit(process.env.APP_TOKEN)
534+
await appOctokit.rest.repos.createDispatchEvent({
535+
owner: 'my-org',
536+
repo: 'another-repo',
537+
event_type: 'trigger-deploy'
538+
})
528539
```
529540

530541
### Using exec package

__test__/async-function.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ describe('callAsyncFunction', () => {
88
expect(result).toEqual('bar')
99
})
1010

11+
test('passes getOctokit through the script context', async () => {
12+
const getOctokit = jest.fn().mockReturnValue('secondary-client')
13+
14+
const result = await callAsyncFunction(
15+
{getOctokit} as any,
16+
"return getOctokit('token')"
17+
)
18+
19+
expect(getOctokit).toHaveBeenCalledWith('token')
20+
expect(result).toEqual('secondary-client')
21+
})
22+
1123
test('throws on ReferenceError', async () => {
1224
expect.assertions(1)
1325

dist/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36289,6 +36289,7 @@ async function main() {
3628936289
__original_require__: require,
3629036290
github,
3629136291
octokit: github,
36292+
getOctokit: lib_github.getOctokit,
3629236293
context: lib_github.context,
3629336294
core: core,
3629436295
exec: exec,

0 commit comments

Comments
 (0)