feat: add type to github cmd

This commit is contained in:
xHyroM 2022-07-16 13:33:25 +02:00
parent 14a9b97f9b
commit 2b2faf7997
2 changed files with 36 additions and 11 deletions

View file

@ -3,7 +3,7 @@ import { Command } from '../structures/Command';
// @ts-expect-error Types :(
import utilities from '../../files/utilities.toml';
import { CommandContext } from '../structures/contexts/CommandContext';
import { getIssueOrPR, search, formatStatus, formatEmojiStatus, IssueState } from '../utils/githubUtils';
import { getIssueOrPR, search, formatStatus, formatEmojiStatus, IssueState, IssueType } from '../utils/githubUtils';
const invalidIssue = (ctx: CommandContext, query: string) => {
return ctx.editResponse(
@ -26,6 +26,7 @@ new Command({
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,
(ctx.options.find(o => o.name === 'type')?.value as string || '(IS|PR)') as IssueType,
)
);
}
@ -54,6 +55,26 @@ new Command({
}
]
},
{
name: 'type',
description: 'Issues or PRs',
type: ApplicationCommandOptionType.String,
required: false,
choices: [
{
name: 'Issues',
value: '(IS)',
},
{
name: 'Pull Requests',
value: '(PR)',
},
{
name: 'Both',
value: '(IS|PR)',
}
]
},
{
name: 'repository',
description: 'Project repository (default oven-sh/bun)',
@ -77,12 +98,13 @@ new Command({
let query: string = (ctx.options[0] as APIApplicationCommandInteractionDataStringOption).value;
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 type: IssueType = ((ctx.options.find(o => o.name === 'type') as APIApplicationCommandInteractionDataStringOption)?.value || '(IS|PR)') as IssueType;
const repositorySplit = repository.split('/');
const repositoryOwner = repositorySplit[0];
const repositoryName = repositorySplit[1];
let issueOrPR = await getIssueOrPR(parseInt(query), repository, state);
let issueOrPR = await getIssueOrPR(parseInt(query), repository, state, type);
if (!issueOrPR) {
const res = await fetch(`https://api.github.com/search/issues?q=${encodeURIComponent(query)}${encodeURIComponent(' repo:oven-sh/bun')}`);

View file

@ -9,6 +9,7 @@ import { Database } from 'bun:sqlite';
import { githubTitleClean } from './regexes';
export type IssueState = 'open' | 'closed' | 'all' | 'merged';
export type IssueType = '(IS)' | '(PR)' | '(IS|PR)';
interface Issue {
id: number;
repository: string;
@ -20,7 +21,7 @@ interface Issue {
html_url: string;
user_login: string;
user_html_url: string;
type: '(IS)' | '(PR)';
type: IssueType;
}
interface PullRequest extends Issue {
@ -180,13 +181,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, state: IssueState): Promise<APIApplicationCommandOptionChoice[]> => {
export const search = async(query: string, repository: string, state: IssueState, type: IssueType): Promise<APIApplicationCommandOptionChoice[]> => {
try {
const sqliteTypePrepase = type !== '(IS|PR)' ? ` AND type = '${type}'` : '';
const arrayFiltered = state === 'all'
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ?`).all(repository)
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ?${sqliteTypePrepase}`).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);
? await db.prepare(`SELECT * FROM issuesandprs WHERE merged_at IS NOT NULL AND repository = ?${sqliteTypePrepase}`).all(repository)
: await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND state = ?${sqliteTypePrepase}`).all(repository, state);
if (!query) {
const array = arrayFiltered.slice(0, 25);
@ -218,12 +220,13 @@ export const search = async(query: string, repository: string, state: IssueState
}
}
export const getIssueOrPR = async(number: number, repository: string, state: IssueState): Promise<Issue | PullRequest> => {
export const getIssueOrPR = async(number: number, repository: string, state: IssueState, type: IssueType): Promise<Issue | PullRequest> => {
const sqliteTypePrepase = type !== '(IS|PR)' ? ` AND type = '${type}'` : '';
const issueOrPR = state === 'all'
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ?`).get(repository, number)
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ?${sqliteTypePrepase}`).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);
? await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ? AND merged_at IS NOT NULL${sqliteTypePrepase}`).get(repository, number)
: await db.prepare(`SELECT * FROM issuesandprs WHERE repository = ? AND number = ? AND state = ?${sqliteTypePrepase}`).get(repository, number, state);
return issueOrPR;
}