summaryrefslogtreecommitdiff
path: root/source/features/add-co-authored-by.tsx
blob: 5cbe6c409d11fbb0df4221c7ae63afaeaf608066 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import select from 'select-dom';
import * as api from '../libs/api';
import features from '../libs/features';
import {getDiscussionNumber, getOP, getRepoGQL} from '../libs/utils';
import onPrMergePanelOpen from '../libs/on-pr-merge-panel-open';

interface Author {
	email: string;
	name: string; // Used when the commit isn't linked to a GitHub user
	user: {
		name: string;
		login: string;
	};
}

let coAuthors: Author[];

async function fetchCoAuthoredData(): Promise<Author[]> {
	const userInfo = await api.v4(`
		repository(${getRepoGQL()}) {
			pullRequest(number: ${getDiscussionNumber()}) {
				commits(first: 100) {
					nodes {
						commit {
							author {
								email
								name
								user {
									login
									name
								}
							}
						}
					}
				}
			}
		}
	`);

	return userInfo.repository.pullRequest.commits.nodes.map((node: AnyObject) => node.commit.author as Author);
}

function addCoAuthors(): void {
	const field = select<HTMLTextAreaElement>('#merge_message_field')!;
	if (field.value.includes('Co-authored-by: ')) {
		// Don't affect existing information
		return;
	}

	const addendum = new Map();
	for (const {email, user, name} of coAuthors) {
		if (user) {
			addendum.set(user.login, `Co-authored-by: ${user.name} <${email}>`);
		} else {
			addendum.set(name, `Co-authored-by: ${name} <${email}>`);
		}
	}

	addendum.delete(getOP());

	if (addendum.size > 0) {
		field.value += '\n\n' + [...addendum.values()].join('\n');
	}
}

async function init(): Promise<void> {
	coAuthors = await fetchCoAuthoredData();

	onPrMergePanelOpen(addCoAuthors);
}

features.add({
	id: __featureName__,
	description: 'Adds `co-authored-by` to the commit when merging PRs with multiple committers.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/51468821-71a42100-1da2-11e9-86aa-fc2a6a29da84.png',
	include: [
		features.isPRConversation
	],
	load: features.onAjaxedPages,
	init
});