From b8fa89198661ef55000a2c129ee690df90671559 Mon Sep 17 00:00:00 2001 From: Andrey Fomin Date: Mon, 12 Feb 2024 23:36:33 +0300 Subject: [PATCH] Properly handle value "https" in clone link name (#806) In Bitbucket Server "http" is used as http link name, but in Bitbucket Cloud "https" is used. Bug was introduced in #796. Fixes #804. --- .../bitbucket/BitbucketGitSCMBuilder.java | 2 +- .../api/BitbucketRepositoryProtocol.java | 23 ++++------ .../bitbucket/BitbucketGitSCMBuilderTest.java | 46 +++++++++---------- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java index 46c00e610..17438b2f4 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java @@ -308,7 +308,7 @@ private void withMirrorRemote() { private String getCloneLink(List cloneLinks) { return cloneLinks.stream() - .filter(link -> protocol.getType().equals(link.getName())) + .filter(link -> protocol.matches(link.getName())) .findAny() .orElseThrow(() -> new IllegalStateException("Can't find clone link for protocol " + protocol)) .getHref(); diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketRepositoryProtocol.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketRepositoryProtocol.java index 8fe349d61..85f40a0c3 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketRepositoryProtocol.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketRepositoryProtocol.java @@ -23,7 +23,6 @@ */ package com.cloudbees.jenkins.plugins.bitbucket.api; -import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; public enum BitbucketRepositoryProtocol { @@ -44,18 +43,16 @@ public enum BitbucketRepositoryProtocol { this.type = type; } - public String getType() { - return type; + /** + * Check if link name matches protocol. + * In Bitbucket Server "http" and "ssh" are used as link names. + * In Bitbucket Cloud "https" and "ssh" are used. + * + * @param linkName link name to check + * @return if link name matches + */ + public boolean matches(@NonNull String linkName) { + return linkName.startsWith(type); } - @CheckForNull - public static BitbucketRepositoryProtocol fromString(String type) { - if (SSH.type.equals(type)) { - return SSH; - } else if (HTTP.type.equals(type)) { - return HTTP; - } else { - return null; - } - } } diff --git a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilderTest.java b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilderTest.java index 9f47279ec..2f6e23852 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilderTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilderTest.java @@ -93,7 +93,7 @@ public void given__cloud_branch_rev_anon__when__build__then__scmBuilt() throws E instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -153,7 +153,7 @@ public void given__cloud_branch_rev_userpass__when__build__then__scmBuilt() thro instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -213,7 +213,7 @@ public void given__cloud_branch_rev_userkey__when__build__then__scmBuilt() throw instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -271,7 +271,7 @@ public void given__cloud_branch_norev_anon__when__build__then__scmBuilt() throws instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -317,7 +317,7 @@ public void given__cloud_branch_norev_userpass__when__build__then__scmBuilt() th instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -363,7 +363,7 @@ public void given__cloud_branch_norev_userkey__when__build__then__scmBuilt() thr instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -915,7 +915,7 @@ public void given__cloud_pullHead_rev_anon__when__build__then__scmBuilt() throws instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -979,7 +979,7 @@ public void given__cloud_pullHead_rev_userpass__when__build__then__scmBuilt() th instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1042,7 +1042,7 @@ public void given__cloud_pullHead_rev_userkey__when__build__then__scmBuilt() thr instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1106,7 +1106,7 @@ public void given__cloud_pullHead_rev_anon_sshtrait_anon__when__build__then__scm instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1176,7 +1176,7 @@ public void given__cloud_pullHead_rev_userpass_sshtrait_anon__when__build__then_ instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1246,7 +1246,7 @@ public void given__cloud_pullHead_rev_userkey_sshtrait_anon__when__build__then__ instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1316,7 +1316,7 @@ public void given__cloud_pullHead_rev_anon_sshtrait_userkey__when__build__then__ instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1386,7 +1386,7 @@ public void given__cloud_pullHead_rev_userpass_sshtrait_userkey__when__build__th instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1456,7 +1456,7 @@ public void given__cloud_pullHead_rev_userkey_sshtrait_userkey__when__build__the instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1522,7 +1522,7 @@ public void given__cloud_pullHead_norev_anon__when__build__then__scmBuilt() thro instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1570,7 +1570,7 @@ public void given__cloud_pullHead_norev_userpass__when__build__then__scmBuilt() instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -1618,7 +1618,7 @@ public void given__cloud_pullHead_norev_userkey__when__build__then__scmBuilt() t instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -2484,7 +2484,7 @@ public void given__cloud_pullMerge_rev_anon__when__build__then__scmBuilt() throw instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -2568,7 +2568,7 @@ public void given__cloud_pullMerge_rev_userpass__when__build__then__scmBuilt() t instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -2652,7 +2652,7 @@ public void given__cloud_pullMerge_rev_userkey__when__build__then__scmBuilt() th instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -2732,7 +2732,7 @@ public void given__cloud_pullMerge_norev_anon__when__build__then__scmBuilt() thr instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of() @@ -2800,7 +2800,7 @@ public void given__cloud_pullMerge_norev_userpass__when__build__then__scmBuilt() instance.withCloneLinks( List.of( - new BitbucketHref("http", "https://bitbucket.org/tester/test-repo.git"), + new BitbucketHref("https", "https://bitbucket.org/tester/test-repo.git"), new BitbucketHref("ssh", "ssh://git@bitbucket.org/tester/test-repo.git") ), List.of()