shortcut.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //! Purpose: Allow the use of single words shortcut to do specific actions on GitHub via comments.
  2. //!
  3. //! Parsing is done in the `parser::command::shortcut` module.
  4. use crate::{
  5. config::ShortcutConfig,
  6. github::{Event, Label},
  7. handlers::Context,
  8. interactions::ErrorComment,
  9. };
  10. use parser::command::shortcut::ShortcutCommand;
  11. pub(super) async fn handle_command(
  12. ctx: &Context,
  13. _config: &ShortcutConfig,
  14. event: &Event,
  15. input: ShortcutCommand,
  16. ) -> anyhow::Result<()> {
  17. let issue = event.issue().unwrap();
  18. // NOTE: if shortcuts available to issues are created, they need to be allowed here
  19. if !issue.is_pr() {
  20. let msg = format!("The \"{:?}\" shortcut only works on pull requests.", input);
  21. let cmnt = ErrorComment::new(&issue, msg);
  22. cmnt.post(&ctx.github).await?;
  23. return Ok(());
  24. }
  25. let issue_labels = issue.labels();
  26. let waiting_on_review = "S-waiting-on-review";
  27. let waiting_on_author = "S-waiting-on-author";
  28. let blocked = "S-blocked";
  29. let status_labels = [waiting_on_review, waiting_on_author, blocked];
  30. let add = match input {
  31. ShortcutCommand::Ready => waiting_on_review,
  32. ShortcutCommand::Author => waiting_on_author,
  33. ShortcutCommand::Blocked => blocked,
  34. };
  35. if !issue_labels.iter().any(|l| l.name == add) {
  36. for remove in status_labels {
  37. if remove != add {
  38. issue.remove_label(&ctx.github, remove).await?;
  39. }
  40. }
  41. issue
  42. .add_labels(
  43. &ctx.github,
  44. vec![Label {
  45. name: add.to_owned(),
  46. }],
  47. )
  48. .await?;
  49. }
  50. Ok(())
  51. }