mirror of
https://github.com/actions/github-script.git
synced 2025-12-08 16:16:21 +00:00
Compare commits
1 Commits
dependabot
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75af19036b |
2
.github/workflows/licensed.yml
vendored
2
.github/workflows/licensed.yml
vendored
@@ -19,7 +19,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0 # prefer to use a full fetch for licensed workflows
|
||||
- uses: ruby/setup-ruby@354a1ad156761f5ee2b7b13fa8e09943a5e8d252 # v1.229.0
|
||||
- uses: ruby/setup-ruby@28c4deda893d5a96a6b2d958c5b47fc18d65c9d3 # v1.213.0
|
||||
with:
|
||||
ruby-version: ruby
|
||||
- uses: github/setup-licensed@v1
|
||||
|
||||
124
README.md
124
README.md
@@ -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.
|
||||
The following arguments will be provided:
|
||||
|
||||
- `github` A pre-authenticated
|
||||
[octokit/rest.js](https://octokit.github.io/rest.js) client with pagination plugins
|
||||
- `octokit` (and `github`) A pre-authenticated
|
||||
[octokit/rest.js](https://octokit.github.io/rest.js) client _instance_ 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)
|
||||
- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package
|
||||
@@ -59,31 +59,6 @@ For example, `github.issues.createComment` in V4 becomes `github.rest.issues.cre
|
||||
|
||||
See [development.md](/docs/development.md).
|
||||
|
||||
## Passing inputs to the script
|
||||
|
||||
Actions expressions are evaluated before the `script` is passed to the action, so the result of any expressions
|
||||
*will be evaluated as JavaScript code*.
|
||||
|
||||
It's highly recommended to *not* evaluate expressions directly in the `script` to avoid
|
||||
[script injections](https://docs.github.com/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections)
|
||||
and potential `SyntaxError`s when the expression is not valid JavaScript code (particularly when it comes to improperly escaped strings).
|
||||
|
||||
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
|
||||
env:
|
||||
TITLE: ${{ github.event.pull_request.title }}
|
||||
with:
|
||||
script: |
|
||||
const title = process.env.TITLE;
|
||||
if (title.startsWith('octocat')) {
|
||||
console.log("PR title starts with 'octocat'");
|
||||
} else {
|
||||
console.error("PR title did not start with 'octocat'");
|
||||
}
|
||||
```
|
||||
|
||||
## Reading step results
|
||||
|
||||
The return value of the script will be in the step's outputs under the
|
||||
@@ -127,14 +102,14 @@ By default, requests made with the `github` instance will not be retried. You ca
|
||||
result-encoding: string
|
||||
retries: 3
|
||||
script: |
|
||||
github.rest.issues.get({
|
||||
octokit.rest.issues.get({
|
||||
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:
|
||||
|
||||
@@ -146,7 +121,7 @@ You can also configure which status codes should be exempt from retries via the
|
||||
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,
|
||||
@@ -187,7 +162,7 @@ jobs:
|
||||
- uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
github.rest.issues.createComment({
|
||||
octokit.rest.issues.createComment({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
@@ -209,7 +184,7 @@ jobs:
|
||||
- uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
github.rest.issues.addLabels({
|
||||
octokit.rest.issues.addLabels({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
@@ -234,12 +209,12 @@ jobs:
|
||||
// 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) {
|
||||
@@ -251,7 +226,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,
|
||||
@@ -277,7 +252,7 @@ jobs:
|
||||
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)
|
||||
```
|
||||
|
||||
@@ -314,7 +289,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)
|
||||
```
|
||||
|
||||
@@ -335,13 +310,13 @@ jobs:
|
||||
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
|
||||
}
|
||||
```
|
||||
@@ -375,15 +350,15 @@ jobs:
|
||||
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}`
|
||||
@@ -469,6 +444,27 @@ export default async ({ core, context }) => {
|
||||
};
|
||||
```
|
||||
|
||||
### Use env as input
|
||||
|
||||
You can set env vars to use them in your script:
|
||||
|
||||
```yaml
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
echo-input:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/github-script@v7
|
||||
env:
|
||||
FIRST_NAME: Mona
|
||||
LAST_NAME: Octocat
|
||||
with:
|
||||
script: |
|
||||
const { FIRST_NAME, LAST_NAME } = process.env
|
||||
|
||||
console.log(`Hello ${FIRST_NAME} ${LAST_NAME}`)
|
||||
```
|
||||
|
||||
### Using a separate GitHub token
|
||||
|
||||
@@ -491,52 +487,10 @@ jobs:
|
||||
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,
|
||||
labels: ['Triage']
|
||||
})
|
||||
```
|
||||
|
||||
### Using exec package
|
||||
|
||||
The provided [@actions/exec](https://github.com/actions/toolkit/tree/main/packages/exec) package allows to execute command or tools in a cross platform way:
|
||||
|
||||
```yaml
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
use-exec:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const exitCode = await exec.exec('echo', ['hello'])
|
||||
|
||||
console.log(exitCode)
|
||||
```
|
||||
|
||||
`exec` packages provides `getExecOutput` function to retrieve stdout and stderr from executed command:
|
||||
|
||||
```yaml
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
use-get-exec-output:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const {
|
||||
exitCode,
|
||||
stdout,
|
||||
stderr
|
||||
} = await exec.getExecOutput('echo', ['hello']);
|
||||
|
||||
console.log(exitCode, stdout, stderr)
|
||||
```
|
||||
|
||||
62
package-lock.json
generated
62
package-lock.json
generated
@@ -1345,22 +1345,24 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@octokit/openapi-types": {
|
||||
"version": "19.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-19.0.0.tgz",
|
||||
"integrity": "sha512-PclQ6JGMTE9iUStpzMkwLCISFn/wDeRjkZFIKALpvJQNBGwDoYYi2fFvuHwssoQ1rXI5mfh6jgTgWuddeUzfWw=="
|
||||
"version": "20.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-20.0.0.tgz",
|
||||
"integrity": "sha512-EtqRBEjp1dL/15V7WiX5LJMIxxkdiGJnabzYx5Apx4FkQIFgAfKumXeYAqqJCj1s+BMX4cPFIFC4OLCR6stlnA==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@octokit/plugin-paginate-rest": {
|
||||
"version": "9.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-9.0.0.tgz",
|
||||
"integrity": "sha512-oIJzCpttmBTlEhBmRvb+b9rlnGpmFgDtZ0bB6nq39qIod6A5DP+7RkVLMOixIgRCYSHDTeayWqmiJ2SZ6xgfdw==",
|
||||
"version": "9.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-9.2.2.tgz",
|
||||
"integrity": "sha512-u3KYkGF7GcZnSD/3UP0S7K5XUFT2FkOQdcfXZGZQPGv3lm4F2Xbf71lvjldr8c1H3nNbF+33cLEkWYbokGWqiQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@octokit/types": "^12.0.0"
|
||||
"@octokit/types": "^12.6.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@octokit/core": ">=5"
|
||||
"@octokit/core": "5"
|
||||
}
|
||||
},
|
||||
"node_modules/@octokit/plugin-request-log": {
|
||||
@@ -1459,11 +1461,12 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@octokit/types": {
|
||||
"version": "12.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/types/-/types-12.0.0.tgz",
|
||||
"integrity": "sha512-EzD434aHTFifGudYAygnFlS1Tl6KhbTynEWELQXIbTY8Msvb5nEqTZIm7sbPEt4mQYLZwu3zPKVdeIrw0g7ovg==",
|
||||
"version": "12.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/types/-/types-12.6.0.tgz",
|
||||
"integrity": "sha512-1rhSOfRa6H9w4YwK0yrf5faDaDTb+yLyBUKOCV4xtCDB5VmIPqd/v9yr9o6SAzOAlRxMiRiCic6JVM1/kunVkw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@octokit/openapi-types": "^19.0.0"
|
||||
"@octokit/openapi-types": "^20.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@pkgr/utils": {
|
||||
@@ -7102,10 +7105,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/undici": {
|
||||
"version": "5.29.0",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-5.29.0.tgz",
|
||||
"integrity": "sha512-raqeBD6NQK4SkWhQzeYKd1KmIG6dllBOTt55Rmkt4HtI9mwdWtJljnrXjAFUBLTSN67HWrOIZ3EPF4kjUw80Bg==",
|
||||
"license": "MIT",
|
||||
"version": "5.28.5",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-5.28.5.tgz",
|
||||
"integrity": "sha512-zICwjrDrcrUE0pyyJc1I2QzBkLM8FINsgOrt6WjA+BgajVq9Nxu2PbFFXUrAggLfDXlZGZBVZYw7WNV5KiBiBA==",
|
||||
"dependencies": {
|
||||
"@fastify/busboy": "^2.0.0"
|
||||
},
|
||||
@@ -8373,16 +8375,16 @@
|
||||
}
|
||||
},
|
||||
"@octokit/openapi-types": {
|
||||
"version": "19.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-19.0.0.tgz",
|
||||
"integrity": "sha512-PclQ6JGMTE9iUStpzMkwLCISFn/wDeRjkZFIKALpvJQNBGwDoYYi2fFvuHwssoQ1rXI5mfh6jgTgWuddeUzfWw=="
|
||||
"version": "20.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-20.0.0.tgz",
|
||||
"integrity": "sha512-EtqRBEjp1dL/15V7WiX5LJMIxxkdiGJnabzYx5Apx4FkQIFgAfKumXeYAqqJCj1s+BMX4cPFIFC4OLCR6stlnA=="
|
||||
},
|
||||
"@octokit/plugin-paginate-rest": {
|
||||
"version": "9.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-9.0.0.tgz",
|
||||
"integrity": "sha512-oIJzCpttmBTlEhBmRvb+b9rlnGpmFgDtZ0bB6nq39qIod6A5DP+7RkVLMOixIgRCYSHDTeayWqmiJ2SZ6xgfdw==",
|
||||
"version": "9.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-9.2.2.tgz",
|
||||
"integrity": "sha512-u3KYkGF7GcZnSD/3UP0S7K5XUFT2FkOQdcfXZGZQPGv3lm4F2Xbf71lvjldr8c1H3nNbF+33cLEkWYbokGWqiQ==",
|
||||
"requires": {
|
||||
"@octokit/types": "^12.0.0"
|
||||
"@octokit/types": "^12.6.0"
|
||||
}
|
||||
},
|
||||
"@octokit/plugin-request-log": {
|
||||
@@ -8462,11 +8464,11 @@
|
||||
}
|
||||
},
|
||||
"@octokit/types": {
|
||||
"version": "12.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/types/-/types-12.0.0.tgz",
|
||||
"integrity": "sha512-EzD434aHTFifGudYAygnFlS1Tl6KhbTynEWELQXIbTY8Msvb5nEqTZIm7sbPEt4mQYLZwu3zPKVdeIrw0g7ovg==",
|
||||
"version": "12.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@octokit/types/-/types-12.6.0.tgz",
|
||||
"integrity": "sha512-1rhSOfRa6H9w4YwK0yrf5faDaDTb+yLyBUKOCV4xtCDB5VmIPqd/v9yr9o6SAzOAlRxMiRiCic6JVM1/kunVkw==",
|
||||
"requires": {
|
||||
"@octokit/openapi-types": "^19.0.0"
|
||||
"@octokit/openapi-types": "^20.0.0"
|
||||
}
|
||||
},
|
||||
"@pkgr/utils": {
|
||||
@@ -12535,9 +12537,9 @@
|
||||
}
|
||||
},
|
||||
"undici": {
|
||||
"version": "5.29.0",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-5.29.0.tgz",
|
||||
"integrity": "sha512-raqeBD6NQK4SkWhQzeYKd1KmIG6dllBOTt55Rmkt4HtI9mwdWtJljnrXjAFUBLTSN67HWrOIZ3EPF4kjUw80Bg==",
|
||||
"version": "5.28.5",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-5.28.5.tgz",
|
||||
"integrity": "sha512-zICwjrDrcrUE0pyyJc1I2QzBkLM8FINsgOrt6WjA+BgajVq9Nxu2PbFFXUrAggLfDXlZGZBVZYw7WNV5KiBiBA==",
|
||||
"requires": {
|
||||
"@fastify/busboy": "^2.0.0"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user