summaryrefslogtreecommitdiff
path: root/source/features/use-first-commit-message-for-new-prs.tsx
blob: 00d5f28c884328fd76bc6fcabcbe371a62364a03 (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
import select from 'select-dom';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';
import * as textFieldEdit from 'text-field-edit';

import features from '.';
import looseParseInt from '../helpers/loose-parse-int';

async function init(): Promise<void | false> {
	const commitCountIcon = await elementReady('div.Box.mb-3 .octicon-git-commit');
	const commitCount = commitCountIcon?.nextElementSibling;
	if (!commitCount || looseParseInt(commitCount) < 2 || !select.exists('#new_pull_request')) {
		return false;
	}

	const [prTitle, ...prMessage] = (pageDetect.isEnterprise()
		? select('#commits_bucket [data-url$="compare/commit"] a[title]')!.title // TODO [2022-05-01]: Remove GHE code
		: select('#commits_bucket .js-commits-list-item a.Link--primary')!.innerHTML.replace(/<\/?code>/g, '`')
	)!.split(/\n\n/);

	textFieldEdit.set(
		select('.discussion-topic-header input')!,
		prTitle,
	);
	textFieldEdit.insert(
		select('#new_pull_request textarea[aria-label="Comment body"]')!,
		prMessage.join('\n\n'),
	);
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isCompare,
	],
	awaitDomReady: false,
	init,
});