Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly parse PRs which introduce new images.yaml files, don't multi… #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,26 @@ pub struct RepoChangeset<C: Client> {
pub changes: Vec<Changeset>,
}

impl<C: Client> RepoChangeset<C> {
pub fn new(name: String, remote: crate::Remote<C>, base_commit: String, head_commit: String) -> RepoChangeset<C> {
Self {
name,
remote,
base_commit,
head_commit,
changes: Vec::new(),
}
}
}

impl<C: Client + Sync + Send + 'static> RepoChangeset<C> {
pub async fn analyze_commits(mut self) -> anyhow::Result<Self> {
// if this is a newly introduced source, compare the commit to itself
if self.head_commit.is_empty() {
self.head_commit.clone_from(&self.base_commit);
self.base_commit += "^1";
}

let compare_commits = self.remote.compare(&self.base_commit, &self.head_commit).await?;

let mut join_set = JoinSet::new();
Expand Down
57 changes: 39 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod helm_config;
mod remote;
mod repo;

use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::sync::LazyLock;
Expand All @@ -32,7 +33,7 @@ use changes::RepoChangeset;
use clap::builder::styling::Style;
use clap::builder::NonEmptyStringValueParser;
use clap::{Parser, Subcommand};
use git2::Repository;
use git2::{Oid, Repository};
use helm_config::ImageRefs;
use remote::Remote;
use tokio::task::JoinSet;
Expand Down Expand Up @@ -157,32 +158,52 @@ fn find_values_yaml(

for diff_delta in diff_tree.deltas() {
let new_file = diff_delta.new_file();
if !new_file.exists() {
continue;
}

let path = new_file.path().ok_or_else(|| anyhow!("failed to get file path"))?;
if !path.ends_with("images.yaml") {
continue;
}

let new_image_refs = ImageRefs::parse(&repo, &new_file).context("while parsing new file")?;

let old_file = diff_delta.old_file();
if !old_file.exists() {
continue;
let mut old_image_refs = ImageRefs {
container_images: HashMap::new(),
};
// only zeros means the file was newly created and there is no old file to parse
if old_file.id() != Oid::from_str("0000000000000000000000000000000000000000")? {
old_image_refs = ImageRefs::parse(&repo, &old_file).context("while parsing old file")?;
}

let new_image_refs = ImageRefs::parse(&repo, &new_file).context("while parsing new file")?;
let old_image_refs = ImageRefs::parse(&repo, &old_file).context("while parsing old file")?;
for (name, image) in &new_image_refs.container_images {
for source in &image.sources {
for container_image_source in &old_image_refs.container_images[name].sources {
changes.push(RepoChangeset {
name: name.clone(),
remote: remote::Remote::parse(&source.repo)?,
base_commit: source.commit.clone(),
head_commit: container_image_source.commit.clone(),
changes: Vec::new(),
});
for new_source in &image.sources {
// Is this a new container image?
if !old_image_refs.container_images.contains_key(name) {
changes.push(RepoChangeset::new(
name.clone(),
remote::Remote::parse(&new_source.repo)?,
new_source.commit.clone(),
String::new(),
));
SuperSandro2000 marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

for old_source in &old_image_refs.container_images[name].sources {
// Did we previously have this source?
if new_source.repo == old_source.repo {
changes.push(RepoChangeset::new(
name.clone(),
remote::Remote::parse(&new_source.repo)?,
new_source.commit.clone(),
old_source.commit.clone(),
SuperSandro2000 marked this conversation as resolved.
Show resolved Hide resolved
));
} else {
changes.push(RepoChangeset::new(
name.clone(),
remote::Remote::parse(&new_source.repo)?,
new_source.commit.clone(),
String::new(),
));
}
}
}
}
Expand Down
Loading