stackless_integrations/providers/gitlab/
project.rs1use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12use crate::registry;
13
14pub const RESOURCE_KIND: &str = "integration-gitlab";
15
16pub const OUTPUT_FIELDS: &[(&str, &str, bool)] = &[
19 ("PROJECT_ID", "project_id", true),
20 ("WEB_URL", "web_url", false),
21];
22
23pub const OUTPUTS: &[&str] = &["project_id", "web_url"];
24
25#[derive(Debug, Serialize)]
26pub struct GitLabProjectConfig {
27 pub name: String,
28 pub visibility: String,
29}
30
31impl CatalogService for GitLabProjectConfig {
32 const REFERENCE: &'static str = "gitlab/project";
33}
34
35#[derive(Debug)]
36pub struct GitLabProject;
37
38impl Hostable for GitLabProject {
39 const PROVIDER: &'static str = "gitlab";
40 const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
41 const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
42 const RESOURCE_KIND: &'static str = RESOURCE_KIND;
43 const OUTPUTS: &'static [&'static str] = OUTPUTS;
44}
45
46impl FamilyResource for GitLabProject {
47 type Config = GitLabProjectConfig;
48 const PROVIDER_PREFIX: &'static str = "GITLAB";
49 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = OUTPUT_FIELDS;
50
51 fn build_config(ctx: &ProvisionContext<'_>) -> Result<GitLabProjectConfig, IntegrationError> {
52 let config = super::integration_config(ctx)?;
53 Ok(GitLabProjectConfig {
54 name: super::interp_required(ctx, &config, "name")?,
55 visibility: super::interp_required(ctx, &config, "visibility")?,
56 })
57 }
58}
59
60pub fn validate_config(
61 name: &str,
62 config: &BTreeMap<String, toml::Value>,
63) -> Result<(), IntegrationError> {
64 registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
65 location: format!("integrations.{name}.name"),
66 detail: err.to_string(),
67 })?;
68 registry::config_string(config, "visibility").map_err(|err| {
69 IntegrationError::ConfigInvalid {
70 location: format!("integrations.{name}.visibility"),
71 detail: err.to_string(),
72 }
73 })?;
74 let _ = (name, config);
75 Ok(())
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81 use crate::ProviderOps;
82 use crate::resource::ResourcePayload;
83 use stackless_core::def::StackDef;
84 use stackless_stripe_projects::stripe::StripeProjects;
85 use stackless_stripe_projects::test_support;
86
87 #[test]
88 fn config_matches_catalog() {
89 const FIXTURE: &str = include_str!(concat!(
90 env!("CARGO_MANIFEST_DIR"),
91 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
92 ));
93 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
94 let failures = stackless_stripe_projects::verify_service(
95 &catalog,
96 &GitLabProjectConfig {
97 name: "test-name".into(),
98 visibility: "private".into(),
99 },
100 );
101 assert!(
102 failures.is_empty(),
103 "gitlab/project catalog gaps:\n{}",
104 failures.join("\n")
105 );
106 }
107
108 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_project","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_gitlab","provider_name":"GitLab","service_id":"project","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"free"},"configuration_schema":{"properties":{"name":{"description":"Name of the project","type":"string"},"visibility":{"description":"Visibility level of the project","enum":["private","public"],"type":"string"}},"required":["name","visibility"],"type":"object"}}]}}"##;
109
110 fn test_def() -> StackDef {
111 StackDef::parse(
112 r#"
113[stack]
114name = "atto"
115[stack.projects.stripe]
116project = "project_1"
117[integrations.res]
118provider = "gitlab"
119name = "test-name"
120visibility = "private"
121[services.api]
122source = { repo = "r", ref = "main" }
123env = { OUT = "${integrations.res.project_id}" }
124health = { path = "/health" }
125[services.api.local]
126run = "true"
127"#,
128 )
129 .unwrap()
130 }
131
132 #[tokio::test]
133 async fn provision_records_outputs() {
134 let runner = test_support::provision_script(
135 CATALOG_ENVELOPE,
136 serde_json::json!({"GITLAB_PROJECT_ID": "val_project_id", "GITLAB_WEB_URL": "val_web_url"}),
137 0,
138 );
139 let dir = tempfile::tempdir().unwrap();
140 std::fs::write(
141 dir.path().join("stackless.toml"),
142 "[stack]\nname=\"atto\"\n",
143 )
144 .unwrap();
145 let stripe = StripeProjects::new(&runner, dir.path());
146
147 let resource = GitLabProject
148 .provision(
149 &stripe.as_dyn(),
150 &test_def(),
151 dir.path(),
152 "demo",
153 "res",
154 "local",
155 false,
156 )
157 .await
158 .unwrap();
159 assert_eq!(resource.resource_kind, "integration-gitlab");
160 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
161 assert_eq!(payload.outputs["project_id"], "val_project_id");
162 }
163}