Skip to content

Commit

Permalink
Github commands (#14)
Browse files Browse the repository at this point in the history
* Switch to ES Modules

* Swap out fuzzysort for js-trgm

* Use node-fetch v3

* Github commands

* Remove dev

* Remove empty default export

* Extensions

Co-authored-by: Nayan Gautam <[email protected]>
  • Loading branch information
gtm-nayan and gtm-nayan authored Dec 27, 2021
1 parent f03e5dc commit 72eab94
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
DISCORD_TOKEN=
NODE_ENV=

GITHUB_TOKEN

SUPABASE_URL=
SUPABASE_KEY=
SUPABASE_KEY=
70 changes: 70 additions & 0 deletions src/commands/github/_common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { CommandInteraction } from 'discord.js';
import fetch, { Headers } from 'node-fetch';
import { GITHUB_TOKEN } from '../../config.js';
import { listOfLinks } from '../../utils/embedBuilder.js';
import { REPOS, REPO_DETAILS } from '../../utils/repositories.js';

async function githubSearch(body: {
query: string;
variables: Record<string, string>;
}) {
const res = await fetch('https://api.github.com/graphql', {
method: 'POST',
body: JSON.stringify(body),
headers: new Headers({
Authorization: `Bearer ${GITHUB_TOKEN}`,
'Content-Type': 'application/json',
}),
});
if (!res.ok) {
return null;
}
const results: Array<Record<string, any>> = ((await res.json()) as any).data
.search.nodes;
if (!results.length) {
return null;
}
return listOfLinks(
results.map(
(result) => `#[${result.number}](${result.url}): ${result.title}`,
),
);
}

export async function githubCommandHandler(
interaction: CommandInteraction,
query: string,
is?: 'issue' | 'pr',
) {
const thisRepoDetails =
REPO_DETAILS[
interaction.options.getInteger('repository', true) as REPOS
];
const topic = interaction.options.getString('topic');

const searchQuery = `${is ? `is:${is}` : ''} repo:${
thisRepoDetails.REPOSITORY_NAME
} ${topic || ''}`;

try {
let results = await githubSearch({
query,
variables: {
searchQuery,
},
});

if (results) {
await interaction.reply({
embeds: [results],
});
} else {
await interaction.reply({
content: 'No results found.',
ephemeral: true,
});
}
} catch {
// TODO: Do nothing or log the error or something
}
}
47 changes: 47 additions & 0 deletions src/commands/github/discussions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ApplicationCommandOptionTypes } from 'discord.js/typings/enums';
import { command } from 'jellycommands';
import { REPOS } from '../../utils/repositories.js';
import { githubCommandHandler } from './_common.js';

const query = `query searchResults($searchQuery: String!) {
search(type: DISCUSSION, query: $searchQuery, first: 5) {
nodes {
... on Discussion {
title
number
url
}
}
}
}
`;

export default command({
name: 'discussion',
description: 'Search for a discussion on github.',
global: true,

options: [
{
name: 'repository',
description: 'The repository to search within',
type: ApplicationCommandOptionTypes.INTEGER,
choices: [
{
name: 'SvelteKit',
value: REPOS.SVELTEKIT,
},
],
required: true,
},
{
name: 'topic',
description: 'What to search for',
type: ApplicationCommandOptionTypes.STRING,
},
],

run: async ({ interaction }) => {
await githubCommandHandler(interaction, query);
},
});
55 changes: 55 additions & 0 deletions src/commands/github/issues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ApplicationCommandOptionTypes } from 'discord.js/typings/enums';
import { command } from 'jellycommands';
import { REPOS } from '../../utils/repositories.js';
import { githubCommandHandler } from './_common.js';

const query = `query searchResults($searchQuery: String!) {
search(type: ISSUE, query: $searchQuery, first: 5) {
nodes {
... on Issue {
title
number
url
}
}
}
}
`;

export default command({
name: 'issue',
description: 'Search for an issue on github',
global: true,

options: [
{
name: 'repository',
description: 'The repository to search within',
type: ApplicationCommandOptionTypes.INTEGER,
choices: [
{
name: 'Svelte',
value: REPOS.SVELTE,
},
{
name: 'SvelteKit',
value: REPOS.SVELTEKIT,
},
{
name: 'Language Tools',
value: REPOS.LANGUAGETOOLS,
},
],
required: true,
},
{
name: 'topic',
description: 'What to search for',
type: ApplicationCommandOptionTypes.STRING,
},
],

run: async ({ interaction }) => {
await githubCommandHandler(interaction, query, 'issue');
},
});
59 changes: 59 additions & 0 deletions src/commands/github/prs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ApplicationCommandOptionTypes } from 'discord.js/typings/enums';
import { command } from 'jellycommands';
import { REPOS } from '../../utils/repositories.js';
import { githubCommandHandler } from './_common.js';

const query = `query searchResults($searchQuery: String!) {
search(type: ISSUE, query: $searchQuery, first: 5) {
nodes {
... on PullRequest {
title
number
url
}
}
}
}
`;

export default command({
name: 'pr',
description: 'Search for a pull request on github',
global: true,

options: [
{
name: 'repository',
description: 'The repository to search within',
type: ApplicationCommandOptionTypes.INTEGER,
choices: [
{
name: 'Svelte',
value: REPOS.SVELTE,
},
{
name: 'SvelteKit',
value: REPOS.SVELTEKIT,
},
{
name: 'RFCs',
value: REPOS.RFCS,
},
{
name: 'Language Tools',
value: REPOS.LANGUAGETOOLS,
},
],
required: true,
},
{
name: 'topic',
description: 'What to search for',
type: ApplicationCommandOptionTypes.STRING,
},
],

run: async ({ interaction }) => {
await githubCommandHandler(interaction, query, 'pr');
},
});
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const DISCORD_TOKEN = process.env.DISCORD_TOKEN;

export const SVELTE_ORANGE = 0xff3e00;

export const GITHUB_TOKEN = process.env.GITHUB_TOKEN;

export const LINK_ONLY_CHANNELS = DEV_MODE
? [
// #test-link-validation
Expand Down

0 comments on commit 72eab94

Please sign in to comment.