Skip to content

Commit

Permalink
feat: add create review/fail on regex mismatch configurations #GH-111
Browse files Browse the repository at this point in the history
  • Loading branch information
MorrisonCole committed Sep 16, 2020
1 parent 525b2e7 commit 93fab5f
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 65 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ jobs:
- uses: morrisoncole/pr-lint-action@v1.2.3
with:
title-regex: "#EX-[0-9]+"
on-failed-regex-create-review: true
on-failed-regex-comment:
"This is just an example. Failed regex: `%regex%`!"
on-failed-regex-fail-action: false
repo-token: "${{ secrets.GITHUB_TOKEN }}"
```
## Changelog
### v1.3.0
- Adds [#111](https://github.com/MorrisonCole/pr-lint-action/issues/111),
ability to specify whether to create a review and whether to fail the action
on a regex mismatch independently.
### v1.2.3
Internal refactoring only:
Expand Down
14 changes: 12 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Pull Request Linter"
description: "Ensures your PR title matches a given regex"
description: "Ensure your PR titles match a given regex."
author: "MorrisonCole"
branding:
icon: "check"
Expand All @@ -9,12 +9,22 @@ inputs:
description: "Regex to ensure PR title matches. Allows anything by default."
required: true
default: ".*"
on-failed-regex-create-review:
description:
"Whether the action should create a PR review & comment when the regex
doesn't match."
required: false
default: true
on-failed-regex-comment:
description:
"Comment for the bot to post on PRs that fail the regex. Use %regex% to
reference regex."
required: true
required: false
default: "PR title failed to match %regex%."
on-failed-regex-fail-action:
description: "Whether the action should fail when the regex doesn't match."
required: false
default: false
repo-token:
description:
"Github token with access to the repository (secrets.GITHUB_TOKEN)."
Expand Down
77 changes: 47 additions & 30 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,64 @@ Object.defineProperty(exports, "__esModule", { value: true });
const github_1 = require("@actions/github/lib/github");
const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
const repoTokenInput = core.getInput("repo-token", { required: true });
const githubClient = github_1.getOctokit(repoTokenInput);
const titleRegexInput = core.getInput("title-regex", {
required: true,
});
const onFailedRegexCreateReviewInput = core.getInput("on-failed-regex-create-review") == "true";
const onFailedRegexCommentInput = core.getInput("on-failed-regex-comment");
const onFailedRegexFailActionInput = core.getInput("on-failed-regex-fail-action") == "true";
async function run() {
const githubContext = github.context;
const githubToken = core.getInput("repo-token");
const githubClient = github_1.getOctokit(githubToken);
const pr = githubContext.issue;
const titleRegex = new RegExp(core.getInput("title-regex"));
const pullRequest = githubContext.issue;
const titleRegex = new RegExp(titleRegexInput);
const title = githubContext.payload.pull_request?.title ?? "";
const onFailedRegexComment = core
.getInput("on-failed-regex-comment")
.replace("%regex%", titleRegex.source);
const comment = onFailedRegexCommentInput.replace("%regex%", titleRegex.source);
core.debug(`Title Regex: ${titleRegex.source}`);
core.debug(`Title: ${title}`);
const titleMatchesRegex = titleRegex.test(title);
if (!titleMatchesRegex) {
void githubClient.pulls.createReview({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
body: onFailedRegexComment,
event: "REQUEST_CHANGES",
});
if (onFailedRegexCreateReviewInput) {
createReview(comment, pullRequest);
}
if (onFailedRegexFailActionInput) {
core.setFailed(comment);
}
}
else {
const reviews = await githubClient.pulls.listReviews({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
});
reviews.data.forEach((review) => {
if (review.user.login == "github-actions[bot]") {
void githubClient.pulls.dismissReview({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
review_id: review.id,
message: "All good!",
});
}
});
if (onFailedRegexCreateReviewInput) {
await dismissReview(pullRequest);
}
}
}
function createReview(comment, pullRequest) {
void githubClient.pulls.createReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
body: comment,
event: "REQUEST_CHANGES",
});
}
async function dismissReview(pullRequest) {
const reviews = await githubClient.pulls.listReviews({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
});
reviews.data.forEach((review) => {
if (review.user.login == "github-actions[bot]") {
void githubClient.pulls.dismissReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
review_id: review.id,
message: "All good!",
});
}
});
}
run().catch((error) => {
core.setFailed(error);
});
99 changes: 66 additions & 33 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,87 @@ import { getOctokit } from "@actions/github/lib/github";
import * as core from "@actions/core";
import * as github from "@actions/github";

const repoTokenInput = core.getInput("repo-token", { required: true });
const githubClient = getOctokit(repoTokenInput);

const titleRegexInput: string = core.getInput("title-regex", {
required: true,
});
const onFailedRegexCreateReviewInput: boolean =
core.getInput("on-failed-regex-create-review") == "true";
const onFailedRegexCommentInput: string = core.getInput(
"on-failed-regex-comment"
);
const onFailedRegexFailActionInput: boolean =
core.getInput("on-failed-regex-fail-action") == "true";

async function run(): Promise<void> {
const githubContext = github.context;
const githubToken = core.getInput("repo-token");
const githubClient = getOctokit(githubToken);
const pullRequest = githubContext.issue;

const pr = githubContext.issue;

const titleRegex = new RegExp(core.getInput("title-regex"));
const titleRegex = new RegExp(titleRegexInput);
const title: string =
(githubContext.payload.pull_request?.title as string) ?? "";

const onFailedRegexComment = core
.getInput("on-failed-regex-comment")
.replace("%regex%", titleRegex.source);
const comment = onFailedRegexCommentInput.replace(
"%regex%",
titleRegex.source
);

core.debug(`Title Regex: ${titleRegex.source}`);
core.debug(`Title: ${title}`);

const titleMatchesRegex: boolean = titleRegex.test(title);
if (!titleMatchesRegex) {
void githubClient.pulls.createReview({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
body: onFailedRegexComment,
event: "REQUEST_CHANGES",
});
if (onFailedRegexCreateReviewInput) {
createReview(comment, pullRequest);
}
if (onFailedRegexFailActionInput) {
core.setFailed(comment);
}
} else {
const reviews = await githubClient.pulls.listReviews({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
});

reviews.data.forEach((review) => {
if (review.user.login == "github-actions[bot]") {
void githubClient.pulls.dismissReview({
owner: pr.owner,
repo: pr.repo,
pull_number: pr.number,
review_id: review.id,
message: "All good!",
});
}
});
if (onFailedRegexCreateReviewInput) {
await dismissReview(pullRequest);
}
}
}

function createReview(
comment: string,
pullRequest: { owner: string; repo: string; number: number }
) {
void githubClient.pulls.createReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
body: comment,
event: "REQUEST_CHANGES",
});
}

async function dismissReview(pullRequest: {
owner: string;
repo: string;
number: number;
}) {
const reviews = await githubClient.pulls.listReviews({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
});

reviews.data.forEach((review) => {
if (review.user.login == "github-actions[bot]") {
void githubClient.pulls.dismissReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
review_id: review.id,
message: "All good!",
});
}
});
}

run().catch((error) => {
core.setFailed(error);
});

0 comments on commit 93fab5f

Please sign in to comment.