nominate.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //! Purpose: Allow team members to nominate issues or PRs.
  2. use crate::{
  3. config::NominateConfig,
  4. github::{self, Event},
  5. handlers::Context,
  6. interactions::ErrorComment,
  7. };
  8. use parser::command::nominate::{NominateCommand, Style};
  9. pub(super) async fn handle_command(
  10. ctx: &Context,
  11. config: &NominateConfig,
  12. event: &Event,
  13. cmd: NominateCommand,
  14. ) -> anyhow::Result<()> {
  15. let is_team_member = if let Err(_) | Ok(false) = event.user().is_team_member(&ctx.github).await
  16. {
  17. false
  18. } else {
  19. true
  20. };
  21. if !is_team_member {
  22. let cmnt = ErrorComment::new(
  23. &event.issue().unwrap(),
  24. format!(
  25. "Nominating and approving issues and pull requests is restricted to members of\
  26. the Rust teams."
  27. ),
  28. );
  29. cmnt.post(&ctx.github).await?;
  30. return Ok(());
  31. }
  32. let issue_labels = event.issue().unwrap().labels();
  33. let mut labels_to_add = vec![];
  34. if cmd.style == Style::BetaApprove {
  35. if !issue_labels.iter().any(|l| l.name == "beta-nominated") {
  36. let cmnt = ErrorComment::new(
  37. &event.issue().unwrap(),
  38. format!(
  39. "This pull request is not beta-nominated, so it cannot be approved yet.\
  40. Perhaps try to beta-nominate it by using `@{} beta-nominate <team>`?",
  41. ctx.username,
  42. ),
  43. );
  44. cmnt.post(&ctx.github).await?;
  45. return Ok(());
  46. }
  47. // Add the beta-accepted label, but don't attempt to remove beta-nominated or the team
  48. // label.
  49. labels_to_add.push(github::Label {
  50. name: "beta-accepted".into(),
  51. });
  52. } else {
  53. if !config.teams.contains_key(&cmd.team) {
  54. let cmnt = ErrorComment::new(
  55. &event.issue().unwrap(),
  56. format!(
  57. "This team (`{}`) cannot be nominated for via this command;\
  58. it may need to be added to `triagebot.toml` on the default branch.",
  59. cmd.team,
  60. ),
  61. );
  62. cmnt.post(&ctx.github).await?;
  63. return Ok(());
  64. }
  65. let label = config.teams[&cmd.team].clone();
  66. labels_to_add.push(github::Label { name: label });
  67. let style_label = match cmd.style {
  68. Style::Decision => "I-nominated",
  69. Style::Beta => "beta-nominated",
  70. Style::BetaApprove => unreachable!(),
  71. };
  72. labels_to_add.push(github::Label {
  73. name: style_label.into(),
  74. });
  75. }
  76. event
  77. .issue()
  78. .unwrap()
  79. .add_labels(&ctx.github, labels_to_add)
  80. .await?;
  81. Ok(())
  82. }