Skip to content

Commit

Permalink
Extract yaml file parsing into own function
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Aug 15, 2024
1 parent ae0668a commit 703d0d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 0 additions & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ match_block_trailing_comma = true
max_width = 120
overflow_delimited_expr = true
reorder_impl_items = true
struct_field_align_threshold = 20
13 changes: 13 additions & 0 deletions src/helm_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use anyhow::Context;
use git2::{DiffFile, Repository};
use serde::{Deserialize, Serialize};

#[allow(clippy::module_name_repetitions)]
Expand All @@ -9,6 +11,17 @@ pub struct ImageRefs {
pub container_images: HashMap<String, ImageRef>,
}

impl ImageRefs {
pub fn parse(repo: &Repository, diff_file: DiffFile) -> Result<Self, anyhow::Error> {
let blob_id = diff_file.id();
let blob = repo
.find_blob(blob_id)
.with_context(|| format!("cannot find Git blob {blob_id}"))?;
Ok(serde_yml::from_slice(blob.content())
.with_context(|| format!("cannot parse yaml file {:?}", diff_file.path()))?)
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ImageRef {
pub account: String,
Expand Down
12 changes: 4 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,24 @@ fn find_values_yaml(workspace: String, base: &str, head: &str) -> Result<Vec<Rep
continue;
}

let new_images_content = repo.find_blob(new_file.id())?;
let new_image_config: ImageRefs = serde_yml::from_slice(new_images_content.content())?;
let new_image_refs = ImageRefs::parse(&repo, new_file)?;

let old_file = diff_delta.old_file();
if old_file.exists() {
let old_images_content = repo.find_blob(old_file.id())?;
let old_image_config: ImageRefs = serde_yml::from_slice(old_images_content.content())?;
let old_image_refs = ImageRefs::parse(&repo, old_file)?;

for (name, image) in &new_image_config.container_images {
for (name, image) in &new_image_refs.container_images {
for source in &image.sources {
changes.push(RepoChangeset {
name: name.clone(),
remote: util::Remote::parse(&source.repo)?,
// TODO: iterate over sources
base_commit: old_image_config.container_images[name].sources[0].commit.clone(),
base_commit: old_image_refs.container_images[name].sources[0].commit.clone(),
head_commit: source.commit.clone(),
changes: Vec::new(),
});
}
}

continue;
}
}

Expand Down

0 comments on commit 703d0d0

Please sign in to comment.