Skip to content

Commit

Permalink
Log task completion if configured
Browse files Browse the repository at this point in the history
Fixes #163
  • Loading branch information
bdukes committed Nov 13, 2023
1 parent 9fe60e1 commit b1916aa
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 46 deletions.
44 changes: 34 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,36 @@ function logIssue(issue) {
return `##vso[task.logissue ${attributes.join('')}]${issue.message}`;
}

module.exports = (results, resultsMeta) => {
function shouldLogTaskComplete() {
return Boolean(process.env.ESLINT_AZDO_LOG_TASK_COMPLETE);
}

function formatResults(results, resultsMeta) {
let hasError = false;
let hasWarning = false;
const formattedResults = results
.filter((result) => result.messages.length > 0)
.map((result) =>
result.messages.map((message) =>
logIssue({
type:
message.fatal || message.severity === ERROR_SEVERITY
? 'error'
: 'warning',
result.messages.map((message) => {
const type =
message.fatal || message.severity === ERROR_SEVERITY
? 'error'
: 'warning';
if (type === 'error') {
hasError = true;
} else {
hasWarning = true;
}

return logIssue({
type,
sourcepath: result.filePath,
linenumber: message.line,
columnnumber: message.column,
code: message.ruleId,
message: message.message,
})
)
});
})
)
.reduce((allMessages, messages) => {
if (messages != null && messages.length > 0) {
Expand All @@ -45,6 +58,7 @@ module.exports = (results, resultsMeta) => {
if (resultsMeta?.maxWarningsExceeded) {
const { maxWarnings, foundWarnings } = resultsMeta.maxWarningsExceeded;
if (maxWarnings < foundWarnings) {
hasError = true;
formattedResults.push(
logIssue({
type: 'error',
Expand All @@ -54,5 +68,15 @@ module.exports = (results, resultsMeta) => {
}
}

if (formatResults.shouldLogTaskComplete() && (hasError || hasWarning)) {
const result = hasError ? 'Failed' : 'SucceededWithIssues';
formattedResults.push(`##vso[task.complete result=${result};]`);
}

return formattedResults.join('\n');
};
}

formatResults.shouldLogTaskComplete = shouldLogTaskComplete;

module.exports = formatResults;

4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
You can reference the formatter [from the command line](https://eslint.org/docs/latest/user-guide/command-line-interface#-f---format),
via [the Node.js API](https://eslint.org/docs/latest/developer-guide/nodejs-api#-eslintloadformatternameorpath), or through your [task](https://www.npmjs.com/package/gulp-eslint-new) [runner](https://npmjs.org/package/grunt-eslint) [of](https://www.npmjs.org/package/broccoli-eslint) [choice](https://www.npmjs.com/package/eslint-webpack-plugin).

### Log Partially Complete

If you would like for the status of the task to be marked as _partially succeeded_ if there are any warnings, set the environment variable `ESLINT_AZDO_LOG_TASK_COMPLETE` before running ESLint (as of version 1.2.0).

## License

MIT © [Engage Software](https://engagesoftware.com)
112 changes: 76 additions & 36 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
const test = require('ava');
const formatter = require('./');

const fixtureWarnings = {
filePath: 'C:\\js\\warnings.js',
messages: [
{
ruleId: 'prefer-const',
severity: 1,
message:
"'initialNavHeight' is never reassigned. Use 'const' instead.",
line: 22,
column: 9,
nodeType: 'Identifier',
source: ' initialNavHeight = $mainNavMenu.outerHeight();',
},
{
ruleId: 'consistent-return',
severity: 1,
message: 'Expected to return a value at the end of this function.',
line: 76,
column: 14,
nodeType: 'FunctionDeclaration',
source: ' function hideSubmenu(menuItem) {',
},
{
ruleId: 'eqeqeq',
severity: 1,
message: "Expected '===' and instead saw '=='.",
line: 234,
column: 25,
nodeType: 'BinaryExpression',
source: ' if (parentLevel == 1) {',
},
],
errorCount: 0,
warningCount: 3,
};

const fixture = [
{
filePath: 'C:\\js\\warnings.js',
messages: [
{
ruleId: 'prefer-const',
severity: 1,
message:
"'initialNavHeight' is never reassigned. Use 'const' instead.",
line: 22,
column: 9,
nodeType: 'Identifier',
source: ' initialNavHeight = $mainNavMenu.outerHeight();',
},
{
ruleId: 'consistent-return',
severity: 1,
message:
'Expected to return a value at the end of this function.',
line: 76,
column: 14,
nodeType: 'FunctionDeclaration',
source: ' function hideSubmenu(menuItem) {',
},
{
ruleId: 'eqeqeq',
severity: 1,
message: "Expected '===' and instead saw '=='.",
line: 234,
column: 25,
nodeType: 'BinaryExpression',
source: ' if (parentLevel == 1) {',
},
],
errorCount: 0,
warningCount: 3,
},
fixtureWarnings,
{
filePath: 'C:\\js\\errors.js',
messages: [
Expand Down Expand Up @@ -174,3 +175,42 @@ test('Given results meta, logs too many warnings error', (t) => {
);
});

test('Does not log task complete without configuration', (t) => {
t.notRegex(formatter([]), /##vso\[task.complete result=/);
t.notRegex(formatter(fixture), /##vso\[task.complete result=/);
t.notRegex(formatter([fixtureWarnings]), /##vso\[task.complete result=/);
t.notRegex(
formatter([], {
maxWarningsExceeded: {
maxWarnings: 7,
foundWarnings: 9,
},
}),
/##vso\[task.complete result=/
);
});

test('Logs task complete with configuration', (t) => {
const shouldLogTaskComplete = formatter.shouldLogTaskComplete;
formatter.shouldLogTaskComplete = () => true;
try {
t.notRegex(formatter([]), /##vso\[task.complete result=/);
t.regex(formatter(fixture), /##vso\[task.complete result=Failed;\]/);
t.regex(
formatter([fixtureWarnings]),
/##vso\[task.complete result=SucceededWithIssues;\]/
);
t.regex(
formatter([], {
maxWarningsExceeded: {
maxWarnings: 7,
foundWarnings: 9,
},
}),
/##vso\[task.complete result=Failed;\]/
);
} finally {
formatter.shouldLogTaskComplete = shouldLogTaskComplete;
}
});

0 comments on commit b1916aa

Please sign in to comment.