prioritize.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. use crate::{
  2. config::PrioritizeConfig,
  3. github::{self, Event},
  4. handlers::{Context, Handler},
  5. };
  6. use futures::future::{BoxFuture, FutureExt};
  7. use parser::command::prioritize::PrioritizeCommand;
  8. use parser::command::{Command, Input};
  9. pub(super) struct PrioritizeHandler;
  10. impl Handler for PrioritizeHandler {
  11. type Input = PrioritizeCommand;
  12. type Config = PrioritizeConfig;
  13. fn parse_input(
  14. &self,
  15. ctx: &Context,
  16. event: &Event,
  17. _config: Option<&Self::Config>,
  18. ) -> Result<Option<Self::Input>, String> {
  19. let body = if let Some(b) = event.comment_body() {
  20. b
  21. } else {
  22. // not interested in other events
  23. return Ok(None);
  24. };
  25. if let Event::Issue(e) = event {
  26. if !matches!(e.action, github::IssuesAction::Opened | github::IssuesAction::Edited) {
  27. // skip events other than opening or editing the issue to avoid retriggering commands in the
  28. // issue body
  29. return Ok(None);
  30. }
  31. }
  32. let mut input = Input::new(&body, &ctx.username);
  33. let command = input.parse_command();
  34. if let Some(previous) = event.comment_from() {
  35. let mut prev_input = Input::new(&previous, &ctx.username);
  36. let prev_command = prev_input.parse_command();
  37. if command == prev_command {
  38. return Ok(None);
  39. }
  40. }
  41. match command {
  42. Command::Prioritize(Ok(PrioritizeCommand)) => Ok(Some(PrioritizeCommand)),
  43. _ => Ok(None),
  44. }
  45. }
  46. fn handle_input<'a>(
  47. &self,
  48. ctx: &'a Context,
  49. config: &'a Self::Config,
  50. event: &'a Event,
  51. cmd: Self::Input,
  52. ) -> BoxFuture<'a, anyhow::Result<()>> {
  53. handle_input(ctx, config, event, cmd).boxed()
  54. }
  55. }
  56. async fn handle_input(
  57. ctx: &Context,
  58. config: &PrioritizeConfig,
  59. event: &Event,
  60. _: PrioritizeCommand,
  61. ) -> anyhow::Result<()> {
  62. let issue = event.issue().unwrap();
  63. let mut labels = issue.labels().to_owned();
  64. // Don't add the label if it's already there
  65. if !labels.iter().any(|l| l.name == config.label) {
  66. labels.push(github::Label { name: config.label.to_owned() });
  67. }
  68. issue.set_labels(&ctx.github, labels).await?;
  69. Ok(())
  70. }