Compare commits

..

31 Commits

Author SHA1 Message Date
Jonathan Clem
95fb649573 Merge pull request #135 from actions/wrap-require
Support installed npm modules and relative require
2021-04-21 17:50:55 -04:00
Jonathan Clem
2923e50f29 Run build script 2021-04-21 17:49:15 -04:00
Jonathan Clem
b616178d6d Merge branch 'wrap-require' of https://github.com/actions/github-script into wrap-require 2021-04-21 17:48:41 -04:00
Jonathan Clem
c758586ea1 v4.0.0 2021-04-21 17:48:25 -04:00
Jonathan Clem
256da4ea4d Update src/wrap-require.ts
Co-authored-by: Josh Gross <joshmgross@github.com>
2021-04-21 17:46:09 -04:00
Jonathan Clem
5ee517dae8 Workflow syntax error 2021-04-21 17:43:39 -04:00
Jonathan Clem
fb7e8f7c67 Add npm ci to integration test 2021-04-21 17:43:02 -04:00
Jonathan Clem
4a93ad9f9e Add actions/checkout 2021-04-21 17:41:41 -04:00
Jonathan Clem
19e7914023 Add integration test running 2021-04-21 17:39:54 -04:00
Jonathan Clem
3ede58996d Add new integration tests 2021-04-21 17:37:37 -04:00
Jonathan Clem
01f87b6c01 Remove caveat about node_modules 2021-04-21 17:21:31 -04:00
Jonathan Clem
b0e12e725b Add Webpack comment vis-a-vis eval 2021-04-21 17:12:33 -04:00
Jonathan Clem
7e12bd7395 Call resolve, then require 2021-04-21 17:10:11 -04:00
Jonathan Clem
7e8659dcd4 Run build 2021-04-21 17:06:44 -04:00
Jonathan Clem
d37f92ff11 Test eval 2021-04-21 17:06:36 -04:00
Jonathan Clem
1f8fc98741 Run build 2021-04-21 17:05:03 -04:00
Jonathan Clem
f57c84a8e8 Try global.module 2021-04-21 17:04:41 -04:00
Jonathan Clem
ade5cea985 Use concat for paths 2021-04-21 17:02:53 -04:00
Jonathan Clem
c1c139b0ab Add a catch to support requiring installed modules 2021-04-21 16:58:37 -04:00
Jonathan Clem
3ca4cd5a00 Update documentation for relative require support 2021-04-21 16:48:53 -04:00
Jonathan Clem
83c92d6511 Rename nativeRequire to __original_require__ 2021-04-21 16:41:31 -04:00
Jonathan Clem
c416f56b51 Pass nativeRequire, as well 2021-04-21 16:40:48 -04:00
Jonathan Clem
75e3a5b35d Move wrapRequire to its own module 2021-04-21 16:37:24 -04:00
Jonathan Clem
e853490b13 Run build script 2021-04-21 16:35:28 -04:00
Jonathan Clem
f4e5d39c2a Use a proxy to support relative requires 2021-04-21 16:32:28 -04:00
Jonathan Clem
ddba1b195d Use non-Webpack-require in evaluated scripts 2021-04-21 16:16:20 -04:00
Josh Gross
59cb74c2ee Merge pull request #125 from karlhorky/patch-1
Link to better octokit docs
2021-04-16 12:00:11 -04:00
Josh Gross
5467f2a1ed Merge pull request #131 from esker-software/Update-readme-with-async-example
Update readme with async example
2021-04-15 12:18:40 -04:00
Léandre DA SILVA
c72dc00003 Update readme with async example 2021-04-09 17:12:13 +02:00
Karl Horky
bb407510e7 Remove version number to default to latest version 2021-03-24 18:20:16 +01:00
Karl Horky
3673bc4c4f Link to better octokit docs 2021-03-24 18:13:28 +01:00
9 changed files with 312 additions and 2226 deletions

View File

