From 780a5985b46ab5d626d73dae1e4b64c9e8927026 Mon Sep 17 00:00:00 2001 From: Bryan MacFarlane Date: Thu, 16 May 2019 23:36:45 -0400 Subject: [PATCH] starting on core --- packages/core/README.md | 53 +++++++++++++++++++++-- packages/core/package-lock.json | 14 ++++++ packages/core/package.json | 5 ++- packages/core/src/core.ts | 75 +++++++++++++++++++++++++++++++++ packages/core/src/interfaces.ts | 20 ++++----- packages/core/src/internal.ts | 6 +-- packages/core/src/lib.ts | 73 -------------------------------- 7 files changed, 154 insertions(+), 92 deletions(-) create mode 100644 packages/core/package-lock.json create mode 100644 packages/core/src/core.ts delete mode 100644 packages/core/src/lib.ts diff --git a/packages/core/README.md b/packages/core/README.md index 9193971e..6a1807fb 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -1,11 +1,58 @@ # `@actions/core` -> TODO: description +> Core functions for setting results, logging, registering secrets and exporting variables across actions ## Usage ``` -const core = require('@actions/core'); +/** + * sets env variable for this action and future actions in the job + * @param name the name of the variable to set + * @param val the value of the variable + */ +export function exportVariable(name: string, val: string, options?:im.ExportOptions); -// TODO: DEMONSTRATE API +/** + * registers a secret which will get masked from logs + * @param val value of the secret + */ +export function setSecret(val: string); + +//----------------------------------------------------------------------- +// Results +//----------------------------------------------------------------------- + +/** + * Sets the action status to neutral + */ +export function setNeutral(); + +/** + * Sets the action status to failed. + * When the action exits it will be with an exit code of 1 + * @param message add error issue message + */ +export function setFailed(message: string); + +//----------------------------------------------------------------------- +// Logging Commands +//----------------------------------------------------------------------- + +/** + * Writes debug message to user log + * @param message debug message + */ +export function debug(message: string); + +/** + * Adds an error issue + * @param message error issue message + */ +export function error(message: string); + +/** + * Adds an warning issue + * @param message warning issue message + */ +export function warning(message: string); ``` diff --git a/packages/core/package-lock.json b/packages/core/package-lock.json new file mode 100644 index 00000000..fef7996a --- /dev/null +++ b/packages/core/package-lock.json @@ -0,0 +1,14 @@ +{ + "name": "@actions/core", + "version": "0.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/node": { + "version": "12.0.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.0.2.tgz", + "integrity": "sha512-5tabW/i+9mhrfEOUcLDu2xBPsHJ+X5Orqy9FKpale3SjDA17j5AEpYq5vfy3oAeAHGcvANRCO3NV3d2D6q3NiA==", + "dev": true + } + } +} diff --git a/packages/core/package.json b/packages/core/package.json index 52200e5b..1c6b1181 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@actions/core", - "version": "1.0.0", + "version": "0.1.0", "description": "Actions core lib", "keywords": [ "core", @@ -30,5 +30,8 @@ }, "bugs": { "url": "https://github.com/actions/toolkit/issues" + }, + "devDependencies": { + "@types/node": "^12.0.2" } } diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts new file mode 100644 index 00000000..87910a0c --- /dev/null +++ b/packages/core/src/core.ts @@ -0,0 +1,75 @@ +import im = require('./interfaces'); +import intm = require('./internal'); +import process = require('process'); + +/** + * sets env variable for this action and future actions in the job + * @param name the name of the variable to set + * @param val the value of the variable + */ +export function exportVariable(name: string, val: string, options?:im.ExportOptions) { + process.env[name] = val; + let props = {'name': name, 'isSecret': options? options.isSecret : false}; + intm._issueCommand('set-variable', props, val); +} + +/** + * registers a secret which will get masked from logs + * @param val value of the secret + */ +export function setSecret(val: string) { + intm._issueCommand('set-secret', {}, val); +} + +//----------------------------------------------------------------------- +// Results +//----------------------------------------------------------------------- + +/** + * Sets the action status to neutral + */ +export function setNeutral() { + process.exitCode = im.ExitCode.Neutral; +} + +/** + * Sets the action status to failed. + * When the action exits it will be with an exit code of 1 + * @param message add error issue message + */ +export function setFailed(message: string) { + process.exitCode = im.ExitCode.Failure; + error(message); +} + +//----------------------------------------------------------------------- +// Logging Commands +// +// error and warning issues do not take FileDetails because while possible, +// that's typically reserved for the agent and the problem matchers. +// +//----------------------------------------------------------------------- + +/** + * Writes debug message to user log + * @param message debug message + */ +export function debug(message: string) { + intm._issueCommand('debug', {}, message); +} + +/** + * Adds an error issue + * @param message error issue message + */ +export function error(message: string) { + intm._issue('error', message); +} + +/** + * Adds an warning issue + * @param message warning issue message + */ +export function warning(message: string) { + intm._issue('warning', message); +} diff --git a/packages/core/src/interfaces.ts b/packages/core/src/interfaces.ts index 164ca103..16696ed9 100644 --- a/packages/core/src/interfaces.ts +++ b/packages/core/src/interfaces.ts @@ -1,17 +1,5 @@ -export interface FileDetails { - /** - * Full path to the file causing the issue. - * Note: the agent will translate to the proper repo when posting - * the issue back to the timeline - */ - File: string, - Line: number, - Column: number -} - /** * The code to exit an action - * Spec: https://github.com/github/dreamlifter/blob/master/docs/actions-model.md#exit-codes */ export enum ExitCode { /** @@ -28,4 +16,12 @@ export enum ExitCode { * A code indicating that the action is complete, but neither succeeded nor failed */ Neutral = 78 +} + +/** + * Interface for exportVariable options + */ +export interface ExportOptions { + /** Optional. Whether the variable should be marked as secret (will be masked from logs). Defaults to false */ + isSecret?: boolean; } \ No newline at end of file diff --git a/packages/core/src/internal.ts b/packages/core/src/internal.ts index 38a7fd8e..af00f9a9 100644 --- a/packages/core/src/internal.ts +++ b/packages/core/src/internal.ts @@ -3,16 +3,15 @@ import os = require('os'); /** * Commands - * Spec: https://github.com/github/dreamlifter/blob/master/docs/actions-model.md#logging-commands * * Command Format: * ##[name key=value;key=value]message * * Examples: * ##[warning]This is the user warning message - * ##[set-secret name=mypassword]definitelyNotAPassword! + * ##[set-secret name=mypassword]definatelyNotAPassword! */ -export function _issueCommand(command: string, properties: {[key: string]: string}, message: string) { +export function _issueCommand(command: string, properties: any, message: string) { var cmd = new _Command(command, properties, message); _writeLine(cmd.toString()); } @@ -67,6 +66,7 @@ export class _Command { } export function _commandFromString(commandLine: string) { + var preLen = CMD_PREFIX.length; var lbPos = commandLine.indexOf('['); var rbPos = commandLine.indexOf(']'); if (lbPos == -1 || rbPos == -1 || rbPos - lbPos < 3) { diff --git a/packages/core/src/lib.ts b/packages/core/src/lib.ts deleted file mode 100644 index 68e97094..00000000 --- a/packages/core/src/lib.ts +++ /dev/null @@ -1,73 +0,0 @@ -import im = require('./interfaces'); -import intm = require('./internal'); -import process = require('process'); - -/** - * Interface for exportVariable options - */ -export interface ExportOptions { - /** Optional. Whether the variable should be marked as secret (will be masked from logs). Defaults to false */ - isSecret?: boolean; -} - -/** - * sets env variable for this action and future actions in the job - * - * @param name the name of the variable to set - * @param val the value of the variable - * @param options optional. See ExportOptions. - */ -export function exportVariable(name: string, val: string, options?: ExportOptions): void { - if (options && options.isSecret) { - intm._issueCommand('set-secret', {'name': name}, val); - } - process.env[name] = val; - intm._issueCommand('set-variable', {'name': name}, val); -} - -/** - * Interface for getInput options - */ -export interface InputOptions { - /** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */ - required?: boolean; -} - -/** - * Gets the value of an input. The value is also trimmed. - * - * @param name name of the input to get - * @param options optional. See InputOptions. - * @returns string - */ -export function getInput(name: string, options?: InputOptions): string | undefined { - // TODO - how are we passing in actions inputs? - return ''; -} - -/** - * fail the action - * @param message - */ -export function setFailure(message: string): void { - process.exitCode = im.ExitCode.Failure; - error(message); -} - -//----------------------------------------------------------------------- -// Logging Commands -// https://github.com/github/dreamlifter/blob/master/docs/actions-model.md#logging-commands -// -//----------------------------------------------------------------------- - -export function error(message: string) { - intm._issue('error', message); -} - -export function warning(message: string) { - intm._issue('warning', message); -} - -export function debug(message: string): void { - intm._issue('debug', message); -} \ No newline at end of file