|
1 | 1 | use anyhow::Context as _; |
2 | 2 |
|
3 | 3 | use crate::config::RangeDiffConfig; |
| 4 | +use crate::db::issue_data::IssueData; |
4 | 5 | use crate::github::GithubCompare; |
5 | 6 | use crate::github::IssueRepository; |
6 | 7 | use crate::github::IssuesEvent; |
| 8 | +use crate::github::ReportedContentClassifiers; |
7 | 9 | use crate::handlers::Context; |
8 | 10 |
|
| 11 | +/// Key for the state in the database |
| 12 | +const RANGE_DIFF_LINK_KEY: &str = "range-diff-link"; |
| 13 | + |
| 14 | +/// State stored in the database |
| 15 | +#[derive(Debug, Default, serde::Deserialize, serde::Serialize, Clone, PartialEq)] |
| 16 | +struct RangeDiffLinkState { |
| 17 | + /// ID of the most recent range-diff comment. |
| 18 | + last_comment: Option<String>, |
| 19 | +} |
| 20 | + |
9 | 21 | pub(super) async fn handle_event( |
10 | 22 | ctx: &Context, |
11 | 23 | host: &str, |
@@ -54,10 +66,49 @@ pub(super) async fn handle_event( |
54 | 66 | let (oldbase, oldhead) = (&compare_before.merge_base_commit.sha, before); |
55 | 67 | let (newbase, newhead) = (&compare_after.merge_base_commit.sha, after); |
56 | 68 |
|
57 | | - // Rebase detected, post a comment to our range-diff. |
58 | | - event.issue.post_comment(&ctx.github, |
59 | | - &format!(r#"This PR was rebased onto a different {branch} commit! Check out the changes with our [`range-diff`]({protocol}://{host}/gh-range-diff/{org}/{repo}/{oldbase}..{oldhead}/{newbase}..{newhead})."#) |
60 | | - ).await.context("failed to post range-diff comment")?; |
| 69 | + let message = format!( |
| 70 | + r#"This PR was rebased onto a different {branch} commit! Check out the changes with our [`range-diff`]({protocol}://{host}/gh-range-diff/{org}/{repo}/{oldbase}..{oldhead}/{newbase}..{newhead})."# |
| 71 | + ); |
| 72 | + |
| 73 | + // Rebase detected, post a comment linking to our range-diff. |
| 74 | + post_new_comment(ctx, event, message).await?; |
| 75 | + |
| 76 | + Ok(()) |
| 77 | +} |
| 78 | + |
| 79 | +async fn post_new_comment( |
| 80 | + ctx: &Context, |
| 81 | + event: &IssuesEvent, |
| 82 | + message: String, |
| 83 | +) -> anyhow::Result<()> { |
| 84 | + let mut db = ctx.db.get().await; |
| 85 | + let mut state: IssueData<'_, RangeDiffLinkState> = |
| 86 | + IssueData::load(&mut db, &event.issue, RANGE_DIFF_LINK_KEY).await?; |
| 87 | + |
| 88 | + // Hide previous range-diff comment. |
| 89 | + if let Some(last_comment) = state.data.last_comment { |
| 90 | + event |
| 91 | + .issue |
| 92 | + .hide_comment( |
| 93 | + &ctx.github, |
| 94 | + &last_comment, |
| 95 | + ReportedContentClassifiers::Outdated, |
| 96 | + ) |
| 97 | + .await |
| 98 | + .context("failed to hide previous range-diff comment")?; |
| 99 | + } |
| 100 | + |
| 101 | + // Post new range-diff comment and remember it. |
| 102 | + state.data.last_comment = Some( |
| 103 | + event |
| 104 | + .issue |
| 105 | + .post_comment(&ctx.github, &message) |
| 106 | + .await |
| 107 | + .context("failed to post range-diff comment")? |
| 108 | + .node_id, |
| 109 | + ); |
| 110 | + |
| 111 | + state.save().await?; |
61 | 112 |
|
62 | 113 | Ok(()) |
63 | 114 | } |
0 commit comments