summaryrefslogtreecommitdiff
path: root/source/features/mark-merge-commits-in-list.tsx
blob: d0d9ec8a4f3cfba334d42e9a612f318335388239 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import './mark-merge-commits-in-list.css';
import React from 'dom-chef';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';
import PullRequestIcon from 'octicon/git-pull-request.svg';

import features from '.';
import * as api from '../github-helpers/api';
import {getRepoGQL} from '../github-helpers';

const filterMergeCommits = async (commits: string[]): Promise<string[]> => {
	const {repository} = await api.v4(`
		repository(${getRepoGQL()}) {
			${commits.map((commit: string) => `
				${api.escapeKey(commit)}: object(expression: "${commit}") {
				... on Commit {
						parents {
							totalCount
						}
					}
				}
			`).join('\n')}
		}
	`);

	const mergeCommits = [];
	for (const [key, commit] of Object.entries<AnyObject>(repository)) {
		if (commit.parents.totalCount >= 2) {
			mergeCommits.push(key.slice(1));
		}
	}

	return mergeCommits;
};

// eslint-disable-next-line import/prefer-default-export
export function getCommitHash(commit: HTMLElement): string {
	return 	commit.dataset.channel!.split(':')[3] ?? // Pre "Repository refresh" layout
	commit.querySelector<HTMLAnchorElement>('a[href]')!.pathname.split('/').pop()!;
}

async function init(): Promise<void> {
	const pageCommits = select.all([
		'li.commit', // Pre "Repository refresh" layout
		'.js-commits-list-item'
	]);
	const mergeCommits = await filterMergeCommits(pageCommits.map(getCommitHash));
	for (const commit of pageCommits) {
		if (mergeCommits.includes(getCommitHash(commit))) {
			commit.classList.add('rgh-merge-commit');
			select([
				'.commit-title', // Pre "Repository refresh" layout
				'div > p'
			], commit)!.prepend(<PullRequestIcon/>);
		}
	}
}

void features.add(__filebasename, {
	include: [
		pageDetect.isCommitList
	],
	init
});