This commit is contained in:
Salman Chishti 2026-03-09 12:02:02 +00:00 committed by GitHub
commit c94e6678f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 74 additions and 9 deletions

View file

@ -154,22 +154,58 @@ jobs:
return endpoint({}).headers['user-agent'] return endpoint({}).headers['user-agent']
result-encoding: string result-encoding: string
- run: | - run: |
matches_user_agent() {
local actual="$1"
local prefix="$2"
[[ "$actual" =~ ^${prefix}(\ actions_orchestration_id/[^[:space:]]+)?\ octokit-core\.js/ ]]
}
echo "- Validating user-agent default" echo "- Validating user-agent default"
expected="actions/github-script octokit-core.js/" expected="actions/github-script"
if [[ "${{steps.user-agent-default.outputs.result}}" != "$expected"* ]]; then if ! matches_user_agent "${{steps.user-agent-default.outputs.result}}" "$expected"; then
echo $'::error::\u274C' "Expected user-agent to start with '$expected', got ${{steps.user-agent-default.outputs.result}}" echo $'::error::\u274C' "Expected user-agent to start with '$expected' and include 'octokit-core.js/', got ${{steps.user-agent-default.outputs.result}}"
exit 1 exit 1
fi fi
echo "- Validating user-agent set to a value" echo "- Validating user-agent set to a value"
expected="foobar octokit-core.js/" expected="foobar"
if [[ "${{steps.user-agent-set.outputs.result}}" != "$expected"* ]]; then if ! matches_user_agent "${{steps.user-agent-set.outputs.result}}" "$expected"; then
echo $'::error::\u274C' "Expected user-agent to start with '$expected', got ${{steps.user-agent-set.outputs.result}}" echo $'::error::\u274C' "Expected user-agent to start with '$expected' and include 'octokit-core.js/', got ${{steps.user-agent-set.outputs.result}}"
exit 1 exit 1
fi fi
echo "- Validating user-agent set to an empty string" echo "- Validating user-agent set to an empty string"
expected="actions/github-script octokit-core.js/" expected="actions/github-script"
if [[ "${{steps.user-agent-empty.outputs.result}}" != "$expected"* ]]; then if ! matches_user_agent "${{steps.user-agent-empty.outputs.result}}" "$expected"; then
echo $'::error::\u274C' "Expected user-agent to start with '$expected', got ${{steps.user-agent-empty.outputs.result}}" echo $'::error::\u274C' "Expected user-agent to start with '$expected' and include 'octokit-core.js/', got ${{steps.user-agent-empty.outputs.result}}"
exit 1
fi
echo $'\u2705 Test passed' | tee -a $GITHUB_STEP_SUMMARY
test-get-octokit:
name: 'Integration test: getOctokit with token'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-dependencies
- id: secondary-client
name: Create a second client with getOctokit
uses: ./
env:
APP_TOKEN: ${{ github.token }}
with:
script: |
const appOctokit = getOctokit(process.env.APP_TOKEN)
const {data} = await appOctokit.rest.repos.get({
owner: context.repo.owner,
repo: context.repo.repo
})
return `${appOctokit !== github}:${data.full_name}`
result-encoding: string
- run: |
echo "- Validating secondary client output"
expected="true:actions/github-script"
if [[ "${{steps.secondary-client.outputs.result}}" != "$expected" ]]; then
echo $'::error::\u274C' "Expected '$expected', got ${{steps.secondary-client.outputs.result}}"
exit 1 exit 1
fi fi
echo $'\u2705 Test passed' | tee -a $GITHUB_STEP_SUMMARY echo $'\u2705 Test passed' | tee -a $GITHUB_STEP_SUMMARY

View file

@ -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 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) [Learn more about creating and using encrypted secrets](https://docs.github.com/actions/reference/encrypted-secrets)
```yaml ```yaml
@ -516,6 +518,8 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/github-script@v8 - uses: actions/github-script@v8
env:
APP_TOKEN: ${{ secrets.MY_OTHER_PAT }}
with: with:
github-token: ${{ secrets.MY_PAT }} github-token: ${{ secrets.MY_PAT }}
script: | script: |
@ -525,6 +529,13 @@ jobs:
repo: context.repo.repo, repo: context.repo.repo,
labels: ['Triage'] 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 ### Using exec package

View file

@ -8,6 +8,18 @@ describe('callAsyncFunction', () => {
expect(result).toEqual('bar') 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 () => { test('throws on ReferenceError', async () => {
expect.assertions(1) expect.assertions(1)

1
dist/index.js vendored
View file

@ -36289,6 +36289,7 @@ async function main() {
__original_require__: require, __original_require__: require,
github, github,
octokit: github, octokit: github,
getOctokit: lib_github.getOctokit,
context: lib_github.context, context: lib_github.context,
core: core, core: core,
exec: exec, exec: exec,

View file

@ -1,5 +1,6 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
import {getOctokit} from '@actions/github'
import {Context} from '@actions/github/lib/context' import {Context} from '@actions/github/lib/context'
import {GitHub} from '@actions/github/lib/utils' import {GitHub} from '@actions/github/lib/utils'
import * as glob from '@actions/glob' import * as glob from '@actions/glob'
@ -12,6 +13,7 @@ export declare type AsyncFunctionArguments = {
core: typeof core core: typeof core
github: InstanceType<typeof GitHub> github: InstanceType<typeof GitHub>
octokit: InstanceType<typeof GitHub> octokit: InstanceType<typeof GitHub>
getOctokit: typeof getOctokit
exec: typeof exec exec: typeof exec
glob: typeof glob glob: typeof glob
io: typeof io io: typeof io

View file

@ -66,6 +66,7 @@ async function main(): Promise<void> {
__original_require__: __non_webpack_require__, __original_require__: __non_webpack_require__,
github, github,
octokit: github, octokit: github,
getOctokit,
context, context,
core, core,
exec, exec,

View file

@ -1,6 +1,7 @@
/// <reference types="node" /> /// <reference types="node" />
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import { getOctokit } from '@actions/github';
import { Context } from '@actions/github/lib/context'; import { Context } from '@actions/github/lib/context';
import { GitHub } from '@actions/github/lib/utils'; import { GitHub } from '@actions/github/lib/utils';
import * as glob from '@actions/glob'; import * as glob from '@actions/glob';
@ -10,6 +11,7 @@ export declare type AsyncFunctionArguments = {
core: typeof core; core: typeof core;
github: InstanceType<typeof GitHub>; github: InstanceType<typeof GitHub>;
octokit: InstanceType<typeof GitHub>; octokit: InstanceType<typeof GitHub>;
getOctokit: typeof getOctokit;
exec: typeof exec; exec: typeof exec;
glob: typeof glob; glob: typeof glob;
io: typeof io; io: typeof io;