mirror of
https://github.com/actions/github-script.git
synced 2025-12-08 16:16:21 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 |
13
.eslintrc.yml
Normal file
13
.eslintrc.yml
Normal file
@@ -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
|
||||
1
.github/workflows/ci.yml
vendored
1
.github/workflows/ci.yml
vendored
@@ -15,4 +15,5 @@ jobs:
|
||||
key: ${{runner.os}}-npm-${{hashFiles('**/package-lock.json')}}
|
||||
restore-keys: ${{runner.os}}-npm-
|
||||
- run: npm ci
|
||||
- run: npm run style:check
|
||||
- run: npm test
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
node_modules
|
||||
/node_modules/
|
||||
!/.vscode/
|
||||
5
.prettierrc.yml
Normal file
5
.prettierrc.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
arrowParens: avoid
|
||||
bracketSpacing: false
|
||||
semi: false
|
||||
singleQuote: true
|
||||
trailingComma: none
|
||||
10
.vscode/settings.json
vendored
Normal file
10
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"editor.formatOnSave": true,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.organizeImports": true
|
||||
},
|
||||
"files.exclude": {
|
||||
"**/dist": true,
|
||||
"**/node_modules": true
|
||||
}
|
||||
}
|
||||
97
README.md
97
README.md
@@ -4,14 +4,15 @@ 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. Two arguments will
|
||||
be provided:
|
||||
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/tree/master/packages/github)
|
||||
- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/master/packages/core) package
|
||||
- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/master/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).
|
||||
@@ -26,6 +27,39 @@ future versions. 🙂
|
||||
|
||||
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@v1
|
||||
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@v1
|
||||
id: my-script
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
result-encoding: string
|
||||
script: return "I will be string (not JSON) encoded!"
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
Note that `github-token` is optional in this action, and the input is there
|
||||
@@ -37,13 +71,14 @@ By default, github-script will use the token provided to your workflow.
|
||||
|
||||
```yaml
|
||||
on:
|
||||
issues: {types: opened}
|
||||
issues:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
comment:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/github-script@0.9.0
|
||||
- uses: actions/github-script@v1
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
script: |
|
||||
@@ -59,13 +94,14 @@ jobs:
|
||||
|
||||
```yaml
|
||||
on:
|
||||
issues: {types: opened}
|
||||
issues:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
apply-label:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/github-script@0.9.0
|
||||
- uses: actions/github-script@v1
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
script: |
|
||||
@@ -86,7 +122,7 @@ jobs:
|
||||
welcome:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/github-script@0.9.0
|
||||
- uses: actions/github-script@v1
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
script: |
|
||||
@@ -130,7 +166,7 @@ jobs:
|
||||
diff:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/github-script@0.9.0
|
||||
- uses: actions/github-script@v1
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
script: |
|
||||
@@ -142,17 +178,42 @@ jobs:
|
||||
This will print the full diff object in the screen; `result.data` will
|
||||
contain the actual diff text.
|
||||
|
||||
### Result encoding
|
||||
### Run a separate file
|
||||
|
||||
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:
|
||||
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
|
||||
- uses: actions/github-script@0.9.0
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
result-encoding: string
|
||||
script: |
|
||||
return "I will be string (not JSON) encoded!"
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
echo-input:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: @actions/checkout@v2
|
||||
- uses: @actions/github-script@v1
|
||||
with:
|
||||
script: |
|
||||
const path = require('path')
|
||||
const scriptPath = path.resolve('./path/to/script.js')
|
||||
console.log(require(scriptPath)({context}))
|
||||
```
|
||||
|
||||
And then export a function from your module:
|
||||
|
||||
```javascript
|
||||
module.exports = ({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'
|
||||
|
||||
describe('callAsyncFunction', () => {
|
||||
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')
|
||||
})
|
||||
|
||||
@@ -10,17 +12,17 @@ describe('callAsyncFunction', () => {
|
||||
expect.assertions(1)
|
||||
|
||||
try {
|
||||
await callAsyncFunction({}, 'proces')
|
||||
await callAsyncFunction({} as any, 'proces')
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(ReferenceError)
|
||||
}
|
||||
})
|
||||
|
||||
test('can access process', async () => {
|
||||
await callAsyncFunction({}, 'process')
|
||||
await callAsyncFunction({} as any, 'process')
|
||||
})
|
||||
|
||||
test('can access console', async () => {
|
||||
await callAsyncFunction({}, 'console')
|
||||
await callAsyncFunction({} as any, 'console')
|
||||
})
|
||||
})
|
||||
|
||||
890
dist/index.js
vendored
890
dist/index.js
vendored
File diff suppressed because it is too large
Load Diff
@@ -15,7 +15,8 @@ bash> npm run build
|
||||
|
||||
It also has a pre-commit hook configured via
|
||||
[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
|
||||
|
||||
|
||||
1054
package-lock.json
generated
1054
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
51
package.json
51
package.json
@@ -1,23 +1,24 @@
|
||||
{
|
||||
"name": "github-script",
|
||||
"description": "A GitHub action for executing a simple script",
|
||||
"version": "0.9.0",
|
||||
"version": "1.1.0",
|
||||
"author": "GitHub",
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.2.3",
|
||||
"@actions/github": "^2.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^25.1.4",
|
||||
"@zeit/ncc": "^0.22.0",
|
||||
"husky": "^4.2.3",
|
||||
"jest": "^25.1.0",
|
||||
"ts-jest": "^25.2.1",
|
||||
"typescript": "^3.8.3"
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "ncc build src/main.ts",
|
||||
"format:check": "prettier --check src __test__",
|
||||
"format:write": "prettier --write src __test__",
|
||||
"lint": "eslint src __test__",
|
||||
"style:check": "run-p --continue-on-error --aggregate-output format:check lint",
|
||||
"style:write": "run-p --continue-on-error --aggregate-output format:write lint",
|
||||
"pre-commit": "run-s style:write test build",
|
||||
"test": "jest"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "npm run build && git add dist/"
|
||||
"pre-commit": "npm run pre-commit && git add dist/"
|
||||
}
|
||||
},
|
||||
"jest": {
|
||||
@@ -33,11 +34,23 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "ncc build src/main.ts",
|
||||
"test": "jest"
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.2.4",
|
||||
"@actions/github": "^2.2.0",
|
||||
"@actions/io": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^25.1.4",
|
||||
"@typescript-eslint/eslint-plugin": "^2.33.0",
|
||||
"@typescript-eslint/parser": "^2.33.0",
|
||||
"@zeit/ncc": "^0.22.0",
|
||||
"eslint": "^7.0.0",
|
||||
"eslint-config-prettier": "^6.11.0",
|
||||
"husky": "^4.2.5",
|
||||
"jest": "^25.1.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.0.5",
|
||||
"ts-jest": "^25.2.1",
|
||||
"typescript": "^3.8.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,22 @@
|
||||
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
|
||||
import * as core from '@actions/core'
|
||||
import {GitHub} from '@actions/github'
|
||||
import {Context} from '@actions/github/lib/context'
|
||||
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: GitHub
|
||||
io: typeof io
|
||||
require: NodeRequire
|
||||
}
|
||||
|
||||
export function callAsyncFunction<T>(
|
||||
args: AsyncFunctionArguments,
|
||||
source: string
|
||||
): Promise<any> {
|
||||
): Promise<T> {
|
||||
const fn = new AsyncFunction(...Object.keys(args), source)
|
||||
return fn(...Object.values(args))
|
||||
}
|
||||
|
||||
27
src/main.ts
27
src/main.ts
@@ -1,25 +1,34 @@
|
||||
import * as core from '@actions/core'
|
||||
import {context, GitHub} from '@actions/github'
|
||||
import * as io from '@actions/io'
|
||||
import {callAsyncFunction} from './async-function'
|
||||
|
||||
process.on('unhandledRejection', 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 debug = core.getInput('debug')
|
||||
const userAgent = core.getInput('user-agent')
|
||||
const previews = core.getInput('previews')
|
||||
const opts: {[key: string]: any} = {}
|
||||
|
||||
const opts: Options = {}
|
||||
if (debug === 'true') opts.log = console
|
||||
if (userAgent != null) opts.userAgent = userAgent
|
||||
if (previews != null) opts.previews = previews.split(',')
|
||||
|
||||
const github = new GitHub(token, opts)
|
||||
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(
|
||||
{require: require, github, context, core},
|
||||
{require: require, github, context, core, io},
|
||||
script
|
||||
)
|
||||
|
||||
@@ -42,12 +51,8 @@ async function main() {
|
||||
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)
|
||||
|
||||
if (err && err.message) {
|
||||
core.setFailed(err.message)
|
||||
} else {
|
||||
core.setFailed(`Unhandled error: ${err}`)
|
||||
}
|
||||
core.setFailed(`Unhandled error: ${err}`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user