Skip to content

Commit caf4f50

Browse files
committed
Hide previous range-diff comment
1 parent d9d2c26 commit caf4f50

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/handlers/check_commits/force_push_range_diff.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
use anyhow::Context as _;
22

33
use crate::config::RangeDiffConfig;
4+
use crate::db::issue_data::IssueData;
45
use crate::github::GithubCompare;
56
use crate::github::IssueRepository;
67
use crate::github::IssuesEvent;
8+
use crate::github::ReportedContentClassifiers;
79
use crate::handlers::Context;
810

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+
921
pub(super) async fn handle_event(
1022
ctx: &Context,
1123
host: &str,
@@ -54,10 +66,49 @@ pub(super) async fn handle_event(
5466
let (oldbase, oldhead) = (&compare_before.merge_base_commit.sha, before);
5567
let (newbase, newhead) = (&compare_after.merge_base_commit.sha, after);
5668

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?;
61112

62113
Ok(())
63114
}

0 commit comments

Comments
 (0)