wip: add helper & base

This commit is contained in:
元凛
2022-02-07 17:56:12 +08:00
parent 69e103e931
commit 18db22e255
14 changed files with 514 additions and 79 deletions

15
src/helper/advanced.ts Normal file
View File

@@ -0,0 +1,15 @@
import { dealStringToArr } from 'actions-util';
import * as core from '../core';
import { TIssueState, TUpdateMode, TEmoji, TLockReasons } from '../types';
import { ELockReasons } from '../shared';
import { IIssueCoreEngine } from '../issue';
let ICE: IIssueCoreEngine;
export function initAdvancedICE(_ICE: IIssueCoreEngine) {
ICE = _ICE;
}
export async function doCheckInactive() {
}

138
src/helper/base.ts Normal file
View File

@@ -0,0 +1,138 @@
import { dealStringToArr } from 'actions-util';
import * as core from '../core';
import { TIssueState, TUpdateMode, TEmoji, TLockReasons } from '../types';
import { ELockReasons } from '../shared';
import { IIssueCoreEngine } from '../issue';
let ICE: IIssueCoreEngine;
export function initBaseICE(_ICE: IIssueCoreEngine) {
ICE = _ICE;
}
export async function doAddAssignees(assignees: string[]) {
await ICE.addAssignees(assignees);
core.info(`[doAddAssignees] [${assignees}] success!`);
}
export async function doAddLabels(labels: string[]) {
await ICE.addLabels(labels);
core.info(`[doAddLabels] [${labels}] success!`);
}
export async function doCloseIssue(issueNumber: number) {
await ICE.closeIssue();
core.info(`[doCloseIssue] [${issueNumber}] success!`);
}
export async function doCreateComment(body: string, emoji?: string) {
if (body) {
const commentId = await ICE.createComment(body);
core.info(`[doCreateComment] [${body}] success!`);
core.setOutput('comment-id', commentId);
if (emoji) {
await doCreateCommentEmoji(commentId, emoji);
}
} else {
core.warning(`[doCreateComment] body is empty!`);
}
}
export async function doCreateCommentEmoji(_commentId: number | void, emoji: string) {
const commentId = _commentId || core.getInput('comment-id');
if (emoji && commentId) {
await ICE.createCommentEmoji(+commentId, dealStringToArr(emoji) as TEmoji[]);
core.info(`[doCreateCommentEmoji] [${emoji}] success!`);
} else {
core.warning(`[doCreateCommentEmoji] emoji or commentId is empty!`);
}
}
export async function doCreateIssue(title: string, body: string, labels: string[] | void, assignees: string[] | void, emoji: string | void) {
if (title) {
const issueNumber = await ICE.createIssue(title, body, labels, assignees);
core.info(`[doCreateIssue] [${title}] success!`);
core.setOutput('issue-number', issueNumber);
if (emoji) {
ICE.setIssueNumber(issueNumber);
await ICE.createIssueEmoji(dealStringToArr(emoji) as TEmoji[]);
core.info(`[createIssueEmoji] [${emoji}] success!`);
}
} else {
core.warning(`[doCreateIssue] title is empty!`);
}
}
export async function doCreateLabel() {
const name = core.getInput('label-name');
const color = core.getInput('label-color') || 'ededed';
const description = core.getInput('label-desc') || '';
if (name) {
await ICE.createLabel(name, color, description);
core.info(`[doCreateLabel] [${name}] success!`);
} else {
core.warning(`[doCreateLabel] label-name is empty!`);
}
}
export async function doDeleteComment(_commentId: number | void) {
const commentId = _commentId || core.getInput('comment-id');
if (commentId) {
await ICE.deleteComment(+commentId);
core.info(`[doDeleteComment] [${commentId}] success!`);
} else {
core.warning(`[doDeleteComment] commentId is empty!`);
}
}
export async function doLockIssue(issueNumber: number) {
const lockReason = core.getInput('lock-reason') || '';
// @ts-ignore
if (lockReason && !ELockReasons[lockReason]) {
core.warning(`[doLockIssue] lock-reason is illegal!`);
return;
}
await ICE.lockIssue(lockReason as TLockReasons);
core.info(`[doLockIssue] ${issueNumber} success!`);
}
export async function doOpenIssue(issueNumber: number) {
await ICE.openIssue();
core.info(`[doOpenIssue] [${issueNumber}] success!`);
}
export async function doRemoveAssignees(assignees: string[]) {
await ICE.removeAssignees(assignees);
core.info(`[doRemoveAssignees] [${assignees}] success!`);
}
export async function doRemoveLabels(labels: string[]) {
await ICE.removeLabels(labels);
core.info(`[doRemoveLabels] [${labels}] success!`);
}
export async function doSetLabels(labels: string[]) {
await ICE.setLabels(labels);
core.info(`[doSetLabels] [${labels}] success!`);
}
export async function doUnlockIssue(issueNumber: number) {
await ICE.unlockIssue();
core.info(`[doUnlockIssue] [${issueNumber}] success!`);
}
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);
core.info(`[doUpdateComment] [${commentId}] success!`);
} else {
core.warning(`[doUpdateComment] commentId is empty!`);
}
}
export async function doUpdateIssue(issueNumber: number, state: TIssueState, title: string | void, body: string | void, updateMode: TUpdateMode, labels?: string[] | void, assignees?: string[] | void) {
await ICE.updateIssue(state, title, body, updateMode, labels, assignees);
core.info(`[doUpdateIssue] [${issueNumber}] success!`);
}

