lib.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #![allow(clippy::new_without_default)]
  2. use anyhow::Context;
  3. use handlers::HandlerError;
  4. use interactions::ErrorComment;
  5. use std::fmt;
  6. pub mod config;
  7. pub mod db;
  8. pub mod github;
  9. pub mod handlers;
  10. pub mod interactions;
  11. pub mod notification_listing;
  12. pub mod payload;
  13. pub mod team;
  14. mod team_data;
  15. pub mod zulip;
  16. #[derive(Debug)]
  17. pub enum EventName {
  18. PullRequest,
  19. PullRequestReview,
  20. PullRequestReviewComment,
  21. IssueComment,
  22. Issue,
  23. Other,
  24. }
  25. impl std::str::FromStr for EventName {
  26. type Err = std::convert::Infallible;
  27. fn from_str(s: &str) -> Result<EventName, Self::Err> {
  28. Ok(match s {
  29. "pull_request_review" => EventName::PullRequestReview,
  30. "pull_request_review_comment" => EventName::PullRequestReviewComment,
  31. "issue_comment" => EventName::IssueComment,
  32. "pull_request" => EventName::PullRequest,
  33. "issues" => EventName::Issue,
  34. _ => EventName::Other,
  35. })
  36. }
  37. }
  38. impl fmt::Display for EventName {
  39. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  40. write!(
  41. f,
  42. "{}",
  43. match self {
  44. EventName::PullRequestReview => "pull_request_review",
  45. EventName::PullRequestReviewComment => "pull_request_review_comment",
  46. EventName::IssueComment => "issue_comment",
  47. EventName::Issue => "issues",
  48. EventName::PullRequest => "pull_request",
  49. EventName::Other => "other",
  50. }
  51. )
  52. }
  53. }
  54. #[derive(Debug)]
  55. pub struct WebhookError(anyhow::Error);
  56. impl From<anyhow::Error> for WebhookError {
  57. fn from(e: anyhow::Error) -> WebhookError {
  58. WebhookError(e)
  59. }
  60. }
  61. pub fn deserialize_payload<T: serde::de::DeserializeOwned>(v: &str) -> anyhow::Result<T> {
  62. let mut deserializer = serde_json::Deserializer::from_str(&v);
  63. let res: Result<T, _> = serde_path_to_error::deserialize(&mut deserializer);
  64. match res {
  65. Ok(r) => Ok(r),
  66. Err(e) => {
  67. let ctx = format!("at {:?}", e.path());
  68. Err(e.into_inner()).context(ctx)
  69. }
  70. }
  71. }
  72. pub async fn webhook(
  73. event: EventName,
  74. payload: String,
  75. ctx: &handlers::Context,
  76. ) -> Result<bool, WebhookError> {
  77. let event = match event {
  78. EventName::PullRequestReview => {
  79. let payload = deserialize_payload::<github::PullRequestReviewEvent>(&payload)
  80. .context("PullRequestReview failed to deserialize")
  81. .map_err(anyhow::Error::from)?;
  82. log::info!("handling pull request review comment {:?}", payload);
  83. // Treat pull request review comments exactly like pull request
  84. // review comments.
  85. github::Event::IssueComment(github::IssueCommentEvent {
  86. action: match payload.action {
  87. github::PullRequestReviewAction::Submitted => {
  88. github::IssueCommentAction::Created
  89. }
  90. github::PullRequestReviewAction::Edited => github::IssueCommentAction::Edited,
  91. github::PullRequestReviewAction::Dismissed => {
  92. github::IssueCommentAction::Deleted
  93. }
  94. },
  95. issue: payload.pull_request,
  96. comment: payload.review,
  97. repository: payload.repository,
  98. })
  99. }
  100. EventName::PullRequestReviewComment => {
  101. let payload = deserialize_payload::<github::PullRequestReviewComment>(&payload)
  102. .context("PullRequestReview(Comment) failed to deserialize")
  103. .map_err(anyhow::Error::from)?;
  104. log::info!("handling pull request review comment {:?}", payload);
  105. // Treat pull request review comments exactly like pull request
  106. // review comments.
  107. github::Event::IssueComment(github::IssueCommentEvent {
  108. action: payload.action,
  109. issue: payload.issue,
  110. comment: payload.comment,
  111. repository: payload.repository,
  112. })
  113. }
  114. EventName::IssueComment => {
  115. let payload = deserialize_payload::<github::IssueCommentEvent>(&payload)
  116. .context("IssueCommentEvent failed to deserialize")
  117. .map_err(anyhow::Error::from)?;
  118. log::info!("handling issue comment {:?}", payload);
  119. github::Event::IssueComment(payload)
  120. }
  121. EventName::Issue | EventName::PullRequest => {
  122. let payload = deserialize_payload::<github::IssuesEvent>(&payload)
  123. .context(format!("{:?} failed to deserialize", event))
  124. .map_err(anyhow::Error::from)?;
  125. log::info!("handling issue event {:?}", payload);
  126. github::Event::Issue(payload)
  127. }
  128. // Other events need not be handled
  129. EventName::Other => {
  130. return Ok(false);
  131. }
  132. };
  133. if let Err(err) = handlers::handle(&ctx, &event).await {
  134. match err {
  135. HandlerError::Message(message) => {
  136. if let Some(issue) = event.issue() {
  137. let cmnt = ErrorComment::new(issue, message);
  138. cmnt.post(&ctx.github).await?;
  139. }
  140. }
  141. HandlerError::Other(err) => {
  142. log::error!("handling event failed: {:?}", err);
  143. return Err(WebhookError(anyhow::anyhow!(
  144. "handling failed, error logged",
  145. )));
  146. }
  147. }
  148. }
  149. Ok(true)
  150. }