Skip to content

Commit

Permalink
Use our own Commit type to simplify testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Aug 28, 2024
1 parent a28e418 commit cbeb401
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
22 changes: 10 additions & 12 deletions src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
// limitations under the License.

use anyhow::{anyhow, Context};
use octocrab::models::commits::Commit;
use tokio::task::JoinSet;

use crate::github::Review;
use crate::github::{Commit, Review};
use crate::remote::Remote;

#[derive(Clone, Debug)]
Expand All @@ -30,10 +29,10 @@ pub struct RepoChangeset {

impl RepoChangeset {
pub async fn analyze_commits(mut self) -> Result<Self, anyhow::Error> {
let compare = self.remote.compare(&self.base_commit, &self.head_commit).await?;
let compare_commits = self.remote.compare(&self.base_commit, &self.head_commit).await?;

let mut join_set = JoinSet::new();
for commit in compare.commits {
for commit in compare_commits {
join_set.spawn(self.clone().analyze_commit(commit));
}

Expand Down Expand Up @@ -145,7 +144,6 @@ pub struct CommitMetadata {
impl CommitMetadata {
pub fn new(commit: &Commit) -> Self {
let headline = commit
.commit
.message
.split('\n')
.next()
Expand Down Expand Up @@ -193,26 +191,26 @@ mod tests {
user: "user2".to_owned(),
},
Review {
approved: false,
commit_id: "00000000000000000000000000000003".to_owned(),
submitted_at: 3,
user: "user3".to_owned(),
},
approved: false,
commit_id: "00000000000000000000000000000003".to_owned(),
submitted_at: 3,
user: "user3".to_owned(),
},
],
)
}

#[test]
fn collect_approved_reviews() {
let (mut changeset, pr_reviews) = gen_change_review();
changeset.collect_approved_reviews(&pr_reviews, &"00000000000000000000000000000002".to_string());
changeset.collect_approved_reviews(&pr_reviews, &"00000000000000000000000000000002".to_owned());
assert_eq!(changeset.approvals, vec!["user2"]);
}

#[test]
fn collect_approved_reviews_extra_commit() {
let (mut changeset, pr_reviews) = gen_change_review();
changeset.collect_approved_reviews(&pr_reviews, &"00000000000000000000000000000003".to_string());
changeset.collect_approved_reviews(&pr_reviews, &"00000000000000000000000000000003".to_owned());
assert_eq!(changeset.approvals, Vec::<String>::new());
}
}
7 changes: 7 additions & 0 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[derive(Clone, Debug)]
pub struct Commit {
pub html_url: String,
pub message: String,
pub sha: String,
}

#[derive(Clone, Debug)]
pub struct Review {
pub approved: bool,
Expand Down
19 changes: 15 additions & 4 deletions src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ use std::sync::Arc;

use anyhow::{anyhow, bail, Context};
use octocrab::commits::PullRequestTarget;
use octocrab::models::commits::CommitComparison;
use octocrab::models::pulls::{PullRequest, ReviewState};
use octocrab::models::repos::RepoCommit;
use octocrab::Octocrab;
use tokio::sync::SemaphorePermit;
use url::Url;

use crate::api_clients::Client;
use crate::github::Commit;
use crate::github::Review;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -80,10 +80,10 @@ impl Remote {
Ok(associated_prs_page.take_items())
}

pub async fn compare(&self, base_commit: &str, head_commit: &str) -> Result<CommitComparison, anyhow::Error> {
pub async fn compare(&self, base_commit: &str, head_commit: &str) -> Result<Vec<Commit>, anyhow::Error> {
let (_permit, octocrab) = self.get_client().await?;

octocrab
let compare = octocrab
.commits(&self.owner, &self.repository)
.compare(base_commit, head_commit)
.send()
Expand All @@ -93,7 +93,18 @@ impl Remote {
self.original.trim_end_matches(".git"),
&base_commit,
&head_commit
))
))?;

let mut commits: Vec<Commit> = vec![];
for commit in compare.commits {
commits.push(Commit {
html_url: commit.html_url,
message: commit.commit.message,
sha: commit.sha,
});
}

Ok(commits)
}

pub async fn pr_head_hash(&self, pr_number: u64) -> Result<String, anyhow::Error> {
Expand Down

0 comments on commit cbeb401

Please sign in to comment.