summaryrefslogtreecommitdiff
path: root/source/github-helpers/get-comment-author.ts
blob: af21ee9debd2b66e97ecc469e831cfb82106407f (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
/**
Given any element in a comment, returns the comment’s author

This works on:
- First comment
- Following comments
- Main review comment
- Following review comments
- Gist comments
- Bots

Note: Bots are used as `name[bot]`, `app/name`, or `apps/name` depending on the context:
- https://github.com/webpack/webpack/commits?author=dependabot%5Bbot%5D
- https://github.com/webpack/webpack/pulls/app%2Fdependabot
- https://github.com/apps/dependabot

@returns user-name
@returns dependabot[bot]

*/
export default function getCommentAuthor(anyElementInsideComment: Element): string {
	const avatar = anyElementInsideComment
		.closest('.TimelineItem, .review-comment')!
		.querySelector('.TimelineItem-avatar img, img.avatar')!;

	const name = avatar
		.alt // Occasionally ends with `[bot]`
		.replace(/^@/, ''); // May or may not be present

	if (!name.endsWith('[bot]') && avatar.closest('[href^="https://github.com/apps/"]')) {
		// Example: https://github.com/webpack/webpack/pull/15926#issuecomment-1170670173
		return name + '[bot]';
	}

	return name;
}