Skip to main content

gitpoulpi_hosting/azure_devops/
spec.rs

1//! The AzureDevops family spec: remote parsing, URL shapes, well-known hosts.
2//!
3//! A pull-request project is `Project/Repo`; an issue project is just
4//! `Project` (plan D5). The web root is the organization (or Server
5//! collection) root, e.g. `https://dev.azure.com/acme`.
6
7use crate::client::encode_component;
8use crate::spec::FamilySpec;
9use gitpoulpi_config::{ApiFamily, IntegrationKind};
10
11pub static SPEC: FamilySpec = FamilySpec {
12    parse_remote: super::remote::parse_remote,
13    owns_remote: super::remote::owns_remote,
14    pr_web_url: |root, project, n| {
15        let (p, r) = split_project(project);
16        Some(format!(
17            "{}/{}/_git/{}/pullrequest/{n}",
18            trim_root(root),
19            enc(p),
20            enc(r)
21        ))
22    },
23    new_pr_url: |root, project, source, target| {
24        let (p, r) = split_project(project);
25        let mut url = format!(
26            "{}/{}/_git/{}/pullrequestcreate?sourceRef={}",
27            trim_root(root),
28            enc(p),
29            enc(r),
30            encode_component(source)
31        );
32        if let Some(target) = target {
33            url.push_str(&format!("&targetRef={}", encode_component(target)));
34        }
35        Some(url)
36    },
37    new_pr_form_url: |root, form| {
38        (SPEC.new_pr_url)(root, form.project, form.source_branch, form.target_branch)
39    },
40    issue_web_url: |root, project, reference| {
41        let reference = reference.trim();
42        let id = reference
43            .strip_prefix("AB#")
44            .or_else(|| reference.strip_prefix('#'))
45            .unwrap_or(reference);
46        let (p, _) = split_project(project);
47        (!id.is_empty() && id.chars().all(|c| c.is_ascii_digit()))
48            .then(|| format!("{}/{}/_workitems/edit/{id}", trim_root(root), enc(p)))
49    },
50    branch_url: |root, project, branch| {
51        let (p, r) = split_project(project);
52        Some(format!(
53            "{}/{}/_git/{}?version=GB{}",
54            trim_root(root),
55            enc(p),
56            enc(r),
57            encode_component(branch)
58        ))
59    },
60    commit_url: |root, project, sha| {
61        let (p, r) = split_project(project);
62        Some(format!(
63            "{}/{}/_git/{}/commit/{sha}",
64            trim_root(root),
65            enc(p),
66            enc(r)
67        ))
68    },
69    well_known: |host| {
70        let host = host.split('/').next().unwrap_or(host);
71        (host == "dev.azure.com"
72            || host == "ssh.dev.azure.com"
73            || host.ends_with(".visualstudio.com"))
74        .then_some(IntegrationKind::AzureDevops)
75    },
76    // A pull request is fetched by its source branch (`CheckoutSource::Branch`,
77    // the default): `refs/pull/{id}/head` is not documented.
78    ..FamilySpec::none(ApiFamily::AzureDevops, "AZ", '!')
79};
80
81fn trim_root(root: &str) -> &str {
82    root.trim_end_matches('/')
83}
84
85fn enc(segment: &str) -> String {
86    encode_component(segment)
87}
88
89/// `Project/Repo` to `(Project, Repo)`; a bare name serves as both.
90pub(super) fn split_project(project: &str) -> (&str, &str) {
91    project.split_once('/').unwrap_or((project, project))
92}