Skip to content

Commit

Permalink
fix: github discussions references (#20)
Browse files Browse the repository at this point in the history
* fix github discussions references

* refactor
  • Loading branch information
danieldietzler authored Dec 14, 2023
1 parent 429d6ed commit fc4babc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@discordx/importer": "^1.2.3",
"@octokit/request-error": "^5.0.1",
"@octokit/rest": "^20.0.2",
"@types/lodash": "^4.14.200",
"@types/luxon": "^3.3.3",
Expand Down
73 changes: 41 additions & 32 deletions src/events/messageEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IMMICH_REPOSITORY_BASE_OPTIONS, Constants } from '../constants.js';
import { Message, MessageFlags, PartialMessage } from 'discord.js';
import { ArgsOf, Discord, On } from 'discordx';
import _ from 'lodash';
import { RequestError } from '@octokit/request-error';

const PREVIEW_BLACKLIST = [Constants.Urls.Immich, Constants.Urls.GitHub];
const octokit = new Octokit();
Expand Down Expand Up @@ -47,40 +48,48 @@ export class MessageEvents {
const matches = content.matchAll(/(^|\W)#(?<id>[0-9]+)/g);
const links = new Set<string>();
for (const match of matches) {
if (match?.groups) {
const id = match.groups.id;

if (Number(id) < 500 || Number(id) > 15000) {
continue;
}

try {
const response = await octokit.rest.issues.get({
...IMMICH_REPOSITORY_BASE_OPTIONS,
issue_number: Number(id),
});

if (response.status === 200) {
const type = response.data.pull_request ? 'PR' : 'ISSUE';
links.add(`[${type}] ${response.data.title} ([#${id}](${response.data.html_url}))`);
continue;
}
} catch (error) {
console.log(`Could not fetch #${id}`);
continue;
}

try {
const { status: discussionStatus } = await fetch(`${Constants.Urls.Discussions}/${id}}`);
if (discussionStatus === 200) {
links.add(`[Discussion] ([#${id}](${Constants.Urls.Discussions}/${id}))`);
}
} catch (error) {
console.log(`Could not fetch #${id}`);
continue;
}
const id = match?.groups?.id;
if (!id) {
continue;
}

if (Number(id) < 500 || Number(id) > 15000) {
continue;
}

const link = (await this.getIssueOrPr(id)) || (await this.getDiscussion(id));

link && links.add(link);
}

return [...links];
}

private async getIssueOrPr(id: string) {
try {
const response = await octokit.rest.issues.get({
...IMMICH_REPOSITORY_BASE_OPTIONS,
issue_number: Number(id),
});

const type = response.data.pull_request ? 'Pull Request' : 'Issue';
return `[${type}] ${response.data.title} ([#${id}](${response.data.html_url}))`;
} catch (error) {
if (error instanceof RequestError && error.status !== 404) {
console.log(`Could not fetch #${id}`);
}
}
}

private async getDiscussion(id: string) {
try {
const { status } = await fetch(`${Constants.Urls.Discussions}/${id}}`);

if (status === 200) {
return `[Discussion] ([#${id}](${Constants.Urls.Discussions}/${id}))`;
}
} catch (error) {
console.log(`Could not fetch #${id}`);
}
}
}

0 comments on commit fc4babc

Please sign in to comment.