summaryrefslogtreecommitdiff
path: root/source/github-helpers/get-default-branch.ts
blob: efbbdd4c70b5b80206edbf4d3e677f6832b5f925 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import cache from 'webext-storage-cache';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';

import * as api from './api.js';
import {getRepo} from './index.js';
import {branchSelector} from './selectors.js';

const isCurrentRepo = ({nameWithOwner}: pageDetect.RepositoryInfo): boolean => Boolean(getRepo()?.nameWithOwner === nameWithOwner);

// DO NOT use optional arguments/defaults in "cached functions" because they can't be memoized effectively
// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1864
const _getDefaultBranch = cache.function('default-branch', async (repository: pageDetect.RepositoryInfo): Promise<string> => {
	// TODO: extract from `ref-selector` if available https://github.com/refined-github/refined-github/issues/6557
	if (isCurrentRepo(repository) && ['', 'commits'].includes(repository.path)) {
		// We're on the default branch, so we can extract it from the current page. This usually happens on the pages:
		// @example /user/repo
		// @example /user/repo/commits (without further path)
		const branchPicker = await elementReady(branchSelector);
		if (branchPicker) {
			return branchPicker.textContent!.trim();
		}
	}

	const response = await api.v4(`
		repository(owner: "${repository.owner}", name: "${repository.name}") {
			defaultBranchRef {
				name
			}
		}
	`);

	return response.repository.defaultBranchRef.name;
}, {
	maxAge: {hours: 1},
	staleWhileRevalidate: {days: 20},
	cacheKey: ([repository]) => repository.nameWithOwner,
});

export default async function getDefaultBranch(repository: pageDetect.RepositoryInfo | undefined = getRepo()): Promise<string> {
	if (!repository) {
		throw new Error('getDefaultBranch was called on a non-repository page');
	}

	return _getDefaultBranch(repository);
}