201
src/helper/helper.ts Normal file
View File

@@ -0,0 +1,201 @@
// 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,
} from '../issue';
import { dealRandomAssignees } from '../util';
import { IIssueHelperEngine } from './types';
import {
initBaseICE,
doAddAssignees,
doAddLabels,
doCloseIssue,
doCreateComment,
doCreateCommentEmoji,
doCreateIssue,
doCreateLabel,
doDeleteComment,
doLockIssue,
doOpenIssue,
doRemoveAssignees,
doRemoveLabels,
doSetLabels,
doUnlockIssue,
doUpdateComment,
doUpdateIssue,
} from './base';
import {
initAdvancedICE,
doCheckInactive,
} from './advanced';
export class IssueHelperEngine implements IIssueHelperEngine {
private ICE!: IIssueCoreEngine;
private owner!: string;
private repo!: string;
private issueNumber!: number;
private githubToken!: string;
private emoji?: string;
private labels?: string[] | void;
private assignees?: string[] | void;
private title: string = '';
private body: string = '';
private state: TIssueState = 'open';
private updateMode: TUpdateMode = 'replace';
public constructor(readonly ctx: Context) {
this.initInput(ctx);
this.initIssueCore();
initBaseICE(this.ICE);
initAdvancedICE(this.ICE);
}
private initInput(ctx: Context) {
// No display to outside
const repoInput = core.getInput('repo');
if (repoInput) {
this.owner = repoInput.split('/')[0];
this.repo = repoInput.split('/')[1];
} else {
this.owner = ctx.repo.owner;
this.repo = ctx.repo.repo;
}
let defaultCtxNumber: number | undefined;
if (ctx.eventName === 'issues' || ctx.eventName === 'issue_comment') {
defaultCtxNumber = ctx.payload.issue?.number;
}
const issueNumber = core.getInput('issue-number') || defaultCtxNumber;
if (issueNumber) {
this.issueNumber = +issueNumber;
} else {
core.setFailed(`issue-number is missing!`);
return;
}
this.githubToken = core.getInput('token', { required: true }) as string;
this.emoji = core.getInput('emoji') || '';
this.labels = dealStringToArr(core.getInput('labels') || '');
const assigneesInput = core.getInput('assignees') || '';
const randomTo = core.getInput('random-to');
this.assignees = dealRandomAssignees(assigneesInput, randomTo);
this.title = core.getInput('title') || '';
this.body = core.getInput('body') || '';
this.state = core.getInput('state') === 'closed' ? 'closed' : 'open';
this.updateMode = core.getInput('update-mode') === 'append' ? 'append' : 'replace';
}
private initIssueCore() {
const { owner, repo, issueNumber, githubToken } = this;
this.ICE = new IssueCoreEngine({
owner,
repo,
issueNumber,
githubToken,
});
core.info(`[Init] [${owner}/${repo}] [${issueNumber}]`);
}
public async doExeAction(action: TAction) {
const { owner, repo, issueNumber, emoji, labels, assignees, title, body, updateMode, state } = this;
switch (action) {
// ---[ Base Begin ]--->>>
case 'add-assignees': {
if (assignees && assignees.length) {
await doAddAssignees(assignees);
} else {
core.warning(`[doAddAssignees] assignees is empty!`);
}
break;
}
case 'add-labels': {
if (labels && labels.length) {
await doAddLabels(labels);
} else {
core.warning(`[doAddLabels] labels is empty!`);
}
break;
}
case 'close-issue': {
await doCloseIssue(issueNumber);
break;
}
case 'create-comment': {
await doCreateComment(body, emoji);
break;
}
case 'create-issue': {
await doCreateIssue(title, body, labels, assignees, emoji);
break;
}
case 'create-label': {
await doCreateLabel();
break;
}
case 'delete-comment': {
await doDeleteComment();
break;
}
case 'lock-issue': {
await doLockIssue(issueNumber);
break;
}
case 'open-issue': {
await doOpenIssue(issueNumber);
break;
}
case 'remove-assignees': {
if (assignees && assignees.length) {
await doRemoveAssignees(assignees);
} else {
core.warning(`[doRemoveAssignees] assignees is empty!`);
}
break;
}
case 'remove-labels': {
if (labels && labels.length) {
await doRemoveLabels(labels);
} else {
core.warning(`[doRemoveLabels] labels is empty!`);
}
break;
}
case 'set-labels': {
if (labels && labels.length) {
await doSetLabels(labels);
} else {
core.warning(`[doSetLabels] labels is empty!`);
}
break;
}
case 'unlock-issue': {
await doUnlockIssue(issueNumber);
break;
}
case 'update-comment': {
await doUpdateComment(0, body, updateMode, emoji);
break;
}
case 'update-issue': {
await doUpdateIssue(issueNumber, state, title, body, updateMode, labels, assignees);
break;
}
// ---[ Base End ]--->>>
// ^_^ ============= ^_^
// -[ Advanced Begin ]->
case 'check-inactive': {
await doCheckInactive();
break;
}
}
}
}

2
src/helper/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from './helper';
export * from './types';

5
src/helper/types.ts Normal file
View File

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