Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for regex flags #275

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- uses: morrisoncole/[email protected]
with:
title-regex: "#[eE][xX]-[0-9]+"
title-regex-flags: "i"
on-failed-regex-fail-action: false
on-failed-regex-request-changes: false
on-failed-regex-create-review: true
Expand Down
257 changes: 131 additions & 126 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,132 +5,137 @@ require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const repoTokenInput = core.getInput("repo-token", { required: true });
const octokit = github.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";
const onFailedRegexRequestChanges = core.getInput("on-failed-regex-request-changes") === "true";
const onSucceededRegexDismissReviewComment = core.getInput("on-succeeded-regex-dismiss-review-comment");
function run() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const githubContext = github.context;
const pullRequest = githubContext.issue;
const titleRegex = new RegExp(titleRegexInput);
const title = (_b = (_a = githubContext.payload.pull_request) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : "";
const comment = onFailedRegexCommentInput.replace("%regex%", titleRegex.source);
core.debug(`Title Regex: ${titleRegex.source}`);
core.debug(`Title: ${title}`);
const titleMatchesRegex = titleRegex.test(title);
if (!titleMatchesRegex) {
if (onFailedRegexCreateReviewInput) {
createReview(comment, pullRequest);
}
if (onFailedRegexFailActionInput) {
core.setFailed(comment);
}
}
else {
core.debug(`Regex pass`);
if (onFailedRegexCreateReviewInput) {
core.debug(`Dismissing review`);
yield dismissReview(pullRequest);
core.debug(`Review dimissed`);
}
}
});
}
function createReview(comment, pullRequest) {
void octokit.rest.pulls.createReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
body: comment,
event: onFailedRegexRequestChanges ? "REQUEST_CHANGES" : "COMMENT",
});
}
function dismissReview(pullRequest) {
return __awaiter(this, void 0, void 0, function* () {
const reviews = yield octokit.rest.pulls.listReviews({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
});
reviews.data.forEach((review) => {
if (review.user != null &&
isGitHubActionUser(review.user.login) &&
alreadyRequiredChanges(review.state)) {
core.debug(`Already required changes`);
if (review.state === "COMMENTED") {
octokit.rest.issues.createComment({
owner: pullRequest.owner,
repo: pullRequest.repo,
issue_number: pullRequest.number,
body: onSucceededRegexDismissReviewComment,
});
}
else {
octokit.rest.pulls.dismissReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
review_id: review.id,
message: onSucceededRegexDismissReviewComment,
});
}
}
});
});
}
function isGitHubActionUser(login) {
const gitHubUser = login === "github-actions[bot]";
core.debug(`isGitHubActionUser output: ${gitHubUser} (login is: ${login})`);
return gitHubUser;
}
function alreadyRequiredChanges(state) {
const requiredChanges = state === "CHANGES_REQUESTED" || state === "COMMENTED";
core.debug(`alreadyRequiredChanges output: ${requiredChanges} (state is: ${state})`);
return requiredChanges;
}
run().catch((error) => {
core.setFailed(error);
});

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const repoTokenInput = core.getInput("repo-token", { required: true });
const octokit = github.getOctokit(repoTokenInput);
const titleRegexInput = core.getInput("title-regex", {
required: true,
});
const titleRegexInputFlags = core.getInput("title-regex-flags");
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";
const onFailedRegexRequestChanges = core.getInput("on-failed-regex-request-changes") === "true";
const onSucceededRegexDismissReviewComment = core.getInput("on-succeeded-regex-dismiss-review-comment");
function run() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const githubContext = github.context;
const pullRequest = githubContext.issue;
const titleRegex = new RegExp(titleRegexInput, titleRegexInputFlags ? titleRegexInputFlags : undefined);
const title = (_b = (_a = githubContext.payload.pull_request) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : "";
const comment = onFailedRegexCommentInput.replace("%regex%", titleRegex.source);
core.debug(`Title Regex: ${titleRegex.source}`);
core.debug(`Title: ${title}`);
const titleMatchesRegex = titleRegex.test(title);
if (!titleMatchesRegex) {
if (onFailedRegexCreateReviewInput) {
createReview(comment, pullRequest);
}
if (onFailedRegexFailActionInput) {
core.setFailed(comment);
}
}
else {
core.debug(`Regex pass`);
if (onFailedRegexCreateReviewInput) {
core.debug(`Dismissing review`);
yield dismissReview(pullRequest);
core.debug(`Review dimissed`);
}
}
});
}
function createReview(comment, pullRequest) {
void octokit.rest.pulls.createReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
body: comment,
event: onFailedRegexRequestChanges ? "REQUEST_CHANGES" : "COMMENT",
});
}
function dismissReview(pullRequest) {
return __awaiter(this, void 0, void 0, function* () {
const reviews = yield octokit.rest.pulls.listReviews({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
});
reviews.data.forEach((review) => {
if (review.user != null &&
isGitHubActionUser(review.user.login) &&
alreadyRequiredChanges(review.state)) {
core.debug(`Already required changes`);
if (review.state === "COMMENTED") {
octokit.rest.issues.createComment({
owner: pullRequest.owner,
repo: pullRequest.repo,
issue_number: pullRequest.number,
body: onSucceededRegexDismissReviewComment,
});
}
else {
octokit.rest.pulls.dismissReview({
owner: pullRequest.owner,
repo: pullRequest.repo,
pull_number: pullRequest.number,
review_id: review.id,
message: onSucceededRegexDismissReviewComment,
});
}
}
});
});
}
function isGitHubActionUser(login) {
const gitHubUser = login === "github-actions[bot]";
core.debug(`isGitHubActionUser output: ${gitHubUser} (login is: ${login})`);
return gitHubUser;
}
function alreadyRequiredChanges(state) {
const requiredChanges = state === "CHANGES_REQUESTED" || state === "COMMENTED";
core.debug(`alreadyRequiredChanges output: ${requiredChanges} (state is: ${state})`);
return requiredChanges;
}
run().catch((error) => {
core.setFailed(error);
});


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
Expand Down Expand Up @@ -35,6 +39,7 @@ const octokit = github.getOctokit(repoTokenInput);
const titleRegexInput = core.getInput("title-regex", {
required: true,
});
const titleRegexInputFlags = core.getInput("title-regex-flags");
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";
Expand All @@ -45,7 +50,7 @@ function run() {
return __awaiter(this, void 0, void 0, function* () {
const githubContext = github.context;
const pullRequest = githubContext.issue;
const titleRegex = new RegExp(titleRegexInput);
const titleRegex = new RegExp(titleRegexInput, titleRegexInputFlags ? titleRegexInputFlags : undefined);
const title = (_b = (_a = githubContext.payload.pull_request) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : "";
const comment = onFailedRegexCommentInput.replace("%regex%", titleRegex.source);
core.debug(`Title Regex: ${titleRegex.source}`);
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const octokit = github.getOctokit(repoTokenInput);
const titleRegexInput: string = core.getInput("title-regex", {
required: true,
});
const titleRegexInputFlags: string = core.getInput("title-regex-flags");

const onFailedRegexCreateReviewInput: boolean =
core.getInput("on-failed-regex-create-review") === "true";
const onFailedRegexCommentInput: string = core.getInput(
Expand All @@ -24,7 +26,7 @@ async function run(): Promise<void> {
const githubContext = github.context;
const pullRequest = githubContext.issue;

const titleRegex = new RegExp(titleRegexInput);
const titleRegex = new RegExp(titleRegexInput, titleRegexInputFlags ? titleRegexInputFlags : undefined);
const title: string =
(githubContext.payload.pull_request?.title as string) ?? "";
const comment = onFailedRegexCommentInput.replace(
Expand Down