Skip to content

Commit

Permalink
Improve lint summary and use it for GitHub checks
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
samuelmeuli committed Jan 9, 2020
1 parent 65fe4e1 commit fcf8fd8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
21 changes: 10 additions & 11 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { readFileSync } = require("fs");
const { name: actionName } = require("../package");
const { getEnv, getInput, log } = require("./utils/action");
const request = require("./utils/request");
const { capitalizeFirstLetter } = require("./utils/string");

/**
* Returns information about the GitHub repository and action trigger event
Expand Down Expand Up @@ -48,15 +49,15 @@ function getContext() {

/**
* Creates a new check on GitHub which annotates the relevant commit with linting errors
* @param {string} checkName - Name which will be displayed in the check list
* @param {string} linterName - Name of the linter for which a check should be created
* @param {string} sha - SHA of the commit which should be annotated
* @param {{actor: string, branch: string, event: object, eventName: string, repository: string,
* token: string, username: string, workspace: string}} context - Object information about the
* GitHub repository and action trigger event
* @param {{isSuccess: boolean, warning: [], error: []}} lintResult - Parsed lint result
* @param {string} summary - Summary for the GitHub check
*/
async function createCheck(checkName, sha, context, lintResult, summary) {
async function createCheck(linterName, sha, context, lintResult, summary) {
let annotations = [];
for (const level of ["warning", "error"]) {
annotations = [
Expand All @@ -74,23 +75,23 @@ async function createCheck(checkName, sha, context, lintResult, summary) {
// Only use the first 50 annotations (limit for a single API request)
if (annotations.length > 50) {
log(
`There are more than 50 errors/warnings from ${checkName}. Annotations are created for the first 50 violations only.`,
`There are more than 50 errors/warnings from ${linterName}. Annotations are created for the first 50 issues only.`,
);
annotations = annotations.slice(0, 50);
}

const body = {
name: checkName,
name: linterName,
head_sha: sha,
conclusion: lintResult.isSuccess ? "success" : "failure",
output: {
title: checkName,
summary,
title: capitalizeFirstLetter(summary),
summary: `${linterName} found ${summary}`,
annotations,
},
};
try {
log(`Creating ${annotations.length} annotations for ${checkName}…`);
log(`Creating GitHub check with ${annotations.length} annotations for ${linterName}…`);
await request(
`https://api.github.com/repos/${context.username}/${context.repository}/check-runs`,
{
Expand All @@ -105,12 +106,10 @@ async function createCheck(checkName, sha, context, lintResult, summary) {
body,
},
);
log(`${checkName} annotations created successfully`);
log(`${linterName} check created successfully`);
} catch (err) {
log(err, "error");
throw new Error(
`Error trying to create "${checkName}" annotations using GitHub API: ${err.message}`,
);
throw new Error(`Error trying to create GitHub check for ${linterName}: ${err.message}`);
}
}

Expand Down
23 changes: 4 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const git = require("./git");
const github = require("./github");
const linters = require("./linters");
const { getInput, log } = require("./utils/action");
const { getSummary } = require("./utils/lint-result");

const GIT_NAME = actionName;
const GIT_EMAIL = `[email protected]`;
Expand Down Expand Up @@ -62,27 +63,11 @@ async function runAction() {

// Parse output of linting command
const lintResult = linter.parseOutput(context.workspace, lintOutput);
log(
`Linting result of ${linter.name} is considered a ${
lintResult.isSuccess ? "success" : "failure"
}`,
);

// Build and log a summary of linting errors/warnings
let summary;
if (lintResult.warning.length > 0 && lintResult.error.length > 0) {
summary = `Found ${lintResult.error.length} errors and ${lintResult.warning.length} warnings with ${linter.name}`;
} else if (lintResult.error.length > 0) {
summary = `Found ${lintResult.error.length} errors with ${linter.name}`;
} else if (lintResult.warning.length > 0) {
summary = `Found ${lintResult.warning.length} warnings with ${linter.name}`;
} else {
summary = `No code style issues found with ${linter.name}`;
}
log(summary);
const summary = getSummary(lintResult);
log(`${linter.name} found ${summary} (${lintResult.isSuccess ? "success" : "failure"})`);

if (autoFix) {
// Commit and push changes from auto-fixing
// Commit and push auto-fix changes
git.commitChanges(commitMsg.replace(/\${linter}/g, linter.name));
git.pushChanges(context);
}
Expand Down
25 changes: 25 additions & 0 deletions src/utils/lint-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ function initLintResult() {
};
}

/**
* Returns a text summary of the number of issues found when linting
* @param {{isSuccess: boolean, warning: object[], error: object[]}} lintResult - Parsed linter
* output
* @returns {string} - Text summary
*/
function getSummary(lintResult) {
const nrErrors = lintResult.error.length;
const nrWarnings = lintResult.warning.length;
// Build and log a summary of linting errors/warnings
if (nrWarnings > 0 && nrErrors > 0) {
return `${nrErrors} error${nrErrors > 1 ? "s" : ""} and ${nrWarnings} warning${
nrWarnings > 1 ? "s" : ""
}`;
}
if (nrErrors > 0) {
return `${nrErrors} error${nrErrors > 1 ? "s" : ""}`;
}
if (nrWarnings > 0) {
return `${nrWarnings} warning${nrWarnings > 1 ? "s" : ""}`;
}
return `no issues`;
}

module.exports = {
getSummary,
initLintResult,
};

0 comments on commit fcf8fd8

Please sign in to comment.