master
 1import { define, Reaction } from '#src'
 2
 3type Where = {
 4  count: number
 5  url: string
 6}
 7
 8export default define({
 9  url: import.meta.url,
10  tiers: true,
11  badges: ['thumbs-down-10', 'thumbs-down-50', 'thumbs-down-100'] as const,
12  present(data, grant) {
13    const moreThan10: Where[] = []
14    const moreThan50: Where[] = []
15    const moreThan100: Where[] = []
16
17    for (const x of [
18      ...data.issues,
19      ...data.pulls,
20      ...data.discussionComments,
21      ...data.issueComments,
22    ]) {
23      if (x.reactions && x.reactions.length > 0) {
24        const counts = count(x.reactions)
25        if (counts.THUMBS_DOWN > 100) {
26          moreThan100.push({ count: counts.THUMBS_DOWN, url: x.url })
27        } else if (counts.THUMBS_DOWN > 50) {
28          moreThan50.push({ count: counts.THUMBS_DOWN, url: x.url })
29        } else if (counts.THUMBS_DOWN > 10) {
30          moreThan10.push({ count: counts.THUMBS_DOWN, url: x.url })
31        }
32      }
33    }
34
35    moreThan10.sort((a, b) => b.count - a.count)
36    moreThan50.sort((a, b) => b.count - a.count)
37    moreThan100.sort((a, b) => b.count - a.count)
38
39    if (moreThan10.length > 0) {
40      grant('thumbs-down-10', `I got more than 10 thumbs down.`)
41        .evidence(text(moreThan10))
42        .tier(1)
43    }
44    if (moreThan50.length > 0) {
45      grant('thumbs-down-50', `I got more than 50 thumbs down.`)
46        .evidence(text(moreThan50))
47        .tier(2)
48    }
49    if (moreThan100.length > 0) {
50      grant('thumbs-down-100', `I got more than 100 thumbs down.`)
51        .evidence(text(moreThan100))
52        .tier(3)
53    }
54  },
55})
56
57function count(reactions: Reaction[] | undefined) {
58  const counts: Record<Reaction['content'], number> = {
59    CONFUSED: 0,
60    EYES: 0,
61    HEART: 0,
62    HOORAY: 0,
63    LAUGH: 0,
64    ROCKET: 0,
65    THUMBS_DOWN: 0,
66    THUMBS_UP: 0,
67  }
68  for (const reaction of reactions ?? []) {
69    counts[reaction.content] = (counts[reaction.content] || 0) + 1
70  }
71  return counts
72}
73
74function text(entries: Where[]): string {
75  const lines: string[] = []
76  for (const where of entries) {
77    lines.push(`* <a href="${where.url}">${where.count} 👎</a>`)
78  }
79  return lines.join('\n')
80}