aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-alm-client
diff options
context:
space:
mode:
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>2021-03-10 12:22:56 +0100
committersonartech <sonartech@sonarsource.com>2021-03-16 20:08:15 +0000
commit74801f89f98c3aca1de39a1e54d2e4336a039d77 (patch)
tree1b333d1ed2ee64434e9c002eac66a3749423a4ec /server/sonar-alm-client
parent5ab143afc9544634555c67ccfaa8e2e9fdcbefc1 (diff)
downloadsonarqube-74801f89f98c3aca1de39a1e54d2e4336a039d77.tar.gz
sonarqube-74801f89f98c3aca1de39a1e54d2e4336a039d77.zip
SONAR-14565 Update SonarQube main branch name during Bitbucket Server project onboarding
Diffstat (limited to 'server/sonar-alm-client')
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java5
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Branch.java50
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BranchesList.java54
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java79
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BranchesListTest.java62
5 files changed, 250 insertions, 0 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java
index e410ac25f56..344c1f1d917 100644
--- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java
@@ -102,6 +102,11 @@ public class BitbucketServerRestClient {
return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), ProjectList.class));
}
+ public BranchesList getBranches(String serverUrl, String token, String projectSlug, String repositorySlug){
+ HttpUrl url = buildUrl(serverUrl, format("/rest/api/1.0/projects/%s/repos/%s/branches", projectSlug, repositorySlug));
+ return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), BranchesList.class));
+ }
+
protected static HttpUrl buildUrl(@Nullable String serverUrl, String relativeUrl) {
if (serverUrl == null || !(serverUrl.toLowerCase(ENGLISH).startsWith("http://") || serverUrl.toLowerCase(ENGLISH).startsWith("https://"))) {
throw new IllegalArgumentException("url must start with http:// or https://");
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Branch.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Branch.java
new file mode 100644
index 00000000000..2d703950e9d
--- /dev/null
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Branch.java
@@ -0,0 +1,50 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.alm.client.bitbucketserver;
+
+import com.google.gson.annotations.SerializedName;
+
+public class Branch {
+
+ @SerializedName("displayId")
+ private String name;
+
+ @SerializedName("isDefault")
+ private boolean isDefault;
+
+ public Branch(){
+ // http://stackoverflow.com/a/18645370/229031
+ }
+
+ public Branch(String name, boolean isDefault) {
+ this.name = name;
+ this.isDefault = isDefault;
+ }
+
+ public boolean isDefault() {
+ return isDefault;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+}
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BranchesList.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BranchesList.java
new file mode 100644
index 00000000000..316c638b654
--- /dev/null
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BranchesList.java
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.alm.client.bitbucketserver;
+
+import com.google.gson.annotations.SerializedName;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+public class BranchesList {
+
+ @SerializedName("values")
+ private List<Branch> branches;
+
+ public BranchesList() {
+ // http://stackoverflow.com/a/18645370/229031
+ this(new ArrayList<>());
+ }
+
+ public BranchesList(List<Branch> values) {
+ this.branches = values;
+ }
+
+ public Optional<Branch> findDefaultBranch() {
+ return branches.stream().filter(Branch::isDefault).findFirst();
+ }
+
+ public void addBranch(Branch branch) {
+ this.branches.add(branch);
+ }
+
+ public List<Branch> getBranches() {
+ return branches;
+ }
+}
diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java
index a4cafcf1941..ccfddb717d5 100644
--- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java
+++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java
@@ -181,6 +181,85 @@ public class BitbucketServerRestClientTest {
}
@Test
+ public void getBranches_given0Branches_returnEmptyList(){
+ String bodyWith0Branches = "{\n" +
+ " \"size\": 0,\n" +
+ " \"limit\": 25,\n" +
+ " \"isLastPage\": true,\n" +
+ " \"values\": [],\n" +
+ " \"start\": 0\n" +
+ "}";
+ server.enqueue(new MockResponse()
+ .setHeader("Content-Type", "application/json;charset=UTF-8")
+ .setBody(bodyWith0Branches));
+
+ BranchesList branches = underTest.getBranches(server.url("/").toString(), "token", "projectSlug", "repoSlug");
+
+ assertThat(branches.getBranches()).isEmpty();
+ }
+
+ @Test
+ public void getBranches_given1Branch_returnListWithOneBranch(){
+ String bodyWith1Branch = "{\n" +
+ " \"size\": 1,\n" +
+ " \"limit\": 25,\n" +
+ " \"isLastPage\": true,\n" +
+ " \"values\": [{\n" +
+ " \"id\": \"refs/heads/demo\",\n" +
+ " \"displayId\": \"demo\",\n" +
+ " \"type\": \"BRANCH\",\n" +
+ " \"latestCommit\": \"3e30a6701af6f29f976e9a6609a6076b32a69ac3\",\n" +
+ " \"latestChangeset\": \"3e30a6701af6f29f976e9a6609a6076b32a69ac3\",\n" +
+ " \"isDefault\": false\n" +
+ " }],\n" +
+ " \"start\": 0\n" +
+ "}";
+ server.enqueue(new MockResponse()
+ .setHeader("Content-Type", "application/json;charset=UTF-8")
+ .setBody(bodyWith1Branch));
+
+ BranchesList branches = underTest.getBranches(server.url("/").toString(), "token", "projectSlug", "repoSlug");
+ assertThat(branches.getBranches()).hasSize(1);
+
+ Branch branch = branches.getBranches().get(0);
+ assertThat(branch.getName()).isEqualTo("demo");
+ assertThat(branch.isDefault()).isFalse();
+
+ }
+
+ @Test
+ public void getBranches_given2Branches_returnListWithTwoBranches(){
+ String bodyWith2Branches = "{\n" +
+ " \"size\": 2,\n" +
+ " \"limit\": 25,\n" +
+ " \"isLastPage\": true,\n" +
+ " \"values\": [{\n" +
+ " \"id\": \"refs/heads/demo\",\n" +
+ " \"displayId\": \"demo\",\n" +
+ " \"type\": \"BRANCH\",\n" +
+ " \"latestCommit\": \"3e30a6701af6f29f976e9a6609a6076b32a69ac3\",\n" +
+ " \"latestChangeset\": \"3e30a6701af6f29f976e9a6609a6076b32a69ac3\",\n" +
+ " \"isDefault\": false\n" +
+ " }, {\n" +
+ " \"id\": \"refs/heads/master\",\n" +
+ " \"displayId\": \"master\",\n" +
+ " \"type\": \"BRANCH\",\n" +
+ " \"latestCommit\": \"66633864d27c531ff43892f6dfea6d91632682fa\",\n" +
+ " \"latestChangeset\": \"66633864d27c531ff43892f6dfea6d91632682fa\",\n" +
+ " \"isDefault\": true\n" +
+ " }],\n" +
+ " \"start\": 0\n" +
+ "}";
+ server.enqueue(new MockResponse()
+ .setHeader("Content-Type", "application/json;charset=UTF-8")
+ .setBody(bodyWith2Branches));
+
+ BranchesList branches = underTest.getBranches(server.url("/").toString(), "token", "projectSlug", "repoSlug");
+
+ assertThat(branches.getBranches()).hasSize(2);
+ }
+
+ @Test
public void invalid_url() {
assertThatThrownBy(() -> BitbucketServerRestClient.buildUrl("file://wrong-url", ""))
.isInstanceOf(IllegalArgumentException.class)
diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BranchesListTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BranchesListTest.java
new file mode 100644
index 00000000000..80fb549952a
--- /dev/null
+++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BranchesListTest.java
@@ -0,0 +1,62 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.alm.client.bitbucketserver;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.*;
+import java.util.Optional;
+
+public class BranchesListTest {
+
+ @Test
+ public void findDefaultBranch_givenNoBranches_returnEmptyOptional(){
+ BranchesList branchesList = new BranchesList();
+
+ Optional<Branch> defaultBranch = branchesList.findDefaultBranch();
+
+ assertThat(defaultBranch).isNotPresent();
+ }
+
+ @Test
+ public void findDefaultBranch_givenBranchesWithoutDefaultOne_returnEmptyOptional(){
+ BranchesList branchesList = new BranchesList();
+ branchesList.addBranch(new Branch("1", false));
+ branchesList.addBranch(new Branch("2", false));
+
+ Optional<Branch> defaultBranch = branchesList.findDefaultBranch();
+
+ assertThat(defaultBranch).isNotPresent();
+ }
+
+ @Test
+ public void findDefaultBranch_givenBranchesWithDefaultOne_returnOptionalWithThisBranch(){
+ BranchesList branchesList = new BranchesList();
+ branchesList.addBranch(new Branch("1", false));
+ branchesList.addBranch(new Branch("2", false));
+ branchesList.addBranch(new Branch("default", true));
+
+ Optional<Branch> defaultBranchOptional = branchesList.findDefaultBranch();
+
+ assertThat(defaultBranchOptional).isPresent();
+ assertThat(defaultBranchOptional.get().isDefault()).isTrue();
+ assertThat(defaultBranchOptional.get().getName()).isEqualTo("default");
+ }
+}