Skip to content

Commit

Permalink
Move collect_approved_reviews to a member function of Changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Aug 15, 2024
1 parent 5e9d9f7 commit 2acf77b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
30 changes: 24 additions & 6 deletions src/changes.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
use octocrab::models::pulls::Review;
use octocrab::models::pulls::ReviewState::Approved;

#[derive(Clone, Debug)]
pub struct RepoChangeset {
pub name: String,
pub remote: String,
pub name: String,
pub remote: String,
pub base_commit: String,
pub head_commit: String,
pub changes: Vec<Changeset>,
pub changes: Vec<Changeset>,
}

#[derive(Clone, Debug)]
pub struct Changeset {
pub commits: Vec<CommitMetadata>,
pub pr_link: Option<String>,
pub commits: Vec<CommitMetadata>,
pub pr_link: Option<String>,
pub approvals: Vec<String>,
}

impl Changeset {
pub fn collect_approved_reviews(&mut self, pr_reviews: &[Review]) {
for pr_review in pr_reviews {
// TODO: do we need to check if this is the last review of the user?
if pr_review.state == Some(Approved) {
let Some(ref user) = pr_review.user else {
continue;
};

self.approvals.push(user.login.clone());
}
}
}
}

#[derive(Clone, Debug)]
pub struct CommitMetadata {
pub headline: String,
pub link: String,
pub link: String,
}
8 changes: 4 additions & 4 deletions src/helm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ pub struct ImageRefs {

#[derive(Debug, Serialize, Deserialize)]
pub struct ImageRef {
pub account: String,
pub account: String,
pub repository: String,
pub tag: String,
pub sources: Vec<SourceRepoRef>,
pub tag: String,
pub sources: Vec<SourceRepoRef>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SourceRepoRef {
pub repo: String,
pub repo: String,
pub commit: String,
}
24 changes: 2 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use git2::Repository;
use helm_config::ImageRefs;
use lazy_static::lazy_static;
use octocrab::commits::PullRequestTarget;
use octocrab::models::pulls;
use octocrab::models::pulls::ReviewState;
use octocrab::Octocrab;
use url::Url;

Expand Down Expand Up @@ -263,7 +261,7 @@ async fn find_reviews(octocrab: &Arc<Octocrab>, repo: &mut RepoChangeset) -> Res

if let Some(changeset) = repo.changes.iter_mut().find(|cs| cs.pr_link == associated_pr_link) {
changeset.commits.push(change_commit.clone());
collect_approved_reviews(&pr_reviews, changeset)?;
changeset.collect_approved_reviews(&pr_reviews);
continue;
}

Expand All @@ -273,7 +271,7 @@ async fn find_reviews(octocrab: &Arc<Octocrab>, repo: &mut RepoChangeset) -> Res
approvals: Vec::new(),
};

collect_approved_reviews(&pr_reviews, &mut changeset)?;
changeset.collect_approved_reviews(&pr_reviews);
repo.changes.push(changeset);
}
}
Expand Down Expand Up @@ -316,24 +314,6 @@ fn print_changes(changes: &[RepoChangeset]) {
}
}

fn collect_approved_reviews(pr_reviews: &[pulls::Review], review: &mut Changeset) -> Result<(), anyhow::Error> {
for pr_review in pr_reviews {
// TODO: do we need to check if this is the last review of the user?
if pr_review.state.ok_or(anyhow!("review has no state"))? == ReviewState::Approved {
review.approvals.push(
pr_review
.user
.as_ref()
.ok_or(anyhow!("review without an user!?"))?
.login
.clone(),
);
}
}

Ok(())
}

// for commits take the first 6 chars
// https://github.com/sapcc/tenso/commit/39241382cc5de6ab54eb34f6ac09dfb740cbee701
// -> [tenso 392413](https://github.com/sapcc/tenso/commit/39241382cc5de6ab54eb34f6ac09dfb740cbee701)
Expand Down

0 comments on commit 2acf77b

Please sign in to comment.