style: format

This commit is contained in:
元凛
2022-02-10 17:55:52 +08:00
parent bdb9ee8890
commit c626dd5ef0
13 changed files with 218 additions and 126 deletions

View File

@@ -2,23 +2,23 @@ import * as core from '@actions/core';
export const baseInfo = (mess: string) => {
core.info(mess);
}
};
export const info = (mess: string) => {
core.info(`[📝 AC] ${mess}`);
}
};
export const error = (mess: string) => {
core.error(`[💥 AC] ${mess}`);
}
};
export const notice = (mess: string) => {
core.notice(`[🏷 AC] ${mess}`);
}
};
export const warning = (mess: string) => {
core.warning(`[🎃 AC] ${mess}`);
}
};
export const getInput = core.getInput;
@@ -26,4 +26,4 @@ export const setOutput = core.setOutput;
export const setFailed = (mess: string) => {
core.setFailed(`[🚨 AC] ${mess}`);
}
};

View File

@@ -1,20 +1,22 @@
import { dealStringToArr, checkPermission, TPermissionType } from 'actions-util';
import type { TPermissionType } from 'actions-util';
import { checkPermission, dealStringToArr } from 'actions-util';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import utc from 'dayjs/plugin/utc';
import * as core from '../core';
import { matchKeyword, checkDuplicate, replaceStr2Arr } from '../util';
import { TIssueState, TOutList, TEmoji } from '../types';
import { IIssueCoreEngine, IListIssuesParams, TIssueList, TCommentInfo } from '../issue';
import type { IIssueCoreEngine, IListIssuesParams, TCommentInfo, TIssueList } from '../issue';
import type { TEmoji, TIssueState, TOutList } from '../types';
import { checkDuplicate, matchKeyword, replaceStr2Arr } from '../util';
import {
doAddAssignees,
doAddLabels,
doCloseIssue,
doCreateComment,
doCreateCommentEmoji,
doCloseIssue,
doLockIssue,
doUpdateComment,
doSetLabels,
doUpdateComment,
} from './base';
let ICE: IIssueCoreEngine;
@@ -22,7 +24,11 @@ export function initAdvancedICE(_ICE: IIssueCoreEngine) {
ICE = _ICE;
}
export async function doQueryIssues(state: TIssueState | 'all', creator?: string, ignoreLabels?: boolean): Promise<TIssueList> {
export async function doQueryIssues(
state: TIssueState | 'all',
creator?: string,
ignoreLabels?: boolean,
): Promise<TIssueList> {
const params = {
state,
} as IListIssuesParams;
@@ -30,14 +36,16 @@ export async function doQueryIssues(state: TIssueState | 'all', creator?: string
const issueCreator = core.getInput('issue-creator');
const issueAssignee = core.getInput('issue-assignee');
const issueMentioned = core.getInput('issue-mentioned');
issueCreator ? (params.creator = issueCreator) : null;
issueAssignee ? (params.assignee = issueAssignee) : null;
issueMentioned ? (params.mentioned = issueMentioned) : null;
if (issueCreator) params.creator = issueCreator;
if (issueAssignee) params.assignee = issueAssignee;
if (issueMentioned) params.mentioned = issueMentioned;
const labels = core.getInput('labels');
labels && !ignoreLabels ? params.labels = labels : null;
creator ? params.creator = creator : null;
if (labels && !ignoreLabels) params.labels = labels;
if (creator) params.creator = creator;
const issuesList = await ICE.listIssues(params);
const issues: TIssueList = [];
@@ -131,7 +139,7 @@ export async function doCheckIssue() {
checkAssignee = true;
}
});
!checkAssignee ? (checkResult = false) : null;
if (!checkAssignee) checkResult = false;
}
const titleRemove = core.getInput('title-excludes');
@@ -234,8 +242,8 @@ export async function doFindIssues() {
state: issue.state,
created: issue.created_at,
updated: issue.updated_at,
}
})
};
});
if (direction === 'desc') {
issues.reverse();
}
@@ -282,7 +290,11 @@ export async function doMarkAssignees(comment: TCommentInfo) {
}
}
export async function doMarkDuplicate(comment: TCommentInfo, labels?: string[] | void, emoji?: string) {
export async function doMarkDuplicate(
comment: TCommentInfo,
labels?: string[] | void,
emoji?: string,
) {
const duplicateCommand = core.getInput('duplicate-command');
const duplicateLabels = core.getInput('duplicate-labels');
const removeLables = core.getInput('remove-labels') || '';
@@ -342,7 +354,14 @@ export async function doMarkDuplicate(comment: TCommentInfo, labels?: string[] |
}
}
export async function doWelcome(auth: string, issueNumber: number, body: string, labels?: string[] | void, assignees?: string[] | void, emoji?: string) {
export async function doWelcome(
auth: string,
issueNumber: number,
body: string,
labels?: string[] | void,
assignees?: string[] | void,
emoji?: string,
) {
core.info(`[doWelcome] [${auth}]`);
const issues = await doQueryIssues('all', auth, true);
if (issues.length == 0 || (issues.length == 1 && issues[0].number == issueNumber)) {

View File

@@ -1,8 +1,9 @@
import { dealStringToArr } from 'actions-util';
import * as core from '../core';
import { TIssueState, TUpdateMode, TEmoji, TLockReasons } from '../types';
import type { IIssueCoreEngine } from '../issue';
import { ELockReasons } from '../shared';
import { IIssueCoreEngine } from '../issue';
import type { TEmoji, TIssueState, TLockReasons, TUpdateMode } from '../types';
let ICE: IIssueCoreEngine;
export function initBaseICE(_ICE: IIssueCoreEngine) {
@@ -50,7 +51,13 @@ export async function doCreateCommentEmoji(_commentId: number | void, emoji: str
}
}
export async function doCreateIssue(title: string, body: string, labels: string[] | void, assignees: string[] | void, emoji: string | void) {
export async function doCreateIssue(
title: string,
body: string,
labels?: string[],
assignees?: string[],
emoji?: string | void,
) {
if (title) {
const issueNumber = await ICE.createIssue(title, body, labels, assignees);
core.info(`[doCreateIssue] [${title}] success!`);
@@ -124,7 +131,12 @@ export async function doUnlockIssue() {
core.info(`[doUnlockIssue] success!`);
}
export async function doUpdateComment(_commentId: number | void, body: string, updateMode: TUpdateMode, emoji: string | void) {
export async function doUpdateComment(
_commentId: number | void,
body: string,
updateMode: TUpdateMode,
emoji: string | void,
) {
const commentId = _commentId || core.getInput('comment-id');
if (commentId) {
await ICE.updateComment(+commentId, body, updateMode);
@@ -137,7 +149,15 @@ export async function doUpdateComment(_commentId: number | void, body: string, u
}
}
export async function doUpdateIssue(issueNumber: number, state: TIssueState, title: string | void, body: string | void, updateMode: TUpdateMode, labels?: string[] | void, assignees?: string[] | void) {
export async function doUpdateIssue(
issueNumber: number,
state: TIssueState,
title: string | void,
body: string | void,
updateMode: TUpdateMode,
labels?: string[] | void,
assignees?: string[] | void,
) {
if (issueNumber) ICE.setIssueNumber(issueNumber);
await ICE.updateIssue(state, title, body, updateMode, labels, assignees);
core.info(`[doUpdateIssue] success!`);

View File

@@ -1,22 +1,28 @@
// import * as github from '@actions/github';
import { dealStringToArr } from 'actions-util';
import * as core from '../core';
import { Context, TIssueState, TUpdateMode, TAction, TEmoji } from '../types';
import {
IssueCoreEngine,
IIssueCoreEngine,
TCommentInfo,
} from '../issue';
import { dealRandomAssignees } from '../util';
import { IIssueHelperEngine } from './types';
import * as core from '../core';
import type { IIssueCoreEngine, TCommentInfo } from '../issue';
import { IssueCoreEngine } from '../issue';
import type { Context, TAction, TIssueState, TUpdateMode } from '../types';
import { dealRandomAssignees } from '../util';
import {
doCheckInactive,
doCheckIssue,
doCloseIssues,
doFindComments,
doFindIssues,
doLockIssues,
doMarkAssignees,
doMarkDuplicate,
doWelcome,
initAdvancedICE,
} from './advanced';
import {
initBaseICE,
doAddAssignees,
doAddLabels,
doCloseIssue,
doCreateComment,
doCreateCommentEmoji,
doCreateIssue,
doCreateLabel,
doDeleteComment,
@@ -28,20 +34,9 @@ import {
doUnlockIssue,
doUpdateComment,
doUpdateIssue,
initBaseICE,
} from './base';
import {
initAdvancedICE,
doCheckInactive,
doCheckIssue,
doCloseIssues,
doFindComments,
doFindIssues,
doLockIssues,
doMarkAssignees,
doMarkDuplicate,
doWelcome,
} from './advanced';
import type { IIssueHelperEngine } from './types';
export class IssueHelperEngine implements IIssueHelperEngine {
private ICE!: IIssueCoreEngine;
@@ -51,8 +46,8 @@ export class IssueHelperEngine implements IIssueHelperEngine {
private issueNumber!: number;
private emoji?: string;
private labels?: string[] | void;
private assignees?: string[] | void;
private labels?: string[];
private assignees?: string[];
private title: string = '';
private body: string = '';
private state: TIssueState = 'open';
@@ -249,7 +244,7 @@ export class IssueHelperEngine implements IIssueHelperEngine {
break;
}
default: {
core.warning(`The ${action} is not allowed.`)
core.warning(`The ${action} is not allowed.`);
break;
}
}
@@ -257,6 +252,9 @@ export class IssueHelperEngine implements IIssueHelperEngine {
private checkEvent4Mark() {
const { ctx } = this;
return ctx.eventName !== 'issue_comment' && (ctx.payload.action === 'created' || ctx.payload.action === 'edited');
return (
ctx.eventName !== 'issue_comment' &&
(ctx.payload.action === 'created' || ctx.payload.action === 'edited')
);
}
}

View File

@@ -1,5 +1,5 @@
import { TAction } from '../types';
import type { TAction } from '../types';
export interface IIssueHelperEngine {
doExeAction(action: TAction): Promise<void>;
doExeAction: (action: TAction) => Promise<void>;
}

View File

@@ -1,7 +1,15 @@
import { Octokit } from '@octokit/rest';
import { EEmoji } from '../shared';
import { TEmoji, TLockReasons, TUpdateMode, TIssueState, TUserPermission } from '../types';
import { IIssueBaseInfo, IIssueCoreEngine, IListIssuesParams, TIssueList, TIssueInfo, TCommentList } from './types';
import type { TEmoji, TIssueState, TLockReasons, TUpdateMode, TUserPermission } from '../types';
import type {
IIssueBaseInfo,
IIssueCoreEngine,
IListIssuesParams,
TCommentList,
TIssueInfo,
TIssueList,
} from './types';
export class IssueCoreEngine implements IIssueCoreEngine {
private owner!: string;
@@ -16,7 +24,7 @@ export class IssueCoreEngine implements IIssueCoreEngine {
this.issueNumber = _info.issueNumber;
this.octokit = new Octokit({ auth: `token ${_info.token}` });
} else {
console && console.error && console.error(`Init failed, need owner、repo!`);
console.error(`Init failed, need owner、repo!`);
}
}
@@ -80,7 +88,12 @@ export class IssueCoreEngine implements IIssueCoreEngine {
}
}
public async createIssue(title: string, body: string, labels?: string[], assignees?: string[]): Promise<number> {
public async createIssue(
title: string,
body: string,
labels?: string[],
assignees?: string[],
): Promise<number> {
const { owner, repo, octokit } = this;
const { data } = await octokit.issues.create({
owner,
@@ -107,7 +120,11 @@ export class IssueCoreEngine implements IIssueCoreEngine {
}
}
public async createLabel(labelName: string, labelColor: string, labelDescription: string | undefined) {
public async createLabel(
labelName: string,
labelColor: string,
labelDescription: string | undefined,
) {
const { owner, repo, octokit } = this;
await octokit.issues.createLabel({
owner,
@@ -185,7 +202,7 @@ export class IssueCoreEngine implements IIssueCoreEngine {
owner,
repo,
issue_number: issueNumber,
}
};
if (lockReason) {
params.lock_reason = lockReason;
}
@@ -275,10 +292,23 @@ export class IssueCoreEngine implements IIssueCoreEngine {
});
}
public async updateIssue(state: TIssueState, title: string | void, body: string | void, mode: TUpdateMode, labels?: string[] | void, assignees?: string[] | void) {
public async updateIssue(
state: TIssueState,
title: string | void,
body: string | void,
mode: TUpdateMode,
labels?: string[] | void,
assignees?: string[] | void,
) {
const { owner, repo, octokit, issueNumber } = this;
const issue = await this.getIssue();
const { body: baseBody, title: baseTitle, labels: baseLabels, assignees: baseAssigness, state: baseState } = issue;
const {
body: baseBody,
title: baseTitle,
labels: baseLabels,
assignees: baseAssigness,
state: baseState,
} = issue;
const baseLabelsName = baseLabels.map(({ name }: any) => name);
const baseAssignessName = baseAssigness?.map(({ login }: any) => login);

View File

@@ -1,4 +1,4 @@
import { TEmoji, TLockReasons, TIssueState, TUpdateMode, TUserPermission } from '../types';
import type { TEmoji, TIssueState, TLockReasons, TUpdateMode, TUserPermission } from '../types';
export interface IIssueBaseInfo {
owner: string;
@@ -32,7 +32,7 @@ export type TIssueInfo = {
created_at: string;
updated_at: string;
pull_request?: any;
}
};
export type TIssueList = TIssueInfo[];
@@ -44,22 +44,22 @@ export type TCommentInfo = {
};
created_at: string;
updated_at: string;
}
};
export type TCommentList = TCommentInfo[];
export interface IIssueCoreEngine {
setIssueNumber(newIssueNumber: number): void;
addAssignees(assignees: string[]): Promise<void>;
addLabels(labels: string[]): Promise<void>;
setIssueNumber: (newIssueNumber: number) => void;
addAssignees: (assignees: string[]) => Promise<void>;
addLabels: (labels: string[]) => Promise<void>;
closeIssue(): Promise<void>;
closeIssue: () => Promise<void>;
/**
* @param body The comment body.
* @returns The create new comment id.
*/
createComment(body: string): Promise<number>;
createCommentEmoji(commentId: number, emoji: TEmoji[]): Promise<void>;
createComment: (body: string) => Promise<number>;
createCommentEmoji: (commentId: number, emoji: TEmoji[]) => Promise<void>;
/**
* @param title
* @param body
@@ -67,28 +67,44 @@ export interface IIssueCoreEngine {
* @param assignees
* @returns The create new issue number.
*/
createIssue(title: string, body: string, labels: string[] | void, assignees: string[] | void): Promise<number>;
createIssueEmoji(emoji: TEmoji[]): Promise<void>;
createLabel(labelName: string, labelColor: string, labelDescription: string | undefined): Promise<void>;
createIssue: (
title: string,
body: string,
labels?: string[],
assignees?: string[],
) => Promise<number>;
createIssueEmoji: (emoji: TEmoji[]) => Promise<void>;
createLabel: (
labelName: string,
labelColor: string,
labelDescription: string | undefined,
) => Promise<void>;
deleteComment(commentId: number): Promise<void>;
deleteComment: (commentId: number) => Promise<void>;
getIssue(): Promise<TIssueInfo>;
getUserPermission(username: string): Promise<TUserPermission>;
getIssue: () => Promise<TIssueInfo>;
getUserPermission: (username: string) => Promise<TUserPermission>;
listComments(): Promise<TCommentList>;
listIssues(params: IListIssuesParams): Promise<TIssueList>;
lockIssue(lockReason: TLockReasons): Promise<void>;
listComments: () => Promise<TCommentList>;
listIssues: (params: IListIssuesParams) => Promise<TIssueList>;
lockIssue: (lockReason: TLockReasons) => Promise<void>;
openIssue(): Promise<void>;
openIssue: () => Promise<void>;
removeAssignees(assignees: string[]): Promise<void>;
removeLabels(labels: string[]): Promise<void>;
removeAssignees: (assignees: string[]) => Promise<void>;
removeLabels: (labels: string[]) => Promise<void>;
setLabels(labels: string[]): Promise<void>;
setLabels: (labels: string[]) => Promise<void>;
unlockIssue(): Promise<void>;
unlockIssue: () => Promise<void>;
updateComment(commentId: number, body: string, mode: TUpdateMode): Promise<void>;
updateIssue(state: TIssueState, title: string | void, body: string | void, mode: TUpdateMode, labels?: string[] | void, assignees?: string[] | void): Promise<void>;
updateComment: (commentId: number, body: string, mode: TUpdateMode) => Promise<void>;
updateIssue: (
state: TIssueState,
title: string | void,
body: string | void,
mode: TUpdateMode,
labels?: string[] | void,
assignees?: string[] | void,
) => Promise<void>;
}

View File

@@ -1,6 +1,5 @@
import { dealStringToArr, THANKS } from 'actions-util';
import * as github from '@actions/github';
import { dealStringToArr, THANKS } from 'actions-util';
import * as core from './core';
import { IssueHelperEngine } from './helper';

View File

@@ -7,25 +7,29 @@ export const dealRandomAssignees = (assignees: string, randomTo: string | void):
arr = sampleSize(arr, Number(randomTo));
}
return arr;
}
};
export const matchKeyword = (content: string, keywords: string[]): boolean => {
return !!keywords.find(item => content.toLowerCase().includes(item));
}
};
export const checkDuplicate = (body: string | void): boolean => {
if (!body || !body.startsWith('Duplicate of')) {
return false;
}
const arr = body.split(' ');
return arr[0] == 'Duplicate' && arr[1] == 'of'
}
return arr[0] == 'Duplicate' && arr[1] == 'of';
};
export const getPreMonth = (m: number): number => {
return m == 1 ? 12 : m - 1;
}
};
// replace some & split & cull empty
export const replaceStr2Arr = (str: string, replace: string, split: string): string[] => {
return str.replace(replace, '').trim().split(split).reduce((result: string[], it) => it ? [...result, it.trim()] : result, []);
}
return str
.replace(replace, '')
.trim()
.split(split)
.reduce((result: string[], it) => (it ? [...result, it.trim()] : result), []);
};