Skip to main content

ephemeris_forge_github/plan/
labels.rs

1use ephemeris_forge::{Collection, FieldDiff, Label, Labels, Report};
2
3use crate::api::model;
4use crate::operation::labels::Operation;
5
6pub(super) fn plan(
7    config: Option<&Labels<Label>>,
8    live: &[model::Label],
9    report: &mut Report,
10) -> Vec<Operation> {
11    let Some(config) = config else {
12        return Vec::new();
13    };
14    Collection {
15        noun: "label",
16        mode: config.mode,
17    }
18    .plan(
19        &config.labels,
20        live,
21        report,
22        |label, name| same_name(&label.name, name),
23        |name, desired, actual, _| {
24            let description = desired.description.clone().unwrap_or_default();
25            let mut diff = FieldDiff::default();
26            diff.check(
27                "color",
28                actual.is_some_and(|label| desired.color.matches(&label.color)),
29                &actual.map(|label| &label.color),
30                &desired.color,
31            );
32            let live_description =
33                actual.map(|label| label.description.clone().unwrap_or_default());
34            diff.want("description", &description, live_description.as_ref());
35            let color = desired.color.clone();
36            let operations = match actual {
37                _ if !diff.has_changes() => None,
38                Some(label) => Some(Operation::Update {
39                    name: label.name.clone(),
40                    color,
41                    description,
42                }),
43                None => Some(Operation::Create {
44                    name: name.to_owned(),
45                    color,
46                    description,
47                }),
48            };
49            (diff, operations.into_iter().collect())
50        },
51        |label, _| {
52            Some((
53                label.name.clone(),
54                Operation::Delete {
55                    name: label.name.clone(),
56                },
57            ))
58        },
59    )
60}
61
62fn same_name(left: &str, right: &str) -> bool {
63    left.to_lowercase() == right.to_lowercase()
64}