Skip to content

Commit 112e4e2

Browse files
vaceletmLeSuisse
authored andcommitted
No need of ia64 + better parsing of ListValues
1 parent bb748c1 commit 112e4e2

1 file changed

Lines changed: 56 additions & 38 deletions

File tree

  • artifact-post-action/compute-risk-and-residual/src

artifact-post-action/compute-risk-and-residual/src/main.rs

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,26 @@ use serde::{Serialize, Deserialize};
2525
use serde_json::json;
2626
use std::error::Error;
2727
use std::io::stdin;
28-
use std::str::FromStr;
2928

3029
#[derive(Serialize, Debug)]
3130
struct FieldValueBinding {
32-
field_id: i64,
33-
bind_value_ids: Vec<i64>,
31+
field_id: u32,
32+
bind_value_ids: Vec<u32>,
3433
}
3534

3635
#[derive(Debug, Serialize, Deserialize)]
37-
struct ListValue {
38-
id: Option<i64>,
39-
label: Option<String>,
40-
color: Option<String>,
41-
tlp_color: Option<String>,
36+
#[serde(untagged)]
37+
enum ListValue {
38+
StaticValue {
39+
id: u32,
40+
label: String,
41+
color: Option<String>,
42+
tlp_color: Option<String>,
43+
},
44+
UserValue {
45+
id: Option<u32>,
46+
real_name: Option<String>,
47+
},
4248
}
4349

4450
#[derive(Debug, Serialize, Deserialize)]
@@ -77,38 +83,41 @@ enum FieldValue {
7783

7884
#[derive(Serialize, Deserialize)]
7985
struct Changeset {
80-
id: i64,
86+
id: u32,
8187
values: Vec<FieldValue>,
8288
}
8389

8490
#[derive(Serialize, Deserialize)]
8591
struct TrackerFieldValue {
86-
id: i64,
92+
id: u32,
8793
label: String,
8894
}
8995

9096
#[derive(Serialize, Deserialize)]
9197
struct TrackerField {
92-
field_id: i64,
98+
field_id: u32,
9399
label: String,
94100
values: Option<Vec<TrackerFieldValue>>,
95101
}
96102

97103
#[derive(Serialize, Deserialize)]
98104
struct Tracker {
99-
id: i64,
105+
id: u32,
100106
fields: Vec<TrackerField>
101107
}
102108

103109
#[derive(Serialize, Deserialize)]
104110
struct Artifact {
105-
id: i64,
111+
id: u32,
106112
current: Changeset,
107113
tracker: Tracker,
108114
}
109115

110-
fn convert_label_to_integer(label: &str) -> Result<i64, Box<dyn Error>> {
111-
i64::from_str(label).map_err(Into::into)
116+
fn convert_label_to_integer(label: String) -> Result<u32, Box<dyn Error>> {
117+
match label.parse::<u32>() {
118+
Ok(n) => Ok(n),
119+
Err(_) => Err("Label is invalid integer".into()),
120+
}
112121
}
113122

114123
fn find_select_box_by_label<'a>(changeset: &'a Changeset, target_label: &str) -> Option<&'a FieldValue> {
@@ -118,31 +127,24 @@ fn find_select_box_by_label<'a>(changeset: &'a Changeset, target_label: &str) ->
118127
})
119128
}
120129

121-
fn find_select_box_value_by_label(artifact: &Artifact, target_label: &str) -> Result<Option<i64>, Box<dyn Error>> {
122-
find_select_box_by_label(&artifact.current, target_label)
123-
.and_then(|field_value| match field_value {
124-
FieldValue::SelectBox { values, .. } => values.first().and_then(|first_value| first_value.label.as_deref()),
130+
fn find_value_matching_label(field_value: &FieldValue) -> Option<String> {
131+
match field_value {
132+
FieldValue::SelectBox { values, .. } => values.first().and_then(|first_value| match first_value {
133+
ListValue::StaticValue { label, .. } => Some(label.to_string()),
134+
_ => None,
135+
}),
125136
_ => None,
126-
})
127-
.map(|label| convert_label_to_integer(label))
128-
.transpose()
129-
}
130-
131-
fn process_risk_values(artifact: &Artifact, severity_field_label: &str, probability_field_label: &str, risk_field_label: &str) -> Result<Option<FieldValueBinding>, Box<dyn Error>> {
132-
133-
let severity = find_select_box_value_by_label(&artifact, severity_field_label)?;
134-
if severity.is_none() {
135-
return Ok(None);
136137
}
137-
let severity_value = severity.unwrap();
138+
}
138139

139-
let probability = find_select_box_value_by_label(&artifact, probability_field_label)?;
140-
if probability.is_none() {
141-
return Ok(None);
142-
}
143-
let probability_value = probability.unwrap();
144-
140+
fn find_select_box_value_by_label(artifact: &Artifact, target_label: &str) -> Result<Option<u32>, Box<dyn Error>> {
141+
find_select_box_by_label(&artifact.current, target_label)
142+
.and_then(|field_value| find_value_matching_label(field_value) )
143+
.and_then(|label| Some(convert_label_to_integer(label)))
144+
.transpose()
145+
}
145146

147+
fn find_risk_value(artifact: &Artifact, risk_field_label: &str, product: u32) -> Result<Option<FieldValueBinding>, Box<dyn Error>> {
146148
let risk_field_option = artifact.tracker.fields.iter().find(|field| field.label == risk_field_label);
147149

148150
if risk_field_option.is_none() {
@@ -155,8 +157,6 @@ fn process_risk_values(artifact: &Artifact, severity_field_label: &str, probabil
155157
}
156158
let risk_values = risk_field.values.as_ref().unwrap();
157159

158-
let product = severity_value * probability_value;
159-
160160
let matching_value = risk_values.iter()
161161
.find(|value| {
162162
value.label.as_str() == product.to_string()
@@ -172,6 +172,24 @@ fn process_risk_values(artifact: &Artifact, severity_field_label: &str, probabil
172172
}
173173
}
174174

175+
fn process_risk_values(artifact: &Artifact, severity_field_label: &str, probability_field_label: &str, risk_field_label: &str) -> Result<Option<FieldValueBinding>, Box<dyn Error>> {
176+
let severity = find_select_box_value_by_label(&artifact, severity_field_label)?;
177+
if severity.is_none() {
178+
return Ok(None);
179+
}
180+
let severity_value = severity.unwrap();
181+
182+
let probability = find_select_box_value_by_label(&artifact, probability_field_label)?;
183+
if probability.is_none() {
184+
return Ok(None);
185+
}
186+
let probability_value = probability.unwrap();
187+
188+
let product = severity_value * probability_value;
189+
190+
find_risk_value(artifact, risk_field_label, product)
191+
}
192+
175193
fn main() -> Result<(), Box<dyn Error>> {
176194
let artifact: Artifact = serde_json::from_reader(stdin()).map_err(|e| {
177195
eprintln!("Serde unserialize error: {e}");

0 commit comments

Comments
 (0)