Skip to content

Commit 9c50aa6

Browse files
nhedgerjoaomoreno
andauthoredJul 19, 2022
feat: github actions logging (#752)
Co-authored-by: João Moreno <joao.moreno@microsoft.com>
1 parent 7e16ed7 commit 9c50aa6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed
 

‎src/util.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import chalk from 'chalk';
66
import { PublicGalleryAPI } from './publicgalleryapi';
77
import { ISecurityRolesApi } from 'azure-devops-node-api/SecurityRolesApi';
88
import { Manifest } from './manifest';
9+
import { EOL } from 'os';
910

1011
const __read = promisify<_read.Options, string>(_read);
1112
export function read(prompt: string, options: _read.Options = {}): Promise<string> {
@@ -129,14 +130,31 @@ function _log(type: LogMessageType, msg: any, ...args: any[]): void {
129130
args = [LogPrefix[type], msg, ...args];
130131

131132
if (type === LogMessageType.WARNING) {
132-
console.warn(...args);
133+
process.env['GITHUB_ACTIONS'] ? logToGitHubActions('warning', msg) : console.warn(...args);
133134
} else if (type === LogMessageType.ERROR) {
134-
console.error(...args);
135+
process.env['GITHUB_ACTIONS'] ? logToGitHubActions('error', msg) : console.error(...args);
135136
} else {
136-
console.log(...args);
137+
process.env['GITHUB_ACTIONS'] ? logToGitHubActions('info', msg) : console.log(...args);
137138
}
138139
}
139140

141+
const EscapeCharacters = new Map([
142+
['%', '%25'],
143+
['\r', '%0D'],
144+
['\n', '%0A'],
145+
]);
146+
147+
const EscapeRegex = new RegExp(`[${[...EscapeCharacters.keys()].join('')}]`, 'g');
148+
149+
function escapeGitHubActionsMessage(message: string): string {
150+
return message.replace(EscapeRegex, c => EscapeCharacters.get(c) ?? c);
151+
}
152+
153+
function logToGitHubActions(type: string, message: string): void {
154+
const command = type === 'info' ? message : `::${type}::${escapeGitHubActionsMessage(message)}`;
155+
process.stdout.write(command + EOL);
156+
}
157+
140158
export interface LogFn {
141159
(msg: any, ...args: any[]): void;
142160
}

0 commit comments

Comments
 (0)
Please sign in to comment.