This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue-commentors.ts
81 lines (70 loc) · 2.59 KB
/
issue-commentors.ts
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
// Gets GitHub login names of all the commentors to a particular repo
//
// NOTE: This API can be really slow as it'll traverse a lot of pages depending
// on issue comment history. So, it's not recommended to use it. Keeping this
// for a reference only.
//
// NOTE: ALso, listing all issue commentors probably doesn't make much sense.
// They might be spammers who shouldn't be treated as contributors just because
// they commented on an issue.
import { requestData } from './utils/rest.js';
import { TTLCache, ImmutableCache } from './utils/cache.js';
interface Comment {
[key: string]: any;
user: {
login: string;
};
created_at: string;
author_association: string;
}
const CACHE_TTL = 7 * 24 * 60 * 60 * 1000;
const cache = new TTLCache<string, string[]>(CACHE_TTL);
interface PersistantCacheEntry {
commentors: string[];
endpoint: string;
}
const persistantCachePromise = new ImmutableCache<string, PersistantCacheEntry>(
'gh/issue-commentors',
).load();
const getEndpoint = (owner: string, repo: string) =>
new URL(`https://api.github.com/repos/${owner}/${repo}/issues/comments`);
/**
* Gets GitHub login names of all the commentors to a particular repo
* @param owner organisation/user, e.g. `"w3c"`
* @param repo repository name, e.g. `"payment-request"`
*/
export async function* getIssueCommentors(owner: string, repo: string) {
const cacheKey = `${owner}-${repo}`;
const cached = cache.get(cacheKey);
if (cached !== undefined) {
yield* cached;
return;
}
const lastCommentURL = getEndpoint(owner, repo);
const persistantCache = await persistantCachePromise;
const { endpoint, commentors } = persistantCache.get(cacheKey, true) || {
endpoint: getEndpoint(owner, repo).href,
commentors: [],
};
const uniqueCommentors = new Set<string>(commentors);
yield* commentors; // previously cached results
for await (const { result: comments } of requestData(endpoint, 100)) {
for (const comment of comments as Comment[]) {
const { login } = comment.user;
if (!uniqueCommentors.has(login)) {
uniqueCommentors.add(login);
yield login;
}
}
// update the persistent cache, so that next calls to getIssueCommentors
// start search from last comment timestamp using the `since` search param
const lastComment = comments[comments.length - 1] as Comment;
lastCommentURL.searchParams.set('since', lastComment.created_at);
persistantCache.set(cacheKey, {
endpoint: lastCommentURL.href,
commentors: [...uniqueCommentors],
});
}
cache.set(cacheKey, [...uniqueCommentors]);
await persistantCache.dump();
}