forked from lowpolyneko/build-pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.rs
More file actions
191 lines (168 loc) · 5.46 KB
/
api.rs
File metadata and controls
191 lines (168 loc) · 5.46 KB
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
//! Structs and methods to interface with Jenkins via the [jenkins_api] crate.
use anyhow::{Error, Result};
use jenkins_api::{
Jenkins,
build::{Build, BuildStatus, ShortBuild},
client::{Path, TreeBuilder},
job::Job,
};
use serde::Deserialize;
use crate::db::{JobBuild, Run};
/// Represents all jobs pulled from [SparseMatrixProject::pull_jobs]
#[derive(Deserialize)]
pub struct SparseMatrixProject {
/// [Vec] of [SparseJob]s
pub jobs: Vec<SparseJob>,
}
/// Represents a job pulled from [SparseMatrixProject::pull_jobs]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SparseJob {
/// Name of the job
pub name: String,
/// URL of the job
pub url: String,
/// Last build of job as a [SparseBuild]
pub builds: Vec<SparseBuild>,
}
/// Represents a job build pulled from [SparseMatrixProject::pull_jobs]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SparseBuild {
/// Build number
pub number: u32,
/// Build URL
pub url: String,
/// Build timestamp
pub timestamp: u64,
/// Build result as a [BuildStatus]
pub result: Option<BuildStatus>,
/// Build runs as a [Vec] of [ShortBuild]s
pub runs: Option<Vec<ShortBuild>>,
}
/// Builds that can be represented as [Run]
pub trait AsRun {
/// Convert `&self` to [Run]
async fn as_run(&self, build_id: i64, jenkins_client: &Jenkins) -> Run;
}
/// Builds that can be represented as [JobBuild]
pub trait AsBuild {
/// Convert `&self` to [JobBuild]
fn as_build(&self, job_id: i64) -> JobBuild;
}
/// Jobs that can be represented as [Job]
pub trait AsJob {
/// Convert `&self` to [Job]
fn as_job(&self, last_n: usize) -> crate::db::Job;
}
/// [Build]s with common fields
pub trait HasBuildFields {
/// Get [BuildStatus]
fn build_status(&self) -> Option<BuildStatus>;
/// Get `display_name`
fn full_display_name_or_default(&self) -> &str;
}
/// Works for most [jenkins_api::build] structs
macro_rules! impl_HasBuildFields {
(for $($t:ty),+) => {
$(impl HasBuildFields for $t {
fn build_status(&self) -> Option<BuildStatus> {
self.result
}
fn full_display_name_or_default(&self) -> &str {
self.full_display_name
.as_ref()
.unwrap_or(&self.display_name)
}
})*
}
}
impl_HasBuildFields!(for jenkins_api::build::CommonBuild);
impl Job for SparseJob {
fn name(&self) -> &str {
&self.name
}
fn url(&self) -> &str {
&self.url
}
}
impl AsJob for SparseJob {
fn as_job(&self, last_n: usize) -> crate::db::Job {
crate::db::Job {
name: self.name.as_str().into(),
last_build: self.builds.iter().take(last_n).last().map(|b| b.number),
url: self.url.as_str().into(),
}
}
}
impl AsBuild for SparseBuild {
fn as_build(&self, job_id: i64) -> JobBuild {
JobBuild {
url: self.url.clone(),
number: self.number,
status: self.result,
timestamp: self.timestamp,
job_id,
}
}
}
impl<T> AsRun for T
where
T: Build + HasBuildFields,
{
async fn as_run(&self, build_id: i64, jenkins_client: &Jenkins) -> Run {
let display_name = self.full_display_name_or_default();
let status = self.build_status();
Run {
url: self.url().into(),
status,
display_name: display_name.into(),
log: match status {
Some(BuildStatus::Failure | BuildStatus::Unstable | BuildStatus::Aborted) => {
// only get log on failure
match self.get_console(jenkins_client).await {
Ok(l) => Some(l.into()),
Err(e) => {
log::error!("Failed to retrieve build log for run {display_name}: {e}");
None
}
}
}
_ => None,
},
tag_schema: None,
build_id,
}
}
}
impl SparseMatrixProject {
/// Query the Jenkins build server for all jobs and their last build from a `project_name`
pub async fn pull_jobs(client: &Jenkins, project_name: &str) -> Result<Self> {
client
.get_object_as(
Path::View { name: project_name },
TreeBuilder::new()
.with_field(
TreeBuilder::object("jobs")
.with_subfield("name")
.with_subfield("url")
.with_subfield(
TreeBuilder::object("builds")
.with_subfield("number")
.with_subfield("url")
.with_subfield("displayName")
.with_subfield("timestamp")
.with_subfield("result")
.with_subfield(
TreeBuilder::object("runs")
.with_subfield("url")
.with_subfield("number"),
),
),
)
.build(),
)
.await
.map_err(Error::from_boxed)
}
}