-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathgitlab.ts
More file actions
174 lines (151 loc) Β· 4.88 KB
/
gitlab.ts
File metadata and controls
174 lines (151 loc) Β· 4.88 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import type {
Backend,
BackendRelease,
BackendAsset,
BackendContributor,
BackendMember
} from './types';
interface GitLabAsset {
name: string;
direct_asset_url: string;
}
interface GitLabRelease {
tag_name: string;
description: string;
created_at: string;
upcoming_release: boolean;
assets: {
links: GitLabAsset[];
};
}
interface GitLabContributor {
name: string;
avatar_url: string;
web_url: string;
commits: number;
}
interface GitLabMember {
id: number;
username: string;
avatar_url: string;
web_url: string;
bio: string | null;
}
interface GitLabGpgKey {
id: number;
}
import { formatDatetime } from '../utils';
function encodeProject(owner: string, repo: string): string {
return encodeURIComponent(`${owner}/${repo}`);
}
export class GitLabBackend implements Backend {
private readonly baseUrl: string;
private readonly webUrl: string;
private readonly headers: HeadersInit;
constructor(url: string, token?: string) {
this.webUrl = url;
this.baseUrl = `${url}/api/v4`;
const headers: Record<string, string> = {
Accept: 'application/json'
};
if (token) {
headers['PRIVATE-TOKEN'] = token;
}
this.headers = headers;
}
private async fetchJson<T>(url: string): Promise<T> {
const response = await fetch(url, { headers: this.headers });
if (!response.ok) {
throw new Error(
`GitLab API error: ${response.status} ${response.statusText} β ${url}`
);
}
return response.json() as Promise<T>;
}
private mapRelease(release: GitLabRelease): BackendRelease {
return {
tag: release.tag_name,
releaseNote: release.description ?? '',
createdAt: formatDatetime(release.created_at),
prerelease: release.upcoming_release,
assets: release.assets.links.map(
(asset): BackendAsset => ({
name: asset.name,
downloadUrl: asset.direct_asset_url
})
)
};
}
async release(
owner: string,
repo: string,
prerelease: boolean
): Promise<BackendRelease> {
const project = encodeProject(owner, repo);
if (prerelease) {
const releases = await this.fetchJson<GitLabRelease[]>(
`${this.baseUrl}/projects/${project}/releases?per_page=1`
);
if (releases.length === 0) {
throw new Error(`No releases found for ${owner}/${repo}`);
}
return this.mapRelease(releases[0]);
}
const release = await this.fetchJson<GitLabRelease>(
`${this.baseUrl}/projects/${project}/releases/permalink/latest`
);
return this.mapRelease(release);
}
async releases(
owner: string,
repo: string,
count: number
): Promise<BackendRelease[]> {
const project = encodeProject(owner, repo);
const releases = await this.fetchJson<GitLabRelease[]>(
`${this.baseUrl}/projects/${project}/releases?per_page=${count}`
);
return releases.map((release) => this.mapRelease(release));
}
async contributors(
owner: string,
repo: string
): Promise<BackendContributor[]> {
const project = encodeProject(owner, repo);
const contributors = await this.fetchJson<GitLabContributor[]>(
`${this.baseUrl}/projects/${project}/repository/contributors?per_page=100`
);
return contributors.map((contributor) => ({
name: contributor.name,
avatarUrl: contributor.avatar_url,
url: contributor.web_url,
contributions: contributor.commits
}));
}
async members(organization: string): Promise<BackendMember[]> {
const groupMembers = await this.fetchJson<GitLabMember[]>(
`${this.baseUrl}/groups/${encodeURIComponent(organization)}/members`
);
const members = await Promise.all(
groupMembers.map(async (member) => {
const gpgKeys = await this.fetchJson<GitLabGpgKey[]>(
`${this.baseUrl}/users/${member.id}/gpg_keys`
);
return {
name: member.username,
avatarUrl: member.avatar_url,
url: member.web_url,
bio: member.bio,
gpgKeys: {
ids: gpgKeys.map((key) => String(key.id)),
url: `${this.webUrl}/${member.username}.gpg`
}
} satisfies BackendMember;
})
);
return members;
}
repositoryUrl(owner: string, repo: string): string {
return `${this.webUrl}/${owner}/${repo}`;
}
}