1use serde::Deserialize;
2
3use crate::{
4 error::PlatformError,
5 platform::{Release, ReleaseAsset, ReleasePlatform},
6};
7
8pub struct Gitlab;
9impl ReleasePlatform for Gitlab {
10 const API_BASE_PRIMARY: &'static str = "https://gitlab.com";
11
12 const API_BASE_PKGFORGE: &'static str = "https://api.gl.pkgforge.dev";
13
14 const TOKEN_ENV_VAR: &'static str = "GITLAB_TOKEN";
15
16 fn format_project_path(project: &str) -> Result<(String, String), PlatformError> {
17 if project.chars().all(|c| c.is_numeric()) {
18 Ok((project.to_string(), String::new()))
19 } else {
20 match project.split_once('/') {
21 Some((owner, repo)) => Ok((owner.to_string(), repo.to_string())),
22 None => Ok((project.to_string(), String::new())),
23 }
24 }
25 }
26
27 fn format_api_path(project: &str) -> Result<String, PlatformError> {
28 if project.chars().all(|c| c.is_numeric()) {
29 Ok(format!("/api/v4/projects/{}/releases", project))
30 } else {
31 let encoded_path = project.replace('/', "%2F");
32 Ok(format!("/api/v4/projects/{}/releases", encoded_path))
33 }
34 }
35}
36
37#[derive(Debug, Deserialize)]
38pub struct GitlabAssets {
39 pub links: Vec<GitlabAsset>,
40}
41
42#[derive(Debug, Deserialize)]
43pub struct GitlabRelease {
44 name: String,
45 tag_name: String,
46 upcoming_release: bool,
47 released_at: String,
48 assets: GitlabAssets,
49}
50
51impl Release<GitlabAsset> for GitlabRelease {
52 fn name(&self) -> &str {
53 &self.name
54 }
55
56 fn tag_name(&self) -> &str {
57 &self.tag_name
58 }
59
60 fn is_prerelease(&self) -> bool {
61 self.upcoming_release
62 }
63
64 fn published_at(&self) -> &str {
65 &self.released_at
66 }
67
68 fn assets(&self) -> Vec<GitlabAsset> {
69 self.assets.links.clone()
70 }
71}
72
73#[derive(Clone, Debug, Deserialize)]
74pub struct GitlabAsset {
75 pub name: String,
76 pub direct_asset_url: String,
77}
78
79impl ReleaseAsset for GitlabAsset {
80 fn name(&self) -> &str {
81 &self.name
82 }
83
84 fn size(&self) -> Option<u64> {
85 None
86 }
87
88 fn download_url(&self) -> &str {
89 &self.direct_asset_url
90 }
91}