this repo has no description

Update top level comment, instead of making separate comment

+27 -29
+27 -29
src/main.rs
··· 1 1 use std::fmt::Write; 2 - use std::future::ready; 3 2 use std::io::Write as _; 4 3 use std::path::PathBuf; 5 - use std::pin::pin; 6 4 use std::{ffi::OsStr, path::Path}; 7 5 8 6 use clap::Parser; ··· 323 321 comment_lines: &[CommentLine], 324 322 pulls: &[PullRequest], 325 323 ) -> color_eyre::Result<String> { 326 - let mut comment = "This pull request is part of a stack:\n".to_owned(); 324 + let mut comment = String::new(); 325 + writeln!(&mut comment, "<!-- jj-sync-prs: {ID} -->")?; 326 + writeln!(&mut comment, "---")?; 327 + writeln!(&mut comment, "> [!NOTE]")?; 328 + writeln!(&mut comment, "> This pull request is part of a stack:")?; 327 329 for line in comment_lines { 330 + write!(&mut comment, "> ")?; 328 331 line.format(branch, pulls, &mut comment)?; 329 - comment.push('\n'); 332 + writeln!(&mut comment)?; 330 333 } 331 - comment.push_str("-------\n"); 332 - write!(comment, "_This comment was auto-generated (id: {ID})_").unwrap(); 334 + writeln!(&mut comment, ">")?; 335 + writeln!( 336 + &mut comment, 337 + "> <sup>Stack auto generated by jj-sync-prs</sup>" 338 + )?; 333 339 Ok(comment) 334 340 } 335 341 ··· 457 463 let comment = 458 464 finalize_comment(&branch, &comment_lines, &pulls).context("failed to finalize comment")?; 459 465 460 - let comment_stream = octocrab 466 + let new_body = if let Some(body) = pull.body.as_deref() { 467 + let body_without_comment = body 468 + .lines() 469 + .take_while(|line| !line.contains(ID)) 470 + .collect::<Vec<_>>() 471 + .join("\n"); 472 + format!("{body_without_comment}\n\n{comment}") 473 + } else { 474 + comment 475 + }; 476 + 477 + octocrab 461 478 .issues(&repo_info.owner, &repo_info.name) 462 - .list_comments(pull.number) 479 + .update(pull.number) 480 + .body(&new_body) 463 481 .send() 464 482 .await 465 - .context("failed to fetch comments")? 466 - .into_stream(&octocrab) 467 - .try_filter(|comment| ready(comment.body.as_ref().is_some_and(|body| body.contains(ID)))); 468 - 469 - if let Some(existing_comment) = pin!(comment_stream).try_next().await? { 470 - if existing_comment.body.is_none_or(|body| body != comment) { 471 - octocrab 472 - .issues(&repo_info.owner, &repo_info.name) 473 - .update_comment(existing_comment.id, comment) 474 - .await 475 - .context("failed to update comment")?; 476 - eprintln!("updated comment on #{}", pull.number); 477 - } 478 - } else { 479 - octocrab 480 - .issues(&repo_info.owner, &repo_info.name) 481 - .create_comment(pull.number, comment) 482 - .await 483 - .context("failed to create comment")?; 484 - eprintln!("created comment on #{}", pull.number); 485 - } 483 + .with_context(|| format!("failed to update comment on #{}", pull.number))?; 486 484 487 485 Ok(()) 488 486 }