Skip to content

Commit

Permalink
feat: fetchIssueNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zugdev committed Dec 5, 2024
1 parent af9c62b commit e5b8de8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/home/fetch-github/fetch-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GitHubPullRequest | null> {
const providerToken = await getGitHubAccessToken();
Expand All @@ -58,6 +63,20 @@ async function fetchPullRequestDetails(pullRequestUrl: string): Promise<GitHubPu
return null;
}

// Function to fetch the issue details
async function fetchIssueDetails(issueUrl: string): Promise<GitHubIssue | null> {
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<GitHubIssue | null> {
const providerToken = await getGitHubAccessToken();
Expand Down Expand Up @@ -99,6 +118,28 @@ export async function fetchPullRequestNotifications(): Promise<GitHubAggregated[
aggregatedData.push({ notification, pullRequest, issue });
}

console.log("aggregatedData", aggregatedData);
console.log("pullRequestNotifications", aggregatedData);
return aggregatedData;
}

// Main function to fetch issue notifications with related issue data
export async function fetchIssueNotifications(): Promise<GitHubAggregated[] | null> {
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;
}
2 changes: 1 addition & 1 deletion src/home/github-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
3 changes: 2 additions & 1 deletion src/home/home.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -29,6 +29,7 @@ if (!notificationsContainer) {
}

export const pullRequestNotifications = void fetchPullRequestNotifications();
export const issueNotifications = void fetchIssueNotifications();

void (async function home() {
void authentication();
Expand Down

0 comments on commit e5b8de8

Please sign in to comment.