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

hotfix: fix io/interface #19

Merged
merged 4 commits into from
Sep 25, 2023
Merged
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
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ inputs:
required: false
default: ''
title-regex-flags:
description: 'regex flags for title'
description: 'regex flags for title. if you want to use regex, you must set title and title-regex-flags: "true"'
required: false
default: ''
body-regex-flags:
description: 'regex flags for body'
description: 'regex flags for body. if you want to use regex, you must set body and body-regex-flags: "true"'
required: false
default: ''
github-token:
Expand Down
48 changes: 35 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9688,27 +9688,36 @@ async function validateIssueTitleAndBody(issueType, issueNumber, titleRegex, bod
}
if (issueType === 'issue') {
const { title, body } = await (0, utils_1.getIssueTitleAndBody)(issueNumber);
if (titleRegex && !titleRegex.test(title)) {
if (titleRegex && !validate(titleRegex, title)) {
return false;
}
if (bodyRegex && !bodyRegex.test(body)) {
if (bodyRegex && !validate(bodyRegex, body)) {
return false;
}
return true;
}
if (issueType === 'pull_request') {
const { title, body } = await (0, utils_1.getPullRequestTitleAndBody)(issueNumber);
if (titleRegex && !titleRegex.test(title)) {
if (titleRegex && !validate(titleRegex, title)) {
return false;
}
if (bodyRegex && !bodyRegex.test(body)) {
if (bodyRegex && !validate(bodyRegex, body)) {
return false;
}
return true;
}
throw new Error(`Invalid issue type: ${issueType}`);
}
exports.validateIssueTitleAndBody = validateIssueTitleAndBody;
function validate(match, str) {
if (!match) {
return true;
}
if (match instanceof RegExp) {
return match.test(str);
}
return str.includes(match);
}


/***/ }),
Expand Down Expand Up @@ -9901,16 +9910,29 @@ const github_1 = __nccwpck_require__(5438);
async function run() {
try {
const title = (0, core_1.getInput)('title') || '';
const titleRegexFlags = (0, core_1.getInput)('title-regex-flags') || '';
const titleRegex = new RegExp(title, titleRegexFlags);
const titleRegexFlags = (0, core_1.getInput)('title-regex-flags' || 0);
const body = (0, core_1.getInput)('body') || '';
const bodyRegexFlags = (0, core_1.getInput)('body-regex-flags') || '';
const bodyRegex = new RegExp(body, bodyRegexFlags);
const issueType = (0, core_1.getInput)('issue-type') || '';
const issueNumber = (0, core_1.getInput)('issue-number') || '';
const isAutoClose = (0, core_1.getInput)('is-auto-close') || '';
const octokit = (0, github_1.getOctokit)((0, core_1.getInput)('github-token', { required: true }));
const result = await (0, validate_1.validateIssueTitleAndBody)(issueType, parseInt(issueNumber), titleRegex, bodyRegex);
const issueType = (0, core_1.getInput)('issue-type') || 'issue';
const issueNumber = github_1.context.issue.number;
const isAutoClose = (0, core_1.getInput)('auto-close') || 'false';
isAutoClose === 'true' ? true : false;
let titleRegex;
let bodyRegex;
if (titleRegexFlags) {
titleRegex = new RegExp(title, titleRegexFlags);
}
else {
titleRegex = title;
}
if (bodyRegexFlags) {
bodyRegex = new RegExp(body, bodyRegexFlags);
}
else {
bodyRegex = body;
}
const result = await (0, validate_1.validateIssueTitleAndBody)(issueType, issueNumber, titleRegex, bodyRegex);
if (result === true) {
(0, core_1.setOutput)('result', 'true');
}
Expand All @@ -9921,14 +9943,14 @@ async function run() {
await octokit.rest.issues.createComment({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
issue_number: parseInt(issueNumber),
issue_number: issueNumber,
body: `Issue #${issueNumber} is not valid: Reason: ${result}: auto closing issue...`,
});
// Close issue
await octokit.rest.issues.update({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
issue_number: parseInt(issueNumber),
issue_number: issueNumber,
state: 'closed',
});
}
Expand Down
32 changes: 22 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ import { getOctokit, context } from '@actions/github';
export async function run() {
try {
const title = getInput('title') || '';
const titleRegexFlags = getInput('title-regex-flags') || '';
const titleRegex = new RegExp(title, titleRegexFlags);
const titleRegexFlags = getInput('title-regex-flags' || '');
const body = getInput('body') || '';
const bodyRegexFlags = getInput('body-regex-flags') || '';
const bodyRegex = new RegExp(body, bodyRegexFlags);
const issueType = getInput('issue-type') || '';
const issueNumber = getInput('issue-number') || '';
const isAutoClose = getInput('is-auto-close') || '';

const octokit = getOctokit(getInput('github-token', { required: true }));
const result = await validateIssueTitleAndBody(issueType, parseInt(issueNumber), titleRegex, bodyRegex);

const issueType = getInput('issue-type') || 'issue';
const issueNumber = context.issue.number;
const isAutoClose = getInput('auto-close') || 'false';
isAutoClose === 'true' ? true : false;

let titleRegex: RegExp | string | null;
let bodyRegex: RegExp | string | null;
if (titleRegexFlags === 'true') {
titleRegex = new RegExp(title);
} else {
titleRegex = title;
}
if (bodyRegexFlags === 'true') {
bodyRegex = new RegExp(body);
} else {
bodyRegex = body;
}
const result = await validateIssueTitleAndBody(issueType, issueNumber, titleRegex, bodyRegex);
if (result === true) {
setOutput('result', 'true');
} else {
Expand All @@ -25,15 +37,15 @@ export async function run() {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: parseInt(issueNumber),
issue_number: issueNumber,
body: `Issue #${issueNumber} is not valid: Reason: ${result}: auto closing issue...`,
});

// Close issue
await octokit.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: parseInt(issueNumber),
issue_number: issueNumber,
state: 'closed',
});
}
Expand Down
22 changes: 16 additions & 6 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,41 @@ import { getIssueTitleAndBody, getPullRequestTitleAndBody } from './utils';
export async function validateIssueTitleAndBody(
issueType: string,
issueNumber: number,
titleRegex: RegExp | null,
bodyRegex: RegExp | null,
titleRegex: RegExp | string | null,
bodyRegex: RegExp | string | null,
): Promise<boolean> {
if (!titleRegex && !bodyRegex) {
return true;
}
if (issueType === 'issue') {
const { title, body } = await getIssueTitleAndBody(issueNumber);
if (titleRegex && !titleRegex.test(title)) {
if (titleRegex && !validate(titleRegex, title)) {
return false;
}
if (bodyRegex && !bodyRegex.test(body)) {
if (bodyRegex && !validate(bodyRegex, body)) {
return false;
}
return true;
}
if (issueType === 'pull_request') {
const { title, body } = await getPullRequestTitleAndBody(issueNumber);
if (titleRegex && !titleRegex.test(title)) {
if (titleRegex && !validate(titleRegex, title)) {
return false;
}
if (bodyRegex && !bodyRegex.test(body)) {
if (bodyRegex && !validate(bodyRegex, body)) {
return false;
}
return true;
}
throw new Error(`Invalid issue type: ${issueType}`);
}

function validate(match: string | RegExp | null, str: string): boolean {
if (!match) {
return true;
}
if (match instanceof RegExp) {
return match.test(str);
}
return str.includes(match);
}