Skip to content

Commit 5535170

Browse files
committed
Add Codeberg client
1 parent eb90417 commit 5535170

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

badgers-web/src/utils/Codeberg.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const API_BASE = 'https://codeberg.org/api/v1';
2+
3+
type ProjectInfo = {
4+
owner: string
5+
repo: string
6+
}
7+
8+
type Repository = {
9+
id: number
10+
default_branch: string
11+
name: string
12+
forks_count: number
13+
stars_count: number
14+
}
15+
16+
type Release = {
17+
name: string
18+
tag_name: string
19+
}
20+
21+
class CodebergClient {
22+
token: string
23+
24+
constructor(token: string) {
25+
this.token = token
26+
}
27+
28+
buildUrl(path: string, query: Record<string, any> = {}): string {
29+
const queryArgs = {
30+
...query,
31+
token: this.token,
32+
}
33+
const queries = Object
34+
.entries(queryArgs)
35+
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
36+
const queryComponent = (queries.length > 0) ? `?${queries.join('&')}` : ''
37+
38+
return `${API_BASE}/${path}${queryComponent}`
39+
}
40+
41+
async getRepository({ owner, repo }: ProjectInfo): Promise<Repository | null> {
42+
const repoId = `${owner}/${repo}`
43+
const url = this.buildUrl(`repos/${repoId}`)
44+
const resp = await fetch(url)
45+
46+
if (resp.status !== 200) return null
47+
return await resp.json() as Repository
48+
}
49+
50+
async getIssuesCount({ owner, repo }: ProjectInfo, query: Record<string, any> = {}): Promise<Number | null> {
51+
const repoId = `${owner}/${repo}`
52+
const url = this.buildUrl(`repos/${repoId}/issues`, query)
53+
const resp = await fetch(url)
54+
55+
if (resp.status !== 200) return null
56+
const count = resp.headers.get('x-total-count')
57+
return Number(count)
58+
}
59+
60+
async getLatestRelease({ owner, repo }: ProjectInfo): Promise<Release | null> {
61+
const repoId = `${owner}/${repo}`
62+
const url = this.buildUrl(`repos/${repoId}/releases/latest`)
63+
const resp = await fetch(url)
64+
65+
if (resp.status !== 200) return null
66+
return await resp.json() as Release
67+
}
68+
}
69+
70+
export default class Codeberg {
71+
static getClient(): CodebergClient {
72+
return new CodebergClient(process.env.CODEBERG_TOKEN as string)
73+
}
74+
}

0 commit comments

Comments
 (0)