Skip to content

Commit

Permalink
feat: post chunked annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
megheaiulian committed Feb 27, 2020
1 parent 2c92230 commit 04ee203
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space

[*.md]
trim_trailing_whitespace = false
69 changes: 48 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const exitWithError = err => {
}
process.exit(1);
},
{ GITHUB_SHA, GITHUB_EVENT_PATH, GITHUB_TOKEN, GITHUB_WORKSPACE } = process.env;
{
GITHUB_SHA, GITHUB_EVENT_PATH, GITHUB_TOKEN, GITHUB_WORKSPACE
} = process.env;

if (GITHUB_TOKEN == null) {
exitWithError(new Error('Missing Github token'));
Expand Down Expand Up @@ -112,8 +114,8 @@ const headers = {
const singleLine = m.line === m.endLine || m.endLine === undefined;
return {
path,
start_column: singleLine && m.column,
end_column: singleLine && m.endColumn,
start_column: singleLine ? m.column : undefined,
end_column: singleLine ? m.endColumn || m.column : undefined,
start_line: m.line,
end_line: m.endLine || m.line,
annotation_level: levels[m.severity],
Expand All @@ -124,39 +126,64 @@ const headers = {
}));
}, []),

{ errorCount, warningCount } = report;

{
errorCount, warningCount
} = report;
return {
conclusion: errorCount > 0 ? 'failure' : 'success',
output: {
title: checkName,
summary: `${ errorCount } error(s), ${ warningCount } warning(s) found`,
text: 'A little bit of text',
annotations
}
annotations,
errorCount,
warningCount
};
},
updateCheck = async (id, conclusion, output) =>
updateCheck = async (id, opts = {}) =>
await request(`https://api.github.com/repos/${ owner }/${ repo }/check-runs/${ id }`, {
method: 'PATCH',
headers,
body: {
name: checkName,
head_sha: checkSha,
status: 'completed',
completed_at: (new Date()).toISOString(),
conclusion,
output
...opts
}
}),
updateChecks = async (id, {
errorCount, warningCount, annotations
}) => {
const chunkSize = 50,
chunksLength = Math.ceil(annotations.length / chunkSize);

await Promise.all(new Array(chunksLength).fill().map((_, i) => updateCheck(id, {
status: 'in_progress',
output: {
title: checkName,
summary: `${ errorCount } error(s), ${ warningCount } warning(s) found`,
annotations: annotations.slice(i * chunkSize, (i + 1) * chunkSize)
}
})));

await updateCheck(id, {
status: 'completed',
completed_at: (new Date()).toISOString(),
conclusion: errorCount > 0 ? 'failure' : 'success',
output: {
title: checkName,
summary: `${ errorCount } error(s), ${ warningCount } warning(s) found`
}
});
},
run = async () => {
const id = await createCheck();
try {
const { conclusion, output } = await eslint();
console.log(conclusion, output);
await updateCheck(id, conclusion, output);
await updateChecks(id, await eslint());
} catch (err) {
await updateCheck(id, 'failure');
await updateCheck(id, {
conclusion: 'failure',
status: 'completed',
completed_at: (new Date()).toISOString(),
output: {
title: checkName,
summary: `Error while performing the check: ${err.message}`
}
});
exitWithError(err);
}
};
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04ee203

Please # to comment.