mirror of
https://github.com/xHyroM/bun-discord-bot.git
synced 2024-11-22 14:41:05 +01:00
feat(githubUtils): show status, support for #XX search
This commit is contained in:
parent
85829503c5
commit
1f99d77bc2
5 changed files with 46 additions and 30 deletions
Binary file not shown.
|
@ -3,9 +3,8 @@ import { Command } from '../structures/Command';
|
|||
// @ts-expect-error Types :(
|
||||
import utilities from '../../files/utilities.toml';
|
||||
import Collection from '@discordjs/collection';
|
||||
import formatStatus from '../utils/formatStatus';
|
||||
import { CommandContext } from '../structures/contexts/CommandContext';
|
||||
import { getIssueOrPR, search } from '../utils/githubUtils';
|
||||
import { getIssueOrPR, search, formatStatus } from '../utils/githubUtils';
|
||||
|
||||
const cooldowns: Collection<string, number> = new Collection();
|
||||
const invalidIssue = (ctx: CommandContext, query: string) => {
|
||||
|
@ -83,7 +82,7 @@ new Command({
|
|||
html_url: item.html_url,
|
||||
user_login: item.user.login,
|
||||
user_html_url: item.user.html_url,
|
||||
type: item.pull_request ? '(PR)' : '(ISSUE)',
|
||||
type: item.pull_request ? '(PR)' : '(IS)',
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -106,7 +106,7 @@ app.post('/github_webhook', bodyParse(), (c) => {
|
|||
html_url: issueOrPr.issue.html_url,
|
||||
user_login: issueOrPr.issue.user.login,
|
||||
user_html_url: issueOrPr.issue.user.html_url,
|
||||
type: '(ISSUE)',
|
||||
type: '(IS)',
|
||||
})
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
export default (data: any) => {
|
||||
let operation = '';
|
||||
let timestamp = '';
|
||||
switch(data.state as 'open' | 'closed' | 'all') {
|
||||
case 'open':
|
||||
operation = 'opened';
|
||||
timestamp = `<t:${Math.floor(new Date(data.created_at).getTime() / 1000)}:R>`;
|
||||
break;
|
||||
case 'closed':
|
||||
operation = data?.pull_request?.merged_at ? 'merged' : 'closed';
|
||||
timestamp = data?.pull_request?.merged_at
|
||||
? `<t:${Math.floor(new Date(data.pull_request.merged_at).getTime() / 1000)}:R>`
|
||||
: `<t:${Math.floor(new Date(data.closed_at).getTime() / 1000)}:R>`;
|
||||
break;
|
||||
}
|
||||
|
||||
return `${operation} ${timestamp}`;
|
||||
}
|
|
@ -18,7 +18,7 @@ interface Issue {
|
|||
html_url: string;
|
||||
user_login: string;
|
||||
user_html_url: string;
|
||||
type: '(ISSUE)' | '(PR)';
|
||||
type: '(IS)' | '(PR)';
|
||||
}
|
||||
|
||||
interface PullRequest extends Issue {
|
||||
|
@ -64,7 +64,7 @@ export const fetchIssues = async() => {
|
|||
issue.html_url,
|
||||
issue.user.login,
|
||||
issue.user.html_url,
|
||||
'(ISSUE)'
|
||||
'(IS)'
|
||||
]);
|
||||
issues++;
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ export const setIssue = async(issue: Issue) => {
|
|||
issue.html_url,
|
||||
issue.user_login,
|
||||
issue.user_html_url,
|
||||
'(ISSUE)'
|
||||
'(IS)'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ export const setPullRequest = async(pull: PullRequest) => {
|
|||
pull.html_url,
|
||||
pull.user_login,
|
||||
pull.user_html_url,
|
||||
'(ISSUE)'
|
||||
'(IS)'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -183,14 +183,14 @@ export const search = async(query: string, repository: string): Promise<APIAppli
|
|||
if (!query) {
|
||||
const array = arrayFiltered.slice(0, 25);
|
||||
return array.map((issueOrPr: Issue | PullRequest) => new Object({
|
||||
name: `${issueOrPr.type} ${issueOrPr.title.slice(0, 93).replace(/[^a-z0-9 ]/gi, '')}`,
|
||||
name: `${issueOrPr.type} ${formatEmojiStatus(issueOrPr)} ${issueOrPr.title.slice(0, 95).replace(/[^a-z0-9 ]/gi, '')}`,
|
||||
value: issueOrPr.number.toString()
|
||||
})) as APIApplicationCommandOptionChoice[]
|
||||
}
|
||||
|
||||
const searcher = new MiniSearch({
|
||||
fields: ['title', 'number', 'type'],
|
||||
storeFields: ['title', 'number', 'type'],
|
||||
fields: query.startsWith('#') ? ['number'] : ['title'],
|
||||
storeFields: ['title', 'number', 'type', 'state', 'merged_at'],
|
||||
searchOptions: {
|
||||
fuzzy: 3,
|
||||
processTerm: term => term.toLowerCase(),
|
||||
|
@ -202,7 +202,7 @@ export const search = async(query: string, repository: string): Promise<APIAppli
|
|||
const result = searcher.search(query);
|
||||
|
||||
return (result as unknown as Issue[] | PullRequest[]).slice(0, 25).map((issueOrPr: Issue | PullRequest) => new Object({
|
||||
name: `${issueOrPr.type} ${issueOrPr.title.slice(0, 93).replace(/[^a-z0-9 ]/gi, '')}`,
|
||||
name: `${issueOrPr.type} ${formatEmojiStatus(issueOrPr)} ${issueOrPr.title.slice(0, 95).replace(/[^a-z0-9 ]/gi, '')}`,
|
||||
value: issueOrPr.number.toString()
|
||||
})) as APIApplicationCommandOptionChoice[]
|
||||
} catch(e) {
|
||||
|
@ -213,4 +213,39 @@ export const search = async(query: string, repository: string): Promise<APIAppli
|
|||
export const getIssueOrPR = async(number: number, repository: string): Promise<Issue | PullRequest> => {
|
||||
const issueOrPR = await db.prepare(`SELECT * FROM issuesandprs WHERE repository = '${repository}' AND number = ${number}`).get();
|
||||
return issueOrPR;
|
||||
}
|
||||
|
||||
export const formatStatus = (data: Issue | PullRequest) => {
|
||||
let operation = '';
|
||||
let timestamp = '';
|
||||
switch(data.state as 'open' | 'closed' | 'all') {
|
||||
case 'open':
|
||||
operation = 'opened';
|
||||
timestamp = `<t:${Math.floor(new Date(data.created_at).getTime() / 1000)}:R>`;
|
||||
break;
|
||||
case 'closed':
|
||||
operation = (data as PullRequest).merged_at ? 'merged' : 'closed';
|
||||
timestamp = (data as PullRequest).merged_at
|
||||
? `<t:${Math.floor(new Date((data as PullRequest).merged_at).getTime() / 1000)}:R>`
|
||||
: `<t:${Math.floor(new Date(data.closed_at).getTime() / 1000)}:R>`;
|
||||
break;
|
||||
}
|
||||
|
||||
return `${operation} ${timestamp}`;
|
||||
}
|
||||
|
||||
export const formatEmojiStatus = (data: Issue | PullRequest) => {
|
||||
let emoji = '';
|
||||
switch(data.state as 'open' | 'closed' | 'all') {
|
||||
case 'open':
|
||||
emoji = '🟢';
|
||||
break;
|
||||
case 'closed':
|
||||
emoji = '🔴';
|
||||
break;
|
||||
}
|
||||
|
||||
if (data.type === '(PR)' && !isNaN(new Date((data as PullRequest).merged_at).getTime())) emoji = '🟣';
|
||||
|
||||
return emoji;
|
||||
}
|
Loading…
Reference in a new issue