From 14a9b97f9b40930e65adc8dcf3de4d19a75dbd5a Mon Sep 17 00:00:00 2001 From: xHyroM Date: Fri, 15 Jul 2022 22:23:21 +0200 Subject: [PATCH] feat: add state to github cmd --- src/commands/github.ts | 42 ++++++++++++++++++++++++++++++++++------ src/utils/githubUtils.ts | 22 +++++++++++++++------ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/commands/github.ts b/src/commands/github.ts index bf18966..9821893 100644 --- a/src/commands/github.ts +++ b/src/commands/github.ts @@ -2,9 +2,8 @@ import { APIApplicationCommandInteractionDataStringOption, ApplicationCommandOpt import { Command } from '../structures/Command'; // @ts-expect-error Types :( import utilities from '../../files/utilities.toml'; -import Collection from '@discordjs/collection'; 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) => { return ctx.editResponse( @@ -22,9 +21,39 @@ new Command({ type: ApplicationCommandOptionType.String, required: true, 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', description: 'Project repository (default oven-sh/bun)', @@ -36,7 +65,7 @@ new Command({ value: repository })) ] - } + }, ], run: async(ctx) => { ctx.command.runEditResponse(ctx) @@ -46,13 +75,14 @@ new Command({ }, runEditResponse: async(ctx) => { 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 repositoryOwner = repositorySplit[0]; const repositoryName = repositorySplit[1]; - let issueOrPR = await getIssueOrPR(parseInt(query), repository); + let issueOrPR = await getIssueOrPR(parseInt(query), repository, state); if (!issueOrPR) { const res = await fetch(`https://api.github.com/search/issues?q=${encodeURIComponent(query)}${encodeURIComponent(' repo:oven-sh/bun')}`); diff --git a/src/utils/githubUtils.ts b/src/utils/githubUtils.ts index 56acdd4..886eb0f 100644 --- a/src/utils/githubUtils.ts +++ b/src/utils/githubUtils.ts @@ -8,12 +8,13 @@ import { APIApplicationCommandOptionChoice } from 'discord-api-types/v10'; import { Database } from 'bun:sqlite'; import { githubTitleClean } from './regexes'; +export type IssueState = 'open' | 'closed' | 'all' | 'merged'; interface Issue { id: number; repository: string; title: string; number: number; - state: 'open' | 'closed', + state: IssueState, created_at: string; closed_at: string | null; 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}`); } -export const search = async(query: string, repository: string): Promise => { +export const search = async(query: string, repository: string, state: IssueState): Promise => { 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) { const array = arrayFiltered.slice(0, 25); return array.map((issueOrPr: Issue | PullRequest) => new Object({ @@ -213,8 +218,13 @@ export const search = async(query: string, repository: string): Promise => { - const issueOrPR = await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ?`).get(repository, number); +export const getIssueOrPR = async(number: number, repository: string, state: IssueState): Promise => { + 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; }