Skip to content

Commit 614aef7

Browse files
committed
Add review-changes-since handler
a
1 parent 2c77a26 commit 614aef7

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub(crate) struct Config {
4949
pub(crate) behind_upstream: Option<BehindUpstreamConfig>,
5050
pub(crate) backport: Option<BackportConfig>,
5151
pub(crate) range_diff: Option<RangeDiffConfig>,
52+
pub(crate) review_changes_since: Option<ReviewChangesSinceConfig>,
5253
}
5354

5455
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
@@ -563,6 +564,12 @@ pub(crate) struct BackportRuleConfig {
563564
#[serde(deny_unknown_fields)]
564565
pub(crate) struct RangeDiffConfig {}
565566

567+
/// Configuration for the changes since link on review body
568+
#[derive(Default, PartialEq, Eq, Debug, serde::Deserialize)]
569+
#[serde(rename_all = "kebab-case")]
570+
#[serde(deny_unknown_fields)]
571+
pub(crate) struct ReviewChangesSinceConfig {}
572+
566573
fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> {
567574
let cache = CONFIG_CACHE.read().unwrap();
568575
cache.get(repo).and_then(|(config, fetch_time)| {
@@ -704,6 +711,8 @@ mod tests {
704711
add-labels = ["stable-nominated"]
705712
706713
[range-diff]
714+
715+
[review-changes-since]
707716
"#;
708717
let config = toml::from_str::<Config>(&config).unwrap();
709718
let mut ping_teams = HashMap::new();
@@ -795,6 +804,7 @@ mod tests {
795804
}),
796805
backport: Some(backport_team_config),
797806
range_diff: Some(RangeDiffConfig {}),
807+
review_changes_since: Some(ReviewChangesSinceConfig {}),
798808
}
799809
);
800810
}
@@ -884,6 +894,7 @@ mod tests {
884894
}),
885895
backport: None,
886896
range_diff: None,
897+
review_changes_since: None,
887898
}
888899
);
889900
}

src/github.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,33 @@ impl Issue {
719719
Ok(comment)
720720
}
721721

722+
pub async fn edit_review(
723+
&self,
724+
client: &GithubClient,
725+
id: u64,
726+
new_body: &str,
727+
) -> anyhow::Result<()> {
728+
let comment_url = format!(
729+
"{}/pulls/{}/reviews/{}",
730+
self.repository().url(client),
731+
self.number,
732+
id
733+
);
734+
#[derive(serde::Serialize)]
735+
struct NewComment<'a> {
736+
body: &'a str,
737+
}
738+
client
739+
.send_req(
740+
client
741+
.put(&comment_url)
742+
.json(&NewComment { body: new_body }),
743+
)
744+
.await
745+
.context("failed to edit review comment")?;
746+
Ok(())
747+
}
748+
722749
pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<Comment> {
723750
#[derive(serde::Serialize)]
724751
struct PostComment<'a> {

src/handlers.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub mod pull_requests_assignment_update;
5353
mod relabel;
5454
mod relnotes;
5555
mod rendered_link;
56+
mod review_changes_since;
5657
mod review_requested;
5758
mod review_submitted;
5859
pub mod rustc_commits;
@@ -160,6 +161,20 @@ pub async fn handle(ctx: &Context, host: &str, event: &Event) -> Vec<HandlerErro
160161
}
161162
}
162163

164+
if let Some(config) = config
165+
.as_ref()
166+
.ok()
167+
.and_then(|c| c.review_changes_since.as_ref())
168+
{
169+
if let Err(e) = review_changes_since::handle(ctx, host, event, config).await {
170+
log::error!(
171+
"failed to process event {:?} with review_changes_since handler: {:?}",
172+
event,
173+
e
174+
)
175+
}
176+
}
177+
163178
if let Some(ghr_config) = config
164179
.as_ref()
165180
.ok()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use anyhow::Context as _;
2+
3+
use crate::{
4+
config::ReviewChangesSinceConfig,
5+
github::{Comment, Event, Issue, IssueCommentAction, IssueCommentEvent},
6+
handlers::Context,
7+
};
8+
9+
/// Checks if this event is a PR review creation and adds in the body a link our `gh-changes-since`
10+
/// endpoint to view changes since this review.
11+
pub(crate) async fn handle(
12+
ctx: &Context,
13+
host: &str,
14+
event: &Event,
15+
_config: &ReviewChangesSinceConfig,
16+
) -> anyhow::Result<()> {
17+
if let Event::IssueComment(
18+
event @ IssueCommentEvent {
19+
action: IssueCommentAction::Created,
20+
issue: Issue {
21+
pull_request: Some(_),
22+
..
23+
},
24+
comment:
25+
Comment {
26+
pr_review_state: Some(_),
27+
..
28+
},
29+
..
30+
},
31+
) = event
32+
{
33+
// Add link our gh-changes-since endpoint to view changes since this review
34+
35+
let issue_repo = event.issue.repository();
36+
let pr_num = event.issue.number;
37+
38+
let base = &event.issue.base.as_ref().context("no base")?.sha;
39+
let head = &event.issue.head.as_ref().context("no head")?.sha;
40+
41+
let link = format!("https://{host}/gh-changes-since/{issue_repo}/{pr_num}/{base}..{head}");
42+
let new_body = format!(
43+
"{}\n\n*[View changes since this review]({link})*",
44+
event.comment.body
45+
);
46+
47+
event
48+
.issue
49+
.edit_review(&ctx.github, event.comment.id, &new_body)
50+
.await
51+
.context("failed to update the review body")?;
52+
}
53+
54+
Ok(())
55+
}

0 commit comments

Comments
 (0)