Skip to content

Commit 9b438e8

Browse files
authored
Reduce usage of deprecated BitbucketRepositoryType. (#518)
1 parent 9962312 commit 9b438e8

13 files changed

+76
-123
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
2828
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
2929
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
30-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;
3130
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient;
3231
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint;
3332
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
@@ -229,7 +228,6 @@ public BitbucketGitSCMBuilder withBitbucketRemote() {
229228
}
230229
}
231230
withRemote(bitbucket.getRepositoryUri(
232-
BitbucketRepositoryType.GIT,
233231
protocol,
234232
cloneLink,
235233
repoOwner,
@@ -256,7 +254,6 @@ public BitbucketGitSCMBuilder withBitbucketRemote() {
256254
String remoteName = remoteName().equals("upstream") ? "upstream-upstream" : "upstream";
257255
withAdditionalRemote(remoteName,
258256
bitbucket.getRepositoryUri(
259-
BitbucketRepositoryType.GIT,
260257
protocol,
261258
cloneLink,
262259
scmSource().getRepoOwner(),

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ public SCM build(SCMHead head, SCMRevision revision) {
918918
}
919919
}
920920
assert type != null;
921+
921922
if (cloneLinks == null) {
922923
BitbucketApi bitbucket = buildBitbucketClient();
923924
try {
@@ -934,7 +935,6 @@ public SCM build(SCMHead head, SCMRevision revision) {
934935
cloneLinks = new ArrayList<>();
935936
cloneLinks.add(new BitbucketHref("ssh",
936937
bitbucket.getRepositoryUri(
937-
type,
938938
BitbucketRepositoryProtocol.SSH,
939939
null,
940940
getRepoOwner(),
@@ -943,7 +943,6 @@ public SCM build(SCMHead head, SCMRevision revision) {
943943
));
944944
cloneLinks.add(new BitbucketHref("https",
945945
bitbucket.getRepositoryUri(
946-
type,
947946
BitbucketRepositoryProtocol.HTTP,
948947
null,
949948
getRepoOwner(),

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketApi.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,14 @@ public interface BitbucketApi {
6060
/**
6161
* Returns the URI of the repository.
6262
*
63-
* @param type the type of repository.
6463
* @param protocol the protocol to access the repository with.
6564
* @param cloneLink the actual clone link for the repository as sent by the server, or {@code null} if unknown.
6665
* @param owner the owner
6766
* @param repository the repository.
6867
* @return the repository URI.
6968
*/
7069
@NonNull
71-
String getRepositoryUri(@NonNull BitbucketRepositoryType type,
72-
@NonNull BitbucketRepositoryProtocol protocol,
70+
String getRepositoryUri(@NonNull BitbucketRepositoryProtocol protocol,
7371
@CheckForNull String cloneLink,
7472
@NonNull String owner,
7573
@NonNull String repository);

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
3535
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
3636
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
37-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;
3837
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException;
3938
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketTeam;
4039
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketWebHook;
@@ -235,30 +234,24 @@ public String getRepositoryName() {
235234
*/
236235
@NonNull
237236
@Override
238-
public String getRepositoryUri(@NonNull BitbucketRepositoryType type,
239-
@NonNull BitbucketRepositoryProtocol protocol,
237+
public String getRepositoryUri(@NonNull BitbucketRepositoryProtocol protocol,
240238
@CheckForNull String cloneLink,
241239
@NonNull String owner,
242240
@NonNull String repository) {
243241
// ignore port override on Cloud
244-
switch (type) {
245-
case GIT:
246-
switch (protocol) {
247-
case HTTP:
248-
if (authenticator != null) {
249-
String username = authenticator.getUserUri();
250-
if (!username.isEmpty()) {
251-
return "https://" + username + "@bitbucket.org/" + owner + "/" + repository + ".git";
252-
}
253-
}
254-
return "https://bitbucket.org/" + owner + "/" + repository + ".git";
255-
case SSH:
256-
return "git@bitbucket.org:" + owner + "/" + repository + ".git";
257-
default:
258-
throw new IllegalArgumentException("Unsupported repository protocol: " + protocol);
242+
switch (protocol) {
243+
case HTTP:
244+
if (authenticator != null) {
245+
String username = authenticator.getUserUri();
246+
if (!username.isEmpty()) {
247+
return "https://" + username + "@bitbucket.org/" + owner + "/" + repository + ".git";
248+
}
259249
}
250+
return "https://bitbucket.org/" + owner + "/" + repository + ".git";
251+
case SSH:
252+
return "git@bitbucket.org:" + owner + "/" + repository + ".git";
260253
default:
261-
throw new IllegalArgumentException("Unsupported repository type: " + type);
254+
throw new IllegalArgumentException("Unsupported repository protocol: " + protocol);
262255
}
263256
}
264257

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
3232
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
3333
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
34-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;
3534
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException;
3635
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketTeam;
3736
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketWebHook;
@@ -239,54 +238,48 @@ public String getRepositoryName() {
239238
*/
240239
@NonNull
241240
@Override
242-
public String getRepositoryUri(@NonNull BitbucketRepositoryType type,
243-
@NonNull BitbucketRepositoryProtocol protocol,
241+
public String getRepositoryUri(@NonNull BitbucketRepositoryProtocol protocol,
244242
@CheckForNull String cloneLink,
245243
@NonNull String owner,
246244
@NonNull String repository) {
247-
switch (type) {
248-
case GIT:
249-
URI baseUri;
250-
try {
251-
baseUri = new URI(baseURL);
252-
} catch (URISyntaxException e) {
253-
throw new IllegalStateException("Server URL is not a valid URI", e);
254-
}
245+
URI baseUri;
246+
try {
247+
baseUri = new URI(baseURL);
248+
} catch (URISyntaxException e) {
249+
throw new IllegalStateException("Server URL is not a valid URI", e);
250+
}
255251

256-
UriTemplate template = UriTemplate.fromTemplate("{scheme}://{+authority}{+path}{/owner,repository}.git");
257-
template.set("owner", owner);
258-
template.set("repository", repository);
252+
UriTemplate template = UriTemplate.fromTemplate("{scheme}://{+authority}{+path}{/owner,repository}.git");
253+
template.set("owner", owner);
254+
template.set("repository", repository);
259255

260-
switch (protocol) {
261-
case HTTP:
262-
template.set("scheme", baseUri.getScheme());
263-
template.set("authority", baseUri.getRawAuthority());
264-
template.set("path", Objects.toString(baseUri.getRawPath(), "") + "/scm");
265-
break;
266-
case SSH:
267-
template.set("scheme", BitbucketRepositoryProtocol.SSH.getType());
268-
template.set("authority", "git@" + baseUri.getHost());
269-
if (cloneLink != null) {
270-
try {
271-
URI cloneLinkUri = new URI(cloneLink);
272-
if (cloneLinkUri.getScheme() != null) {
273-
template.set("scheme", cloneLinkUri.getScheme());
274-
}
275-
if (cloneLinkUri.getRawAuthority() != null) {
276-
template.set("authority", cloneLinkUri.getRawAuthority());
277-
}
278-
} catch (@SuppressWarnings("unused") URISyntaxException ignored) {
279-
// fall through
280-
}
256+
switch (protocol) {
257+
case HTTP:
258+
template.set("scheme", baseUri.getScheme());
259+
template.set("authority", baseUri.getRawAuthority());
260+
template.set("path", Objects.toString(baseUri.getRawPath(), "") + "/scm");
261+
break;
262+
case SSH:
263+
template.set("scheme", BitbucketRepositoryProtocol.SSH.getType());
264+
template.set("authority", "git@" + baseUri.getHost());
265+
if (cloneLink != null) {
266+
try {
267+
URI cloneLinkUri = new URI(cloneLink);
268+
if (cloneLinkUri.getScheme() != null) {
269+
template.set("scheme", cloneLinkUri.getScheme());
281270
}
282-
break;
283-
default:
284-
throw new IllegalArgumentException("Unsupported repository protocol: " + protocol);
271+
if (cloneLinkUri.getRawAuthority() != null) {
272+
template.set("authority", cloneLinkUri.getRawAuthority());
273+
}
274+
} catch (@SuppressWarnings("unused") URISyntaxException ignored) {
275+
// fall through
276+
}
285277
}
286-
return template.expand();
287-
default:
288-
throw new IllegalArgumentException("Unsupported repository type: " + type);
278+
break;
279+
default:
280+
throw new IllegalArgumentException("Unsupported repository protocol: " + protocol);
289281
}
282+
return template.expand();
290283
}
291284

292285
/**

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
3131
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
3232
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
33-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;
3433
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
3534
import com.cloudbees.jenkins.plugins.bitbucket.filesystem.BitbucketSCMFile;
3635
import hudson.model.Action;
@@ -117,8 +116,7 @@ private WorkflowMultiBranchProject prepareFirstCheckoutCompletedInvisibleActionT
117116
when(api.resolveCommit(sampleRepo.head())).thenReturn(commit);
118117
when(commit.getDateMillis()).thenReturn(System.currentTimeMillis());
119118
when(api.checkPathExists(Mockito.anyString(), eq(jenkinsfile))).thenReturn(true);
120-
when(api.getRepositoryUri(eq(BitbucketRepositoryType.GIT),
121-
any(BitbucketRepositoryProtocol.class),
119+
when(api.getRepositoryUri(any(BitbucketRepositoryProtocol.class),
122120
anyString(),
123121
eq(repoOwner),
124122
eq(repositoryName)))

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketClientMockUtils.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
2727
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
2828
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
29-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;
3029
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient;
3130
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudAuthor;
3231
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudBranch;
@@ -55,10 +54,12 @@
5554

5655
public class BitbucketClientMockUtils {
5756

58-
public static BitbucketCloudApiClient getAPIClientMock(BitbucketRepositoryType type, boolean includePullRequests,
57+
public static BitbucketCloudApiClient getAPIClientMock(boolean includePullRequests,
5958
boolean includeWebHooks) throws IOException, InterruptedException {
6059
BitbucketCloudApiClient bitbucket = mock(BitbucketCloudApiClient.class);
61-
when(bitbucket.getRepositoryUri(any(BitbucketRepositoryType.class), any(BitbucketRepositoryProtocol.class), nullable(String.class), anyString(), anyString())).thenCallRealMethod();
60+
when(bitbucket.getRepositoryUri(any(BitbucketRepositoryProtocol.class), nullable(String.class),
61+
anyString(), anyString())).thenCallRealMethod();
62+
6263
// mock branch list
6364
List<BitbucketCloudBranch> branches = new ArrayList<>();
6465
branches.add(getBranch("branch1", "52fc8e220d77ec400f7fc96a91d2fd0bb1bc553a"));
@@ -88,7 +89,7 @@ public static BitbucketCloudApiClient getAPIClientMock(BitbucketRepositoryType t
8889

8990
// Team discovering mocks
9091
when(bitbucket.getTeam()).thenReturn(getTeam());
91-
when(bitbucket.getRepositories()).thenReturn(getRepositories(type));
92+
when(bitbucket.getRepositories()).thenReturn(getRepositories());
9293

9394
// Auto-registering hooks
9495
if (includeWebHooks) {
@@ -101,19 +102,13 @@ public static BitbucketCloudApiClient getAPIClientMock(BitbucketRepositoryType t
101102
return bitbucket;
102103
}
103104

104-
public static BitbucketCloudApiClient getAPIClientMock(BitbucketRepositoryType type, boolean includePullRequests)
105-
throws IOException, InterruptedException {
106-
return getAPIClientMock(type, includePullRequests, false);
107-
}
108-
109105
private static List<BitbucketRepositoryHook> getWebHooks() {
110106
BitbucketRepositoryHook hook = new BitbucketRepositoryHook();
111107
hook.setUrl(Jenkins.get().getRootUrl() + BitbucketSCMSourcePushHookReceiver.FULL_PATH);
112108
return Collections.singletonList(hook);
113109
}
114110

115-
private static List<BitbucketCloudRepository> getRepositories(
116-
BitbucketRepositoryType type) {
111+
private static List<BitbucketCloudRepository> getRepositories() {
117112
BitbucketCloudRepository r1 = new BitbucketCloudRepository();
118113
r1.setFullName("myteam/repo1");
119114
HashMap<String, List<BitbucketHref>> links = new HashMap<>();

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BranchScanningIntegrationTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket;
2525

26-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;
2726
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
2827
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint;
2928
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
@@ -75,8 +74,8 @@ public class BranchScanningIntegrationTest {
7574
public void indexingTest() throws Exception {
7675
BitbucketEndpointConfiguration.get()
7776
.addEndpoint(new BitbucketServerEndpoint("test", "http://bitbucket.test", false, null));
78-
BitbucketMockApiFactory.add("http://bitbucket.test", BitbucketClientMockUtils.getAPIClientMock(
79-
BitbucketRepositoryType.GIT, false));
77+
BitbucketMockApiFactory.add("http://bitbucket.test", BitbucketClientMockUtils.getAPIClientMock(false, false));
78+
8079
MultiBranchProjectImpl p = j.jenkins.createProject(MultiBranchProjectImpl.class, "test");
8180
BitbucketSCMSource source = new BitbucketSCMSource("amuniz", "test-repos");
8281
source.setTraits(Arrays.asList(

0 commit comments

Comments
 (0)