|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import axios, { AxiosRequestConfig } from 'axios'; |
| 18 | +import { USER_AGENT } from './githubAuth'; |
| 19 | +import type { GitProvider, SCMRepositoryMetadata } from './GitProvider'; |
| 20 | + |
| 21 | +interface GitLabRepoResponse { |
| 22 | + description?: string; |
| 23 | + license?: { nickname: string }; |
| 24 | + web_url: string; |
| 25 | + forked_from_project?: { full_name: string; web_url: string }; |
| 26 | + avatar_url?: string; |
| 27 | + namespace?: { web_url: string }; |
| 28 | +} |
| 29 | + |
| 30 | +function gitlabConfig(): AxiosRequestConfig { |
| 31 | + return { |
| 32 | + headers: { 'User-Agent': USER_AGENT }, |
| 33 | + validateStatus: () => true, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +export class GitLabProvider implements GitProvider { |
| 38 | + constructor(private readonly hostname: string) {} |
| 39 | + |
| 40 | + async getMetadata(project: string, name: string): Promise<SCMRepositoryMetadata | null> { |
| 41 | + const projectPath = encodeURIComponent(`${project}/${name}`); |
| 42 | + const base = `https://${this.hostname}`; |
| 43 | + const config = gitlabConfig(); |
| 44 | + |
| 45 | + try { |
| 46 | + const response = await axios.get<GitLabRepoResponse>( |
| 47 | + `${base}/api/v4/projects/${projectPath}`, |
| 48 | + config, |
| 49 | + ); |
| 50 | + if (response.status !== 200) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + let primaryLanguage: string | undefined; |
| 55 | + try { |
| 56 | + const languagesResponse = await axios.get<Record<string, number>>( |
| 57 | + `${base}/api/v4/projects/${projectPath}/languages`, |
| 58 | + config, |
| 59 | + ); |
| 60 | + if (languagesResponse.status === 200 && languagesResponse.data) { |
| 61 | + primaryLanguage = Object.keys(languagesResponse.data)[0]; |
| 62 | + } |
| 63 | + } catch { |
| 64 | + /* optional enrichment */ |
| 65 | + } |
| 66 | + |
| 67 | + const data = response.data; |
| 68 | + return { |
| 69 | + description: data.description, |
| 70 | + language: primaryLanguage, |
| 71 | + license: data.license?.nickname, |
| 72 | + htmlUrl: data.web_url, |
| 73 | + parentName: data.forked_from_project?.full_name, |
| 74 | + parentUrl: data.forked_from_project?.web_url, |
| 75 | + avatarUrl: data.avatar_url, |
| 76 | + profileUrl: data.namespace?.web_url, |
| 77 | + }; |
| 78 | + } catch { |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments