Compare commits

...

58 Commits

Author SHA1 Message Date
Jonathan Clem
c20b4a79d3 1.0.1 2020-05-18 14:53:09 -04:00
Jonathan Clem
048309c447 Bump to v1 2020-05-18 14:32:21 -04:00
Jonathan Clem
a8704b62a3 Add note about checkout to separate file docs 2020-05-18 12:59:15 -04:00
Jonathan Clem
e16145c72e Merge pull request #50 from actions/readme-update
Add step results section to readme
2020-05-18 12:55:07 -04:00
Jonathan Clem
dd16c14e71 Move another section in the wrong place 2020-05-18 12:54:07 -04:00
Jonathan Clem
5d33ffc89d Fix step ID in output example 2020-05-18 12:53:18 -04:00
Jonathan Clem
adb3d5168d Fix section not moved 2020-05-18 12:52:37 -04:00
Jonathan Clem
181dcc219c Add step results section to readme 2020-05-18 12:51:39 -04:00
Jonathan Clem
6f0504cb03 Merge pull request #45 from tevoinea/patch-1
Add example on how to get the step result
2020-05-18 12:48:21 -04:00
Jonathan Clem
d89db5b6f4 s/script/module 2020-05-18 12:41:53 -04:00
Jonathan Clem
52110c52e9 Add a separate file example 2020-05-18 12:38:49 -04:00
Jonathan Clem
f498913621 Merge pull request #48 from actions/code-quality
Code quality improvements
2020-05-18 11:34:07 -04:00
Jonathan Clem
58f0ff84d6 Add style:check to ci workflow 2020-05-18 11:33:04 -04:00
Jonathan Clem
3037861304 Add ESLint and Prettier 2020-05-18 11:28:54 -04:00
Jonathan Clem
b945d091bf Check in .vscode 2020-05-18 11:27:26 -04:00
Jonathan Clem
ca14121875 Merge pull request #47 from actions/users/paquirk/ghesfixes
Bumping actions/core and actions/github dependencies for GHES
2020-05-14 18:08:58 -04:00
PJ Quirk
ca6d0aaa59 Fixing exports in generated file 2020-05-14 18:05:57 -04:00
PJ Quirk
5d879b69aa Revert changes to tsc/ncc 2020-05-14 17:16:50 -04:00
Jonathan Clem
1bc9cbef6c Merge pull request #46 from nschonni/patch-1
fix: Example YAML schema to match Actions Docs
2020-05-14 16:22:01 -04:00
PJ Quirk
9a58186a54 Combine build/pack 2020-05-12 15:54:30 -04:00
PJ Quirk
8934ce0ffe Fix pack step and re-run 2020-05-12 15:20:35 -04:00
PJ Quirk
05997a2463 Fix build steps 2020-05-12 15:09:19 -04:00
PJ Quirk
1bc2687309 Bump package versions 2020-05-12 15:08:00 -04:00
Nick Schonning
97fd3f1973 fix: Example YAML schema to match Actions Docs
Both ways should be valid, but the regular nested YAML keys seems to be the format of the regular GitHub Actions docs
2020-05-09 13:09:30 -04:00
Teo Voinea
f8e6050e29 Add example on how to get the step result 2020-05-07 18:19:02 -07:00
Jonathan Clem
648bc46b8a Merge pull request #37 from localheinz/fix/three
Fix: README.md
2020-04-20 12:25:29 -04:00
Andreas Möller
1268370776 Fix: README.md 2020-03-27 12:38:18 +01:00
Jonathan Clem
5d03ada4b0 0.9.0 2020-03-24 15:14:05 -04:00
Jonathan Clem
293ee0ca2d Uninstall/reinstall ts-jest 2020-03-24 15:07:44 -04:00
Jonathan Clem
67d29bab15 Merge pull request #36 from actions/bump-dependencies
Uninstall/reinstall Jest (for minimist update)
2020-03-24 15:00:02 -04:00
Jonathan Clem
0baa1791ca Merge pull request #35 from actions/bump-dependencies
Bump outdated npm packages
2020-03-24 14:58:02 -04:00
Jonathan Clem
b4fd05ccdf Uninstall/reinstall Jest (for minimist update) 2020-03-24 14:57:31 -04:00
Jonathan Clem
2ccd1edaf4 Update ncc 2020-03-24 14:38:07 -04:00
Jonathan Clem
a59996ab35 Update TypeScript 2020-03-24 14:37:22 -04:00
Jonathan Clem
7ca8635cd0 Bump @types/jest 2020-03-24 14:36:26 -04:00
Jonathan Clem
8117de6119 Bump @actions/core 2020-03-24 14:35:48 -04:00
Jonathan Clem
3f4f5a83e8 Merge pull request #34 from actions/dependabot/npm_and_yarn/acorn-6.4.1
Bump acorn from 6.4.0 to 6.4.1
2020-03-24 14:14:58 -04:00
dependabot[bot]
0e2c0d5c7c Bump acorn from 6.4.0 to 6.4.1
Bumps [acorn](https://github.com/acornjs/acorn) from 6.4.0 to 6.4.1.
- [Release notes](https://github.com/acornjs/acorn/releases)
- [Commits](https://github.com/acornjs/acorn/compare/6.4.0...6.4.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-03-24 18:11:43 +00:00
Jonathan Clem
0d15461bee Merge pull request #33 from actions/add-core
Add core to execution context
2020-03-24 14:08:04 -04:00
Jonathan Clem
e4cc5a8d47 Add core to README 2020-03-24 14:06:45 -04:00
Jonathan Clem
dc16f26602 Add core to integration test 2020-03-24 14:02:45 -04:00
Jonathan Clem
8982156783 Add core to the script context 2020-03-24 13:57:27 -04:00
Jonathan Clem
80a5e943b4 v0.8.0 2020-02-28 09:18:07 -05:00
Jonathan Clem
0cb5c91bd3 Update README.md 2020-02-28 09:12:55 -05:00
Jonathan Clem
493b839630 Remove output log 2020-02-28 09:09:06 -05:00
Jonathan Clem
904b439f45 Set output 2020-02-28 09:07:52 -05:00
Jonathan Clem
1e169ae445 Remove result temporarily 2020-02-28 09:06:03 -05:00
Jonathan Clem
62fdca610c Remove log result 2020-02-28 08:54:30 -05:00
Jonathan Clem
26ee3d2d09 Test log result 2020-02-28 08:52:13 -05:00
Jonathan Clem
be5d094bf9 Remove async from callAsyncFunction 2020-02-28 08:51:05 -05:00
Jonathan Clem
3758c2b05b Add result log 2020-02-28 08:50:21 -05:00
Jonathan Clem
317a0746d1 Remove core again for now 2020-02-28 08:49:25 -05:00
Jonathan Clem
ec171b8961 Use master 2020-02-27 17:53:27 -05:00
Jonathan Clem
d73e75dea8 Separate integration workflow 2020-02-27 17:52:43 -05:00
Jonathan Clem
7afad1e364 Add core 2020-02-27 17:43:18 -05:00
Jonathan Clem
ebe3cb13e0 Add some test workflows 2020-02-27 17:39:50 -05:00
Jonathan Clem
38e3ffe4c6 Add tests for the AsyncFunction 2020-02-27 17:27:49 -05:00
Jonathan Clem
6eefe48bc9 Update @actions NPM dependencies 2020-02-27 16:52:48 -05:00
15 changed files with 20681 additions and 785 deletions

13
.eslintrc.yml Normal file
View 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

19
.github/workflows/ci.yml vendored Normal file
View File

@@ -0,0 +1,19 @@
on:
push: {branches: master}
pull_request: {branches: master}
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with: {node-version: 13.x}
- uses: actions/cache@v1
with:
path: ~/.npm
key: ${{runner.os}}-npm-${{hashFiles('**/package-lock.json')}}
restore-keys: ${{runner.os}}-npm-
- run: npm ci
- run: npm run style:check
- run: npm test

17
.github/workflows/integration.yml vendored Normal file
View File

@@ -0,0 +1,17 @@
on:
push: {branches: master}
jobs:
integration:
runs-on: ubuntu-latest
steps:
- id: output-set
uses: actions/github-script@master
with:
script: return core.getInput('input-value')
result-encoding: string
input-value: output
- run: |
if [[ "${{steps.output-set.outputs.result}}" != "output" ]]; then
exit 1
fi

3
.gitignore vendored
View File

@@ -1 +1,2 @@
node_modules
/node_modules/
!/.vscode/

5
.prettierrc.yml Normal file
View File

@@ -0,0 +1,5 @@
arrowParens: avoid
bracketSpacing: false
semi: false
singleQuote: true
trailingComma: none

10
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"files.exclude": {
"**/dist": true,
"**/node_modules": true
}
}

105
README.md
View File

@@ -1,16 +1,17 @@
# github-script
# github-script ![.github/workflows/integration.yml](https://github.com/actions/github-script/workflows/.github/workflows/integration.yml/badge.svg?event=push) ![.github/workflows/ci.yml](https://github.com/actions/github-script/workflows/.github/workflows/ci.yml/badge.svg?event=push)
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
Since the `script` is just a function body, these values will already be
defined, so you don't have to (see examples below).
@@ -19,12 +20,45 @@ 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. 🙂
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@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
@@ -36,13 +70,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.6.0
- uses: actions/github-script@v1
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
@@ -58,13 +93,14 @@ jobs:
```yaml
on:
issues: {types: opened}
issues:
types: [opened]
jobs:
apply-label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@0.6.0
- uses: actions/github-script@v1
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
@@ -85,7 +121,7 @@ jobs:
welcome:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@0.6.0
- uses: actions/github-script@v1
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
@@ -123,14 +159,13 @@ You can use the `github` object to access the Octokit API. For
instance, `github.request`
```yaml
on:
pull_request
on: pull_request
jobs:
diff:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@0.6.0
- uses: actions/github-script@v1
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
@@ -142,18 +177,42 @@ jobs:
This will print the full diff object in the screen; `result.data` will
contain the actual diff text.
### 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:
### 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
- uses: actions/github-script@0.6.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.

View File

@@ -0,0 +1,28 @@
/* 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'} as any, 'return foo')
expect(result).toEqual('bar')
})
test('throws on ReferenceError', async () => {
expect.assertions(1)
try {
await callAsyncFunction({} as any, 'proces')
} catch (err) {
expect(err).toBeInstanceOf(ReferenceError)
}
})
test('can access process', async () => {
await callAsyncFunction({} as any, 'process')
})
test('can access console', async () => {
await callAsyncFunction({} as any, 'console')
})
})

15457
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -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

5686
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +1,55 @@
{
"name": "github-script",
"version": "0.6.0",
"private": true,
"main": "dist/index.js",
"scripts": {
"build": "ncc build src/main.ts"
},
"description": "A GitHub action for executing a simple script",
"version": "1.0.1",
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.1.0",
"@actions/github": "^1.1.0"
},
"devDependencies": {
"@zeit/ncc": "^0.20.5",
"husky": "^4.0.1"
"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": {
"preset": "ts-jest",
"testEnvironment": "node",
"globals": {
"ts-jest": {
"diagnostics": {
"ignoreCodes": [
"151001"
]
}
}
}
},
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/github": "^2.2.0"
},
"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"
}
}

20
src/async-function.ts Normal file
View File

@@ -0,0 +1,20 @@
import * as core from '@actions/core'
import {GitHub} from '@actions/github'
import {Context} from '@actions/github/lib/context'
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor
type AsyncFunctionArguments = {
context: Context
core: typeof core
github: GitHub
require: NodeRequire
}
export function callAsyncFunction<T>(
args: AsyncFunctionArguments,
source: string
): Promise<T> {
const fn = new AsyncFunction(...Object.keys(args), source)
return fn(...Object.values(args))
}

View File

@@ -1,23 +1,35 @@
import * as core from '@actions/core'
import {context, GitHub} from '@actions/github'
import {callAsyncFunction} from './async-function'
process.on('unhandledRejection', handleError)
main().catch(handleError)
async function main() {
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
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 client = new GitHub(token, opts)
const github = new GitHub(token, opts)
const script = core.getInput('script', {required: true})
const fn = new AsyncFunction('require', 'github', 'context', script)
const result = await fn(require, client, context)
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
const result = await callAsyncFunction(
{require: require, github, context, core},
script
)
let encoding = core.getInput('result-encoding')
encoding = encoding ? encoding : 'json'
@@ -38,12 +50,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}`)
}

View File

@@ -1,8 +1,9 @@
{
"compilerOptions": {
"target": "es2018",
"target": "es2018",
"moduleResolution": "node",
"strict": true,
"strict": true,
"forceConsistentCasingInFileNames": true
}
},
"exclude": ["__test__"]
}