feat: add state to github cmd

This commit is contained in:
xHyroM 2022-07-15 22:23:21 +02:00
parent 8c32e4c0fd
commit 14a9b97f9b
2 changed files with 52 additions and 12 deletions

View file

@ -2,9 +2,8 @@ import { APIApplicationCommandInteractionDataStringOption, ApplicationCommandOpt
import { Command } from '../structures/Command'; import { Command } from '../structures/Command';
// @ts-expect-error Types :( // @ts-expect-error Types :(
import utilities from '../../files/utilities.toml'; import utilities from '../../files/utilities.toml';
import Collection from '@discordjs/collection';
import { CommandContext } from '../structures/contexts/CommandContext'; import { CommandContext } from '../structures/contexts/CommandContext';
import { getIssueOrPR, search, formatStatus, formatEmojiStatus } from '../utils/githubUtils'; import { getIssueOrPR, search, formatStatus, formatEmojiStatus, IssueState } from '../utils/githubUtils';
const invalidIssue = (ctx: CommandContext, query: string) => { const invalidIssue = (ctx: CommandContext, query: string) => {
return ctx.editResponse( return ctx.editResponse(
@ -22,9 +21,39 @@ new Command({
type: ApplicationCommandOptionType.String, type: ApplicationCommandOptionType.String,
required: true, required: true,
run: async(ctx) => { run: async(ctx) => {
return ctx.respond(await search(ctx.value, ctx?.options?.[1]?.value as string || 'oven-sh/bun')); return ctx.respond(
await search(
ctx.value,
(ctx.options.find(o => o.name === 'repository'))?.value as string || 'oven-sh/bun',
(ctx.options.find(o => o.name === 'state')?.value as string || 'all') as IssueState,
)
);
} }
}, },
{
name: 'state',
description: 'Issue or PR state',
type: ApplicationCommandOptionType.String,
required: false,
choices: [
{
name: 'open',
value: 'open',
},
{
name: 'closed',
value: 'closed',
},
{
name: 'merged',
value: 'merged',
},
{
name: 'all (default)',
value: 'all',
}
]
},
{ {
name: 'repository', name: 'repository',
description: 'Project repository (default oven-sh/bun)', description: 'Project repository (default oven-sh/bun)',
@ -36,7 +65,7 @@ new Command({
value: repository value: repository
})) }))
] ]
} },
], ],
run: async(ctx) => { run: async(ctx) => {
ctx.command.runEditResponse(ctx) ctx.command.runEditResponse(ctx)
@ -46,13 +75,14 @@ new Command({
}, },
runEditResponse: async(ctx) => { runEditResponse: async(ctx) => {
let query: string = (ctx.options[0] as APIApplicationCommandInteractionDataStringOption).value; let query: string = (ctx.options[0] as APIApplicationCommandInteractionDataStringOption).value;
const repository: string = (ctx.options?.[1] as APIApplicationCommandInteractionDataStringOption)?.value || 'oven-sh/bun'; const repository: string = (ctx.options.find(o => o.name === 'repository') as APIApplicationCommandInteractionDataStringOption)?.value || 'oven-sh/bun';
const state: IssueState = ((ctx.options.find(o => o.name === 'state') as APIApplicationCommandInteractionDataStringOption)?.value || 'all') as IssueState;
const repositorySplit = repository.split('/'); const repositorySplit = repository.split('/');
const repositoryOwner = repositorySplit[0]; const repositoryOwner = repositorySplit[0];
const repositoryName = repositorySplit[1]; const repositoryName = repositorySplit[1];
let issueOrPR = await getIssueOrPR(parseInt(query), repository); let issueOrPR = await getIssueOrPR(parseInt(query), repository, state);
if (!issueOrPR) { if (!issueOrPR) {
const res = await fetch(`https://api.github.com/search/issues?q=${encodeURIComponent(query)}${encodeURIComponent(' repo:oven-sh/bun')}`); const res = await fetch(`https://api.github.com/search/issues?q=${encodeURIComponent(query)}${encodeURIComponent(' repo:oven-sh/bun')}`);

View file

@ -8,12 +8,13 @@ import { APIApplicationCommandOptionChoice } from 'discord-api-types/v10';
import { Database } from 'bun:sqlite'; import { Database } from 'bun:sqlite';
import { githubTitleClean } from './regexes'; import { githubTitleClean } from './regexes';
export type IssueState = 'open' | 'closed' | 'all' | 'merged';
interface Issue { interface Issue {
id: number; id: number;
repository: string; repository: string;
title: string; title: string;
number: number; number: number;
state: 'open' | 'closed', state: IssueState,
created_at: string; created_at: string;
closed_at: string | null; closed_at: string | null;
html_url: string; html_url: string;
@ -179,10 +180,14 @@ export const deleteIssueOrPR = (number: number, repository: string) => {
db.exec(`DELETE FROM issuesandprs WHERE repository = '${repository}' AND number = ${number}`); db.exec(`DELETE FROM issuesandprs WHERE repository = '${repository}' AND number = ${number}`);
} }
export const search = async(query: string, repository: string): Promise<APIApplicationCommandOptionChoice[]> => { export const search = async(query: string, repository: string, state: IssueState): Promise<APIApplicationCommandOptionChoice[]> => {
try { try {
const arrayFiltered = await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ?`).all(repository); const arrayFiltered = state === 'all'
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ?`).all(repository)
: state === 'merged'
? await db.prepare(`SELECT * FROM issuesandprs WHERE merged_at IS NOT NULL AND repository = ?`).all(repository)
: await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND state = ?`).all(repository, state);
if (!query) { if (!query) {
const array = arrayFiltered.slice(0, 25); const array = arrayFiltered.slice(0, 25);
return array.map((issueOrPr: Issue | PullRequest) => new Object({ return array.map((issueOrPr: Issue | PullRequest) => new Object({
@ -213,8 +218,13 @@ export const search = async(query: string, repository: string): Promise<APIAppli
} }
} }
export const getIssueOrPR = async(number: number, repository: string): Promise<Issue | PullRequest> => { export const getIssueOrPR = async(number: number, repository: string, state: IssueState): Promise<Issue | PullRequest> => {
const issueOrPR = await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ?`).get(repository, number); const issueOrPR = state === 'all'
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ?`).get(repository, number)
: state === 'merged'
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ? AND merged_at IS NOT NULL`).get(repository, number)
: await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ? AND state = ?`).get(repository, number, state);
return issueOrPR; return issueOrPR;
} }