feat: support agit prs in pr detection

This commit is contained in:
Cyborus 2024-11-03 23:08:41 -05:00
parent 07436b556b
commit 6b3a98d470

View file

@ -1601,17 +1601,27 @@ async fn guess_pr(
let head = local_repo.head()?;
eyre::ensure!(head.is_branch(), "head is not on branch");
let local_branch = git2::Branch::wrap(head);
let remote_branch = local_branch.upstream()?;
let remote_head_name = remote_branch
.get()
.name()
.ok_or_eyre("remote branch does not have valid name")?;
let local_branch_name = local_branch.name()?.ok_or_eyre("branch name is not utf8")?;
let config = local_repo.config()?;
let remote_head_name = config.get_string(&format!("branch.{local_branch_name}.merge"))?;
let maybe_agit = remote_head_name.strip_prefix("refs/for/").and_then(|s| s.split_once("/"));
match maybe_agit {
Some((base, head)) => {
let username = api.user_get_current().await?.login.ok_or_eyre("user does not have username")?.to_lowercase();
let head = format!("{username}/{head}");
return Ok(api.repo_get_pull_request_by_base_head(repo.owner(), repo.name(), base, &head).await?);
}
None => {
let remote_head_short = remote_head_name
.rsplit_once("/")
.map(|(_, b)| b)
.unwrap_or(remote_head_name);
.unwrap_or(&remote_head_name);
let this_repo = api.repo_get(repo.owner(), repo.name()).await?;
// check for PRs on the main branch first
let base = this_repo
.default_branch
@ -1664,6 +1674,8 @@ async fn guess_pr(
return Ok(pr);
}
}
}
}
eyre::bail!("could not find PR");
}