Merge pull request #508 from iamstarkov/patch-2

make octokit instance available as octokit on top of github, to make it easier to seamlessly copy examples from GitHub rest api or octokit documentations
This commit is contained in:
Josh Gross
2025-02-04 14:47:03 -05:00
committed by GitHub
5 changed files with 22 additions and 18 deletions

View File

@@ -10,8 +10,8 @@ uses the GitHub API and the workflow run context.
To use this action, provide an input named `script` that contains the body of an asynchronous JavaScript function call. To use this action, provide an input named `script` that contains the body of an asynchronous JavaScript function call.
The following arguments will be provided: The following arguments will be provided:
- `github` A pre-authenticated - `octokit` (and `github`) A pre-authenticated
[octokit/rest.js](https://octokit.github.io/rest.js) client with pagination plugins [octokit/rest.js](https://octokit.github.io/rest.js) client _instance_ with pagination plugins
- `context` An object containing the [context of the workflow - `context` An object containing the [context of the workflow
run](https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts) run](https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts)
- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package - `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package
@@ -102,14 +102,14 @@ By default, requests made with the `github` instance will not be retried. You ca
result-encoding: string result-encoding: string
retries: 3 retries: 3
script: | script: |
github.rest.issues.get({ octokit.rest.issues.get({
issue_number: context.issue.number, issue_number: context.issue.number,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
}) })
``` ```
In this example, request failures from `github.rest.issues.get()` will be retried up to 3 times. In this example, request failures from `octokit.rest.issues.get()` will be retried up to 3 times.
You can also configure which status codes should be exempt from retries via the `retry-exempt-status-codes` option: You can also configure which status codes should be exempt from retries via the `retry-exempt-status-codes` option:
@@ -121,7 +121,7 @@ You can also configure which status codes should be exempt from retries via the
retries: 3 retries: 3
retry-exempt-status-codes: 400,401 retry-exempt-status-codes: 400,401
script: | script: |
github.rest.issues.get({ octokit.rest.issues.get({
issue_number: context.issue.number, issue_number: context.issue.number,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
@@ -162,7 +162,7 @@ jobs:
- uses: actions/github-script@v7 - uses: actions/github-script@v7
with: with:
script: | script: |
github.rest.issues.createComment({ octokit.rest.issues.createComment({
issue_number: context.issue.number, issue_number: context.issue.number,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
@@ -184,7 +184,7 @@ jobs:
- uses: actions/github-script@v7 - uses: actions/github-script@v7
with: with:
script: | script: |
github.rest.issues.addLabels({ octokit.rest.issues.addLabels({
issue_number: context.issue.number, issue_number: context.issue.number,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
@@ -209,12 +209,12 @@ jobs:
// Get a list of all issues created by the PR opener // Get a list of all issues created by the PR opener
// See: https://octokit.github.io/rest.js/#pagination // See: https://octokit.github.io/rest.js/#pagination
const creator = context.payload.sender.login const creator = context.payload.sender.login
const opts = github.rest.issues.listForRepo.endpoint.merge({ const opts = octokit.rest.issues.listForRepo.endpoint.merge({
...context.issue, ...context.issue,
creator, creator,
state: 'all' state: 'all'
}) })
const issues = await github.paginate(opts) const issues = await octokit.paginate(opts)
for (const issue of issues) { for (const issue of issues) {
if (issue.number === context.issue.number) { if (issue.number === context.issue.number) {
@@ -226,7 +226,7 @@ jobs:
} }
} }
await github.rest.issues.createComment({ await octokit.rest.issues.createComment({
issue_number: context.issue.number, issue_number: context.issue.number,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
@@ -252,7 +252,7 @@ jobs:
with: with:
script: | script: |
const diff_url = context.payload.pull_request.diff_url const diff_url = context.payload.pull_request.diff_url
const result = await github.request(diff_url) const result = await octokit.request(diff_url)
console.log(result) console.log(result)
``` ```
@@ -289,7 +289,7 @@ jobs:
name: context.repo.repo, name: context.repo.repo,
label: 'wontfix' label: 'wontfix'
} }
const result = await github.graphql(query, variables) const result = await octokit.graphql(query, variables)
console.log(result) console.log(result)
``` ```
@@ -310,13 +310,13 @@ jobs:
with: with:
script: | script: |
const script = require('./path/to/script.js') const script = require('./path/to/script.js')
console.log(script({github, context})) console.log(script({octokit, context}))
``` ```
And then export a function from your module: And then export a function from your module:
```javascript ```javascript
module.exports = ({github, context}) => { module.exports = ({octokit, context}) => {
return context.payload.client_payload.value return context.payload.client_payload.value
} }
``` ```
@@ -350,15 +350,15 @@ jobs:
with: with:
script: | script: |
const script = require('./path/to/script.js') const script = require('./path/to/script.js')
await script({github, context, core}) await script({octokit, context, core})
``` ```
And then export an async function from your module: And then export an async function from your module:
```javascript ```javascript
module.exports = async ({github, context, core}) => { module.exports = async ({octokit, context, core}) => {
const {SHA} = process.env const {SHA} = process.env
const commit = await github.rest.repos.getCommit({ const commit = await octokit.rest.repos.getCommit({
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
ref: `${SHA}` ref: `${SHA}`
@@ -487,7 +487,7 @@ jobs:
with: with:
github-token: ${{ secrets.MY_PAT }} github-token: ${{ secrets.MY_PAT }}
script: | script: |
github.rest.issues.addLabels({ octokit.rest.issues.addLabels({
issue_number: context.issue.number, issue_number: context.issue.number,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,

1
dist/index.js vendored
View File

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

View File

@@ -11,6 +11,7 @@ export declare type AsyncFunctionArguments = {
context: Context context: Context
core: typeof core core: typeof core
github: InstanceType<typeof GitHub> github: InstanceType<typeof GitHub>
octokit: InstanceType<typeof GitHub>
exec: typeof exec exec: typeof exec
glob: typeof glob glob: typeof glob
io: typeof io io: typeof io

View File

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

View File

@@ -9,6 +9,7 @@ export declare type AsyncFunctionArguments = {
context: Context; context: Context;
core: typeof core; core: typeof core;
github: InstanceType<typeof GitHub>; github: InstanceType<typeof GitHub>;
octokit: InstanceType<typeof GitHub>;
exec: typeof exec; exec: typeof exec;
glob: typeof glob; glob: typeof glob;
io: typeof io; io: typeof io;