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