From e5b8de80f742b3e2191c8607a771f79ed7623fd1 Mon Sep 17 00:00:00 2001 From: zugdev Date: Thu, 5 Dec 2024 20:01:13 -0300 Subject: [PATCH] feat: fetchIssueNotifications --- src/home/fetch-github/fetch-data.ts | 43 ++++++++++++++++++++++++++++- src/home/github-types.ts | 2 +- src/home/home.ts | 3 +- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/home/fetch-github/fetch-data.ts b/src/home/fetch-github/fetch-data.ts index e385c9e..684d556 100644 --- a/src/home/fetch-github/fetch-data.ts +++ b/src/home/fetch-github/fetch-data.ts @@ -44,6 +44,11 @@ function filterPullRequestNotifications(notifications: GitHubNotification[]): Gi return preFilterNotifications(notifications).filter((notification) => notification.subject.type === "PullRequest"); } +// Function to filter issue notifications +function filterIssueNotifications(notifications: GitHubNotification[]): GitHubNotifications { + return preFilterNotifications(notifications).filter((notification) => notification.subject.type === "Issue"); +} + // Function to fetch the pull request details async function fetchPullRequestDetails(pullRequestUrl: string): Promise { const providerToken = await getGitHubAccessToken(); @@ -58,6 +63,20 @@ async function fetchPullRequestDetails(pullRequestUrl: string): Promise { + const providerToken = await getGitHubAccessToken(); + const octokit = new Octokit({ auth: providerToken }); + + try { + const issue = (await octokit.request(`GET ${issueUrl}`)).data as GitHubIssue; + return issue; + } catch (error) { + console.warn("Error fetching issue:", error); + } + return null; +} + // Function to fetch the issue associated with a pull request async function fetchIssueFromPullRequest(pullRequest: GitHubPullRequest): Promise { const providerToken = await getGitHubAccessToken(); @@ -99,6 +118,28 @@ export async function fetchPullRequestNotifications(): Promise { + const notifications = await fetchNotifications(); + if (!notifications) return null; + + const aggregatedData: GitHubAggregated[] = []; + const filteredNotifications = filterIssueNotifications(notifications); + + for (const notification of filteredNotifications) { + const issueUrl = notification.subject.url; + const issue = await fetchIssueDetails(issueUrl); + if (!issue || issue.state === "closed") { + continue; // Skip closed issues + } + + aggregatedData.push({ notification, pullRequest: null, issue }); + } + + console.log("issueNotifications", aggregatedData); + return aggregatedData; +} \ No newline at end of file diff --git a/src/home/github-types.ts b/src/home/github-types.ts index f9f897a..9682061 100644 --- a/src/home/github-types.ts +++ b/src/home/github-types.ts @@ -20,7 +20,7 @@ export type GitHubNotifications = RestEndpointMethodTypes["activity"]["listNotif export type GitHubNotification = GitHubNotifications[0]; export type GitHubAggregated = { issue: GitHubIssue; - pullRequest: GitHubPullRequest; + pullRequest: GitHubPullRequest | null; notification: GitHubNotification; }; export type GitHubLabel = diff --git a/src/home/home.ts b/src/home/home.ts index 0781240..e6e6a18 100644 --- a/src/home/home.ts +++ b/src/home/home.ts @@ -1,7 +1,7 @@ import { grid } from "../the-grid"; import { authentication } from "./authentication"; import { displayNotifications } from "./fetch-github/fetch-and-display-previews"; -import { fetchPullRequestNotifications } from "./fetch-github/fetch-data"; +import { fetchIssueNotifications, fetchPullRequestNotifications } from "./fetch-github/fetch-data"; import { readyToolbar } from "./ready-toolbar"; import { renderServiceMessage } from "./render-service-message"; import { renderErrorInModal } from "./rendering/display-popup-modal"; @@ -29,6 +29,7 @@ if (!notificationsContainer) { } export const pullRequestNotifications = void fetchPullRequestNotifications(); +export const issueNotifications = void fetchIssueNotifications(); void (async function home() { void authentication();