Fix GitHub unread count when the Link header omits rel="last"
This commit is contained in:
1 parent
a093064bf0
commit
b2e7d74b4f
5 files changed
+126
-88
No files matched your search
+17
-16
@@ -46,30 +46,31 @@ export function apiBaseUrl(instanceUrl = GITHUB_DEFAULT_URL) {
|
||||
return isDotComHost(hostname) ? 'https://api.github.com' : `${origin}/api/v3`;
|
||||
}
|
||||
|
||||
// The notifications endpoint returns unread threads only by default, so
|
||||
// `per_page=1` lets us read the total from the Link header instead of
|
||||
// downloading every notification.
|
||||
// The notifications endpoint returns unread threads only by default. GitHub no
|
||||
// longer guarantees a `rel="last"` Link (it is omitted when the final page
|
||||
// can't be calculated), so the count comes from listing the unread threads and
|
||||
// following `rel="next"`. `per_page=50` is the documented maximum, which keeps
|
||||
// the common case to a single request.
|
||||
export function notificationsUrl(instanceUrl) {
|
||||
return `${apiBaseUrl(instanceUrl)}/notifications?per_page=1`;
|
||||
return `${apiBaseUrl(instanceUrl)}/notifications?per_page=50`;
|
||||
}
|
||||
|
||||
// Upper bound on pages walked while counting, so a pathological or looping
|
||||
// `rel="next"` can't spin forever. 20 pages at 50 each = 1000 notifications.
|
||||
export const GITHUB_NOTIFICATIONS_MAX_PAGES = 20;
|
||||
|
||||
export function authorizationHeader(token) {
|
||||
return `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// GitHub paginates with RFC 5988 Link headers. With `per_page=1` the last page
|
||||
// number equals the total unread count; when there is no Link header there is
|
||||
// at most one page, so the fallback length is already exact.
|
||||
export function unreadCountFromLink(linkHeader, fallback = 0) {
|
||||
// Extracts the `rel="next"` URL from an RFC 5988 Link header, or null when
|
||||
// there is no next page. Matches on the angle brackets so a comma inside a
|
||||
// pagination URL's query string can't split it.
|
||||
export function nextLink(linkHeader) {
|
||||
if (!linkHeader) {
|
||||
return fallback;
|
||||
return null;
|
||||
}
|
||||
|
||||
const match = /[?&]page=(\d+)[^>]*>;\s*rel="last"/.exec(linkHeader);
|
||||
if (!match) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const count = Number.parseInt(match[1], 10);
|
||||
return Number.isNaN(count) ? fallback : count;
|
||||
const match = /<([^>]+)>;\s*rel="next"/.exec(linkHeader);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
@@ -4,9 +4,10 @@ import { HttpError } from './http';
|
||||
import { authorizationHeader, unreadCountUrl } from '../config/feedbin';
|
||||
import {
|
||||
GITHUB_ACCEPT,
|
||||
GITHUB_NOTIFICATIONS_MAX_PAGES,
|
||||
authorizationHeader as githubAuthorizationHeader,
|
||||
nextLink,
|
||||
notificationsUrl,
|
||||
unreadCountFromLink,
|
||||
} from '../config/github';
|
||||
|
||||
export const NOTIFICATIONS_DEFAULT_INTERVAL = 60000;
|
||||
@@ -58,23 +59,33 @@ async function fetchFeedbinNotifications(integration, http) {
|
||||
}
|
||||
|
||||
// GitHub's notifications endpoint returns unread threads only by default.
|
||||
// Requesting a single thread lets us read the total from the Link header's
|
||||
// `rel="last"` page instead of downloading every notification.
|
||||
// GitHub no longer guarantees a `rel="last"` Link, so the count comes from
|
||||
// walking the unread list via `rel="next"` and summing the threads. Most
|
||||
// accounts fit in a single page.
|
||||
async function fetchGithubNotifications(integration, http) {
|
||||
const response = await http.request(notificationsUrl(integration.url), {
|
||||
headers: {
|
||||
Authorization: githubAuthorizationHeader(integration.token),
|
||||
Accept: GITHUB_ACCEPT,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
const fallback = Array.isArray(data) ? data.length : 0;
|
||||
|
||||
return {
|
||||
unreadCount: unreadCountFromLink(response.headers.get('Link'), fallback),
|
||||
notifications: [],
|
||||
const headers = {
|
||||
Authorization: githubAuthorizationHeader(integration.token),
|
||||
Accept: GITHUB_ACCEPT,
|
||||
};
|
||||
|
||||
let url = notificationsUrl(integration.url);
|
||||
let unreadCount = 0;
|
||||
const seen = new Set();
|
||||
|
||||
while (url && !seen.has(url) && seen.size < GITHUB_NOTIFICATIONS_MAX_PAGES) {
|
||||
seen.add(url);
|
||||
|
||||
const response = await http.request(url, { headers });
|
||||
const data = await response.json();
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
unreadCount += data.filter((thread) => thread?.unread !== false).length;
|
||||
}
|
||||
|
||||
url = nextLink(response.headers.get('Link'));
|
||||
}
|
||||
|
||||
return { unreadCount, notifications: [] };
|
||||
}
|
||||
|
||||
const ADAPTERS = {
|
||||
|
||||
@@ -80,7 +80,7 @@ module(
|
||||
assert.strictEqual(requests.length, 1);
|
||||
assert.strictEqual(
|
||||
requests[0].url,
|
||||
'https://api.github.com/notifications?per_page=1'
|
||||
'https://api.github.com/notifications?per_page=50'
|
||||
);
|
||||
assert.strictEqual(
|
||||
requests[0].options.headers['Authorization'],
|
||||
@@ -129,7 +129,7 @@ module(
|
||||
|
||||
assert.strictEqual(
|
||||
requests[0].url,
|
||||
'https://git.example.com/api/v3/notifications?per_page=1'
|
||||
'https://git.example.com/api/v3/notifications?per_page=50'
|
||||
);
|
||||
assert.strictEqual(saved[0].url, 'https://git.example.com');
|
||||
});
|
||||
@@ -253,7 +253,7 @@ module(
|
||||
assert.strictEqual(requests.length, 1);
|
||||
assert.strictEqual(
|
||||
requests[0].url,
|
||||
'https://git.example.com/api/v3/notifications?per_page=1'
|
||||
'https://git.example.com/api/v3/notifications?per_page=50'
|
||||
);
|
||||
assert.strictEqual(
|
||||
requests[0].options.headers['Authorization'],
|
||||
|
||||
@@ -3,9 +3,9 @@ import {
|
||||
apiBaseUrl,
|
||||
authorizationHeader,
|
||||
isGithubDotCom,
|
||||
nextLink,
|
||||
normalizeGithubUrl,
|
||||
notificationsUrl,
|
||||
unreadCountFromLink,
|
||||
} from 'dashtab/config/github';
|
||||
|
||||
module('Unit | Config | github', function () {
|
||||
@@ -43,14 +43,14 @@ module('Unit | Config | github', function () {
|
||||
);
|
||||
});
|
||||
|
||||
test('notificationsUrl requests a single unread thread', function (assert) {
|
||||
test('notificationsUrl requests the maximum page size', function (assert) {
|
||||
assert.strictEqual(
|
||||
notificationsUrl('https://github.com'),
|
||||
'https://api.github.com/notifications?per_page=1'
|
||||
'https://api.github.com/notifications?per_page=50'
|
||||
);
|
||||
assert.strictEqual(
|
||||
notificationsUrl('https://git.example.com'),
|
||||
'https://git.example.com/api/v3/notifications?per_page=1'
|
||||
'https://git.example.com/api/v3/notifications?per_page=50'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -58,22 +58,24 @@ module('Unit | Config | github', function () {
|
||||
assert.strictEqual(authorizationHeader('ghp_x'), 'Bearer ghp_x');
|
||||
});
|
||||
|
||||
test('unreadCountFromLink reads the last page and falls back otherwise', function (assert) {
|
||||
test('nextLink extracts the next page and tolerates its absence', function (assert) {
|
||||
assert.strictEqual(
|
||||
unreadCountFromLink(
|
||||
'<https://api.github.com/notifications?per_page=1&page=2>; rel="next", <https://api.github.com/notifications?per_page=1&page=7>; rel="last"',
|
||||
0
|
||||
nextLink(
|
||||
'<https://api.github.com/notifications?per_page=50&page=2>; rel="next", <https://api.github.com/notifications?per_page=50&page=7>; rel="last"'
|
||||
),
|
||||
7
|
||||
'https://api.github.com/notifications?per_page=50&page=2'
|
||||
);
|
||||
assert.strictEqual(
|
||||
unreadCountFromLink(
|
||||
'<https://api.github.com/notifications?per_page=1&page=2>; rel="next"',
|
||||
1
|
||||
nextLink(
|
||||
'<https://api.github.com/notifications?per_page=50&after=cursor,1&page=2>; rel="next"'
|
||||
),
|
||||
1
|
||||
'https://api.github.com/notifications?per_page=50&after=cursor,1&page=2'
|
||||
);
|
||||
assert.strictEqual(unreadCountFromLink(null, 3), 3);
|
||||
assert.strictEqual(unreadCountFromLink('garbage', 4), 4);
|
||||
assert.strictEqual(
|
||||
nextLink('<https://api.github.com/notifications?page=7>; rel="last"'),
|
||||
null
|
||||
);
|
||||
assert.strictEqual(nextLink(null), null);
|
||||
assert.strictEqual(nextLink('garbage'), null);
|
||||
});
|
||||
});
|
||||
@@ -265,7 +265,7 @@ module('Unit | Service | notifications', function (hooks) {
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite reads the GitHub unread count from the Link header', async function (assert) {
|
||||
test('fetchSite counts the GitHub unread threads', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'github', {
|
||||
type: 'github',
|
||||
@@ -285,13 +285,12 @@ module('Unit | Service | notifications', function (hooks) {
|
||||
requestedHeaders = options.headers;
|
||||
return {
|
||||
ok: true,
|
||||
headers: {
|
||||
get: (name) =>
|
||||
name === 'Link'
|
||||
? '<https://api.github.com/notifications?per_page=1&page=2>; rel="next", <https://api.github.com/notifications?per_page=1&page=7>; rel="last"'
|
||||
: null,
|
||||
},
|
||||
json: async () => [],
|
||||
headers: { get: () => null },
|
||||
json: async () => [
|
||||
{ id: '1', unread: true },
|
||||
{ id: '2', unread: true },
|
||||
{ id: '3', unread: false },
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
@@ -300,7 +299,7 @@ module('Unit | Service | notifications', function (hooks) {
|
||||
|
||||
assert.strictEqual(
|
||||
requestedUrl,
|
||||
'https://api.github.com/notifications?per_page=1'
|
||||
'https://api.github.com/notifications?per_page=50'
|
||||
);
|
||||
assert.strictEqual(requestedHeaders['Authorization'], 'Bearer ghp_token');
|
||||
assert.strictEqual(
|
||||
@@ -310,12 +309,64 @@ module('Unit | Service | notifications', function (hooks) {
|
||||
|
||||
const state = service.stateFor(site);
|
||||
assert.strictEqual(state.status, 'ready');
|
||||
assert.strictEqual(state.unreadCount, 7);
|
||||
assert.strictEqual(state.unreadCount, 2);
|
||||
assert.deepEqual(state.items, []);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite follows the GitHub next link across pages', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'github', {
|
||||
type: 'github',
|
||||
status: 'active',
|
||||
url: 'https://github.com',
|
||||
token: 'ghp_token',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:notifications');
|
||||
service.fetchAll = async () => {};
|
||||
await service.ready;
|
||||
|
||||
const requested = [];
|
||||
globalThis.fetch = async (url) => {
|
||||
requested.push(url);
|
||||
|
||||
if (requested.length === 1) {
|
||||
return {
|
||||
ok: true,
|
||||
headers: {
|
||||
get: (name) =>
|
||||
name === 'Link'
|
||||
? '<https://api.github.com/notifications?per_page=50&page=2>; rel="next"'
|
||||
: null,
|
||||
},
|
||||
json: async () => [
|
||||
{ id: '1', unread: true },
|
||||
{ id: '2', unread: true },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
headers: { get: () => null },
|
||||
json: async () => [{ id: '3', unread: true }],
|
||||
};
|
||||
};
|
||||
|
||||
const [site] = service.sites.all;
|
||||
await service.fetchSite(site);
|
||||
|
||||
assert.deepEqual(requested, [
|
||||
'https://api.github.com/notifications?per_page=50',
|
||||
'https://api.github.com/notifications?per_page=50&page=2',
|
||||
]);
|
||||
assert.strictEqual(service.stateFor(site).unreadCount, 3);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite derives the GitHub Enterprise API base from the host', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'github', {
|
||||
@@ -340,39 +391,12 @@ module('Unit | Service | notifications', function (hooks) {
|
||||
|
||||
assert.strictEqual(
|
||||
requestedUrl,
|
||||
'https://git.example.com/api/v3/notifications?per_page=1'
|
||||
'https://git.example.com/api/v3/notifications?per_page=50'
|
||||
);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite treats a missing GitHub Link header as the returned length', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'github', {
|
||||
type: 'github',
|
||||
status: 'active',
|
||||
url: 'https://github.com',
|
||||
token: 'ghp_token',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:notifications');
|
||||
service.fetchAll = async () => {};
|
||||
await service.ready;
|
||||
|
||||
globalThis.fetch = async () => ({
|
||||
ok: true,
|
||||
headers: { get: () => null },
|
||||
json: async () => [{ id: '1', unread: true }],
|
||||
});
|
||||
|
||||
const [site] = service.sites.all;
|
||||
await service.fetchSite(site);
|
||||
|
||||
assert.strictEqual(service.stateFor(site).unreadCount, 1);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite flags an invalid GitHub token', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'github', {
|
||||
|
||||
Reference in new issue
Block a user