diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index b43f4e766..c95eaa505 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -5,10 +5,11 @@ jobs: integration: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v2 - id: output-set - uses: actions/github-script@master + uses: lukka/github-script@master with: - script: return core.getInput('input-value') + script: ${{ github.workspace }}/__test__/script.js result-encoding: string input-value: output - run: | diff --git a/README.md b/README.md index 7de7ec58d..0dc8b96e6 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ 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: +input should be the file path that contains 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 @@ -13,7 +13,7 @@ arguments will be provided: 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 +Since the file specified by `script` contains 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 @@ -44,11 +44,15 @@ jobs: comment: runs-on: ubuntu-latest steps: - - uses: actions/github-script@0.9.0 + - uses: lukka/github-script@master with: github-token: ${{secrets.GITHUB_TOKEN}} - script: | - github.issues.createComment({ + script: ${{ github.workspace }}/script.js +``` + +File `script.js`: +```js + github.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, @@ -70,7 +74,11 @@ jobs: - uses: actions/github-script@0.9.0 with: github-token: ${{secrets.GITHUB_TOKEN}} - script: | + script: ${{ github.workspace }}/script.js +``` + +File `script.js`: +```js github.issues.addLabels({ issue_number: context.issue.number, owner: context.repo.owner, @@ -91,7 +99,11 @@ jobs: - uses: actions/github-script@0.9.0 with: github-token: ${{secrets.GITHUB_TOKEN}} - script: | + script: ${{ github.workspace }}/script.js +``` + +File `script.js`: +```js // 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 @@ -135,7 +147,11 @@ jobs: - uses: actions/github-script@0.9.0 with: github-token: ${{secrets.GITHUB_TOKEN}} - script: | + script: ${{ github.workspace }}/script.js +``` + +File `script.js`: +```js const diff_url = context.payload.pull_request.diff_url const result = await github.request(diff_url) console.log(result) @@ -155,6 +171,10 @@ output of a github-script step. For some workflows, string encoding is preferred with: github-token: ${{secrets.GITHUB_TOKEN}} result-encoding: string - script: | + script: ${{ github.workspace }}/script.js +``` + +File `script.js`: +```js return "I will be string (not JSON) encoded!" ``` diff --git a/__test__/async-function.test.ts b/__test__/async-function.test.ts index c85845c4d..2986d96b6 100644 --- a/__test__/async-function.test.ts +++ b/__test__/async-function.test.ts @@ -1,4 +1,9 @@ import {callAsyncFunction} from '../src/async-function' +import * as fs from 'fs' +import { mocked } from 'ts-jest/utils'; + +jest.mock('fs'); +mocked(fs.readFileSync).mockImplementation(path => Buffer.from(path as string, "utf8")) describe('callAsyncFunction', () => { test('calls the function with its arguments', async () => { diff --git a/__test__/script.js b/__test__/script.js new file mode 100644 index 000000000..32ce3fb8e --- /dev/null +++ b/__test__/script.js @@ -0,0 +1 @@ +return core.getInput('input-value') \ No newline at end of file diff --git a/action.yml b/action.yml index 59a1ab789..4deff17ef 100644 --- a/action.yml +++ b/action.yml @@ -6,7 +6,7 @@ branding: icon: code inputs: script: - description: The script to run + description: The path to the file containing the script to run required: true github-token: description: The GitHub token used to create an authenticated client diff --git a/dist/index.js b/dist/index.js index 055d6da0f..cc1bf790d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2009,7 +2009,7 @@ module.exports = require("https"); /***/ 215: /***/ (function(module) { -module.exports = {"name":"@octokit/rest","version":"16.43.1","publishConfig":{"access":"public"},"description":"GitHub REST API client for Node.js","keywords":["octokit","github","rest","api-client"],"author":"Gregor Martynus (https://github.com/gr2m)","contributors":[{"name":"Mike de Boer","email":"info@mikedeboer.nl"},{"name":"Fabian Jakobs","email":"fabian@c9.io"},{"name":"Joe Gallo","email":"joe@brassafrax.com"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"repository":"https://github.com/octokit/rest.js","dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"types":"index.d.ts","scripts":{"coverage":"nyc report --reporter=html && open coverage/index.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","pretest":"npm run -s lint","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","build":"npm-run-all build:*","build:ts":"npm run -s update-endpoints:typescript","prebuild:browser":"mkdirp dist/","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","prevalidate:ts":"npm run -s build:ts","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","start-fixtures-server":"octokit-fixtures-server"},"license":"MIT","files":["index.js","index.d.ts","lib","plugins"],"nyc":{"ignore":["test"]},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_from":"@octokit/rest@16.43.1"}; +module.exports = {"_args":[["@octokit/rest@16.43.1","/Users/luca/github/github-script"]],"_from":"@octokit/rest@16.43.1","_id":"@octokit/rest@16.43.1","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@octokit/rest","_phantomChildren":{"@octokit/types":"2.16.0","deprecation":"2.3.1","once":"1.4.0","os-name":"3.1.0"},"_requested":{"type":"version","registry":true,"raw":"@octokit/rest@16.43.1","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"16.43.1","saveSpec":null,"fetchSpec":"16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_spec":"16.43.1","_where":"/Users/luca/github/github-script","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"info@mikedeboer.nl"},{"name":"Fabian Jakobs","email":"fabian@c9.io"},{"name":"Joe Gallo","email":"joe@brassafrax.com"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"}; /***/ }), @@ -9270,6 +9270,9 @@ __webpack_require__.r(__webpack_exports__); // EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js var core = __webpack_require__(470); +// EXTERNAL MODULE: external "fs" +var external_fs_ = __webpack_require__(747); + // EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js var lib_github = __webpack_require__(469); @@ -9284,6 +9287,7 @@ function callAsyncFunction(args, source) { + process.on('unhandledRejection', handleError); main().catch(handleError); async function main() { @@ -9300,8 +9304,9 @@ async function main() { opts.previews = previews.split(','); const github = new lib_github.GitHub(token, opts); const script = Object(core.getInput)('script', { required: true }); + const scriptContent = Object(external_fs_.readFileSync)(script); // Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors. - const result = await callAsyncFunction({ require: __webpack_require__(875), github, context: lib_github.context, core: core }, script); + const result = await callAsyncFunction({ require: __webpack_require__(875), github, context: lib_github.context, core: core }, scriptContent.toString()); let encoding = Object(core.getInput)('result-encoding'); encoding = encoding ? encoding : 'json'; let output; diff --git a/package-lock.json b/package-lock.json index 3179ea8c2..8b0456768 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1014,9 +1014,9 @@ } }, "@types/node": { - "version": "13.13.5", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.5.tgz", - "integrity": "sha512-3ySmiBYJPqgjiHA7oEaIo2Rzz0HrOZ7yrNO5HWyaE5q0lQ3BppDZ3N53Miz8bw2I7gh1/zir2MGVZBvpb1zq9g==" + "version": "14.0.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.1.tgz", + "integrity": "sha512-FAYBGwC+W6F9+huFIDtn43cpy7+SzG+atzRiTfdp3inUKL2hXnd4rG8hylJLIh4+hqrQy1P17kvJByE/z825hA==" }, "@types/parse-json": { "version": "4.0.0", diff --git a/package.json b/package.json index 245981869..2ecadb728 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "author": "GitHub", "dependencies": { "@actions/core": "^1.2.4", - "@actions/github": "^2.2.0" + "@actions/github": "^2.2.0", + "@types/node": "14.0.1" }, "devDependencies": { "@types/jest": "^25.1.4", diff --git a/src/main.ts b/src/main.ts index 00e95a1fe..89d993dd2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,9 @@ import * as core from '@actions/core' +import * as fs from 'fs' import {context, GitHub} from '@actions/github' import {callAsyncFunction} from './async-function' + process.on('unhandledRejection', handleError) main().catch(handleError) @@ -16,11 +18,12 @@ async function main() { if (previews != null) opts.previews = previews.split(',') const github = new GitHub(token, opts) const script = core.getInput('script', {required: true}) + const scriptContent = fs.readFileSync(script) // Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors. const result = await callAsyncFunction( {require: require, github, context, core}, - script + scriptContent.toString() ) let encoding = core.getInput('result-encoding')