master
1import { Commit, define, Repository } from '#src'
2
3export default define({
4 url: import.meta.url,
5 tiers: true,
6 badges: [
7 'a-commit',
8 'ab-commit',
9 'abc-commit',
10 'abcd-commit',
11 'abcde-commit',
12 'abcdef-commit',
13 ] as const,
14 present(data, grant) {
15 const types: [string, (typeof this.badges)[number]][] = [
16 ['abcdef', 'abcdef-commit'],
17 ['abcde', 'abcde-commit'],
18 ['abcd', 'abcd-commit'],
19 ['abc', 'abc-commit'],
20 ['ab', 'ab-commit'],
21 ['a', 'a-commit'],
22 ]
23 const order: (Function | undefined)[] = Array.from(Array(10))
24
25 for (const repo of data.repos) {
26 for (const commit of repo.commits) {
27 for (const [prefix, badge] of types) {
28 const re = new RegExp(`^(${prefix})`)
29 if (re.test(commit.sha)) {
30 order[prefix.length] = () =>
31 grant(badge, `One of my commit sha starts with "${prefix}".`)
32 .evidence(link(re, repo, commit))
33 .tier(prefix.length)
34 break
35 }
36 }
37 }
38 }
39
40 for (const fn of order) {
41 if (fn) {
42 fn()
43 }
44 }
45 },
46})
47
48function link(re: RegExp, repo: Repository, commit: Commit) {
49 const sha = commit.sha.replace(re, '<strong>$1</strong>')
50 return `- <a href="https://github.com/${repo.owner.login}/${repo.name}/commit/${commit.sha}">${sha}</a>`
51}