Skip to content

Commit 6d5385f

Browse files
authored
Merge pull request #1849 from Urgau/hide-no-merges
Hide no-merge comments once merge commits removed
2 parents f563bc9 + 9921cb0 commit 6d5385f

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

src/github.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ impl ZulipGitHubReference {
391391

392392
#[derive(Debug, serde::Deserialize)]
393393
pub struct Comment {
394+
pub id: u64,
395+
pub node_id: String,
394396
#[serde(deserialize_with = "opt_string")]
395397
pub body: String,
396398
pub html_url: String,
@@ -403,6 +405,17 @@ pub struct Comment {
403405
pub pr_review_state: Option<PullRequestReviewState>,
404406
}
405407

408+
#[derive(Debug, serde::Deserialize, serde::Serialize, Eq, PartialEq)]
409+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
410+
pub enum ReportedContentClassifiers {
411+
Abuse,
412+
Duplicate,
413+
OffTopic,
414+
Outdated,
415+
Resolved,
416+
Spam,
417+
}
418+
406419
#[derive(Debug, serde::Deserialize, Eq, PartialEq)]
407420
#[serde(rename_all = "snake_case")]
408421
pub enum PullRequestReviewState {
@@ -581,24 +594,24 @@ impl Issue {
581594
client: &GithubClient,
582595
id: u64,
583596
new_body: &str,
584-
) -> anyhow::Result<()> {
597+
) -> anyhow::Result<Comment> {
585598
let comment_url = format!("{}/issues/comments/{}", self.repository().url(client), id);
586599
#[derive(serde::Serialize)]
587600
struct NewComment<'a> {
588601
body: &'a str,
589602
}
590-
client
591-
.send_req(
603+
let comment = client
604+
.json(
592605
client
593606
.patch(&comment_url)
594607
.json(&NewComment { body: new_body }),
595608
)
596609
.await
597610
.context("failed to edit comment")?;
598-
Ok(())
611+
Ok(comment)
599612
}
600613

601-
pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
614+
pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<Comment> {
602615
#[derive(serde::Serialize)]
603616
struct PostComment<'a> {
604617
body: &'a str,
@@ -608,10 +621,32 @@ impl Issue {
608621
.strip_prefix("https://api.github.com")
609622
.expect("expected api host");
610623
let comments_url = format!("{}{comments_path}", client.api_url);
611-
client
612-
.send_req(client.post(&comments_url).json(&PostComment { body }))
624+
let comment = client
625+
.json(client.post(&comments_url).json(&PostComment { body }))
613626
.await
614627
.context("failed to post comment")?;
628+
Ok(comment)
629+
}
630+
631+
pub async fn hide_comment(
632+
&self,
633+
client: &GithubClient,
634+
node_id: &str,
635+
reason: ReportedContentClassifiers,
636+
) -> anyhow::Result<()> {
637+
client
638+
.graphql_query(
639+
"mutation($node_id: ID!, $reason: ReportedContentClassifiers!) {
640+
minimizeComment(input: {subjectId: $node_id, classifier: $reason}) {
641+
__typename
642+
}
643+
}",
644+
serde_json::json!({
645+
"node_id": node_id,
646+
"reason": reason,
647+
}),
648+
)
649+
.await?;
615650
Ok(())
616651
}
617652

src/handlers/no_merges.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::{
55
config::NoMergesConfig,
66
db::issue_data::IssueData,
7-
github::{IssuesAction, IssuesEvent, Label},
7+
github::{IssuesAction, IssuesEvent, Label, ReportedContentClassifiers},
88
handlers::Context,
99
};
1010
use anyhow::Context as _;
@@ -24,6 +24,9 @@ pub(super) struct NoMergesInput {
2424
struct NoMergesState {
2525
/// Hashes of merge commits that have already been mentioned by triagebot in a comment.
2626
mentioned_merge_commits: HashSet<String>,
27+
/// List of all the no_merge comments as GitHub GraphQL NodeId.
28+
#[serde(default)]
29+
no_merge_comments: Vec<String>,
2730
/// Labels that the bot added as part of the no-merges check.
2831
#[serde(default)]
2932
added_labels: Vec<String>,
@@ -124,10 +127,22 @@ pub(super) async fn handle_input(
124127
.context("failed to remove label")?;
125128
}
126129

127-
// FIXME: Minimize prior no_merges comments.
130+
// Minimize prior no_merges comments.
131+
for node_id in state.data.no_merge_comments.iter() {
132+
event
133+
.issue
134+
.hide_comment(
135+
&ctx.github,
136+
node_id.as_str(),
137+
ReportedContentClassifiers::Resolved,
138+
)
139+
.await
140+
.context("failed to hide previous merge commit comment")?;
141+
}
128142

129143
// Clear from state.
130144
state.data.mentioned_merge_commits.clear();
145+
state.data.no_merge_comments.clear();
131146
state.data.added_labels.clear();
132147
state.save().await?;
133148
return Ok(());
@@ -202,11 +217,13 @@ pub(super) async fn handle_input(
202217
.context("failed to set no_merges labels")?;
203218

204219
// Post comment
205-
event
220+
let comment = event
206221
.issue
207222
.post_comment(&ctx.github, &message)
208223
.await
209224
.context("failed to post no_merges comment")?;
225+
226+
state.data.no_merge_comments.push(comment.node_id);
210227
state.save().await?;
211228
}
212229
Ok(())

src/interactions.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ impl<'a> ErrorComment<'a> {
2626
"Please file an issue on GitHub at [triagebot](https://github.com/rust-lang/triagebot) if there's \
2727
a problem with this bot, or reach out on [#t-infra](https://rust-lang.zulipchat.com/#narrow/stream/242791-t-infra) on Zulip."
2828
)?;
29-
self.issue.post_comment(client, &body).await
29+
self.issue.post_comment(client, &body).await?;
30+
Ok(())
3031
}
3132
}
3233

@@ -45,7 +46,8 @@ impl<'a> PingComment<'a> {
4546
for user in self.users {
4647
write!(body, "@{} ", user)?;
4748
}
48-
self.issue.post_comment(client, &body).await
49+
self.issue.post_comment(client, &body).await?;
50+
Ok(())
4951
}
5052
}
5153

0 commit comments

Comments
 (0)