@@ -2,13 +2,15 @@ name: Integration
on: on:
push: {branches: main} push: {branches: main}
pull_request: {branches: main}
jobs: jobs:
integration: test-return:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2
- id: output-set - id: output-set
uses: actions/github-script@main uses: ./
with: with:
script: return core.getInput('input-value') script: return core.getInput('input-value')
result-encoding: string result-encoding: string
@@ -17,3 +19,39 @@ jobs:
if [[ "${{steps.output-set.outputs.result}}" != "output" ]]; then if [[ "${{steps.output-set.outputs.result}}" != "output" ]]; then
exit 1 exit 1
fi fi
test-relative-require:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- id: output-set
uses: ./
with:
script: return require('./package.json').name
result-encoding: string
input-value: output
- run: |
if [[ "${{steps.output-set.outputs.result}}" != "github-script" ]]; then
exit 1
fi
test-npm-require:
runs-on: ubuntu-latest
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
- id: output-set
uses: ./
with:
script: return require('@actions/core/package.json').name
result-encoding: string
input-value: output
- run: |
if [[ "${{steps.output-set.outputs.result}}" != "@actions/core" ]]; then
exit 1
fi

View File

@@ -12,12 +12,18 @@ input should be the body of an asynchronous function call. The following
arguments will be provided: arguments will be provided:
- `github` A pre-authenticated - `github` A pre-authenticated
[octokit/core.js](https://github.com/octokit/core.js#readme) client with REST endpoints and pagination plugins [octokit/rest.js](https://octokit.github.io/rest.js) client with pagination plugins
- `context` An object containing the [context of the workflow - `context` An object containing the [context of the workflow
run](https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts) 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 - `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 - `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 - `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package
- `require` A proxy wrapper around the normal Node.js `require` to enable
requiring relative paths (relative to the current working directory) and
requiring npm packages installed in the current working directory. If for
some reason you need the non-wrapped `require`, there is an escape hatch
available: `__original_require__` is the original value of `require` without
our wrapping applied.
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).
@@ -38,7 +44,7 @@ The return value of the script will be in the step's outputs under the
"result" key. "result" key.
```yaml ```yaml
- uses: actions/github-script@v3 - uses: actions/github-script@v4
id: set-result id: set-result
with: with:
script: return "Hello!" script: return "Hello!"
@@ -57,7 +63,7 @@ output of a github-script step. For some workflows, string encoding is preferred
`result-encoding` input: `result-encoding` input:
```yaml ```yaml
- uses: actions/github-script@v3 - uses: actions/github-script@v4
id: my-script id: my-script
with: with:
github-token: ${{secrets.GITHUB_TOKEN}} github-token: ${{secrets.GITHUB_TOKEN}}
@@ -76,7 +82,7 @@ By default, github-script will use the token provided to your workflow.
```yaml ```yaml
- name: View context attributes - name: View context attributes
uses: actions/github-script@v3 uses: actions/github-script@v4
with: with:
script: console.log(context) script: console.log(context)
``` ```
@@ -92,7 +98,7 @@ jobs:
comment: comment:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/github-script@v3 - uses: actions/github-script@v4
with: with:
github-token: ${{secrets.GITHUB_TOKEN}} github-token: ${{secrets.GITHUB_TOKEN}}
script: | script: |
@@ -115,7 +121,7 @@ jobs:
apply-label: apply-label:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/github-script@v3 - uses: actions/github-script@v4
with: with:
github-token: ${{secrets.GITHUB_TOKEN}} github-token: ${{secrets.GITHUB_TOKEN}}
script: | script: |
@@ -136,7 +142,7 @@ jobs:
welcome: welcome:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/github-script@v3 - uses: actions/github-script@v4
with: with:
github-token: ${{secrets.GITHUB_TOKEN}} github-token: ${{secrets.GITHUB_TOKEN}}
script: | script: |
@@ -180,7 +186,7 @@ jobs:
diff: diff:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/github-script@v3 - uses: actions/github-script@v4
with: with:
github-token: ${{secrets.GITHUB_TOKEN}} github-token: ${{secrets.GITHUB_TOKEN}}
script: | script: |
@@ -205,7 +211,7 @@ jobs:
list-issues: list-issues:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/github-script@v3 - uses: actions/github-script@v4
with: with:
github-token: ${{secrets.GITHUB_TOKEN}} github-token: ${{secrets.GITHUB_TOKEN}}
script: | script: |
@@ -240,15 +246,13 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions/github-script@v3 - uses: actions/github-script@v4
with: with:
script: | script: |
const script = require(`${process.env.GITHUB_WORKSPACE}/path/to/script.js`) const script = require('./path/to/script.js')
console.log(script({github, context})) 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: And then export a function from your module:
```javascript ```javascript
@@ -257,9 +261,6 @@ module.exports = ({github, context}) => {
} }
``` ```
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 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 Actions Toolkit libraries, you'll want to pass them as arguments to your
external function. external function.
@@ -268,6 +269,44 @@ Additionally, you'll want to use the [checkout
action](https://github.com/actions/checkout) to make sure your script file is action](https://github.com/actions/checkout) to make sure your script file is
available. available.
### Run a separate file with an async function
You can also use async functions in this manner, as long as you `await` it in
the inline script.
In your workflow:
```yaml
on: push
jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/github-script@v4
env:
SHA: '${{env.parentSHA}}'
with:
script: |
const script = require('./path/to/script.js')
await script({github, context, core})
```
And then export an async function from your module:
```javascript
module.exports = async ({github, context, core}) => {
const {SHA} = process.env
const commit = await github.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `${SHA}`
})
core.exportVariable('author', commit.data.commit.author.email)
}
```
### Use npm packages ### Use npm packages
Like importing your own files above, you can also use installed modules: Like importing your own files above, you can also use installed modules:
@@ -286,10 +325,10 @@ jobs:
- run: npm ci - run: npm ci
# or one-off: # or one-off:
- run: npm install execa - run: npm install execa
- uses: actions/github-script@v3 - uses: actions/github-script@v4
with: with:
script: | script: |
const execa = require(`${process.env.GITHUB_WORKSPACE}/node_modules/execa`) const execa = require('execa')
const { stdout } = await execa('echo', ['hello', 'world']) const { stdout } = await execa('echo', ['hello', 'world'])
@@ -307,7 +346,7 @@ jobs:
echo-input: echo-input:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/github-script@v3 - uses: actions/github-script@v4
env: env:
FIRST_NAME: Mona FIRST_NAME: Mona
LAST_NAME: Octocat LAST_NAME: Octocat

2318
dist/index.js vendored

File diff suppressed because it is too large Load Diff

55
package-lock.json generated
View File

@@ -5,11 +5,10 @@
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "github-script",
"version": "3.1.1", "version": "3.1.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/core": "^1.10.0", "@actions/core": "^1.2.6",
"@actions/github": "^4.0.0", "@actions/github": "^4.0.0",
"@actions/glob": "^0.1.1", "@actions/glob": "^0.1.1",
"@actions/io": "^1.0.2", "@actions/io": "^1.0.2",
@@ -33,21 +32,9 @@
} }
}, },
"node_modules/@actions/core": { "node_modules/@actions/core": {
"version": "1.10.0", "version": "1.2.6",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.0.tgz", "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.6.tgz",
"integrity": "sha512-2aZDDa3zrrZbP5ZYg159sNoLRb61nQ7awl5pSvIq5Qpj81vwDzdMRKzkWJGJuwVvWpvZKx7vspJALyvaaIQyug==", "integrity": "sha512-ZQYitnqiyBc3D+k7LsgSBmMDVkOVidaagDG7j3fOym77jNunWRuYx7VSHa9GNfFZh+zh61xsCjRj4JxMZlDqTA=="
"dependencies": {
"@actions/http-client": "^2.0.1",
"uuid": "^8.3.2"
}
},
"node_modules/@actions/core/node_modules/@actions/http-client": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.1.0.tgz",
"integrity": "sha512-BonhODnXr3amchh4qkmjPMUO8mFi/zLaaCeCAJZqch8iQqyDnVIkySjB38VHAC8IJ+bnlgfOqlhpyCUZHlQsqw==",
"dependencies": {
"tunnel": "^0.0.6"
}
}, },
"node_modules/@actions/github": { "node_modules/@actions/github": {
"version": "4.0.0", "version": "4.0.0",
@@ -8744,9 +8731,10 @@
} }
}, },
"node_modules/uuid": { "node_modules/uuid": {
"version": "8.3.2", "version": "8.3.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.0.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", "integrity": "sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ==",
"dev": true,
"bin": { "bin": {
"uuid": "dist/bin/uuid" "uuid": "dist/bin/uuid"
} }
@@ -9095,23 +9083,9 @@
}, },
"dependencies": { "dependencies": {
"@actions/core": { "@actions/core": {
"version": "1.10.0", "version": "1.2.6",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.0.tgz", "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.6.tgz",
"integrity": "sha512-2aZDDa3zrrZbP5ZYg159sNoLRb61nQ7awl5pSvIq5Qpj81vwDzdMRKzkWJGJuwVvWpvZKx7vspJALyvaaIQyug==", "integrity": "sha512-ZQYitnqiyBc3D+k7LsgSBmMDVkOVidaagDG7j3fOym77jNunWRuYx7VSHa9GNfFZh+zh61xsCjRj4JxMZlDqTA=="
"requires": {
"@actions/http-client": "^2.0.1",
"uuid": "^8.3.2"
},
"dependencies": {
"@actions/http-client": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.1.0.tgz",
"integrity": "sha512-BonhODnXr3amchh4qkmjPMUO8mFi/zLaaCeCAJZqch8iQqyDnVIkySjB38VHAC8IJ+bnlgfOqlhpyCUZHlQsqw==",
"requires": {
"tunnel": "^0.0.6"
}
}
}
}, },
"@actions/github": { "@actions/github": {
"version": "4.0.0", "version": "4.0.0",
@@ -16329,9 +16303,10 @@
"dev": true "dev": true
}, },
"uuid": { "uuid": {
"version": "8.3.2", "version": "8.3.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.0.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" "integrity": "sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ==",
"dev": true
}, },
"v8-compile-cache": { "v8-compile-cache": {
"version": "2.1.1", "version": "2.1.1",

View File

@@ -1,7 +1,7 @@
{ {
"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": "3.2.0", "version": "4.0.0",
"author": "GitHub", "author": "GitHub",
"license": "MIT", "license": "MIT",
"main": "dist/index.js", "main": "dist/index.js",
@@ -35,7 +35,7 @@
} }
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.10.0", "@actions/core": "^1.2.6",
"@actions/github": "^4.0.0", "@actions/github": "^4.0.0",
"@actions/glob": "^0.1.1", "@actions/glob": "^0.1.1",
"@actions/io": "^1.0.2", "@actions/io": "^1.0.2",

View File

@@ -13,6 +13,7 @@ type AsyncFunctionArguments = {
glob: typeof glob glob: typeof glob
io: typeof io io: typeof io
require: NodeRequire require: NodeRequire
__original_require__: NodeRequire
} }
export function callAsyncFunction<T>( export function callAsyncFunction<T>(

View File

@@ -3,6 +3,7 @@ import {context, getOctokit} from '@actions/github'
import * as glob from '@actions/glob' import * as glob from '@actions/glob'
import * as io from '@actions/io' import * as io from '@actions/io'
import {callAsyncFunction} from './async-function' import {callAsyncFunction} from './async-function'
import {wrapRequire} from './wrap-require'
process.on('unhandledRejection', handleError) process.on('unhandledRejection', handleError)
main().catch(handleError) main().catch(handleError)
@@ -29,7 +30,15 @@ async function main(): Promise<void> {
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation 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, core, glob, io}, {
require: wrapRequire,
__original_require__: __non_webpack_require__,
github,
context,
core,
glob,
io
},
script script
) )

29
src/wrap-require.ts Normal file
View File

@@ -0,0 +1,29 @@
import * as path from 'path'
export const wrapRequire = new Proxy(__non_webpack_require__, {
apply: (target, thisArg, [moduleID]) => {
if (moduleID.startsWith('.')) {
moduleID = path.resolve(moduleID)
return target.apply(thisArg, [moduleID])
}
try {
return target.apply(thisArg, [moduleID])
} catch (err) {
const modulePath = target.resolve.apply(thisArg, [
moduleID,
{
// Webpack does not have an escape hatch for getting the actual
// module, other than `eval`.
paths: eval('module').paths.concat(process.cwd())
}
])
return target.apply(thisArg, [modulePath])
}
},
get: (target, prop, receiver) => {
Reflect.get(target, prop, receiver)
}
})

View File

@@ -0,0 +1 @@
declare const __non_webpack_require__: NodeRequire