gitlab/api/projects/variables/
variable.rs1use derive_builder::Builder;
8
9use crate::api::common::{self, NameOrId};
10use crate::api::endpoint_prelude::*;
11
12#[derive(Debug, Clone, Builder)]
14#[builder(setter(strip_option))]
15pub struct ProjectVariableFilter<'a> {
16 #[builder(setter(into), default)]
18 environment_scope: Option<Cow<'a, str>>,
19}
20
21impl<'a> ProjectVariableFilter<'a> {
22 pub fn builder() -> ProjectVariableFilterBuilder<'a> {
24 ProjectVariableFilterBuilder::default()
25 }
26
27 pub(crate) fn add_query<'b>(&'b self, params: &mut FormParams<'b>) {
28 params.push_opt("filter[environment_scope]", self.environment_scope.as_ref());
29 }
30}
31
32#[derive(Debug, Builder, Clone)]
34#[builder(setter(strip_option))]
35pub struct ProjectVariable<'a> {
36 #[builder(setter(into))]
38 project: NameOrId<'a>,
39 #[builder(setter(into))]
41 key: Cow<'a, str>,
42 #[builder(default)]
44 filter: Option<ProjectVariableFilter<'a>>,
45}
46
47impl<'a> ProjectVariable<'a> {
48 pub fn builder() -> ProjectVariableBuilder<'a> {
50 ProjectVariableBuilder::default()
51 }
52}
53
54impl Endpoint for ProjectVariable<'_> {
55 fn method(&self) -> Method {
56 Method::GET
57 }
58
59 fn endpoint(&self) -> Cow<'static, str> {
60 format!(
61 "projects/{}/variables/{}",
62 self.project,
63 common::path_escaped(self.key.as_ref()),
64 )
65 .into()
66 }
67
68 fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
69 let mut params = FormParams::default();
70
71 if let Some(filter) = self.filter.as_ref() {
72 filter.add_query(&mut params);
73 }
74
75 params.into_body()
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use http::Method;
82
83 use crate::api::projects::variables::variable::{
84 ProjectVariable, ProjectVariableBuilderError, ProjectVariableFilter,
85 };
86 use crate::api::{self, Query};
87 use crate::test::client::{ExpectedUrl, SingleTestClient};
88
89 #[test]
90 fn all_parameters_are_needed() {
91 let err = ProjectVariable::builder().build().unwrap_err();
92 crate::test::assert_missing_field!(err, ProjectVariableBuilderError, "project");
93 }
94
95 #[test]
96 fn project_is_necessary() {
97 let err = ProjectVariable::builder()
98 .key("testkey")
99 .build()
100 .unwrap_err();
101 crate::test::assert_missing_field!(err, ProjectVariableBuilderError, "project");
102 }
103
104 #[test]
105 fn key_is_necessary() {
106 let err = ProjectVariable::builder().project(1).build().unwrap_err();
107 crate::test::assert_missing_field!(err, ProjectVariableBuilderError, "key");
108 }
109
110 #[test]
111 fn sufficient_parameters() {
112 ProjectVariable::builder()
113 .project(1)
114 .key("testkey")
115 .build()
116 .unwrap();
117 }
118
119 #[test]
120 fn endpoint() {
121 let endpoint = ExpectedUrl::builder()
122 .method(Method::GET)
123 .endpoint("projects/simple%2Fproject/variables/testkey%2F")
124 .content_type("application/x-www-form-urlencoded")
125 .body_str(concat!(""))
126 .build()
127 .unwrap();
128 let client = SingleTestClient::new_raw(endpoint, "");
129
130 let endpoint = ProjectVariable::builder()
131 .project("simple/project")
132 .key("testkey/")
133 .build()
134 .unwrap();
135 api::ignore(endpoint).query(&client).unwrap();
136 }
137
138 #[test]
139 fn endpoint_filter() {
140 let endpoint = ExpectedUrl::builder()
141 .method(Method::GET)
142 .endpoint("projects/simple%2Fproject/variables/testkey%2F")
143 .content_type("application/x-www-form-urlencoded")
144 .body_str("filter%5Benvironment_scope%5D=production")
145 .build()
146 .unwrap();
147 let client = SingleTestClient::new_raw(endpoint, "");
148
149 let endpoint = ProjectVariable::builder()
150 .project("simple/project")
151 .key("testkey/")
152 .filter(
153 ProjectVariableFilter::builder()
154 .environment_scope("production")
155 .build()
156 .unwrap(),
157 )
158 .build()
159 .unwrap();
160 api::ignore(endpoint).query(&client).unwrap();
161 }
162}