|
| 1 | +//! This handler is used to canonicalize linked GitHub issues into their long form |
| 2 | +//! so that when pulling subtree into the main repository we don't accidentaly |
| 3 | +//! close issues in the wrong repository. |
| 4 | +//! |
| 5 | +//! Example: `Fixes #123` (in rust-lang/clippy) would now become `Fixes rust-lang/clippy#123` |
| 6 | +
|
| 7 | +use std::borrow::Cow; |
| 8 | +use std::sync::LazyLock; |
| 9 | + |
| 10 | +use regex::Regex; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + config::CanonicalizeIssueLinksConfig, |
| 14 | + github::{IssuesAction, IssuesEvent}, |
| 15 | + handlers::Context, |
| 16 | +}; |
| 17 | + |
| 18 | +// Taken from https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue?quot#linking-a-pull-request-to-an-issue-using-a-keyword |
| 19 | +static LINKED_RE: LazyLock<Regex> = LazyLock::new(|| { |
| 20 | + Regex::new("(?i)(?P<action>close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)(?P<spaces>:? +)(?P<issue>#[0-9]+)") |
| 21 | + .unwrap() |
| 22 | +}); |
| 23 | + |
| 24 | +pub(super) struct CanonicalizeIssueLinksInput {} |
| 25 | + |
| 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); |
| 33 | + } |
| 34 | + |
| 35 | + if !matches!( |
| 36 | + event.action, |
| 37 | + IssuesAction::Opened | IssuesAction::Reopened | IssuesAction::Edited |
| 38 | + ) { |
| 39 | + return Ok(None); |
| 40 | + } |
| 41 | + |
| 42 | + // Require a `[canonicalize-issue-links]` configuration block to enable the handler. |
| 43 | + if config.is_none() { |
| 44 | + return Ok(None); |
| 45 | + }; |
| 46 | + |
| 47 | + Ok(Some(CanonicalizeIssueLinksInput {})) |
| 48 | +} |
| 49 | + |
| 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(); |
| 57 | + |
| 58 | + let new_body = fix_linked_issues(&e.issue.body, full_repo_name.as_str()); |
| 59 | + |
| 60 | + if e.issue.body != new_body { |
| 61 | + e.issue.edit_body(&ctx.github, &new_body).await?; |
| 62 | + } |
| 63 | + |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +fn fix_linked_issues<'a>(body: &'a str, full_repo_name: &str) -> Cow<'a, str> { |
| 68 | + let replace_by = format!("${{action}}${{spaces}}{full_repo_name}${{issue}}"); |
| 69 | + LINKED_RE.replace_all(body, replace_by) |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn fixed_body() { |
| 74 | + let full_repo_name = "rust-lang/rust"; |
| 75 | + |
| 76 | + let body = r#" |
| 77 | + This is a PR. |
| 78 | +
|
| 79 | + Fix #123 |
| 80 | + fixed #456 |
| 81 | + Fixes #7895 |
| 82 | + Closes: #987 |
| 83 | + resolves: #655 |
| 84 | + Resolves #00000 Closes #888 |
| 85 | + "#; |
| 86 | + |
| 87 | + let fixed_body = r#" |
| 88 | + This is a PR. |
| 89 | +
|
| 90 | + Fix rust-lang/rust#123 |
| 91 | + fixed rust-lang/rust#456 |
| 92 | + Fixes rust-lang/rust#7895 |
| 93 | + Closes: rust-lang/rust#987 |
| 94 | + resolves: rust-lang/rust#655 |
| 95 | + Resolves rust-lang/rust#00000 Closes rust-lang/rust#888 |
| 96 | + "#; |
| 97 | + |
| 98 | + let new_body = fix_linked_issues(body, full_repo_name); |
| 99 | + assert_eq!(new_body, fixed_body); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn untouched_body() { |
| 104 | + let full_repo_name = "rust-lang/rust"; |
| 105 | + |
| 106 | + let body = r#" |
| 107 | + This is a PR. |
| 108 | +
|
| 109 | + Fix rust-lang#123 |
| 110 | + Fixesd #7895 |
| 111 | + Resolves #abgt |
| 112 | + Resolves: #abgt |
| 113 | + "#; |
| 114 | + |
| 115 | + let new_body = fix_linked_issues(body, full_repo_name); |
| 116 | + assert_eq!(new_body, body); |
| 117 | +} |
0 commit comments