rfc_helper.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. use crate::{
  2. github::{Event, IssuesAction, IssuesEvent},
  3. handlers::Context,
  4. };
  5. pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
  6. let e = if let Event::Issue(e) = event {
  7. e
  8. } else {
  9. return Ok(());
  10. };
  11. let repo = e.issue.repository();
  12. if !(repo.organization == "rust-lang" && repo.repository == "rfcs") {
  13. return Ok(());
  14. }
  15. if let Err(e) = add_rendered_link(&ctx, &e).await {
  16. tracing::error!("Error adding rendered link: {:?}", e);
  17. }
  18. Ok(())
  19. }
  20. async fn add_rendered_link(ctx: &Context, e: &IssuesEvent) -> anyhow::Result<()> {
  21. if e.action == IssuesAction::Opened {
  22. let files = e.issue.files(&ctx.github).await?;
  23. if let Some(file) = files.iter().find(|f| f.filename.starts_with("text/")) {
  24. if !e.issue.body.contains("[Rendered]") {
  25. // This URL should be stable while the PR is open, even if the
  26. // user pushes new commits.
  27. //
  28. // It will go away if the user deletes their branch, or if
  29. // they reset it (such as if they created a PR from master).
  30. // That should usually only happen after the PR is closed.
  31. // During the closing process, the closer should update the
  32. // Rendered link to the new location (which we should
  33. // automate!).
  34. let head = e.issue.head.as_ref().unwrap();
  35. let url = format!(
  36. "https://github.com/{}/blob/{}/{}",
  37. head.repo.full_name, head.git_ref, file.filename
  38. );
  39. e.issue
  40. .edit_body(
  41. &ctx.github,
  42. &format!("{}\n\n[Rendered]({})", e.issue.body, url),
  43. )
  44. .await?;
  45. }
  46. }
  47. }
  48. Ok(())
  49. }