From d2678434979bb5fb43b43b194fc9f3e2f5208fff Mon Sep 17 00:00:00 2001 From: Nicolas Hedger Date: Sat, 9 Jul 2022 13:24:05 +0200 Subject: [PATCH 1/6] feat: add github actions logging --- src/util.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/util.ts b/src/util.ts index d1795551..c0155767 100644 --- a/src/util.ts +++ b/src/util.ts @@ -129,11 +129,23 @@ function _log(type: LogMessageType, msg: any, ...args: any[]): void { args = [LogPrefix[type], msg, ...args]; if (type === LogMessageType.WARNING) { - console.warn(...args); + if (process.env['GITHUB_ACTIONS']) { + process.stdout.write(`::warning::${msg}`); + } else { + console.warn(...args); + } } else if (type === LogMessageType.ERROR) { - console.error(...args); + if (process.env['GITHUB_ACTIONS']) { + process.stdout.write(`::error::${msg}`); + } else { + console.error(...args); + } } else { - console.log(...args); + if (process.env['GITHUB_ACTIONS']) { + process.stdout.write(`::notice::${msg}`); + } else { + console.log(...args); + } } } From 537a64bfdf7ab7c4cc44e4426a56363ffc32ba9d Mon Sep 17 00:00:00 2001 From: Nicolas Hedger Date: Sat, 9 Jul 2022 13:33:05 +0200 Subject: [PATCH 2/6] fix: add missing eol --- src/util.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/util.ts b/src/util.ts index c0155767..555ad36a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -6,6 +6,7 @@ import chalk from 'chalk'; import { PublicGalleryAPI } from './publicgalleryapi'; import { ISecurityRolesApi } from 'azure-devops-node-api/SecurityRolesApi'; import { Manifest } from './manifest'; +import * as os from 'os'; const __read = promisify<_read.Options, string>(_read); export function read(prompt: string, options: _read.Options = {}): Promise { @@ -130,19 +131,19 @@ function _log(type: LogMessageType, msg: any, ...args: any[]): void { if (type === LogMessageType.WARNING) { if (process.env['GITHUB_ACTIONS']) { - process.stdout.write(`::warning::${msg}`); + process.stdout.write(`::warning::${msg}` + os.EOL); } else { console.warn(...args); } } else if (type === LogMessageType.ERROR) { if (process.env['GITHUB_ACTIONS']) { - process.stdout.write(`::error::${msg}`); + process.stdout.write(`::error::${msg}` + os.EOL); } else { console.error(...args); } } else { if (process.env['GITHUB_ACTIONS']) { - process.stdout.write(`::notice::${msg}`); + process.stdout.write(`::notice::${msg}` + os.EOL); } else { console.log(...args); } From ac4885130e807111d777403d5868a6be08c36691 Mon Sep 17 00:00:00 2001 From: Nicolas Hedger Date: Sat, 9 Jul 2022 13:46:40 +0200 Subject: [PATCH 3/6] feat: use github actions toolkit --- package-lock.json | 33 +++++++++++++++++++++++++++++++++ package.json | 1 + src/util.ts | 20 ++++---------------- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index e0530cc6..6d8542d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.0", "license": "MIT", "dependencies": { + "@actions/core": "^1.9.0", "azure-devops-node-api": "^11.0.1", "chalk": "^2.4.2", "cheerio": "^1.0.0-rc.9", @@ -65,6 +66,22 @@ "node": ">= 14" } }, + "node_modules/@actions/core": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.9.0.tgz", + "integrity": "sha512-5pbM693Ih59ZdUhgk+fts+bUWTnIdHV3kwOSr+QIoFHMLg7Gzhwm0cifDY/AG68ekEJAkHnQVpcy4f6GjmzBCA==", + "dependencies": { + "@actions/http-client": "^2.0.1" + } + }, + "node_modules/@actions/http-client": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz", + "integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==", + "dependencies": { + "tunnel": "^0.0.6" + } + }, "node_modules/@babel/code-frame": { "version": "7.10.4", "resolved": "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz", @@ -9079,6 +9096,22 @@ } }, "dependencies": { + "@actions/core": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.9.0.tgz", + "integrity": "sha512-5pbM693Ih59ZdUhgk+fts+bUWTnIdHV3kwOSr+QIoFHMLg7Gzhwm0cifDY/AG68ekEJAkHnQVpcy4f6GjmzBCA==", + "requires": { + "@actions/http-client": "^2.0.1" + } + }, + "@actions/http-client": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz", + "integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==", + "requires": { + "tunnel": "^0.0.6" + } + }, "@babel/code-frame": { "version": "7.10.4", "resolved": "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz", diff --git a/package.json b/package.json index 2a2391c5..0d721c6a 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "node": ">= 14" }, "dependencies": { + "@actions/core": "^1.9.0", "azure-devops-node-api": "^11.0.1", "chalk": "^2.4.2", "cheerio": "^1.0.0-rc.9", diff --git a/src/util.ts b/src/util.ts index 555ad36a..c9559a4e 100644 --- a/src/util.ts +++ b/src/util.ts @@ -6,7 +6,7 @@ import chalk from 'chalk'; import { PublicGalleryAPI } from './publicgalleryapi'; import { ISecurityRolesApi } from 'azure-devops-node-api/SecurityRolesApi'; import { Manifest } from './manifest'; -import * as os from 'os'; +import { error, info, warning } from '@actions/core'; const __read = promisify<_read.Options, string>(_read); export function read(prompt: string, options: _read.Options = {}): Promise { @@ -130,23 +130,11 @@ function _log(type: LogMessageType, msg: any, ...args: any[]): void { args = [LogPrefix[type], msg, ...args]; if (type === LogMessageType.WARNING) { - if (process.env['GITHUB_ACTIONS']) { - process.stdout.write(`::warning::${msg}` + os.EOL); - } else { - console.warn(...args); - } + process.env['GITHUB_ACTIONS'] ? warning(msg) : console.warn(...args); } else if (type === LogMessageType.ERROR) { - if (process.env['GITHUB_ACTIONS']) { - process.stdout.write(`::error::${msg}` + os.EOL); - } else { - console.error(...args); - } + process.env['GITHUB_ACTIONS'] ? error(msg) : console.error(...args); } else { - if (process.env['GITHUB_ACTIONS']) { - process.stdout.write(`::notice::${msg}` + os.EOL); - } else { - console.log(...args); - } + process.env['GITHUB_ACTIONS'] ? info(msg) : console.log(...args); } } From 14af64b214ec6936dfd77e488326bc92c97e8a2f Mon Sep 17 00:00:00 2001 From: Nicolas Hedger Date: Sun, 17 Jul 2022 09:11:47 +0200 Subject: [PATCH 4/6] chore(deps): remove actions toolkit --- package-lock.json | 33 --------------------------------- package.json | 1 - 2 files changed, 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6d8542d2..e0530cc6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "0.0.0", "license": "MIT", "dependencies": { - "@actions/core": "^1.9.0", "azure-devops-node-api": "^11.0.1", "chalk": "^2.4.2", "cheerio": "^1.0.0-rc.9", @@ -66,22 +65,6 @@ "node": ">= 14" } }, - "node_modules/@actions/core": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.9.0.tgz", - "integrity": "sha512-5pbM693Ih59ZdUhgk+fts+bUWTnIdHV3kwOSr+QIoFHMLg7Gzhwm0cifDY/AG68ekEJAkHnQVpcy4f6GjmzBCA==", - "dependencies": { - "@actions/http-client": "^2.0.1" - } - }, - "node_modules/@actions/http-client": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz", - "integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==", - "dependencies": { - "tunnel": "^0.0.6" - } - }, "node_modules/@babel/code-frame": { "version": "7.10.4", "resolved": "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz", @@ -9096,22 +9079,6 @@ } }, "dependencies": { - "@actions/core": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.9.0.tgz", - "integrity": "sha512-5pbM693Ih59ZdUhgk+fts+bUWTnIdHV3kwOSr+QIoFHMLg7Gzhwm0cifDY/AG68ekEJAkHnQVpcy4f6GjmzBCA==", - "requires": { - "@actions/http-client": "^2.0.1" - } - }, - "@actions/http-client": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz", - "integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==", - "requires": { - "tunnel": "^0.0.6" - } - }, "@babel/code-frame": { "version": "7.10.4", "resolved": "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz", diff --git a/package.json b/package.json index 0d721c6a..2a2391c5 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "node": ">= 14" }, "dependencies": { - "@actions/core": "^1.9.0", "azure-devops-node-api": "^11.0.1", "chalk": "^2.4.2", "cheerio": "^1.0.0-rc.9", From 92d3b633b92e315789d1b750c75fe503619c304a Mon Sep 17 00:00:00 2001 From: Nicolas Hedger Date: Sun, 17 Jul 2022 09:30:19 +0200 Subject: [PATCH 5/6] feat: replace toolkit logging methods with own methods --- src/util.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/util.ts b/src/util.ts index c9559a4e..61e1ae7d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -6,7 +6,7 @@ import chalk from 'chalk'; import { PublicGalleryAPI } from './publicgalleryapi'; import { ISecurityRolesApi } from 'azure-devops-node-api/SecurityRolesApi'; import { Manifest } from './manifest'; -import { error, info, warning } from '@actions/core'; +import { EOL } from 'os'; const __read = promisify<_read.Options, string>(_read); export function read(prompt: string, options: _read.Options = {}): Promise { @@ -130,14 +130,24 @@ function _log(type: LogMessageType, msg: any, ...args: any[]): void { args = [LogPrefix[type], msg, ...args]; if (type === LogMessageType.WARNING) { - process.env['GITHUB_ACTIONS'] ? warning(msg) : console.warn(...args); + process.env['GITHUB_ACTIONS'] ? logToGitHubActions('warning', msg) : console.warn(...args); } else if (type === LogMessageType.ERROR) { - process.env['GITHUB_ACTIONS'] ? error(msg) : console.error(...args); + process.env['GITHUB_ACTIONS'] ? logToGitHubActions('error', msg) : console.error(...args); } else { - process.env['GITHUB_ACTIONS'] ? info(msg) : console.log(...args); + process.env['GITHUB_ACTIONS'] ? logToGitHubActions('info', msg) : console.log(...args); } } +function logToGitHubActions(type: string, message: string): void { + const escape = (message: string) => { + return message.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A'); + }; + + const command = type === 'info' ? message : `::${type}::${escape(message)}`; + + process.stdout.write(command + EOL); +} + export interface LogFn { (msg: any, ...args: any[]): void; } From 13bca51a06a5b529f97ce4c002a161d2fd6a05e2 Mon Sep 17 00:00:00 2001 From: Nicolas Hedger <649677+nhedger@users.noreply.github.com> Date: Mon, 18 Jul 2022 19:15:02 +0200 Subject: [PATCH 6/6] Update src/util.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: João Moreno --- src/util.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/util.ts b/src/util.ts index 61e1ae7d..e5023dd0 100644 --- a/src/util.ts +++ b/src/util.ts @@ -138,13 +138,20 @@ function _log(type: LogMessageType, msg: any, ...args: any[]): void { } } -function logToGitHubActions(type: string, message: string): void { - const escape = (message: string) => { - return message.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A'); - }; +const EscapeCharacters = new Map([ + ['%', '%25'], + ['\r', '%0D'], + ['\n', '%0A'], +]); + +const EscapeRegex = new RegExp(`[${[...EscapeCharacters.keys()].join('')}]`, 'g'); - const command = type === 'info' ? message : `::${type}::${escape(message)}`; +function escapeGitHubActionsMessage(message: string): string { + return message.replace(EscapeRegex, c => EscapeCharacters.get(c) ?? c); +} +function logToGitHubActions(type: string, message: string): void { + const command = type === 'info' ? message : `::${type}::${escapeGitHubActionsMessage(message)}`; process.stdout.write(command + EOL); }