Skip to content

Commit

Permalink
Move commit update into module
Browse files Browse the repository at this point in the history
  • Loading branch information
Zabot committed Sep 3, 2023
1 parent 34c502b commit 91618e1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 83 deletions.
97 changes: 14 additions & 83 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use git2::BranchType;
use git2::Config;
use git2::Repository;
use git2::Sort;
use std::borrow::Borrow;
use std::sync::Arc;

mod auth;
mod gh;
mod metadata;
mod push;
mod update;
use push::Pusher;

use crate::metadata::Metadata;

#[tokio::main]
async fn main() -> Result<()> {
// TODO Move these to a config file
Expand Down Expand Up @@ -92,91 +92,22 @@ async fn main() -> Result<()> {

let pusher = Pusher::new();

let futures: FuturesUnordered<_> = walk
let futures: Result<FuturesUnordered<_>> = walk
.enumerate()
.map(|(i, oid)| {
let repo = &repo;
let octocrab = &octocrab;
let pusher = &pusher;
let gh_repo = &gh_repo;
async move {
let commit = repo.find_commit(oid?)?;
anyhow::ensure!(
commit.parent_count() == 1,
"fel stacks cannot contain merge commits"
);

let metadata = Metadata::new(&repo, commit.id())?;
let (branch, force) = match &metadata.branch {
Some(branch) => (branch.clone(), true),
None => (format!("fel/{}/{}", branch_name, i), false),
};
let branch = pusher
.push(commit.id(), branch, force)
.await
.map_err(|error| anyhow::anyhow!("failed to push branch: {}", error))?;

// The parent branch is either the default branch, or the pushed branch of the
// parent commit
let base = if i == 0 {
String::from("master")
} else {
pusher.wait(commit.parent_id(0)?).await.map_err(|error| {
anyhow::anyhow!("failed to get parent branch: {}", error)
})?
};

let pr = match metadata.pr {
None => {
tracing::debug!(
owner = gh_repo.owner,
repo = gh_repo.repo,
branch,
base,
"creating PR"
);
octocrab
.pulls(&gh_repo.owner, &gh_repo.repo)
.create(
commit.summary().context("commit header not valid UTF-8")?,
&branch,
&base,
)
.body(commit.body().unwrap_or(""))
.send()
.await
.context("failed to create pr")?
}
Some(pr) => {
tracing::debug!(
pr,
owner = gh_repo.owner,
repo = gh_repo.repo,
base,
"amending PR"
);
octocrab
.pulls(&gh_repo.owner, &gh_repo.repo)
.update(pr)
.base(base)
.send()
.await
.context("failed to update pr")?
}
};

let metadata = Metadata {
pr: Some(pr.number),
branch: Some(branch),
};
tracing::debug!(?metadata, ?commit, "updating commit metadata");
metadata
.write(repo, &commit)
.context("failed to write commit metadata")?;
Ok::<_, anyhow::Error>(())
}
let oid = oid.context("")?;
Ok(update::update_commit(
i,
branch_name,
repo.borrow(),
&octocrab,
&pusher,
&gh_repo,
oid,
))
})
.collect();
let futures = futures.context("failed to generate futures")?;
let branches = futures.len();
let mut futures = futures.collect::<Vec<_>>();

Expand Down
92 changes: 92 additions & 0 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use anyhow::{Context, Result};
use git2::{Oid, Repository};
use octocrab::Octocrab;

use crate::{gh::GHRepo, metadata::Metadata, push::Pusher};

pub async fn update_commit(
index: usize,
branch_name: &str,
repo: &Repository,
octocrab: &Octocrab,
pusher: &Pusher,
gh_repo: &GHRepo,
oid: Oid,
) -> Result<()> {
let commit = repo.find_commit(oid).context("failed to get commit")?;
anyhow::ensure!(
commit.parent_count() == 1,
"fel stacks cannot contain merge commits"
);

let metadata = Metadata::new(&repo, commit.id())?;
let (branch, force) = match &metadata.branch {
Some(branch) => (branch.clone(), true),
None => (format!("fel/{}/{}", branch_name, index), false),
};
let branch = pusher
.push(commit.id(), branch, force)
.await
.map_err(|error| anyhow::anyhow!("failed to push branch: {}", error))?;

// The parent branch is either the default branch, or the pushed branch of the
// parent commit
let base = if index == 0 {
String::from("master")
} else {
pusher
.wait(commit.parent_id(0)?)
.await
.map_err(|error| anyhow::anyhow!("failed to get parent branch: {}", error))?
};

let pr = match metadata.pr {
None => {
tracing::debug!(
owner = gh_repo.owner,
repo = gh_repo.repo,
branch,
base,
"creating PR"
);
octocrab
.pulls(&gh_repo.owner, &gh_repo.repo)
.create(
commit.summary().context("commit header not valid UTF-8")?,
&branch,
&base,
)
.body(commit.body().unwrap_or(""))
.send()
.await
.context("failed to create pr")?
}
Some(pr) => {
tracing::debug!(
pr,
owner = gh_repo.owner,
repo = gh_repo.repo,
base,
"amending PR"
);
octocrab
.pulls(&gh_repo.owner, &gh_repo.repo)
.update(pr)
.base(base)
.send()
.await
.context("failed to update pr")?
}
};

let metadata = Metadata {
pr: Some(pr.number),
branch: Some(branch),
};
tracing::debug!(?metadata, ?commit, "updating commit metadata");
metadata
.write(repo, &commit)
.context("failed to write commit metadata")?;

Ok(())
}

0 comments on commit 91618e1

Please sign in to comment.