Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_ui: Use pull request link from push output in push toast (#60522)
Matt Van Horn8d932b0e0671
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rsindex d20065cc6ca..6c0375fcbfa 100644--- a/crates/git_ui/src/git_panel.rs+++ b/crates/git_ui/src/git_panel.rs@@ -4693,7 +4693,14 @@ impl GitPanel {.color(Color::Muted),);match (style, is_push) {+ (PushPrLink { label, url }, _) => {+ this.action(label, move |_window, cx| cx.open_url(&url))+ }(Toast | ToastWithLog { .. }, true) => {+ // If we were not able to parse a valid URL from the+ // output of a push command, we'll simply dispatch the+ // generic `CreatePullRequest` action when the toast+ // button is pressed.this.action("Create Pull Request", move |window, cx| {window.dispatch_action(Box::new(zed_actions::git::CreatePullRequest), cx);diff --git a/crates/git_ui/src/remote_output.rs b/crates/git_ui/src/remote_output.rsindex 157ce831677..3c9921c0f96 100644--- a/crates/git_ui/src/remote_output.rs+++ b/crates/git_ui/src/remote_output.rs@@ -4,6 +4,17 @@ use git::repository::{Remote, RemoteCommandOutput};use ui::SharedString;use util::ResultExt as _;+const PULL_REQUEST_HINTS: &[(&str, &str)] = &[+ // GitHub: "Create a pull request for 'branch' on GitHub by visiting:"+ ("Create a pull request", "Create Pull Request"),+ // Bitbucket: "Create pull request for branch:"+ ("Create pull request", "Create Pull Request"),+ // GitLab: "To create a merge request for branch, visit:"+ ("create a merge request", "Create Merge Request"),+ // GitLab: "View merge request for branch:"+ ("View merge request", "View Merge Request"),+];+#[derive(Clone)]pub enum RemoteAction {Fetch(Option<Remote>),@@ -24,6 +35,7 @@ impl RemoteAction {pub enum SuccessStyle {Toast,ToastWithLog { output: RemoteCommandOutput },+ PushPrLink { label: &'static str, url: String },}pub struct SuccessMessage {@@ -31,6 +43,42 @@ pub struct SuccessMessage {pub style: SuccessStyle,}+fn extract_pull_request_link(output: &RemoteCommandOutput) -> Option<(&'static str, String)> {+ let mut pending_label: Option<&'static str> = None;++ for line in output.stderr.lines() {+ let Some(remote_line) = line.trim_start().strip_prefix("remote:") else {+ pending_label = None;+ continue;+ };++ if let Some((_, label)) = PULL_REQUEST_HINTS+ .iter()+ .find(|(hint, _)| remote_line.contains(hint))+ {+ pending_label = Some(label);+ }++ if let Some(url) = extract_url(remote_line)+ && let Some(label) = pending_label+ {+ return Some((label, url));+ }+ }++ None+}++fn extract_url(line: &str) -> Option<String> {+ let http_index = line.find("https://").or_else(|| line.find("http://"))?;+ let url = line[http_index..]+ .split_whitespace()+ .next()?+ .trim_end_matches(|character| matches!(character, ',' | '.' | ')' | ']' | '>'));++ Some(url.to_string())+}+pub fn format_output(action: &RemoteAction, output: RemoteCommandOutput) -> SuccessMessage {match action {RemoteAction::Fetch(remote) => {@@ -122,6 +170,11 @@ pub fn format_output(action: &RemoteAction, output: RemoteCommandOutput) -> Succmessage: "Push: Everything is up-to-date".to_string(),style: SuccessStyle::Toast,}+ } else if let Some((label, url)) = extract_pull_request_link(&output) {+ SuccessMessage {+ message: format!("Pushed {} to {}", branch_name, remote_ref.name),+ style: SuccessStyle::PushPrLink { label, url },+ }} else {SuccessMessage {message: format!("Pushed {} to {}", branch_name, remote_ref.name),@@ -161,9 +214,13 @@ mod tests {};let msg = format_output(&action, output);-- assert!(matches!(msg.style, SuccessStyle::ToastWithLog { .. }));- assert_eq!(msg.message, "Pushed test_branch to test_remote");+ if let SuccessStyle::PushPrLink { label, url } = msg.style {+ assert_eq!(msg.message, "Pushed test_branch to test_remote");+ assert_eq!(label, "Create Pull Request");+ assert_eq!(url, "https://example.com/test/test/pull/new/test");+ } else {+ panic!("Expected PushPrLink variant");+ }}#[test]@@ -191,8 +248,37 @@ mod tests {let msg = format_output(&action, output);- assert!(matches!(msg.style, SuccessStyle::ToastWithLog { .. }));- assert_eq!(msg.message, "Pushed test_branch to test_remote");+ if let SuccessStyle::PushPrLink { label, url } = msg.style {+ assert_eq!(msg.message, "Pushed test_branch to test_remote");+ assert_eq!(label, "Create Merge Request");+ assert_eq!(+ url,+ "https://example.com/test/test/-/merge_requests/new?merge_request%5Bsource_branch%5D=test"+ )+ } else {+ panic!("Expected PushPrLink variant")+ }+ }++ #[test]+ fn test_push_new_branch_bitbucket_pull_request() {+ let output = RemoteCommandOutput {+ stdout: String::new(),+ stderr: indoc! {"+ remote:+ remote: Create pull request for test:+ remote: https://bitbucket.example.com/projects/TEST/repos/test/pull-requests?create&sourceBranch=refs/heads/test+ "}+ .to_string(),+ };++ assert_eq!(+ extract_pull_request_link(&output),+ Some((+ "Create Pull Request",+ "https://bitbucket.example.com/projects/TEST/repos/test/pull-requests?create&sourceBranch=refs/heads/test".to_string()+ ))+ );}#[test]@@ -206,7 +292,9 @@ mod tests {let output = RemoteCommandOutput {stdout: String::new(),- // Simulate an extraneous link that should not be found in top 3 lines+ // Include an unrelated URL outside of the `remote:` lines, in this+ // case, an OpenSSH warning, to ensure that it is not mistaken for+ // the merge request link.stderr: indoc! {"** WARNING: connection is not using a post-quantum key exchange algorithm.** This session may be vulnerable to \"store now, decrypt later\" attacks.@@ -224,8 +312,13 @@ mod tests {let msg = format_output(&action, output);- assert!(matches!(msg.style, SuccessStyle::ToastWithLog { .. }));- assert_eq!(msg.message, "Pushed test_branch to test_remote");+ if let SuccessStyle::PushPrLink { label, url } = msg.style {+ assert_eq!(msg.message, "Pushed test_branch to test_remote");+ assert_eq!(label, "View Merge Request");+ assert_eq!(url, "https://example.com/test/test/-/merge_requests/99999");+ } else {+ panic!("Expected PushPrLink variant")+ }}#[test]@@ -254,6 +347,7 @@ mod tests {output.stderr,"To http://example.com/test/test.git\n * [new branch] test -> test\n");+ assert_eq!(extract_pull_request_link(output), None);} else {panic!("Expected ToastWithLog variant");}