Skip to content

Commit 32d1aff

Browse files
committed
Transform relink handler to canonicalize-issue-links issue handler
1 parent f351be3 commit 32d1aff

File tree

3 files changed

+40
-39
lines changed

3 files changed

+40
-39
lines changed

src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(crate) struct Config {
4646
pub(crate) merge_conflicts: Option<MergeConflictConfig>,
4747
pub(crate) bot_pull_requests: Option<BotPullRequests>,
4848
pub(crate) rendered_link: Option<RenderedLinkConfig>,
49-
pub(crate) relink: Option<RelinkConfig>,
49+
pub(crate) canonicalize_issue_links: Option<CanonicalizeIssueLinksConfig>,
5050
}
5151

5252
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
@@ -418,7 +418,7 @@ pub(crate) struct RenderedLinkConfig {
418418
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
419419
#[serde(rename_all = "kebab-case")]
420420
#[serde(deny_unknown_fields)]
421-
pub(crate) struct RelinkConfig {}
421+
pub(crate) struct CanonicalizeIssueLinksConfig {}
422422

423423
fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> {
424424
let cache = CONFIG_CACHE.read().unwrap();
@@ -541,7 +541,7 @@ mod tests {
541541
542542
[shortcut]
543543
544-
[relink]
544+
[canonicalize-issue-links]
545545
546546
[rendered-link]
547547
trigger-files = ["posts/"]
@@ -607,7 +607,7 @@ mod tests {
607607
rendered_link: Some(RenderedLinkConfig {
608608
trigger_files: vec!["posts/".to_string()]
609609
}),
610-
relink: Some(RelinkConfig {}),
610+
canonicalize_issue_links: Some(CanonicalizeIssueLinksConfig {}),
611611
}
612612
);
613613
}
@@ -671,7 +671,7 @@ mod tests {
671671
merge_conflicts: None,
672672
bot_pull_requests: None,
673673
rendered_link: None,
674-
relink: None
674+
canonicalize_issue_links: None
675675
}
676676
);
677677
}

src/handlers.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl fmt::Display for HandlerError {
2727
mod assign;
2828
mod autolabel;
2929
mod bot_pull_requests;
30+
mod canonicalize_issue_links;
3031
mod close;
3132
pub mod docs_update;
3233
mod github_releases;
@@ -46,7 +47,6 @@ mod prioritize;
4647
pub mod project_goals;
4748
pub mod pull_requests_assignment_update;
4849
mod relabel;
49-
mod relink;
5050
mod relnotes;
5151
mod rendered_link;
5252
mod review_requested;
@@ -104,16 +104,6 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
104104
);
105105
}
106106

107-
if let Some(relink_config) = config.as_ref().ok().and_then(|c| c.relink.as_ref()) {
108-
if let Err(e) = relink::handle(ctx, event, relink_config).await {
109-
log::error!(
110-
"failed to process event {:?} with relink handler: {:?}",
111-
event,
112-
e
113-
);
114-
}
115-
}
116-
117107
if let Some(rendered_link_config) = config.as_ref().ok().and_then(|c| c.rendered_link.as_ref())
118108
{
119109
if let Err(e) = rendered_link::handle(ctx, event, rendered_link_config).await {
@@ -224,6 +214,7 @@ macro_rules! issue_handlers {
224214
issue_handlers! {
225215
assign,
226216
autolabel,
217+
canonicalize_issue_links,
227218
major_change,
228219
mentions,
229220
no_merges,

src/handlers/relink.rs renamed to src/handlers/canonicalize_issue_links.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! This handler is used to "relink" linked GitHub issues into their long form
1+
//! This handler is used to canonicalize linked GitHub issues into their long form
22
//! so that when pulling subtree into the main repository we don't accidentaly
3-
//! closes issue in the wrong repository.
3+
//! close issues in the wrong repository.
44
//!
55
//! Example: `Fixes #123` (in rust-lang/clippy) would now become `Fixes rust-lang/clippy#123`
66
@@ -10,8 +10,8 @@ use std::sync::LazyLock;
1010
use regex::Regex;
1111

1212
use crate::{
13-
config::RelinkConfig,
14-
github::{Event, IssuesAction, IssuesEvent},
13+
config::CanonicalizeIssueLinksConfig,
14+
github::{IssuesAction, IssuesEvent},
1515
handlers::Context,
1616
};
1717

@@ -21,34 +21,44 @@ static LINKED_RE: LazyLock<Regex> = LazyLock::new(|| {
2121
.unwrap()
2222
});
2323

24-
pub async fn handle(ctx: &Context, event: &Event, config: &RelinkConfig) -> anyhow::Result<()> {
25-
let Event::Issue(e) = event else {
26-
return Ok(());
27-
};
24+
pub(super) struct CanonicalizeIssueLinksInput {}
2825

29-
if !e.issue.is_pr() {
30-
return Ok(());
26+
pub(super) async fn parse_input(
27+
_ctx: &Context,
28+
event: &IssuesEvent,
29+
config: Option<&CanonicalizeIssueLinksConfig>,
30+
) -> Result<Option<CanonicalizeIssueLinksInput>, String> {
31+
if event.issue.is_pr() {
32+
return Ok(None);
3133
}
3234

33-
if let Err(e) = relink_pr(&ctx, &e, config).await {
34-
tracing::error!("Error relinking pr: {:?}", e);
35+
if !matches!(
36+
event.action,
37+
IssuesAction::Opened | IssuesAction::Reopened | IssuesAction::Edited
38+
) {
39+
return Ok(None);
3540
}
3641

37-
Ok(())
42+
// Require a `[canoncalize-issue-links]` configuration block to enable the handler.
43+
if config.is_none() {
44+
return Ok(None);
45+
};
46+
47+
Ok(Some(CanonicalizeIssueLinksInput {}))
3848
}
3949

40-
async fn relink_pr(ctx: &Context, e: &IssuesEvent, _config: &RelinkConfig) -> anyhow::Result<()> {
41-
if e.action == IssuesAction::Opened
42-
|| e.action == IssuesAction::Reopened
43-
|| e.action == IssuesAction::Edited
44-
{
45-
let full_repo_name = e.issue.repository().full_repo_name();
50+
pub(super) async fn handle_input(
51+
ctx: &Context,
52+
_config: &CanonicalizeIssueLinksConfig,
53+
e: &IssuesEvent,
54+
_input: CanonicalizeIssueLinksInput,
55+
) -> anyhow::Result<()> {
56+
let full_repo_name = e.issue.repository().full_repo_name();
4657

47-
let new_body = fix_linked_issues(&e.issue.body, full_repo_name.as_str());
58+
let new_body = fix_linked_issues(&e.issue.body, full_repo_name.as_str());
4859

49-
if e.issue.body != new_body {
50-
e.issue.edit_body(&ctx.github, &new_body).await?;
51-
}
60+
if e.issue.body != new_body {
61+
e.issue.edit_body(&ctx.github, &new_body).await?;
5262
}
5363

5464
Ok(())

0 commit comments

Comments
 (0)