Skip to content

Commit

Permalink
remove comment on success
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminParisel committed Oct 12, 2023
1 parent a242b3e commit 0d3ba32
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
32 changes: 24 additions & 8 deletions packages/pr-files-checker/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.updateComment = exports.createComment = exports.isCommentExist = exports.getModifiedFiles = exports.getFileContent = exports.publishComment = void 0;
exports.deleteComment = exports.updateComment = exports.createComment = exports.isCommentExist = exports.getModifiedFiles = exports.getFileContent = exports.publishComment = void 0;
const github = __importStar(__nccwpck_require__(5438));
const core = __importStar(__nccwpck_require__(2186));
// Publish a comment on the PR with the results
Expand Down Expand Up @@ -131,6 +131,17 @@ function updateComment({ octokit, body, comment_id }) {
});
}
exports.updateComment = updateComment;
function deleteComment({ octokit, commentIdToDelete }) {
return __awaiter(this, void 0, void 0, function* () {
yield octokit.rest.issues.deleteComment({
issue_number: github.context.issue.number,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
comment_id: commentIdToDelete,
});
});
}
exports.deleteComment = deleteComment;


/***/ }),
Expand Down Expand Up @@ -179,6 +190,7 @@ const validation_1 = __nccwpck_require__(5375);
const github_utils_1 = __nccwpck_require__(6270);
const AttributesCheckingStep_1 = __nccwpck_require__(1591);
const ForbiddenPatternStep_1 = __nccwpck_require__(7637);
const template = "<!-- previewCommentContributionChecker -->\n";
function run() {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -202,27 +214,31 @@ function run() {
steps.push(new ForbiddenPatternStep_1.ForbiddenPatternStep(modifiedFiles, filesToCheckInput, forbiddenPatternToCheckInput));
}
core.info("Input parameters:");
core.info(`files-to-check: ${filesToCheckInput.join(", ")}`);
core.info(`attributes-to-check: ${attributesToCheckInput}`);
core.info(`forbidden-pattern-to-check: ${forbiddenPatternToCheckInput}`);
core.info(`* files-to-check: ${filesToCheckInput.join(", ")}`);
core.info(`* attributes-to-check: ${attributesToCheckInput}`);
core.info(`* forbidden-pattern-to-check: ${forbiddenPatternToCheckInput}`);
for (const step of steps) {
core.debug(`------- ${step.name} -------`);
let stepResult = yield step.validate(octokit, modifiedFiles);
actionResult.concat(stepResult);
}
core.setOutput("checker-result", actionResult);
const filterResultOnError = actionResult.filter((result) => result.status === validation_1.Status.ERROR);
if (filterResultOnError.length > 1) {
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 didn't following all guideline, check the comments to see more details`);
}
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;
else {
// Delete old comment if exist
const { id } = yield (0, github_utils_1.isCommentExist)({ octokit, template, prNumber });
yield (0, github_utils_1.deleteComment)({ octokit, commentIdToDelete: id });
}
if (prNumber) {
const template = "<!-- previewCommentContributionChecker -->\n";
let commentBody = template + "# PR Guideline checker\n";
steps.forEach((step) => {
commentBody += step.formatCommentBody();
});
core.debug(`Publish comment for PR ${prNumber}`);
core.info(`Publish comment for PR ${prNumber}`);
yield (0, github_utils_1.publishComment)(octokit, template, commentBody, prNumber);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pr-files-checker/dist/index.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions packages/pr-files-checker/src/github-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ export async function updateComment({ octokit, body, comment_id }) {
});
return comment?.id;
}

export async function deleteComment ({octokit, commentIdToDelete}) {
await octokit.rest.issues.deleteComment({
issue_number: github.context.issue.number,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
comment_id: commentIdToDelete,
});
}
23 changes: 15 additions & 8 deletions packages/pr-files-checker/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { ActionResult, Status, ValidationStep } from "./validation";
import { getModifiedFiles, publishComment } from "./github-utils";
import { getModifiedFiles, publishComment ,isCommentExist, deleteComment } from "./github-utils";
import { AttributesCheckingStep } from "./steps/AttributesCheckingStep";
import { ForbiddenPatternStep } from "./steps/ForbiddenPatternStep";

const template = "<!-- previewCommentContributionChecker -->\n";
async function run(): Promise<void> {
try {
const token = core.getInput("github-token");
const octokit = github.getOctokit(token);
let actionResult: ActionResult[] = [];
const modifiedFiles: string[] = await getModifiedFiles(octokit);

const filesToCheckInput = core.getInput("files-to-check").split(",");
const attributesToCheckInput = core
.getInput("attributes-to-check")
Expand Down Expand Up @@ -40,9 +42,9 @@ async function run(): Promise<void> {
}

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

for (const step of steps) {
core.debug(`------- ${step.name} -------`);
Expand All @@ -55,20 +57,25 @@ async function run(): Promise<void> {
const filterResultOnError = actionResult.filter(
(result) => result.status === Status.ERROR
);
if (filterResultOnError.length > 1) {

const prNumber = github?.context?.payload?.pull_request?.number;

if (filterResultOnError.length >= 1) {
core.setFailed(
`This PR didn't following all guideline, check the comments to see more details`
);
} else {
// Delete old comment if exist
const {id} = await isCommentExist({ octokit, template, prNumber })
await deleteComment({octokit, commentIdToDelete: id});
}

const prNumber = github?.context?.payload?.pull_request?.number;
if (prNumber) {
const template = "<!-- previewCommentContributionChecker -->\n";
let commentBody: string = template + "# PR Guideline checker\n";
steps.forEach((step) => {
commentBody += step.formatCommentBody();
});
core.debug(`Publish comment for PR ${prNumber}`);
core.info(`Publish comment for PR ${prNumber}`);
await publishComment(octokit, template, commentBody, prNumber);
}
} catch (error) {
Expand Down

0 comments on commit 0d3ba32

Please sign in to comment.