Skip to content

Commit

Permalink
feat: improve logs for contribution check (#121)
Browse files Browse the repository at this point in the history
In log trace:

Put comment links
Log the step failed
  • Loading branch information
benjaminParisel authored Oct 25, 2023
1 parent 2d675f0 commit ec8ff37
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
38 changes: 23 additions & 15 deletions packages/pr-antora-content-guidelines-checker/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ function publishComment(octokit, template, commentBody, prNumber) {
if (commentBody) {
if (exists && id) {
core.debug(`Update comment ${id}`);
yield updateComment({ octokit, comment_id: id, body: commentBody });
return yield updateComment({
octokit,
comment_id: id,
body: commentBody,
});
}
else {
core.debug(`Create comment for #${prNumber}`);
yield createComment({ octokit, body: commentBody, prNumber });
return yield createComment({ octokit, body: commentBody, prNumber });
}
}
});
Expand Down Expand Up @@ -113,25 +117,23 @@ function isCommentExist({ octokit, template, prNumber }) {
exports.isCommentExist = isCommentExist;
function createComment({ octokit, body, prNumber }) {
return __awaiter(this, void 0, void 0, function* () {
const comment = yield octokit.rest.issues.createComment({
return yield octokit.rest.issues.createComment({
issue_number: prNumber,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
body: body,
});
return comment === null || comment === void 0 ? void 0 : comment.id;
});
}
exports.createComment = createComment;
function updateComment({ octokit, body, comment_id }) {
return __awaiter(this, void 0, void 0, function* () {
const comment = yield octokit.rest.issues.updateComment({
return yield octokit.rest.issues.updateComment({
comment_id: comment_id,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
body: body,
});
return comment === null || comment === void 0 ? void 0 : comment.id;
});
}
exports.updateComment = updateComment;
Expand Down Expand Up @@ -217,38 +219,44 @@ function run() {
if (core.getInput("forbidden-pattern-to-check") !== "") {
steps.push(new ForbiddenPatternStep_1.ForbiddenPatternStep(modifiedFiles, filesToCheckInput, forbiddenPatternToCheckInput));
}
core.info("Input parameters:");
core.startGroup("Input parameters:");
core.info(`* files-to-check: ${filesToCheckInput.join(", ")}`);
core.info(`* attributes-to-check: ${attributesToCheckInput}`);
core.info(`* forbidden-pattern-to-check: ${forbiddenPatternToCheckInput}`);
core.endGroup();
for (const step of steps) {
core.debug(`------- ${step.name} -------`);
let stepResult = yield step.validate(octokit, modifiedFiles);
actionResult.push(stepResult);
}
core.setOutput("checker-result", actionResult);
const filterResultOnError = actionResult.filter((result) => result.status === validation_1.Status.ERROR);
const errorsStep = steps.filter((step) => { var _a; return ((_a = step.stepResult) === null || _a === void 0 ? void 0 : _a.status) === validation_1.Status.ERROR; });
const prNumber = (_c = (_b = (_a = github === null || github === void 0 ? void 0 : github.context) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.pull_request) === null || _c === void 0 ? void 0 : _c.number;
if (filterResultOnError.length >= 1) {
core.setFailed(`This PR did not meet all the guidelines, see PR comments for details.`);
if (errorsStep.length >= 1) {
core.info(`❌ This following checks are failed: `);
errorsStep.forEach((result) => {
core.info(` * ${result.name}`);
});
let comment;
if (prNumber) {
let commentBody = template + "# Contribution Guidelines checks\n";
commentBody += `The content of the files modified by this Pull Request doesn't match the Contribution Guidelines. \n
Please update the following files.\n`;
commentBody += `The content of the files modified by this Pull Request doesn't match the Contribution Guidelines. \n \n Please update the following files.\n`;
steps.forEach((step) => {
commentBody += step.formatCommentBody();
});
core.info(`Publish comment for PR #${prNumber}`);
yield (0, github_utils_1.publishComment)(octokit, template, commentBody, prNumber);
comment = yield (0, github_utils_1.publishComment)(octokit, template, commentBody, prNumber);
core.info(`📝 Publish comment for PR #${prNumber}`);
core.info(`💡 See ${comment.data.html_url} for more details`);
}
core.setFailed(`❌ This PR did not meet all the guidelines, see PR comments for details. (${comment.data.html_url})`);
}
else {
const { exists, id } = yield (0, github_utils_1.isCommentExist)({
octokit,
template,
prNumber,
});
core.info(`The Contribution follows the guideline.`);
core.info(`The Contribution follows the guideline. Well done !`);
if (exists && id) {
core.info(`Delete oldest comment for PR #${prNumber}`);
yield (0, github_utils_1.deleteComment)({ octokit, commentIdToDelete: id });
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ export async function publishComment(
if (commentBody) {
if (exists && id) {
core.debug(`Update comment ${id}`);
await updateComment({ octokit, comment_id: id, body: commentBody });
return await updateComment({
octokit,
comment_id: id,
body: commentBody,
});
} else {
core.debug(`Create comment for #${prNumber}`);
await createComment({ octokit, body: commentBody, prNumber });
return await createComment({ octokit, body: commentBody, prNumber });
}
}
}
Expand Down Expand Up @@ -73,22 +77,20 @@ export async function isCommentExist({ octokit, template, prNumber }) {
};
}
export async function createComment({ octokit, body, prNumber }) {
const comment = await octokit.rest.issues.createComment({
return await octokit.rest.issues.createComment({
issue_number: prNumber,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
body: body,
});
return comment?.id;
}
export async function updateComment({ octokit, body, comment_id }) {
const comment = await octokit.rest.issues.updateComment({
return await octokit.rest.issues.updateComment({
comment_id: comment_id,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
body: body,
});
return comment?.id;
}

export async function deleteComment({ octokit, commentIdToDelete }) {
Expand Down
38 changes: 23 additions & 15 deletions packages/pr-antora-content-guidelines-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,55 @@ async function run(): Promise<void> {
)
);
}

core.info("Input parameters:");
core.startGroup("Input parameters:");
core.info(`* files-to-check: ${filesToCheckInput.join(", ")}`);
core.info(`* attributes-to-check: ${attributesToCheckInput}`);
core.info(`* forbidden-pattern-to-check: ${forbiddenPatternToCheckInput}`);
core.endGroup();

for (const step of steps) {
core.debug(`------- ${step.name} -------`);
let stepResult = await step.validate(octokit, modifiedFiles);
actionResult.push(stepResult);
}

core.setOutput("checker-result", actionResult);

const filterResultOnError = actionResult.filter(
(result) => result.status === Status.ERROR
const errorsStep = steps.filter(
(step) => step.stepResult?.status === Status.ERROR
);

const prNumber = github?.context?.payload?.pull_request?.number;
if (filterResultOnError.length >= 1) {
core.setFailed(
`This PR did not meet all the guidelines, see PR comments for details.`
);
if (errorsStep.length >= 1) {
core.info(`❌ This following checks are failed: `);
errorsStep.forEach((result) => {
core.info(` * ${result.name}`);
});
let comment;
if (prNumber) {
let commentBody: string =
template + "# Contribution Guidelines checks\n";
commentBody += `The content of the files modified by this Pull Request doesn't match the Contribution Guidelines. \n
Please update the following files.\n`;
commentBody += `The content of the files modified by this Pull Request doesn't match the Contribution Guidelines. \n \n Please update the following files.\n`;
steps.forEach((step) => {
commentBody += step.formatCommentBody();
});
core.info(`Publish comment for PR #${prNumber}`);
await publishComment(octokit, template, commentBody, prNumber);
comment = await publishComment(
octokit,
template,
commentBody,
prNumber
);
core.info(`📝 Publish comment for PR #${prNumber}`);
core.info(`💡 See ${comment.data.html_url} for more details`);
}
core.setFailed(
`❌ This PR did not meet all the guidelines, see PR comments for details. (${comment.data.html_url})`
);
} else {
const { exists, id } = await isCommentExist({
octokit,
template,
prNumber,
});
core.info(`The Contribution follows the guideline.`);
core.info(`The Contribution follows the guideline. Well done !`);
if (exists && id) {
core.info(`Delete oldest comment for PR #${prNumber}`);
await deleteComment({ octokit, commentIdToDelete: id });
Expand Down

0 comments on commit ec8ff37

Please sign in to comment.