Skip to content

Commit

Permalink
Move commit update into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Zabot committed Sep 3, 2023
1 parent 91618e1 commit f7b6a0b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/gh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use anyhow::{Context, Result};
use git2::Remote;
use git_url_parse::GitUrl;

#[derive(Clone)]
pub struct GHRepo {
pub owner: String,
pub repo: String,
Expand Down
30 changes: 17 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod push;
mod update;
use push::Pusher;

use update::CommitUpdater;

#[tokio::main]
async fn main() -> Result<()> {
// TODO Move these to a config file
Expand Down Expand Up @@ -74,9 +76,11 @@ async fn main() -> Result<()> {
.context("failed to set sorting")?;

// Push every commit
let octocrab = octocrab::OctocrabBuilder::default()
.personal_token(gh_pat.clone())
.build()?;
let octocrab = Arc::new(
octocrab::OctocrabBuilder::default()
.personal_token(gh_pat.clone())
.build()?,
);

let mut remote = repo
.find_remote(default_remote)
Expand All @@ -90,21 +94,21 @@ async fn main() -> Result<()> {
.context("failed to connect to repo")?;
tracing::debug!(connected = conn.connected(), "remote connected");

let pusher = Pusher::new();
let pusher = Arc::new(Pusher::new());

let updater = CommitUpdater::new(
octocrab.clone(),
branch_name,
"master",
&gh_repo,
pusher.clone(),
);

let futures: Result<FuturesUnordered<_>> = walk
.enumerate()
.map(|(i, oid)| {
let oid = oid.context("")?;
Ok(update::update_commit(
i,
branch_name,
repo.borrow(),
&octocrab,
&pusher,
&gh_repo,
oid,
))
Ok(updater.update(i, repo.borrow(), oid))
})
.collect();
let futures = futures.context("failed to generate futures")?;
Expand Down
177 changes: 99 additions & 78 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,113 @@
use std::sync::Arc;

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"
);
pub struct CommitUpdater {
octocrab: Arc<Octocrab>,
branch_name: String,
upstream_branch: String,
gh_repo: GHRepo,
pusher: Arc<Pusher>,
}

impl CommitUpdater {
pub fn new(
octocrab: Arc<Octocrab>,
branch_name: &str,
upstream_branch: &str,
gh_repo: &GHRepo,
pusher: Arc<Pusher>,
) -> Self {
Self {
octocrab,
branch_name: branch_name.to_string(),
upstream_branch: upstream_branch.to_string(),
gh_repo: gh_repo.clone(),
pusher,
}
}

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))?;
pub async fn update(&self, index: usize, repo: &Repository, 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"
);

// 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)?)
let metadata = Metadata::new(&repo, commit.id())?;
let (branch, force) = match &metadata.branch {
Some(branch) => (branch.clone(), true),
None => (format!("fel/{}/{}", self.branch_name, index), false),
};
let branch = self
.pusher
.push(commit.id(), branch, force)
.await
.map_err(|error| anyhow::anyhow!("failed to get parent branch: {}", error))?
};
.map_err(|error| anyhow::anyhow!("failed to push 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()
// The parent branch is either the default branch, or the pushed branch of the
// parent commit
let base = if index == 0 {
self.upstream_branch.clone()
} else {
self.pusher
.wait(commit.parent_id(0)?)
.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")?
}
};
.map_err(|error| anyhow::anyhow!("failed to get parent branch: {}", error))?
};

let pr = match metadata.pr {
None => {
tracing::debug!(
owner = self.gh_repo.owner,
repo = self.gh_repo.repo,
branch,
base,
"creating PR"
);
self.octocrab
.pulls(&self.gh_repo.owner, &self.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 = self.gh_repo.owner,
repo = self.gh_repo.repo,
base,
"amending PR"
);
self.octocrab
.pulls(&self.gh_repo.owner, &self.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")?;
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(())
Ok(())
}
}

0 comments on commit f7b6a0b

Please sign in to comment.