master
 1import allBadges from '#badges'
 2import { linkCommit, linkIssue, linkPull } from './utils.js'
 3import { Data } from './data.js'
 4import { Commit } from './task/commits/commits.graphql.js'
 5import { PullRequest } from './task/pulls/pulls.graphql.js'
 6import { Issue } from './task/issues/issues.graphql.js'
 7
 8export type Presenters = (typeof allBadges)[number]['default']
 9
10export type ID = Presenters['badges'][number]
11
12export type List = readonly [string, ...string[]]
13
14export type Presenter<B extends List> = {
15  url: string
16  badges: B
17  tiers?: boolean
18  present: (
19    data: Data,
20    grant: (id: B[number], desc: string) => Evidence,
21  ) => void
22}
23
24export function define<B extends List>(presenter: Presenter<B>): Presenter<B> {
25  return presenter
26}
27
28export type Badge = {
29  id: ID
30  tier: number
31  desc: string
32  body: string
33  image: string
34}
35
36export class Evidence {
37  constructor(private badge: Badge) {}
38
39  tier(tier: number) {
40    this.badge.tier = tier
41    return this
42  }
43
44  evidence(text: string) {
45    this.badge.body = text
46    return this
47  }
48
49  evidenceCommits(...commits: Commit[]) {
50    this.evidence(
51      'Commits:\n\n' + commits.map((x) => `- ${linkCommit(x)}`).join('\n'),
52    )
53    return this
54  }
55
56  evidenceCommitsWithMessage(...commits: Commit[]) {
57    this.evidence(
58      'Commits:\n\n' +
59        commits.map((x) => `- ${linkCommit(x)}: ${x.message}`).join('\n'),
60    )
61    return this
62  }
63
64  evidencePRs(...pulls: PullRequest[]) {
65    this.evidence(
66      'Pull requests:\n\n' +
67        pulls
68          .map(linkPull)
69          .map((x) => '- ' + x)
70          .join('\n'),
71    )
72    return this
73  }
74
75  evidencePRsWithTitle(...pulls: PullRequest[]) {
76    this.evidence(
77      'Pull requests:\n\n' +
78        pulls.map((x) => `- ${linkPull(x)}: ${x.title}`).join('\n'),
79    )
80    return this
81  }
82
83  evidenceIssuesWithTitles(...issues: Issue[]) {
84    this.evidence(
85      'Issues:\n\n' +
86        issues.map((x) => `- ${linkIssue(x)}: ${x.title}`).join('\n'),
87    )
88    return this
89  }
90}