forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-comment-author.ts
More file actions
46 lines (39 loc) · 1.48 KB
/
get-comment-author.ts
File metadata and controls
46 lines (39 loc) · 1.48 KB
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
/**
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 or dependabot[bot]
*/
export default function getCommentAuthor(anyElementInsideComment: Element): string {
const avatar: HTMLImageElement = anyElementInsideComment
.closest([
'.TimelineItem', // PR comments (and pre-issue redesign issue comments)
'.review-comment', // PR review comments
'.react-issue-body', // First issue comment
'.react-issue-comment', // Issue comments
'[data-testid="comment-header"]', // Commit comments
])!
.querySelector([
'.TimelineItem-avatar img', // PR comments (and pre-issue redesign issue comments)
'img.avatar', // PR review comments
'img[data-testid="github-avatar"]', // Issue comments
'img[data-component="Avatar"]', // Commit comments
])!;
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;
}