Skip to content

Commit b8065de

Browse files
committed
josh-sync: Replace #xxxx-style links in messages
Often our short summaries will pick up a Bors "Auto merge of #xxxx ...` commit message. Replace these with something like `rust-lang/rust#1234` to avoid broken links when going between repositories.
1 parent 1e2ebeb commit b8065de

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

crates/josh-sync/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ publish = false
55

66
[dependencies]
77
directories = "6.0.0"
8+
regex-lite = "0.1.6"

crates/josh-sync/src/sync.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use std::borrow::Cow;
12
use std::net::{SocketAddr, TcpStream};
23
use std::process::{Command, Stdio, exit};
34
use std::time::Duration;
45
use std::{env, fs, process, thread};
56

7+
use regex_lite::Regex;
8+
69
const JOSH_PORT: u16 = 42042;
710
const DEFAULT_PR_BRANCH: &str = "update-builtins";
811

@@ -77,6 +80,7 @@ impl GitSync {
7780
"--depth=1",
7881
]);
7982
let new_summary = check_output(["git", "log", "-1", "--format=%h %s", &new_upstream_base]);
83+
let new_summary = replace_references(&new_summary, &self.upstream_repo);
8084

8185
// Update rust-version file. As a separate commit, since making it part of
8286
// the merge has confused the heck out of josh in the past.
@@ -297,6 +301,13 @@ fn check_output_cfg(prog: &str, f: impl FnOnce(&mut Command) -> &mut Command) ->
297301
String::from_utf8(out.stdout.trim_ascii().to_vec()).expect("non-UTF8 output")
298302
}
299303

304+
/// Replace `#1234`-style issue/PR references with `prefix#1234` to ensure links work across
305+
/// repositories.
306+
fn replace_references<'a>(s: &'a str, repo: &str) -> Cow<'a, str> {
307+
let re = Regex::new(r"\B(?P<id>#\d+)\b").unwrap();
308+
re.replace(s, &format!("{repo}$id"))
309+
}
310+
300311
/// Create a wrapper that stops Josh on drop.
301312
pub struct Josh(process::Child);
302313

@@ -369,3 +380,22 @@ impl Drop for Josh {
369380
self.0.kill().expect("failed to SIGKILL josh-proxy");
370381
}
371382
}
383+
384+
#[cfg(test)]
385+
mod tests {
386+
use super::*;
387+
388+
#[test]
389+
fn test_replace() {
390+
assert_eq!(replace_references("#1234", "r-l/rust"), "r-l/rust#1234");
391+
assert_eq!(replace_references("#1234x", "r-l/rust"), "#1234x");
392+
assert_eq!(
393+
replace_references("merge #1234", "r-l/rust"),
394+
"merge r-l/rust#1234"
395+
);
396+
assert_eq!(
397+
replace_references("foo/bar#1234", "r-l/rust"),
398+
"foo/bar#1234"
399+
);
400+
}
401+
}

0 commit comments

Comments
 (0)