77 lines
2.6 KiB
JavaScript
77 lines
2.6 KiB
JavaScript
export const GITHUB_DEFAULT_URL = 'https://github.com';
|
|
|
|
// The bundled icon is an inverted, transparent GitHub mark (a white circle
|
|
// with the cat punched out), so it is rendered on a very dark grey backdrop.
|
|
export const GITHUB_ICON_URL = '/img/icons/github-inverted.png';
|
|
|
|
export const GITHUB_ICON_BACKGROUND = '#1f2937';
|
|
|
|
export const GITHUB_ACCEPT = 'application/vnd.github+json';
|
|
|
|
// Normalizes the user-supplied instance URL to a bare origin. GitHub.com and
|
|
// self-hosted GitHub Enterprise Server instances are both accepted.
|
|
export function normalizeGithubUrl(input) {
|
|
let value = (input ?? '').trim();
|
|
|
|
if (!value || /\s/.test(value)) {
|
|
return null;
|
|
}
|
|
|
|
if (!/^https?:\/\//i.test(value)) {
|
|
value = `https://${value}`;
|
|
}
|
|
|
|
try {
|
|
return new URL(value).origin;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function isDotComHost(hostname) {
|
|
return hostname === 'github.com' || hostname === 'www.github.com';
|
|
}
|
|
|
|
export function isGithubDotCom(instanceUrl) {
|
|
const origin = normalizeGithubUrl(instanceUrl);
|
|
return origin ? isDotComHost(new URL(origin).hostname) : false;
|
|
}
|
|
|
|
// GitHub.com serves the REST API from a dedicated host; GitHub Enterprise
|
|
// Server exposes it under `/api/v3` on the instance itself.
|
|
export function apiBaseUrl(instanceUrl = GITHUB_DEFAULT_URL) {
|
|
const origin = normalizeGithubUrl(instanceUrl) ?? GITHUB_DEFAULT_URL;
|
|
const { hostname } = new URL(origin);
|
|
|
|
return isDotComHost(hostname) ? 'https://api.github.com' : `${origin}/api/v3`;
|
|
}
|
|
|
|
// 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=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}`;
|
|
}
|
|
|
|
// 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 null;
|
|
}
|
|
|
|
const match = /<([^>]+)>;\s*rel="next"/.exec(linkHeader);
|
|
return match ? match[1] : null;
|
|
}
|