Skip to content

Commit

Permalink
feat(ado-improvements-1): Pipeline specific formatting (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveTryon committed Feb 12, 2022
1 parent 11c16e4 commit c88525c
Show file tree
Hide file tree
Showing 21 changed files with 355 additions and 94 deletions.
9 changes: 3 additions & 6 deletions packages/ado-extension/src/ado-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
import 'reflect-metadata';
import './module-name-mapper';

import { ExitCode } from '@accessibility-insights-action/shared';
import { Logger } from '@accessibility-insights-action/shared';
import { hookStderr } from '@accessibility-insights-action/shared';
import { hookStdout } from '@accessibility-insights-action/shared';
import { Scanner } from '@accessibility-insights-action/shared';
import { ExitCode, hookStderr, hookStdout, Logger, Scanner } from '@accessibility-insights-action/shared';
import { setupIocContainer } from './ioc/setup-ioc-container';
import { adoStdoutTransformer } from './output-hooks/ado-stdout-transformer';

export function runScan(): void {
(async () => {
hookStderr();
hookStdout();
hookStdout(adoStdoutTransformer);

const container = setupIocContainer();
const logger = container.get(Logger);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { stdoutTransformer } from './stdout-transformer';
import { adoStdoutTransformer } from './ado-stdout-transformer';

describe(stdoutTransformer, () => {
describe(adoStdoutTransformer, () => {
it.each`
input | expectedOutput
${'abc'} | ${'##[debug]abc'}
${'[Trace][info] ==='} | ${'##[debug][Trace][info] ==='}
${'\u001B[32mINFO\u001b[39m'} | ${'##[debug]\u001B[32mINFO\u001b[39m'}
${'Processing page'} | ${'##[debug]Processing page'}
${'Discovered 12 links on'} | ${'##[debug]Discovered 12 links on'}
`(`Debug tag added to raw input - input value '$input' returned as '$expectedOutput'`, ({ input, expectedOutput }) => {
const output = stdoutTransformer(input);
const output = adoStdoutTransformer(input);
expect(output).toBe(expectedOutput);
});

it.each`
input | expectedOutput
${'\u001B[32mINFO\u001b[39m '} | ${'##[debug]'}
${'\u001B[32mINFO\u001b[39m abc'} | ${'##[debug]abc'}
${'##[verbose]abc'} | ${'##[debug]abc'}
`(`Debug tag added to modified input - input value '$input' returned as '$expectedOutput'`, ({ input, expectedOutput }) => {
const output = stdoutTransformer(input);
const output = adoStdoutTransformer(input);
expect(output).toBe(expectedOutput);
});

it.each`
input
${'##[error]abc'}
${'##vso[task.debug]abc'}
${'Processing page abc'}
${'Discovered 2 links on page abc'}
${'Discovered 2345 links on page abc'}
${'Found 3 accessibility issues on page abc'}
${'Found 3456 accessibility issues on page abc'}
`(`Debug tag not added - input value '$input' returned as '$input'`, ({ input }) => {
const output = stdoutTransformer(input);
const output = adoStdoutTransformer(input);
expect(output).toBe(input);
});

it.each`
input | expectedOutput
${'##[warn]abc'} | ${'##[warning]abc'}
${'##[info]abc'} | ${'abc'}
input | expectedOutput
${'[group]abc'} | ${'##[group]abc'}
${'[endgroup]abc'} | ${'##[endgroup]abc'}
${'[debug]abc'} | ${'##[debug]abc'}
${'[warning]abc'} | ${'##[warning]abc'}
${'[info]abc'} | ${'abc'}
`(`LogLevel tags mapped input as '$input', returned as '$expectedOutput'`, ({ input, expectedOutput }) => {
const output = stdoutTransformer(input);
const output = adoStdoutTransformer(input);
expect(output).toBe(expectedOutput);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ type RegexTransformation = {
};

const regexTransformations: RegexTransformation[] = [
{
regex: new RegExp('^##\\[error\\]'),
method: useUnmodifiedString,
},
{
regex: new RegExp('^##vso\\[task.debug\\]'),
method: useUnmodifiedString,
Expand All @@ -30,27 +26,43 @@ const regexTransformations: RegexTransformation[] = [
method: useUnmodifiedString,
},
{
regex: new RegExp('^##\\[info\\]'),
regex: new RegExp('^\\[error\\]'),
method: replaceFirstMatchWithErrorPrefix,
},
{
regex: new RegExp('^\\[info\\]'),
method: removeFirstMatch,
},
{
regex: new RegExp('^##\\[warn\\]'),
regex: new RegExp('^\\[warning\\]'),
method: replaceFirstMatchWithWarningPrefix,
},
{
regex: new RegExp('^##\\[verbose\\]'),
regex: new RegExp('^\\[verbose\\]'),
method: replaceFirstMatchWithDebugPrefix,
},
{
regex: new RegExp('^\\[debug\\]'),
method: replaceFirstMatchWithDebugPrefix,
},
{
regex: new RegExp('^\\[group\\]'),
method: replaceFirstMatchWithGroupPrefix,
},
{
regex: new RegExp('^\\[endgroup\\]'),
method: replaceFirstMatchWithEndgroupPrefix,
},
{
// eslint-disable-next-line no-control-regex
regex: new RegExp('^\u001B\\[32mINFO\u001b\\[39m '), // Includes escape characters used for color formatting)
method: replaceFirstMatchWithDebugPrefix,
},
];

export const stdoutTransformer = (rawData: string): string => {
export const adoStdoutTransformer = (rawData: string): string | null => {
for (const startSubstitution of regexTransformations) {
const newData = evaluateRegexTransformation(rawData, startSubstitution.regex, startSubstitution.method);
const newData = regexTransformation(rawData, startSubstitution.regex, startSubstitution.method);

if (newData) {
return newData;
Expand All @@ -60,18 +72,14 @@ export const stdoutTransformer = (rawData: string): string => {
return prependDebugPrefix(rawData);
};

export function evaluateRegexTransformation(
input: string,
regex: RegExp,
modifier: (input: string, regex?: RegExp) => string,
): string | null {
const regexTransformation = (input: string, regex: RegExp, modifier: (input: string, regex?: RegExp) => string): string | null => {
const matches = input.match(regex);
if (matches) {
return modifier(input, regex);
}

return null;
}
};

function useUnmodifiedString(input: string): string {
return input;
Expand All @@ -89,6 +97,18 @@ function replaceFirstMatchWithWarningPrefix(input: string, regex: RegExp): strin
return `##[warning]${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithErrorPrefix(input: string, regex: RegExp): string {
return `##[error]${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithGroupPrefix(input: string, regex: RegExp): string {
return `##[group]${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithEndgroupPrefix(input: string, regex: RegExp): string {
return `##[endgroup]${input.replace(regex, '$`')}`;
}

function prependDebugPrefix(input: string): string {
return `${debugPrefix}${input}`;
}
2 changes: 1 addition & 1 deletion packages/gh-action/src/console/console-comment-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ConsoleCommentCreator extends ProgressReporter {

public start(): Promise<void> {
// We don't do anything for pull request flow
this.logger.logVerbose('console comment creator started');
this.logger.logDebug('console comment creator started');
return Promise.resolve();
}

Expand Down
9 changes: 3 additions & 6 deletions packages/gh-action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import 'reflect-metadata';
import './module-name-mapper';

import { ExitCode } from '@accessibility-insights-action/shared';
import { Logger } from '@accessibility-insights-action/shared';
import { hookStderr } from '@accessibility-insights-action/shared';
import { hookStdout } from '@accessibility-insights-action/shared';
import { Scanner } from '@accessibility-insights-action/shared';
import { ExitCode, hookStderr, hookStdout, Logger, Scanner } from '@accessibility-insights-action/shared';
import { setupIocContainer } from './ioc/setup-ioc-container';
import { ghStdoutTransformer } from './output-hooks/gh-stdout-transformer';

(async () => {
hookStderr();
hookStdout();
hookStdout(ghStdoutTransformer);

const container = setupIocContainer();
const logger = container.get(Logger);
Expand Down
50 changes: 50 additions & 0 deletions packages/gh-action/src/output-hooks/gh-stdout-transformer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { ghStdoutTransformer } from './gh-stdout-transformer';

describe(ghStdoutTransformer, () => {
it.each`
input | expectedOutput
${'abc'} | ${'::debug::abc'}
${'\u001B[32mINFO\u001b[39m'} | ${'::debug::\u001B[32mINFO\u001b[39m'}
${'Processing page'} | ${'::debug::Processing page'}
${'Discovered 12 links on'} | ${'::debug::Discovered 12 links on'}
`(`Debug tag added to raw input - input value '$input' returned as '$expectedOutput'`, ({ input, expectedOutput }) => {
const output = ghStdoutTransformer(input);
expect(output).toBe(expectedOutput);
});

it.each`
input | expectedOutput
${'\u001B[32mINFO\u001b[39m '} | ${'::debug::'}
${'\u001B[32mINFO\u001b[39m abc'} | ${'::debug::abc'}
`(`Debug tag added to modified input - input value '$input' returned as '$expectedOutput'`, ({ input, expectedOutput }) => {
const output = ghStdoutTransformer(input);
expect(output).toBe(expectedOutput);
});

it.each`
input
${'Processing page abc'}
${'Discovered 2 links on page abc'}
${'Discovered 2345 links on page abc'}
${'Found 3 accessibility issues on page abc'}
${'Found 3456 accessibility issues on page abc'}
`(`Debug tag not added - input value '$input' returned as '$input'`, ({ input }) => {
const output = ghStdoutTransformer(input);
expect(output).toBe(input);
});

it.each`
input | expectedOutput
${'[group]abc'} | ${'::group::abc'}
${'[endgroup]abc'} | ${'::endgroup::abc'}
${'[debug]abc'} | ${'::debug::abc'}
${'[warning]abc'} | ${'::warning::abc'}
${'[info]abc'} | ${'abc'}
`(`LogLevel tags mapped input as '$input', returned as '$expectedOutput'`, ({ input, expectedOutput }) => {
const output = ghStdoutTransformer(input);
expect(output).toBe(expectedOutput);
});
});
110 changes: 110 additions & 0 deletions packages/gh-action/src/output-hooks/gh-stdout-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

const debugPrefix = '::debug::';

type RegexTransformation = {
regex: RegExp;
method: (rawData: string, regex?: RegExp) => string;
};

const regexTransformations: RegexTransformation[] = [
{
regex: new RegExp('^Processing page .*'),
method: useUnmodifiedString,
},
{
regex: new RegExp('^Discovered \\d* links on page '),
method: useUnmodifiedString,
},
{
regex: new RegExp('^Found \\d* accessibility issues on page '),
method: useUnmodifiedString,
},
{
regex: new RegExp('^\\[error\\]'),
method: replaceFirstMatchWithErrorPrefix,
},
{
regex: new RegExp('^\\[info\\]'),
method: removeFirstMatch,
},
{
regex: new RegExp('^\\[warning\\]'),
method: replaceFirstMatchWithWarningPrefix,
},
{
regex: new RegExp('^\\[verbose\\]'),
method: replaceFirstMatchWithDebugPrefix,
},
{
regex: new RegExp('^\\[debug\\]'),
method: replaceFirstMatchWithDebugPrefix,
},
{
regex: new RegExp('^\\[group\\]'),
method: replaceFirstMatchWithGroupPrefix,
},
{
regex: new RegExp('^\\[endgroup\\]'),
method: replaceFirstMatchWithEndgroupPrefix,
},
{
// eslint-disable-next-line no-control-regex
regex: new RegExp('^\u001B\\[32mINFO\u001b\\[39m '), // Includes escape characters used for color formatting)
method: replaceFirstMatchWithDebugPrefix,
},
];

export const ghStdoutTransformer = (rawData: string): string | null => {
for (const startSubstitution of regexTransformations) {
const newData = regexTransformation(rawData, startSubstitution.regex, startSubstitution.method);

if (newData) {
return newData;
}
}

return prependDebugPrefix(rawData);
};

const regexTransformation = (input: string, regex: RegExp, modifier: (input: string, regex?: RegExp) => string): string | null => {
const matches = input.match(regex);
if (matches) {
return modifier(input, regex);
}

return null;
};

function useUnmodifiedString(input: string): string {
return input;
}

function removeFirstMatch(input: string, regex: RegExp): string {
return `${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithDebugPrefix(input: string, regex: RegExp): string {
return `${debugPrefix}${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithWarningPrefix(input: string, regex: RegExp): string {
return `::warning::${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithErrorPrefix(input: string, regex: RegExp): string {
return `::error::${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithGroupPrefix(input: string, regex: RegExp): string {
return `::group::${input.replace(regex, '$`')}`;
}

function replaceFirstMatchWithEndgroupPrefix(input: string, regex: RegExp): string {
return `::endgroup::${input.replace(regex, '$`')}`;
}

function prependDebugPrefix(input: string): string {
return `${debugPrefix}${input}`;
}
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export { BaselineInfo } from './baseline-info';
export { ArtifactsInfoProvider } from './artifacts-info-provider';
export { hookStdout } from './output-hooks/hook-stdout';
export { hookStderr } from './output-hooks/hook-stderr';
export { StreamTransformer } from './output-hooks/stream-transformer';
export { ExitCode } from './exit-code';

0 comments on commit c88525c

Please sign in to comment.