aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/forgejo_url.rs
blob: 90a14248adeb1a8254f48e87112f2b32aae5f86e (plain)
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
//! ref: composer/src/Composer/Util/ForgejoUrl.php

use anyhow::Result;
use shirabe_external_packages::composer::pcre::Preg;
use shirabe_php_shim::InvalidArgumentException;

#[derive(Debug)]
pub struct ForgejoUrl {
    pub owner: String,
    pub repository: String,
    pub origin_url: String,
    pub api_url: String,
}

impl ForgejoUrl {
    pub const URL_REGEX: &'static str =
        r"^(?:(?:https?|git)://([^/]+)/|git@([^:]+):/?)([^/]+)/([^/]+?)(?:\.git|/)?$";

    fn new(owner: String, repository: String, origin_url: String, api_url: String) -> Self {
        Self {
            owner,
            repository,
            origin_url,
            api_url,
        }
    }

    pub fn create(repo_url: &str) -> Result<Self> {
        match Self::try_from(Some(repo_url)) {
            Some(url) => Ok(url),
            None => Err(InvalidArgumentException {
                message: format!("This is not a valid Forgejo URL: {}", repo_url),
                code: 0,
            }
            .into()),
        }
    }

    pub fn try_from(repo_url: Option<&str>) -> Option<Self> {
        let repo_url = repo_url?;
        let mut matches: indexmap::IndexMap<
            shirabe_external_packages::composer::pcre::CaptureKey,
            String,
        > = indexmap::IndexMap::new();
        if !Preg::match3(Self::URL_REGEX, repo_url, Some(&mut matches)).unwrap_or(false) {
            return None;
        }
        use shirabe_external_packages::composer::pcre::CaptureKey;
        let m: Vec<String> = (0..5)
            .map(|i| {
                matches
                    .get(&CaptureKey::ByIndex(i))
                    .cloned()
                    .unwrap_or_default()
            })
            .collect();

        let origin_url = if !m[1].is_empty() {
            m[1].clone()
        } else {
            m[2].clone()
        }
        .to_lowercase();
        let api_base = format!("{}/api/v1", origin_url);

        Some(Self::new(
            m[3].clone(),
            m[4].clone(),
            origin_url.clone(),
            format!("https://{}/repos/{}/{}", api_base, m[3], m[4]),
        ))
    }

    pub fn generate_ssh_url(&self) -> String {
        format!(
            "git@{}:{}/{}.git",
            self.origin_url, self.owner, self.repository
        )
    }
}