[github] Fix Notification Links for PRs (#55)

If a notification was related to a pull request the link we added to the
item didn't work. This should now be fixed, so that when a user clicks
on the item he is redirected to the correct pull request which is
associated with the item.

This commit also removes an unnecessary "console.log" statement.
This commit is contained in:
Rico Berger
2023-10-29 13:49:06 +01:00
committed by GitHub
parent e994ab6214
commit 8e3586f315
2 changed files with 20 additions and 7 deletions

View File

@@ -110,12 +110,7 @@ export const getGithubFeed = async (
columnId: source.columnId,
sourceId: source.id,
title: notification.subject?.title ?? "Notification",
link: notification.subject?.url
? `https://github.com/${notification.subject?.url.replace(
"https://api.github.com/repos/",
"",
)}`
: "",
link: getLinkFromApiUrl(notification.subject?.url),
media: notification.repository.owner.avatar_url,
description: formatDescription(notification),
author: notification.repository?.full_name,
@@ -399,6 +394,25 @@ const request = async (
return await res.json();
};
/**
* `getLinkFromApiUrl` returns a link which can be clicked by a user based on the given GitHub API url. If there is no
* url given we return the default GitHub notifications link.
*/
const getLinkFromApiUrl = (url?: string): string => {
if (!url) {
return "https://github.com/notifications";
}
if (/^https:\/\/api.github.com\/repos\/.*\/.*\/pulls\/\d+$/.test(url)) {
const n = url.lastIndexOf("pulls");
url = url.slice(0, n) + url.slice(n).replace("pulls", "pull");
}
return `https://github.com/${
url.replace("https://api.github.com/repos/", "")
}`;
};
/**
* `formatDescription` formats the description for a notification based on the notification reason.
* See: https://docs.github.com/en/rest/activity/notifications?apiVersion=2022-11-28#about-notification-reasons

View File

@@ -41,7 +41,6 @@ export const getXFeed = async (
throw new Error("Invalid feed");
}
const feed = JSON.parse(matches[1]) as Feed;
console.log(feed);
/**
* Generate a source id based on the user id, column id and the normalized `twitter` options. Besides that we also set