Merge pull request 'read detected pr from correct repo' (#90) from pr-repo-name-fix into main

Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/90
This commit is contained in:
Cyborus 2024-07-10 22:41:14 +00:00
commit 1003575645
2 changed files with 55 additions and 37 deletions

View file

@ -288,11 +288,11 @@ impl PrCommand {
match command.unwrap_or(ViewCommand::Body) { match command.unwrap_or(ViewCommand::Body) {
ViewCommand::Body => view_pr(&repo, &api, id).await?, ViewCommand::Body => view_pr(&repo, &api, id).await?,
ViewCommand::Comment { idx } => { ViewCommand::Comment { idx } => {
let id = try_get_pr_number(&repo, &api, id).await?; let (repo, id) = try_get_pr_number(&repo, &api, id).await?;
crate::issues::view_comment(&repo, &api, id, idx).await? crate::issues::view_comment(&repo, &api, id, idx).await?
} }
ViewCommand::Comments => { ViewCommand::Comments => {
let id = try_get_pr_number(&repo, &api, id).await?; let (repo, id) = try_get_pr_number(&repo, &api, id).await?;
crate::issues::view_comments(&repo, &api, id).await? crate::issues::view_comments(&repo, &api, id).await?
} }
ViewCommand::Labels => view_pr_labels(&repo, &api, id).await?, ViewCommand::Labels => view_pr_labels(&repo, &api, id).await?,
@ -318,16 +318,16 @@ impl PrCommand {
let pr = pr.map(|pr| pr.number); let pr = pr.map(|pr| pr.number);
match command { match command {
EditCommand::Title { new_title } => { EditCommand::Title { new_title } => {
let pr = try_get_pr_number(&repo, &api, pr).await?; let (repo, id) = try_get_pr_number(&repo, &api, pr).await?;
crate::issues::edit_title(&repo, &api, pr, new_title).await? crate::issues::edit_title(&repo, &api, id, new_title).await?
} }
EditCommand::Body { new_body } => { EditCommand::Body { new_body } => {
let pr = try_get_pr_number(&repo, &api, pr).await?; let (repo, id) = try_get_pr_number(&repo, &api, pr).await?;
crate::issues::edit_body(&repo, &api, pr, new_body).await? crate::issues::edit_body(&repo, &api, id, new_body).await?
} }
EditCommand::Comment { idx, new_body } => { EditCommand::Comment { idx, new_body } => {
let pr = try_get_pr_number(&repo, &api, pr).await?; let (repo, id) = try_get_pr_number(&repo, &api, pr).await?;
crate::issues::edit_comment(&repo, &api, pr, idx, new_body).await? crate::issues::edit_comment(&repo, &api, id, idx, new_body).await?
} }
EditCommand::Labels { add, rm } => { EditCommand::Labels { add, rm } => {
edit_pr_labels(&repo, &api, pr, add, rm).await? edit_pr_labels(&repo, &api, pr, add, rm).await?
@ -335,16 +335,16 @@ impl PrCommand {
} }
} }
Close { pr, with_msg } => { Close { pr, with_msg } => {
let pr = try_get_pr_number(&repo, &api, pr.map(|pr| pr.number)).await?; let (repo, pr) = try_get_pr_number(&repo, &api, pr.map(|pr| pr.number)).await?;
crate::issues::close_issue(&repo, &api, pr, with_msg).await? crate::issues::close_issue(&repo, &api, pr, with_msg).await?
} }
Checkout { pr, branch_name } => checkout_pr(&repo, &api, pr, branch_name).await?, Checkout { pr, branch_name } => checkout_pr(&repo, &api, pr, branch_name).await?,
Browse { id } => { Browse { id } => {
let id = try_get_pr_number(&repo, &api, id.map(|id| id.number)).await?; let (repo, id) = try_get_pr_number(&repo, &api, id.map(|pr| pr.number)).await?;
browse_pr(&repo, &api, id).await? browse_pr(&repo, &api, id).await?
} }
Comment { pr, body } => { Comment { pr, body } => {
let pr = try_get_pr_number(&repo, &api, pr.map(|pr| pr.number)).await?; let (repo, pr) = try_get_pr_number(&repo, &api, pr.map(|pr| pr.number)).await?;
crate::issues::add_comment(&repo, &api, pr, body).await? crate::issues::add_comment(&repo, &api, pr, body).await?
} }
} }
@ -414,6 +414,7 @@ pub async fn view_pr(repo: &RepoName, api: &Forgejo, id: Option<u64>) -> eyre::R
} = crate::special_render(); } = crate::special_render();
let pr = try_get_pr(repo, api, id).await?; let pr = try_get_pr(repo, api, id).await?;
let id = pr.number.ok_or_eyre("pr does not have number")?; let id = pr.number.ok_or_eyre("pr does not have number")?;
let repo = repo_name_from_pr(&pr)?;
let mut additions = 0; let mut additions = 0;
let mut deletions = 0; let mut deletions = 0;
@ -588,6 +589,7 @@ fn darken(r: u8, g: u8, b: u8) -> (u8, u8, u8) {
async fn view_pr_status(repo: &RepoName, api: &Forgejo, id: Option<u64>) -> eyre::Result<()> { async fn view_pr_status(repo: &RepoName, api: &Forgejo, id: Option<u64>) -> eyre::Result<()> {
let pr = try_get_pr(repo, api, id).await?; let pr = try_get_pr(repo, api, id).await?;
let repo = repo_name_from_pr(&pr)?;
let SpecialRender { let SpecialRender {
bright_magenta, bright_magenta,
@ -708,7 +710,9 @@ async fn edit_pr_labels(
add: Vec<String>, add: Vec<String>,
rm: Vec<String>, rm: Vec<String>,
) -> eyre::Result<()> { ) -> eyre::Result<()> {
let pr_number = try_get_pr_number(repo, api, pr).await?; let pr = try_get_pr(repo, api, pr).await?;
let pr_number = pr.number.ok_or_eyre("pr does not have number")?;
let repo = repo_name_from_pr(&pr)?;
let query = forgejo_api::structs::IssueListLabelsQuery { let query = forgejo_api::structs::IssueListLabelsQuery {
limit: Some(u32::MAX), limit: Some(u32::MAX),
@ -859,6 +863,7 @@ async fn merge_pr(
let repo_info = api.repo_get(repo.owner(), repo.name()).await?; let repo_info = api.repo_get(repo.owner(), repo.name()).await?;
let pr_info = try_get_pr(repo, api, pr).await?; let pr_info = try_get_pr(repo, api, pr).await?;
let repo = repo_name_from_pr(&pr_info)?;
let pr_html_url = pr_info let pr_html_url = pr_info
.html_url .html_url
.as_ref() .as_ref()
@ -945,17 +950,7 @@ async fn checkout_pr(
PrNumber::This(_) => api.repo_get(repo.owner(), repo.name()).await?, PrNumber::This(_) => api.repo_get(repo.owner(), repo.name()).await?,
}; };
let repo_owner = remote_repo let (repo_owner, repo_name) = repo_name_from_repo(&remote_repo)?;
.owner
.as_ref()
.ok_or_eyre("repo does not have owner")?
.login
.as_deref()
.ok_or_eyre("owner does not have login")?;
let repo_name = remote_repo
.name
.as_ref()
.ok_or_eyre("repo does not have name")?;
let pull_data = api let pull_data = api
.repo_get_pull_request(repo_owner, repo_name, pr.number()) .repo_get_pull_request(repo_owner, repo_name, pr.number())
@ -1082,13 +1077,15 @@ async fn view_diff(
patch: bool, patch: bool,
editor: bool, editor: bool,
) -> eyre::Result<()> { ) -> eyre::Result<()> {
let pr = try_get_pr_number(repo, api, pr).await?; let pr = try_get_pr(repo, api, pr).await?;
let pr_number = pr.number.ok_or_eyre("pr does not have number")?;
let repo = repo_name_from_pr(&pr)?;
let diff_type = if patch { "patch" } else { "diff" }; let diff_type = if patch { "patch" } else { "diff" };
let diff = api let diff = api
.repo_download_pull_diff_or_patch( .repo_download_pull_diff_or_patch(
repo.owner(), repo.owner(),
repo.name(), repo.name(),
pr, pr_number,
diff_type, diff_type,
forgejo_api::structs::RepoDownloadPullDiffOrPatchQuery::default(), forgejo_api::structs::RepoDownloadPullDiffOrPatchQuery::default(),
) )
@ -1106,7 +1103,9 @@ async fn view_diff(
} }
async fn view_pr_files(repo: &RepoName, api: &Forgejo, pr: Option<u64>) -> eyre::Result<()> { async fn view_pr_files(repo: &RepoName, api: &Forgejo, pr: Option<u64>) -> eyre::Result<()> {
let pr = try_get_pr_number(repo, api, pr).await?; let pr = try_get_pr(repo, api, pr).await?;
let pr_number = pr.number.ok_or_eyre("pr does not have number")?;
let repo = repo_name_from_pr(&pr)?;
let crate::SpecialRender { let crate::SpecialRender {
bright_red, bright_red,
bright_green, bright_green,
@ -1119,7 +1118,7 @@ async fn view_pr_files(repo: &RepoName, api: &Forgejo, pr: Option<u64>) -> eyre:
..Default::default() ..Default::default()
}; };
let (_, files) = api let (_, files) = api
.repo_get_pull_request_files(repo.owner(), repo.name(), pr, query) .repo_get_pull_request_files(repo.owner(), repo.name(), pr_number, query)
.await?; .await?;
let max_additions = files let max_additions = files
.iter() .iter()
@ -1150,14 +1149,16 @@ async fn view_pr_commits(
pr: Option<u64>, pr: Option<u64>,
oneline: bool, oneline: bool,
) -> eyre::Result<()> { ) -> eyre::Result<()> {
let pr = try_get_pr_number(repo, api, pr).await?; let pr = try_get_pr(repo, api, pr).await?;
let pr_number = pr.number.ok_or_eyre("pr does not have number")?;
let repo = repo_name_from_pr(&pr)?;
let query = RepoGetPullRequestCommitsQuery { let query = RepoGetPullRequestCommitsQuery {
limit: Some(u32::MAX), limit: Some(u32::MAX),
files: Some(false), files: Some(false),
..Default::default() ..Default::default()
}; };
let (_headers, commits) = api let (_headers, commits) = api
.repo_get_pull_request_commits(repo.owner(), repo.name(), pr, query) .repo_get_pull_request_commits(repo.owner(), repo.name(), pr_number, query)
.await?; .await?;
let max_additions = commits let max_additions = commits
@ -1251,14 +1252,17 @@ async fn try_get_pr_number(
repo: &RepoName, repo: &RepoName,
api: &Forgejo, api: &Forgejo,
number: Option<u64>, number: Option<u64>,
) -> eyre::Result<u64> { ) -> eyre::Result<(RepoName, u64)> {
let pr = match number { let pr = match number {
Some(number) => number, Some(number) => (repo.clone(), number),
None => guess_pr(repo, api) None => {
.await let pr = guess_pr(repo, api)
.wrap_err("could not guess pull request number, please specify")? .await
.number .wrap_err("could not guess pull request number, please specify")?;
.ok_or_eyre("pr does not have number")?, let number = pr.number.ok_or_eyre("pr does not have number")?;
let repo = repo_name_from_pr(&pr)?;
(repo, number)
}
}; };
Ok(pr) Ok(pr)
} }
@ -1421,6 +1425,17 @@ fn repo_name_from_repo(repo: &forgejo_api::structs::Repository) -> eyre::Result<
Ok((owner, name)) Ok((owner, name))
} }
fn repo_name_from_pr(pr: &forgejo_api::structs::PullRequest) -> eyre::Result<RepoName> {
let base_branch = pr.base.as_ref().ok_or_eyre("pr does not have base")?;
let repo = base_branch
.repo
.as_ref()
.ok_or_eyre("branch does not have repo")?;
let (owner, name) = repo_name_from_repo(repo)?;
let repo_name = RepoName::new(owner.to_owned(), name.to_owned());
Ok(repo_name)
}
//async fn guess_pr( //async fn guess_pr(
// repo: &RepoName, // repo: &RepoName,
// api: &Forgejo, // api: &Forgejo,

View file

@ -213,13 +213,16 @@ fn url_strip_repo_name(mut url: Url) -> eyre::Result<(Url, RepoName)> {
Ok((url, RepoName { owner, name })) Ok((url, RepoName { owner, name }))
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct RepoName { pub struct RepoName {
owner: String, owner: String,
name: String, name: String,
} }
impl RepoName { impl RepoName {
pub fn new(owner: String, name: String) -> Self {
Self { owner, name }
}
pub fn owner(&self) -> &str { pub fn owner(&self) -> &str {
&self.owner &self.owner
} }