mirror of
https://github.com/actions/github-script.git
synced 2026-10-15 20:06:55 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
626af12fe9 | ||
|
|
79d44e3558 | ||
|
|
11dbe182e4 | ||
|
|
563cb72ab7 | ||
|
|
3c682f9960 | ||
|
|
fc4d627901 | ||
|
|
d77e8e702f | ||
|
|
fb0feb9324 | ||
|
|
21ae42ffb7 | ||
|
|
408aa6532b | ||
|
|
44b873bc97 | ||
|
|
b92989f423 | ||
|
|
5e49adabf5 | ||
|
|
4db7989ab1 | ||
|
|
a4a34d1dea | ||
|
|
3f9262325d | ||
|
|
07ecbc5ac0 | ||
|
|
a656f437f9 | ||
|
|
83107c8695 | ||
|
|
603dfa3ce2 | ||
|
|
62c8044fd6 | ||
|
|
c19d6ba8b3 | ||
|
|
1ba1e95cf2 | ||
|
|
b507739f17 | ||
|
|
95130f1202 | ||
|
|
82b33c82ef | ||
|
|
53cd48b63b | ||
|
|
6e5ee1dc1c | ||
|
|
fa718ed671 | ||
|
|
4789b35ac3 | ||
|
|
89e3c40f24 | ||
|
|
1b10125da5 | ||
|
|
9b7fae11e2 | ||
|
|
c7971c2fc0 | ||
|
|
7ec2818723 | ||
|
|
287e10e610 | ||
|
|
b2c461f3a2 | ||
|
|
048309c447 | ||
|
|
a8704b62a3 | ||
|
|
e16145c72e | ||
|
|
dd16c14e71 | ||
|
|
5d33ffc89d | ||
|
|
adb3d5168d | ||
|
|
181dcc219c | ||
|
|
6f0504cb03 | ||
|
|
d89db5b6f4 | ||
|
|
52110c52e9 | ||
|
|
f498913621 | ||
|
|
58f0ff84d6 | ||
|
|
3037861304 | ||
|
|
b945d091bf | ||
|
|
ca14121875 | ||
|
|
ca6d0aaa59 | ||
|
|
5d879b69aa | ||
|
|
1bc9cbef6c | ||
|
|
9a58186a54 | ||
|
|
8934ce0ffe | ||
|
|
05997a2463 | ||
|
|
1bc2687309 | ||
|
|
97fd3f1973 | ||
|
|
f8e6050e29 | ||
|
|
648bc46b8a | ||
|
|
1268370776 | ||
|
|
5d03ada4b0 | ||
|
|
293ee0ca2d | ||
|
|
67d29bab15 | ||
|
|
0baa1791ca | ||
|
|
b4fd05ccdf | ||
|
|
2ccd1edaf4 | ||
|
|
a59996ab35 | ||
|
|
7ca8635cd0 | ||
|
|
8117de6119 | ||
|
|
3f4f5a83e8 | ||
|
|
0e2c0d5c7c | ||
|
|
0d15461bee | ||
|
|
e4cc5a8d47 | ||
|
|
dc16f26602 | ||
|
|
8982156783 | ||
|
|
80a5e943b4 |
No files matched your search
@@ -0,0 +1,13 @@
|
|||||||
|
root: true
|
||||||
|
parser: '@typescript-eslint/parser'
|
||||||
|
plugins: ['@typescript-eslint']
|
||||||
|
extends:
|
||||||
|
- eslint:recommended
|
||||||
|
- plugin:@typescript-eslint/eslint-recommended
|
||||||
|
- plugin:@typescript-eslint/recommended
|
||||||
|
- prettier/@typescript-eslint
|
||||||
|
rules:
|
||||||
|
# '@typescript-eslint/explicit-function-return-type': 0
|
||||||
|
'@typescript-eslint/no-use-before-define':
|
||||||
|
- 2
|
||||||
|
- functions: false
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
.licenses/** -diff linguist-generated=true
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
---
|
||||||
|
name: Bug report
|
||||||
|
about: Create a report to help us improve
|
||||||
|
title: ''
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Describe the bug**
|
||||||
|
A clear and concise description of what the bug is.
|
||||||
|
|
||||||
|
**To Reproduce**
|
||||||
|
Steps to reproduce the behavior:
|
||||||
|
1. Go to '...'
|
||||||
|
2. Click on '....'
|
||||||
|
3. Scroll down to '....'
|
||||||
|
4. See error
|
||||||
|
|
||||||
|
**Expected behavior**
|
||||||
|
A clear and concise description of what you expected to happen.
|
||||||
|
|
||||||
|
**Screenshots**
|
||||||
|
If applicable, add screenshots to help explain your problem.
|
||||||
|
|
||||||
|
**Desktop (please complete the following information):**
|
||||||
|
- OS: [e.g. iOS]
|
||||||
|
- Browser [e.g. chrome, safari]
|
||||||
|
- Version [e.g. 22]
|
||||||
|
|
||||||
|
**Smartphone (please complete the following information):**
|
||||||
|
- Device: [e.g. iPhone6]
|
||||||
|
- OS: [e.g. iOS8.1]
|
||||||
|
- Browser [e.g. stock browser, safari]
|
||||||
|
- Version [e.g. 22]
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context about the problem here.
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
name: Feature request
|
||||||
|
about: Suggest an idea for this project
|
||||||
|
title: ''
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Is your feature request related to a problem? Please describe.**
|
||||||
|
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||||
|
|
||||||
|
**Describe the solution you'd like**
|
||||||
|
A clear and concise description of what you want to happen.
|
||||||
|
|
||||||
|
**Describe alternatives you've considered**
|
||||||
|
A clear and concise description of any alternative solutions or features you've considered.
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context or screenshots about the feature request here.
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
on:
|
on:
|
||||||
push: {branches: master}
|
push: {branches: main}
|
||||||
pull_request: {branches: master}
|
pull_request: {branches: main}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
ci:
|
ci:
|
||||||
@@ -15,4 +15,5 @@ jobs:
|
|||||||
key: ${{runner.os}}-npm-${{hashFiles('**/package-lock.json')}}
|
key: ${{runner.os}}-npm-${{hashFiles('**/package-lock.json')}}
|
||||||
restore-keys: ${{runner.os}}-npm-
|
restore-keys: ${{runner.os}}-npm-
|
||||||
- run: npm ci
|
- run: npm ci
|
||||||
|
- run: npm run style:check
|
||||||
- run: npm test
|
- run: npm test
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
on:
|
on:
|
||||||
push: {branches: master}
|
push: {branches: main}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
integration:
|
integration:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- id: output-set
|
- id: output-set
|
||||||
uses: actions/github-script@master
|
uses: actions/github-script@main
|
||||||
with:
|
with:
|
||||||
script: return 'test'
|
script: return core.getInput('input-value')
|
||||||
result-encoding: string
|
result-encoding: string
|
||||||
|
input-value: output
|
||||||
- run: |
|
- run: |
|
||||||
if [[ "${{steps.output-set.outputs.result}}" != "test" ]]; then
|
if [[ "${{steps.output-set.outputs.result}}" != "output" ]]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
name: Licensed
|
||||||
|
|
||||||
|
on:
|
||||||
|
push: {branches: main}
|
||||||
|
pull_request: {branches: main}
|
||||||
|
repository_dispatch:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
name: Check licenses
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: ~/.npm
|
||||||
|
key: ${{runner.os}}-npm-${{hashFiles('**/package-lock.json')}}
|
||||||
|
restore-keys: ${{runner.os}}-npm-
|
||||||
|
- run: npm ci
|
||||||
|
- name: Install licensed
|
||||||
|
run: |-
|
||||||
|
cd $RUNNER_TEMP
|
||||||
|
curl -Lfs -o licensed.tar.gz https://github.com/github/licensed/releases/download/2.9.2/licensed-2.9.2-linux-x64.tar.gz
|
||||||
|
sudo tar -xzf licensed.tar.gz
|
||||||
|
sudo mv licensed /usr/local/bin/licensed
|
||||||
|
- run: licensed status
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches: main
|
||||||
|
types: [opened, synchronize]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
pull-request-test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: ./
|
||||||
|
with:
|
||||||
|
script: |
|
||||||
|
// Get the existing comments.
|
||||||
|
const {data: comments} = await github.issues.listComments({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
issue_number: context.payload.number,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Find any comment already made by the bot.
|
||||||
|
const botComment = comments.find(comment => comment.user.id === 41898282)
|
||||||
|
const commentBody = "Hello from actions/github-script! (${{ github.sha }})"
|
||||||
|
|
||||||
|
if (botComment) {
|
||||||
|
await github.issues.updateComment({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
comment_id: botComment.id,
|
||||||
|
body: commentBody
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
await github.issues.createComment({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
issue_number: context.payload.number,
|
||||||
|
body: commentBody
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
name: Stale Issues & PRs
|
||||||
|
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: '0 0 * * *'
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
mark_stale:
|
||||||
|
name: Mark issues and PRs as stale
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/stale@v3
|
||||||
|
with:
|
||||||
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
exempt-issue-labels: Not Stale
|
||||||
|
exempt-pr-labels: Not Stale
|
||||||
|
stale-issue-message: >
|
||||||
|
This issue is stale because it has been open for 60 days with no
|
||||||
|
activity. Remove the "Stale" label or comment on the issue, or it
|
||||||
|
will be closed in 7 days.
|
||||||
|
stale-pr-message: >
|
||||||
|
This pull request is stale because it has been open for 60 days
|
||||||
|
with no activity. Remove the "Stale" label or comment on the pull
|
||||||
|
request, or it will be closed in 7 days.
|
||||||
|
close-issue-message: >
|
||||||
|
This issue has been marked as stale and closed due to no activity
|
||||||
|
on it.
|
||||||
|
close-pr-message: >
|
||||||
|
This pull request has been marked as stale and closed due to no
|
||||||
|
activity on it.
|
||||||
+2
-1
@@ -1 +1,2 @@
|
|||||||
node_modules
|
/node_modules/
|
||||||
|
!/.vscode/
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
sources:
|
||||||
|
npm: true
|
||||||
|
|
||||||
|
allowed:
|
||||||
|
- apache-2.0
|
||||||
|
- bsd-2-clause
|
||||||
|
- bsd-3-clause
|
||||||
|
- isc
|
||||||
|
- mit
|
||||||
|
- cc0-1.0
|
||||||
|
- unlicense
|
||||||
|
|
||||||
|
reviewed:
|
||||||
|
npm:
|
||||||
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
BIN
Binary file not shown.
Generated
BIN
Binary file not shown.
@@ -0,0 +1,5 @@
|
|||||||
|
arrowParens: avoid
|
||||||
|
bracketSpacing: false
|
||||||
|
semi: false
|
||||||
|
singleQuote: true
|
||||||
|
trailingComma: none
|
||||||
Vendored
+10
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.codeActionsOnSave": {
|
||||||
|
"source.organizeImports": true
|
||||||
|
},
|
||||||
|
"files.exclude": {
|
||||||
|
"**/dist": true,
|
||||||
|
"**/node_modules": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,18 @@
|
|||||||
# github-script  
|
# github-script   
|
||||||
|
|
||||||
This action makes it easy to quickly write a script in your workflow that
|
This action makes it easy to quickly write a script in your workflow that
|
||||||
uses the GitHub API and the workflow run context.
|
uses the GitHub API and the workflow run context.
|
||||||
|
|
||||||
In order to use this action, a `script` input is provided. The value of that
|
In order to use this action, a `script` input is provided. The value of that
|
||||||
input should be the body of an asynchronous function call. Two arguments will
|
input should be the body of an asynchronous function call. The following
|
||||||
be provided:
|
arguments will be provided:
|
||||||
|
|
||||||
- `github` A pre-authenticated
|
- `github` A pre-authenticated
|
||||||
[octokit/rest.js](https://github.com/octokit/rest.js) client
|
[octokit/rest.js](https://github.com/octokit/rest.js) client
|
||||||
- `context` An object containing the [context of the workflow
|
- `context` An object containing the [context of the workflow
|
||||||
run](https://github.com/actions/toolkit/tree/master/packages/github)
|
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
|
||||||
|
- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package
|
||||||
|
|
||||||
Since the `script` is just a function body, these values will already be
|
Since the `script` is just a function body, these values will already be
|
||||||
defined, so you don't have to (see examples below).
|
defined, so you don't have to (see examples below).
|
||||||
@@ -19,12 +21,45 @@ See [octokit/rest.js](https://octokit.github.io/rest.js/) for the API client
|
|||||||
documentation.
|
documentation.
|
||||||
|
|
||||||
**Note** This action is still a bit of an experiment—the API may change in
|
**Note** This action is still a bit of an experiment—the API may change in
|
||||||
*future versions. 🙂
|
future versions. 🙂
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
See [development.md](/docs/development.md).
|
See [development.md](/docs/development.md).
|
||||||
|
|
||||||
|
## Reading step results
|
||||||
|
|
||||||
|
The return value of the script will be in the step's outputs under the
|
||||||
|
"result" key.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- uses: actions/github-script@v3
|
||||||
|
id: set-result
|
||||||
|
with:
|
||||||
|
script: return "Hello!"
|
||||||
|
result-encoding: string
|
||||||
|
- name: Get result
|
||||||
|
run: echo "${{steps.set-result.outputs.result}}"
|
||||||
|
```
|
||||||
|
|
||||||
|
See ["Result encoding"](#result-encoding) for details on how the encoding of
|
||||||
|
these outputs can be changed.
|
||||||
|
|
||||||
|
## Result encoding
|
||||||
|
|
||||||
|
By default, the JSON-encoded return value of the function is set as the "result" in the
|
||||||
|
output of a github-script step. For some workflows, string encoding is preferred. This option can be set using the
|
||||||
|
`result-encoding` input:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- uses: actions/github-script@v3
|
||||||
|
id: my-script
|
||||||
|
with:
|
||||||
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
result-encoding: string
|
||||||
|
script: return "I will be string (not JSON) encoded!"
|
||||||
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
Note that `github-token` is optional in this action, and the input is there
|
Note that `github-token` is optional in this action, and the input is there
|
||||||
@@ -36,13 +71,14 @@ By default, github-script will use the token provided to your workflow.
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
on:
|
on:
|
||||||
issues: {types: opened}
|
issues:
|
||||||
|
types: [opened]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
comment:
|
comment:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/github-script@0.6.0
|
- uses: actions/github-script@v3
|
||||||
with:
|
with:
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
script: |
|
script: |
|
||||||
@@ -58,13 +94,14 @@ jobs:
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
on:
|
on:
|
||||||
issues: {types: opened}
|
issues:
|
||||||
|
types: [opened]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
apply-label:
|
apply-label:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/github-script@0.6.0
|
- uses: actions/github-script@v3
|
||||||
with:
|
with:
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
script: |
|
script: |
|
||||||
@@ -85,7 +122,7 @@ jobs:
|
|||||||
welcome:
|
welcome:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/github-script@0.6.0
|
- uses: actions/github-script@v3
|
||||||
with:
|
with:
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
script: |
|
script: |
|
||||||
@@ -123,14 +160,13 @@ You can use the `github` object to access the Octokit API. For
|
|||||||
instance, `github.request`
|
instance, `github.request`
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
on:
|
on: pull_request
|
||||||
pull_request
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
diff:
|
diff:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/github-script@0.6.0
|
- uses: actions/github-script@v3
|
||||||
with:
|
with:
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
script: |
|
script: |
|
||||||
@@ -139,21 +175,274 @@ jobs:
|
|||||||
console.log(result)
|
console.log(result)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
_(Note that this particular example only works for a public URL, where the
|
||||||
|
diff URL is publicly accessible. Getting the diff for a private URL requires
|
||||||
|
using the API.)_
|
||||||
|
|
||||||
This will print the full diff object in the screen; `result.data` will
|
This will print the full diff object in the screen; `result.data` will
|
||||||
contain the actual diff text.
|
contain the actual diff text.
|
||||||
|
|
||||||
### Result encoding
|
### Run a separate file
|
||||||
|
|
||||||
|
If you don't want to inline your entire script that you want to run, you can
|
||||||
|
use a separate JavaScript module in your repository like so:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
on: push
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
echo-input:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/github-script@v2
|
||||||
|
with:
|
||||||
|
script: |
|
||||||
|
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
|
||||||
|
console.log(script({github, context}))
|
||||||
|
```
|
||||||
|
|
||||||
|
*Note that the script path given to `require()` must be an **absolute path** in this case, hence using [`GITHUB_WORKSPACE`](https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables).*
|
||||||
|
|
||||||
|
And then export a function from your module:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
module.exports = (github, context) => {
|
||||||
|
return context.payload.client_payload.value
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also use async functions in this manner, as long as you `await` it in
|
||||||
|
the inline script.
|
||||||
|
|
||||||
|
Note that because you can't `require` things like the GitHub context or
|
||||||
|
Actions Toolkit libraries, you'll want to pass them as arguments to your
|
||||||
|
external function.
|
||||||
|
|
||||||
|
Additionally, you'll want to use the [checkout
|
||||||
|
action](https://github.com/actions/checkout) to make sure your script file is
|
||||||
|
available.
|
||||||
|
|
||||||
|
|
||||||
|
This action makes it easy to quickly write a script in your workflow that
|
||||||
|
uses the GitHub API and the workflow run context.
|
||||||
|
|
||||||
|
In order to use this action, a `script` input is provided. The value of that
|
||||||
|
input should be the body of an asynchronous function call. The following
|
||||||
|
arguments will be provided:
|
||||||
|
|
||||||
|
- `github` A pre-authenticated
|
||||||
|
[octokit/rest.js](https://github.com/octokit/rest.js) client
|
||||||
|
- `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
|
||||||
|
- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package
|
||||||
|
|
||||||
|
Since the `script` is just a function body, these values will already be
|
||||||
|
defined, so you don't have to (see examples below).
|
||||||
|
|
||||||
|
See [octokit/rest.js](https://octokit.github.io/rest.js/) for the API client
|
||||||
|
documentation.
|
||||||
|
|
||||||
|
**Note** This action is still a bit of an experiment—the API may change in
|
||||||
|
future versions. 🙂
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
See [development.md](/docs/development.md).
|
||||||
|
|
||||||
|
## Reading step results
|
||||||
|
|
||||||
|
The return value of the script will be in the step's outputs under the
|
||||||
|
"result" key.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- uses: actions/github-script@v3
|
||||||
|
id: set-result
|
||||||
|
with:
|
||||||
|
script: return "Hello!"
|
||||||
|
result-encoding: string
|
||||||
|
- name: Get result
|
||||||
|
run: echo "${{steps.set-result.outputs.result}}"
|
||||||
|
```
|
||||||
|
|
||||||
|
See ["Result encoding"](#result-encoding) for details on how the encoding of
|
||||||
|
these outputs can be changed.
|
||||||
|
|
||||||
|
## Result encoding
|
||||||
|
|
||||||
By default, the JSON-encoded return value of the function is set as the "result" in the
|
By default, the JSON-encoded return value of the function is set as the "result" in the
|
||||||
output of a github-script step. For some workflows, string encoding is preferred. This option can be set using the
|
output of a github-script step. For some workflows, string encoding is preferred. This option can be set using the
|
||||||
`result-encoding` input:
|
`result-encoding` input:
|
||||||
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/github-script@0.6.0
|
- uses: actions/github-script@v3
|
||||||
|
id: my-script
|
||||||
with:
|
with:
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
result-encoding: string
|
result-encoding: string
|
||||||
script: |
|
script: return "I will be string (not JSON) encoded!"
|
||||||
return "I will be string (not JSON) encoded!"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
Note that `github-token` is optional in this action, and the input is there
|
||||||
|
in case you need to use a non-default token.
|
||||||
|
|
||||||
|
By default, github-script will use the token provided to your workflow.
|
||||||
|
|
||||||
|
### Comment on an issue
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
on:
|
||||||
|
issues:
|
||||||
|
types: [opened]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
comment:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/github-script@v3
|
||||||
|
with:
|
||||||
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
script: |
|
||||||
|
github.issues.createComment({
|
||||||
|
issue_number: context.issue.number,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
body: '👋 Thanks for reporting!'
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Apply a label to an issue
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
on:
|
||||||
|
issues:
|
||||||
|
types: [opened]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
apply-label:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/github-script@v3
|
||||||
|
with:
|
||||||
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
script: |
|
||||||
|
github.issues.addLabels({
|
||||||
|
issue_number: context.issue.number,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
labels: ['Triage']
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Welcome a first-time contributor
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
on: pull_request
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
welcome:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/github-script@v3
|
||||||
|
with:
|
||||||
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
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.issues.listForRepo.endpoint.merge({
|
||||||
|
...context.issue,
|
||||||
|
creator,
|
||||||
|
state: 'all'
|
||||||
|
})
|
||||||
|
const issues = await github.paginate(opts)
|
||||||
|
|
||||||
|
for (const issue of issues) {
|
||||||
|
if (issue.number === context.issue.number) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (issue.pull_request) {
|
||||||
|
return // Creator is already a contributor.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await github.issues.createComment({
|
||||||
|
issue_number: context.issue.number,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
body: 'Welcome, new contributor!'
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Download data from a URL
|
||||||
|
|
||||||
|
You can use the `github` object to access the Octokit API. For
|
||||||
|
instance, `github.request`
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
on: pull_request
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
diff:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/github-script@v3
|
||||||
|
with:
|
||||||
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
script: |
|
||||||
|
const diff_url = context.payload.pull_request.diff_url
|
||||||
|
const result = await github.request(diff_url)
|
||||||
|
console.log(result)
|
||||||
|
```
|
||||||
|
|
||||||
|
_(Note that this particular example only works for a public URL, where the
|
||||||
|
diff URL is publicly accessible. Getting the diff for a private URL requires
|
||||||
|
using the API.)_
|
||||||
|
|
||||||
|
This will print the full diff object in the screen; `result.data` will
|
||||||
|
contain the actual diff text.
|
||||||
|
|
||||||
|
### Run a separate file
|
||||||
|
|
||||||
|
If you don't want to inline your entire script that you want to run, you can
|
||||||
|
use a separate JavaScript module in your repository like so:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
on: push
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
echo-input:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/github-script@v2
|
||||||
|
with:
|
||||||
|
script: |
|
||||||
|
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
|
||||||
|
console.log(script({github, context}))
|
||||||
|
```
|
||||||
|
|
||||||
|
*Note that the script path given to `require()` must be an **absolute path** in this case, hence using [`GITHUB_WORKSPACE`](https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables).*
|
||||||
|
|
||||||
|
And then export a function from your module:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
module.exports = (github, context) => {
|
||||||
|
return context.payload.client_payload.value
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also use async functions in this manner, as long as you `await` it in
|
||||||
|
the inline script.
|
||||||
|
|
||||||
|
Note that because you can't `require` things like the GitHub context or
|
||||||
|
Actions Toolkit libraries, you'll want to pass them as arguments to your
|
||||||
|
external function.
|
||||||
|
|
||||||
|
Additionally, you'll want to use the [checkout
|
||||||
|
action](https://github.com/actions/checkout) to make sure your script file is
|
||||||
|
available.
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
|
||||||
import {callAsyncFunction} from '../src/async-function'
|
import {callAsyncFunction} from '../src/async-function'
|
||||||
|
|
||||||
describe('callAsyncFunction', () => {
|
describe('callAsyncFunction', () => {
|
||||||
test('calls the function with its arguments', async () => {
|
test('calls the function with its arguments', async () => {
|
||||||
const result = await callAsyncFunction({foo: 'bar'}, 'return foo')
|
const result = await callAsyncFunction({foo: 'bar'} as any, 'return foo')
|
||||||
expect(result).toEqual('bar')
|
expect(result).toEqual('bar')
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -10,17 +12,17 @@ describe('callAsyncFunction', () => {
|
|||||||
expect.assertions(1)
|
expect.assertions(1)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await callAsyncFunction({}, 'proces')
|
await callAsyncFunction({} as any, 'proces')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err).toBeInstanceOf(ReferenceError)
|
expect(err).toBeInstanceOf(ReferenceError)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test('can access process', async () => {
|
test('can access process', async () => {
|
||||||
await callAsyncFunction({}, 'process')
|
await callAsyncFunction({} as any, 'process')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('can access console', async () => {
|
test('can access console', async () => {
|
||||||
await callAsyncFunction({}, 'console')
|
await callAsyncFunction({} as any, 'console')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Vendored
+3239
-22060
File diff suppressed because it is too large.
Load diff
+3
-2
@@ -15,7 +15,8 @@ bash> npm run build
|
|||||||
|
|
||||||
It also has a pre-commit hook configured via
|
It also has a pre-commit hook configured via
|
||||||
[husky](https://www.npmjs.com/package/husky) that should run the build script
|
[husky](https://www.npmjs.com/package/husky) that should run the build script
|
||||||
before each commit.
|
before each commit. Additionally, this hook formats code and lints it, as
|
||||||
|
well.
|
||||||
|
|
||||||
## Releasing
|
## Releasing
|
||||||
|
|
||||||
@@ -24,4 +25,4 @@ Releases are done manually, for now:
|
|||||||
1. Ensure that the build is up to date with `npm run build`.
|
1. Ensure that the build is up to date with `npm run build`.
|
||||||
1. Bump the [package.json](/package.json#L3) and [package-lock.json](/package-lock.json#L3) version numbers and commit them.
|
1. Bump the [package.json](/package.json#L3) and [package-lock.json](/package-lock.json#L3) version numbers and commit them.
|
||||||
1. Update documentation (including updated version numbers).
|
1. Update documentation (including updated version numbers).
|
||||||
1. Tag master with the new version number and create a GitHub release.
|
1. Tag main with the new version number and create a GitHub release.
|
||||||
Generated
+3397
-1154
File diff suppressed because it is too large.
Load diff
+35
-19
@@ -1,23 +1,24 @@
|
|||||||
{
|
{
|
||||||
"name": "github-script",
|
"name": "github-script",
|
||||||
"description": "A GitHub action for executing a simple script",
|
"description": "A GitHub action for executing a simple script",
|
||||||
"version": "0.6.0",
|
"version": "3.0.0",
|
||||||
"author": "GitHub",
|
"author": "GitHub",
|
||||||
"dependencies": {
|
"license": "MIT",
|
||||||
"@actions/core": "^1.2.2",
|
"main": "dist/index.js",
|
||||||
"@actions/github": "^2.1.1"
|
"private": true,
|
||||||
},
|
"scripts": {
|
||||||
"devDependencies": {
|
"build": "ncc build src/main.ts",
|
||||||
"@types/jest": "^25.1.3",
|
"format:check": "prettier --check src __test__",
|
||||||
"@zeit/ncc": "^0.21.1",
|
"format:write": "prettier --write src __test__",
|
||||||
"husky": "^4.2.3",
|
"lint": "eslint src __test__",
|
||||||
"jest": "^25.1.0",
|
"style:check": "run-p --continue-on-error --aggregate-output format:check lint",
|
||||||
"ts-jest": "^25.2.1",
|
"style:write": "run-p --continue-on-error --aggregate-output format:write lint",
|
||||||
"typescript": "^3.8.2"
|
"pre-commit": "run-s style:write test build",
|
||||||
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"husky": {
|
"husky": {
|
||||||
"hooks": {
|
"hooks": {
|
||||||
"pre-commit": "npm run build && git add dist/"
|
"pre-commit": "npm run pre-commit && git add dist/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
@@ -33,11 +34,26 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"license": "MIT",
|
"dependencies": {
|
||||||
"main": "dist/index.js",
|
"@actions/core": "^1.2.4",
|
||||||
"private": true,
|
"@actions/github": "^4.0.0",
|
||||||
"scripts": {
|
"@actions/io": "^1.0.2",
|
||||||
"build": "ncc build src/main.ts",
|
"@octokit/core": "^3.1.2",
|
||||||
"test": "jest"
|
"@octokit/plugin-paginate-rest": "^2.3.0",
|
||||||
|
"@octokit/plugin-rest-endpoint-methods": "^4.1.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/jest": "^26.0.10",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^3.9.1",
|
||||||
|
"@typescript-eslint/parser": "^3.9.1",
|
||||||
|
"@vercel/ncc": "^0.23.0",
|
||||||
|
"eslint": "^7.7.0",
|
||||||
|
"eslint-config-prettier": "^6.11.0",
|
||||||
|
"husky": "^4.2.5",
|
||||||
|
"jest": "^26.4.1",
|
||||||
|
"npm-run-all": "^4.1.5",
|
||||||
|
"prettier": "^2.0.5",
|
||||||
|
"ts-jest": "^26.2.0",
|
||||||
|
"typescript": "^4.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+15
-4
@@ -1,11 +1,22 @@
|
|||||||
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
|
import * as core from '@actions/core'
|
||||||
|
import {Context} from '@actions/github/lib/context'
|
||||||
|
import {GitHub} from '@actions/github/lib/utils'
|
||||||
|
import * as io from '@actions/io'
|
||||||
|
|
||||||
type AsyncFunctionArguments = {[key: string]: any}
|
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor
|
||||||
|
|
||||||
export function callAsyncFunction(
|
type AsyncFunctionArguments = {
|
||||||
|
context: Context
|
||||||
|
core: typeof core
|
||||||
|
github: InstanceType<typeof GitHub>
|
||||||
|
io: typeof io
|
||||||
|
require: NodeRequire
|
||||||
|
}
|
||||||
|
|
||||||
|
export function callAsyncFunction<T>(
|
||||||
args: AsyncFunctionArguments,
|
args: AsyncFunctionArguments,
|
||||||
source: string
|
source: string
|
||||||
): Promise<any> {
|
): Promise<T> {
|
||||||
const fn = new AsyncFunction(...Object.keys(args), source)
|
const fn = new AsyncFunction(...Object.keys(args), source)
|
||||||
return fn(...Object.values(args))
|
return fn(...Object.values(args))
|
||||||
}
|
}
|
||||||
+18
-13
@@ -1,25 +1,34 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import {context, GitHub} from '@actions/github'
|
import {context, getOctokit} from '@actions/github'
|
||||||
|
import * as io from '@actions/io'
|
||||||
import {callAsyncFunction} from './async-function'
|
import {callAsyncFunction} from './async-function'
|
||||||
|
|
||||||
process.on('unhandledRejection', handleError)
|
process.on('unhandledRejection', handleError)
|
||||||
main().catch(handleError)
|
main().catch(handleError)
|
||||||
|
|
||||||
async function main() {
|
type Options = {
|
||||||
|
log?: Console
|
||||||
|
userAgent?: string
|
||||||
|
previews?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main(): Promise<void> {
|
||||||
const token = core.getInput('github-token', {required: true})
|
const token = core.getInput('github-token', {required: true})
|
||||||
const debug = core.getInput('debug')
|
const debug = core.getInput('debug')
|
||||||
const userAgent = core.getInput('user-agent')
|
const userAgent = core.getInput('user-agent')
|
||||||
const previews = core.getInput('previews')
|
const previews = core.getInput('previews')
|
||||||
const opts: {[key: string]: any} = {}
|
|
||||||
|
const opts: Options = {}
|
||||||
if (debug === 'true') opts.log = console
|
if (debug === 'true') opts.log = console
|
||||||
if (userAgent != null) opts.userAgent = userAgent
|
if (userAgent != null) opts.userAgent = userAgent
|
||||||
if (previews != null) opts.previews = previews.split(',')
|
if (previews != null) opts.previews = previews.split(',')
|
||||||
const github = new GitHub(token, opts)
|
|
||||||
|
const github = getOctokit(token, opts)
|
||||||
const script = core.getInput('script', {required: true})
|
const script = core.getInput('script', {required: true})
|
||||||
|
|
||||||
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors.
|
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
|
||||||
const result = await callAsyncFunction(
|
const result = await callAsyncFunction(
|
||||||
{require: require, github, context},
|
{require: require, github, context, core, io},
|
||||||
script
|
script
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,12 +51,8 @@ async function main() {
|
|||||||
core.setOutput('result', output)
|
core.setOutput('result', output)
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleError(err: any) {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
function handleError(err: any): void {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
core.setFailed(`Unhandled error: ${err}`)
|
||||||
if (err && err.message) {
|
|
||||||
core.setFailed(err.message)
|
|
||||||
} else {
|
|
||||||
core.setFailed(`Unhandled error: ${err}`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user