Skip to content

Replace @action/github dependency with latest Octokit #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ 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.
The following arguments will be provided:

- `github` A pre-authenticated
- `github`/ `octokit` A pre-authenticated
[octokit/rest.js](https://octokit.github.io/rest.js) client with pagination plugins
- `context` An object containing the [context of the workflow
run](https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts)
- `context` An object containing partial [context of the workflow run](./src/context.ts)
- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package
- `glob` A reference to the [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) package
- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package
Expand All @@ -33,6 +32,11 @@ documentation.

## Breaking Changes

### V8

Version 8 of this action upgraded `@octokit/core` from v5 to v7, which could impact scripts that would
be impacted by those breaking changes.

### V7

Version 7 of this action updated the runtime to Node 20 - https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-javascript-actions
Expand Down Expand Up @@ -71,7 +75,7 @@ and potential `SyntaxError`s when the expression is not valid JavaScript code (p
To pass inputs, set `env` vars on the action step and reference them in your script with `process.env`:

```yaml
- uses: actions/github-script@v7
- uses: actions/github-script@v8
env:
TITLE: ${{ github.event.pull_request.title }}
with:
Expand All @@ -90,7 +94,7 @@ The return value of the script will be in the step's outputs under the
"result" key.

```yaml
- uses: actions/github-script@v7
- uses: actions/github-script@v8
id: set-result
with:
script: return "Hello!"
Expand All @@ -109,7 +113,7 @@ output of a github-script step. For some workflows, string encoding is preferred
`result-encoding` input:

```yaml
- uses: actions/github-script@v7
- uses: actions/github-script@v8
id: my-script
with:
result-encoding: string
Expand All @@ -121,32 +125,32 @@ output of a github-script step. For some workflows, string encoding is preferred
By default, requests made with the `github` instance will not be retried. You can configure this with the `retries` option:

```yaml
- uses: actions/github-script@v7
- uses: actions/github-script@v8
id: my-script
with:
result-encoding: string
retries: 3
script: |
github.rest.issues.get({
octokit.rest.issues.get({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverting the README changes from #557 - with #508 we should steer users towards using octokit as that will be more documentation and AI friendly 😄

issue_number: context.issue.number,
owner: context.repo.owner,
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:

```yaml
- uses: actions/github-script@v7
- uses: actions/github-script@v8
id: my-script
with:
result-encoding: string
retries: 3
retry-exempt-status-codes: 400,401
script: |
github.rest.issues.get({
octokit.rest.issues.get({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -168,7 +172,7 @@ By default, github-script will use the token provided to your workflow.

```yaml
- name: View context attributes
uses: actions/github-script@v7
uses: actions/github-script@v8
with:
script: console.log(context)
```
Expand All @@ -184,10 +188,10 @@ jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
github.rest.issues.createComment({
octokit.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -206,10 +210,10 @@ jobs:
apply-label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
github.rest.issues.addLabels({
octokit.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -228,18 +232,18 @@ jobs:
welcome:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
// Get a list of all issues created by the PR opener
// See: https://octokit.github.io/rest.js/#pagination
const creator = context.payload.sender.login
const opts = github.rest.issues.listForRepo.endpoint.merge({
const opts = octokit.rest.issues.listForRepo.endpoint.merge({
...context.issue,
creator,
state: 'all'
})
const issues = await github.paginate(opts)
const issues = await octokit.paginate(opts)

for (const issue of issues) {
if (issue.number === context.issue.number) {
Expand All @@ -251,7 +255,7 @@ jobs:
}
}

await github.rest.issues.createComment({
await octokit.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -273,11 +277,11 @@ jobs:
diff:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
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)
```

Expand All @@ -297,7 +301,7 @@ jobs:
list-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
const query = `query($owner:String!, $name:String!, $label:String!) {
Expand All @@ -314,7 +318,7 @@ jobs:
name: context.repo.repo,
label: 'wontfix'
}
const result = await github.graphql(query, variables)
const result = await octokit.graphql(query, variables)
console.log(result)
```

Expand All @@ -331,17 +335,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
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:

```javascript
module.exports = ({github, context}) => {
module.exports = ({octokit, context}) => {
return context.payload.client_payload.value
}
```
Expand Down Expand Up @@ -369,21 +373,21 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
- uses: actions/github-script@v8
env:
SHA: '${{env.parentSHA}}'
with:
script: |
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:

```javascript
module.exports = async ({github, context, core}) => {
module.exports = async ({octokit, context, core}) => {
const {SHA} = process.env
const commit = await github.rest.repos.getCommit({
const commit = await octokit.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `${SHA}`
Expand Down Expand Up @@ -413,7 +417,7 @@ jobs:
- run: npm ci
# or one-off:
- run: npm install execa
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
const execa = require('execa')
Expand Down Expand Up @@ -443,7 +447,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
const { default: printStuff } = await import('${{ github.workspace }}/src/print-stuff.js')
Expand Down Expand Up @@ -487,11 +491,11 @@ jobs:
apply-label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
github-token: ${{ secrets.MY_PAT }}
script: |
github.rest.issues.addLabels({
octokit.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -511,7 +515,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
const exitCode = await exec.exec('echo', ['hello'])
Expand All @@ -529,7 +533,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
- uses: actions/github-script@v8
with:
script: |
const {
Expand Down
44 changes: 4 additions & 40 deletions __test__/get-retry-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,30 @@ import {getRetryOptions} from '../src/retry-options'

describe('getRequestOptions', () => {
test('retries disabled if retries == 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(
0,
[400, 500, 502],
[]
)
const retryOptions = getRetryOptions(0, [400, 500, 502])

expect(retryOptions.enabled).toBe(false)
expect(retryOptions.doNotRetry).toBeFalsy()

expect(requestOptions?.retries).toBeFalsy()
})

test('properties set if retries > 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(
1,
[400, 500, 502],
[]
)
const retryOptions = getRetryOptions(1, [400, 500, 502])

expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])

expect(requestOptions?.retries).toEqual(1)
})

test('properties set if retries > 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(
1,
[400, 500, 502],
[]
)
const retryOptions = getRetryOptions(1, [400, 500, 502])

expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])

expect(requestOptions?.retries).toEqual(1)
})

test('retryOptions.doNotRetry not set if exemptStatusCodes isEmpty', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, [], [])

expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toBeUndefined()

expect(requestOptions?.retries).toEqual(1)
})

test('requestOptions does not override defaults from @actions/github', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, [], {
request: {
agent: 'default-user-agent'
},
foo: 'bar'
})
const retryOptions = getRetryOptions(1, [])

expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toBeUndefined()

expect(requestOptions?.retries).toEqual(1)
expect(requestOptions?.agent).toEqual('default-user-agent')
expect(requestOptions?.foo).toBeUndefined() // this should not be in the `options.request` object, but at the same level as `request`
})
})
Loading