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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use serde::{self, Deserialize, Serialize};
use crate::helpers::Class;
use crate::client::{self, Result};
use crate::client_internals::{Name, Path};
use crate::job::{JobName, ShortJob};
use crate::property::CommonProperty;
use crate::Jenkins;
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ShortView {
pub name: String,
pub url: String,
#[cfg(not(feature = "extra-fields-visibility"))]
#[serde(flatten)]
pub(crate) extra_fields: Option<serde_json::Value>,
#[cfg(feature = "extra-fields-visibility")]
#[serde(flatten)]
pub extra_fields: Option<serde_json::Value>,
}
impl ShortView {
pub fn get_full_view(&self, jenkins_client: &Jenkins) -> Result<CommonView> {
let path = jenkins_client.url_to_path(&self.url);
if let Path::View { .. } = path {
Ok(jenkins_client.get(&path)?.json()?)
} else {
Err(client::Error::InvalidUrl {
url: self.url.clone(),
expected: client::error::ExpectedType::View,
}
.into())
}
}
}
#[derive(Debug)]
pub struct ViewName<'a>(pub &'a str);
impl<'a> From<&'a str> for ViewName<'a> {
fn from(v: &'a str) -> ViewName<'a> {
ViewName(v)
}
}
impl<'a> From<&'a String> for ViewName<'a> {
fn from(v: &'a String) -> ViewName<'a> {
ViewName(v)
}
}
impl<'a> From<&'a ShortView> for ViewName<'a> {
fn from(v: &'a ShortView) -> ViewName<'a> {
ViewName(&v.name)
}
}
impl<'a, T: View> From<&'a T> for ViewName<'a> {
fn from(v: &'a T) -> ViewName<'a> {
ViewName(v.name())
}
}
pub trait View {
fn name(&self) -> &str;
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CommonView {
#[serde(rename = "_class")]
pub class: Option<String>,
pub description: Option<String>,
pub name: String,
pub url: String,
pub jobs: Vec<ShortJob>,
pub property: Vec<CommonProperty>,
#[cfg(not(feature = "extra-fields-visibility"))]
#[serde(flatten)]
extra_fields: serde_json::Value,
#[cfg(feature = "extra-fields-visibility")]
#[serde(flatten)]
pub extra_fields: serde_json::Value,
}
specialize!(CommonView => View);
impl View for CommonView {
fn name(&self) -> &str {
&self.name
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ListView {
pub description: Option<String>,
pub name: String,
pub url: String,
pub jobs: Vec<ShortJob>,
pub property: Vec<CommonProperty>,
}
register_class!("hudson.model.ListView" => ListView);
impl View for ListView {
fn name(&self) -> &str {
&self.name
}
}
impl ListView {
pub fn add_job<'a, J>(&self, jenkins_client: &Jenkins, job_name: J) -> Result<()>
where
J: Into<JobName<'a>>,
{
let path = jenkins_client.url_to_path(&self.url);
if let Path::View { name } = path {
let _ = jenkins_client.post(&Path::AddJobToView {
job_name: Name::Name(&job_name.into().0),
view_name: name,
})?;
Ok(())
} else {
Err(client::Error::InvalidUrl {
url: self.url.clone(),
expected: client::error::ExpectedType::View,
}
.into())
}
}
pub fn remove_job<'a, J>(&self, jenkins_client: &Jenkins, job_name: J) -> Result<()>
where
J: Into<JobName<'a>>,
{
let path = jenkins_client.url_to_path(&self.url);
if let Path::View { name } = path {
let _ = jenkins_client.post(&Path::RemoveJobFromView {
job_name: Name::Name(&job_name.into().0),
view_name: name,
})?;
Ok(())
} else {
Err(client::Error::InvalidUrl {
url: self.url.clone(),
expected: client::error::ExpectedType::View,
}
.into())
}
}
}
impl Jenkins {
pub fn get_view<'a, V>(&self, view_name: V) -> Result<CommonView>
where
V: Into<ViewName<'a>>,
{
Ok(self
.get(&Path::View {
name: Name::Name(&view_name.into().0),
})?
.json()?)
}
pub fn add_job_to_view<'a, 'b, V, J>(&self, view_name: V, job_name: J) -> Result<()>
where
V: Into<ViewName<'a>>,
J: Into<JobName<'a>>,
{
let _ = self.post(&Path::AddJobToView {
job_name: Name::Name(&job_name.into().0),
view_name: Name::Name(&view_name.into().0),
})?;
Ok(())
}
pub fn remove_job_from_view<'a, 'b, V, J>(&self, view_name: V, job_name: J) -> Result<()>
where
V: Into<ViewName<'a>>,
J: Into<JobName<'a>>,
{
let _ = self.post(&Path::AddJobToView {
job_name: Name::Name(&job_name.into().0),
view_name: Name::Name(&view_name.into().0),
})?;
Ok(())
}
}