mirror of
https://github.com/actions/github-script.git
synced 2026-02-09 04:27:25 +00:00
Run from a script or a file
This commit is contained in:
parent
64af053a78
commit
35288c3418
3 changed files with 61 additions and 4 deletions
14
.github/workflows/integration.yml
vendored
14
.github/workflows/integration.yml
vendored
|
|
@ -16,13 +16,23 @@ jobs:
|
||||||
if [[ "${{steps.output-set.outputs.result}}" != "output" ]]; then
|
if [[ "${{steps.output-set.outputs.result}}" != "output" ]]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
- id: file-output-set
|
- id: file-relative
|
||||||
uses: actions/github-script@run-file
|
uses: actions/github-script@run-file
|
||||||
with:
|
with:
|
||||||
file: __test__/test-output.js
|
file: __test__/test-output.js
|
||||||
result-encoding: string
|
result-encoding: string
|
||||||
input-value: output
|
input-value: output
|
||||||
- run: |
|
- run: |
|
||||||
if [[ "${{steps.file-output-set.outputs.result}}" != "output" ]]; then
|
if [[ "${{steps.file-relative.outputs.result}}" != "output" ]]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
- id: file-absolute
|
||||||
|
uses: actions/github-script@run-file
|
||||||
|
with:
|
||||||
|
file: ${{github.workspace}}/__test__/test-output.js
|
||||||
|
result-encoding: string
|
||||||
|
input-value: output
|
||||||
|
- run: |
|
||||||
|
if [[ "${{steps.file-absolute.outputs.result}}" != "output" ]]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
26
dist/index.js
vendored
26
dist/index.js
vendored
|
|
@ -9273,6 +9273,12 @@ var core = __webpack_require__(470);
|
||||||
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
|
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
|
||||||
var lib_github = __webpack_require__(469);
|
var lib_github = __webpack_require__(469);
|
||||||
|
|
||||||
|
// EXTERNAL MODULE: external "fs"
|
||||||
|
var external_fs_ = __webpack_require__(747);
|
||||||
|
|
||||||
|
// EXTERNAL MODULE: external "path"
|
||||||
|
var external_path_ = __webpack_require__(622);
|
||||||
|
|
||||||
// CONCATENATED MODULE: ./src/async-function.ts
|
// CONCATENATED MODULE: ./src/async-function.ts
|
||||||
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
|
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
|
||||||
function callAsyncFunction(args, source) {
|
function callAsyncFunction(args, source) {
|
||||||
|
|
@ -9284,6 +9290,8 @@ function callAsyncFunction(args, source) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
process.on('unhandledRejection', handleError);
|
process.on('unhandledRejection', handleError);
|
||||||
main().catch(handleError);
|
main().catch(handleError);
|
||||||
async function main() {
|
async function main() {
|
||||||
|
|
@ -9299,7 +9307,7 @@ async function main() {
|
||||||
if (previews != null)
|
if (previews != null)
|
||||||
opts.previews = previews.split(',');
|
opts.previews = previews.split(',');
|
||||||
const github = new lib_github.GitHub(token, opts);
|
const github = new lib_github.GitHub(token, opts);
|
||||||
const script = Object(core.getInput)('script', { required: true });
|
const script = getScript();
|
||||||
// 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({ 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 }, script);
|
||||||
let encoding = Object(core.getInput)('result-encoding');
|
let encoding = Object(core.getInput)('result-encoding');
|
||||||
|
|
@ -9317,6 +9325,22 @@ async function main() {
|
||||||
}
|
}
|
||||||
Object(core.setOutput)('result', output);
|
Object(core.setOutput)('result', output);
|
||||||
}
|
}
|
||||||
|
function getScript() {
|
||||||
|
const script = Object(core.getInput)('script');
|
||||||
|
const filePath = Object(core.getInput)('file');
|
||||||
|
if (script && filePath) {
|
||||||
|
Object(core.setFailed)('A script and a file were provided; only one is allowed');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
if (!(script || filePath)) {
|
||||||
|
Object(core.setFailed)('Neither a script nor a file were provided');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
if (filePath) {
|
||||||
|
return Object(external_fs_.readFileSync)(Object(external_path_.resolve)(filePath)).toString('utf-8');
|
||||||
|
}
|
||||||
|
return script;
|
||||||
|
}
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
function handleError(err) {
|
function handleError(err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
|
||||||
25
src/main.ts
25
src/main.ts
|
|
@ -1,5 +1,7 @@
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import {context, GitHub} from '@actions/github'
|
import {context, GitHub} from '@actions/github'
|
||||||
|
import * as fs from 'fs'
|
||||||
|
import * as path from 'path'
|
||||||
import {callAsyncFunction} from './async-function'
|
import {callAsyncFunction} from './async-function'
|
||||||
|
|
||||||
process.on('unhandledRejection', handleError)
|
process.on('unhandledRejection', handleError)
|
||||||
|
|
@ -23,7 +25,7 @@ async function main(): Promise<void> {
|
||||||
if (previews != null) opts.previews = previews.split(',')
|
if (previews != null) opts.previews = previews.split(',')
|
||||||
|
|
||||||
const github = new GitHub(token, opts)
|
const github = new GitHub(token, opts)
|
||||||
const script = core.getInput('script', {required: true})
|
const script = getScript()
|
||||||
|
|
||||||
// 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(
|
||||||
|
|
@ -50,6 +52,27 @@ async function main(): Promise<void> {
|
||||||
core.setOutput('result', output)
|
core.setOutput('result', output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getScript(): string {
|
||||||
|
const script = core.getInput('script')
|
||||||
|
const filePath = core.getInput('file')
|
||||||
|
|
||||||
|
if (script && filePath) {
|
||||||
|
core.setFailed('A script and a file were provided; only one is allowed')
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(script || filePath)) {
|
||||||
|
core.setFailed('Neither a script nor a file were provided')
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filePath) {
|
||||||
|
return fs.readFileSync(path.resolve(filePath)).toString('utf-8')
|
||||||
|
}
|
||||||
|
|
||||||
|
return script
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
function handleError(err: any): void {
|
function handleError(err: any): void {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue