Skip to content

josh-sync: Replace #xxxx-style links in messages #955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/josh-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ publish = false

[dependencies]
directories = "6.0.0"
regex-lite = "0.1.6"
30 changes: 30 additions & 0 deletions crates/josh-sync/src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::borrow::Cow;
use std::net::{SocketAddr, TcpStream};
use std::process::{Command, Stdio, exit};
use std::time::Duration;
use std::{env, fs, process, thread};

use regex_lite::Regex;

const JOSH_PORT: u16 = 42042;
const DEFAULT_PR_BRANCH: &str = "update-builtins";

Expand Down Expand Up @@ -77,6 +80,7 @@ impl GitSync {
"--depth=1",
]);
let new_summary = check_output(["git", "log", "-1", "--format=%h %s", &new_upstream_base]);
let new_summary = replace_references(&new_summary, &self.upstream_repo);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's great, I had no idea \B was a thing. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid false positives (like #123az) I would also highly recommend using \b at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, missed that. Added, thanks


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

/// Replace `#1234`-style issue/PR references with `repo#1234` to ensure links work across
/// repositories.
fn replace_references<'a>(s: &'a str, repo: &str) -> Cow<'a, str> {
let re = Regex::new(r"\B(?P<id>#\d+)\b").unwrap();
re.replace(s, &format!("{repo}$id"))
}

/// Create a wrapper that stops Josh on drop.
pub struct Josh(process::Child);

Expand Down Expand Up @@ -369,3 +380,22 @@ impl Drop for Josh {
self.0.kill().expect("failed to SIGKILL josh-proxy");
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_replace() {
assert_eq!(replace_references("#1234", "r-l/rust"), "r-l/rust#1234");
assert_eq!(replace_references("#1234x", "r-l/rust"), "#1234x");
assert_eq!(
replace_references("merge #1234", "r-l/rust"),
"merge r-l/rust#1234"
);
assert_eq!(
replace_references("foo/bar#1234", "r-l/rust"),
"foo/bar#1234"
);
}
}