diff --git a/src/companion.rs b/src/companion.rs index 15ede264..53d6e16b 100644 --- a/src/companion.rs +++ b/src/companion.rs @@ -17,7 +17,7 @@ use crate::{ wait_to_merge, AppState, MergeRequest, }, MergeAllowedOutcome, Result, COMPANION_LONG_REGEX, COMPANION_PREFIX_REGEX, - COMPANION_SHORT_REGEX, PR_HTML_URL_REGEX, + COMPANION_SHORT_REGEX, OWNER_AND_REPO_SEQUENCE, PR_HTML_URL_REGEX, }; async fn update_companion_repository( @@ -808,4 +808,20 @@ mod tests { vec![] ); } + + #[test] + fn test_restricted_regex() { + let owner = "paritytech"; + let repo = "polkadot"; + let pr_number = 1234; + let companion_url = format!("{}/{}#{}", owner, repo, pr_number); + assert_eq!( + parse_all_companions( + &[], + // the companion expression should not be matched because of the " for" part + &format!("companion for {}", &companion_url) + ), + vec![] + ); + } } diff --git a/src/github.rs b/src/github.rs index 8115f843..54ffd749 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,7 +1,7 @@ use crate::{ companion::parse_all_companions, error::*, utils::parse_bot_comment_from_text, webhook::MergeRequestBase, - PlaceholderDeserializationItem, PR_HTML_URL_REGEX, + PlaceholderDeserializationItem, OWNER_AND_REPO_SEQUENCE, PR_HTML_URL_REGEX, }; use regex::Regex; use serde::{Deserialize, Serialize}; diff --git a/src/macros.rs b/src/macros.rs index 60e8274d..36322e00 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,8 +1,19 @@ +#[macro_export] +macro_rules! OWNER_AND_REPO_SEQUENCE { + () => { + r"(?P[^ \t\n]+)/(?P[^ \t\n]+)" + }; +} + #[macro_export] macro_rules! PR_HTML_URL_REGEX { - () => { - r"(?Phttps://[^/\n]+/(?P[^/\n]+)/(?P[^/\n]+)/pull/(?P[[:digit:]]+))" - }; + () => { + concat!( + r"(?Phttps://[^ \t\n]+/", + OWNER_AND_REPO_SEQUENCE!(), + r"/pull/(?P[[:digit:]]+))" + ) + }; } #[macro_export] @@ -24,7 +35,8 @@ macro_rules! COMPANION_SHORT_REGEX { () => { concat!( COMPANION_PREFIX_REGEX!(), - r"(?P[^/\n]+)/(?P[^/\n]+)#(?P[[:digit:]]+)" + OWNER_AND_REPO_SEQUENCE!(), + r"#(?P[[:digit:]]+)" ) }; }