import static java.util.Arrays.stream;
import static org.sonar.db.almsettings.AlmSettingsTesting.newAzureAlmSettingDto;
+import static org.sonar.db.almsettings.AlmSettingsTesting.newAzureProjectAlmSettingDto;
import static org.sonar.db.almsettings.AlmSettingsTesting.newBitbucketAlmSettingDto;
import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto;
import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubProjectAlmSettingDto;
@SafeVarargs
public final ProjectAlmSettingDto insertGitHubProjectAlmSetting(AlmSettingDto githubAlmSetting, ComponentDto project, Consumer<ProjectAlmSettingDto>... populators) {
- ProjectAlmSettingDto dto = newGithubProjectAlmSettingDto(githubAlmSetting, project);
- stream(populators).forEach(p -> p.accept(dto));
+ return insertProjectAlmSetting(newGithubProjectAlmSettingDto(githubAlmSetting, project), populators);
+ }
+
+ public ProjectAlmSettingDto insertAzureProjectAlmSetting(AlmSettingDto azureAlmSetting, ComponentDto project) {
+ return insertProjectAlmSetting(newAzureProjectAlmSettingDto(azureAlmSetting, project));
+ }
+ private ProjectAlmSettingDto insertProjectAlmSetting(ProjectAlmSettingDto dto, Consumer<ProjectAlmSettingDto>... populators) {
+ stream(populators).forEach(p -> p.accept(dto));
db.getDbClient().projectAlmSettingDao().insertOrUpdate(db.getSession(), dto);
db.commit();
return dto;
private AlmSettingDto insert(AlmSettingDto dto, Consumer<AlmSettingDto>[] populators) {
stream(populators).forEach(p -> p.accept(dto));
-
db.getDbClient().almSettingDao().insert(db.getSession(), dto);
db.commit();
return dto;
.setAlmRepo(randomAlphanumeric(256))
.setAlmSlug(randomAlphanumeric(256));
}
+
+ public static ProjectAlmSettingDto newAzureProjectAlmSettingDto(AlmSettingDto githubAlmSetting, ComponentDto project) {
+ return new ProjectAlmSettingDto()
+ .setAlmSettingUuid(githubAlmSetting.getUuid())
+ .setProjectUuid(project.uuid());
+ }
}
import static java.lang.String.format;
import static org.sonar.api.web.UserRole.ADMIN;
-class AlmSettingsSupport {
+public class AlmSettingsSupport {
private final DbClient dbClient;
private final UserSession userSession;
AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, almSetting);
dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, new ProjectAlmSettingDto()
.setProjectUuid(project.uuid())
- .setAlmSettingUuid(almSettingDto.getUuid()));
+ .setAlmSettingUuid(almSettingDto.getUuid())
+ .setAlmRepo(null)
+ .setAlmSlug(null));
dbSession.commit();
}
}
"key": "GitHub Server - Dev Team",
"alm": "github",
"url": "https://github.enterprise.com"
+ },
+ {
+ "key": "Azure Server - Dev Team",
+ "alm": "azure",
+ "url": "https://azure.com"
}
]
}
"key": "Azure Devops Server - Dev Team",
"personalAccessToken": "12345"
}
+ ],
+ "bitbucket": [
+ {
+ "key": "Bitbucket Server - Dev Team",
+ "url": "https://bitbucket.enterprise.com",
+ "personalAccessToken": "abcdef"
+ }
]
}
new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null))));
@Test
- public void count_binding() {
+ public void count_github_binding() {
UserDto user = db.users().insertUser();
userSession.logIn(user).setSystemAdministrator();
- AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
+ AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
ComponentDto project1 = db.components().insertPrivateProject();
ComponentDto project2 = db.components().insertPrivateProject();
- db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, project1);
- db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, project2);
+ db.almSettings().insertGitHubProjectAlmSetting(almSetting, project1);
+ db.almSettings().insertGitHubProjectAlmSetting(almSetting, project2);
CountBindingWsResponse response = ws.newRequest()
- .setParam("almSetting", githubAlmSetting.getKey())
+ .setParam("almSetting", almSetting.getKey())
.executeProtobuf(CountBindingWsResponse.class);
- assertThat(response.getKey()).isEqualTo(githubAlmSetting.getKey());
+ assertThat(response.getKey()).isEqualTo(almSetting.getKey());
assertThat(response.getProjects()).isEqualTo(2);
}
+ @Test
+ public void count_azure_binding() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
+ ComponentDto project1 = db.components().insertPrivateProject();
+ db.almSettings().insertAzureProjectAlmSetting(almSetting, project1);
+
+ CountBindingWsResponse response = ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .executeProtobuf(CountBindingWsResponse.class);
+
+ assertThat(response.getKey()).isEqualTo(almSetting.getKey());
+ assertThat(response.getProjects()).isEqualTo(1);
+ }
+
@Test
public void fail_when_alm_setting_does_not_exist() {
UserDto user = db.users().insertUser();
private WsActionTester ws = new WsActionTester(new GetBindingAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null)));
@Test
- public void get_project_binding() {
+ public void get_github_project_binding() {
UserDto user = db.users().insertUser();
ComponentDto project = db.components().insertPrivateProject();
userSession.logIn(user).addProjectPermission(ADMIN, project);
assertThat(response.getUrl()).isEqualTo(githubAlmSetting.getUrl());
}
+ @Test
+ public void get_azure_project_binding() {
+ UserDto user = db.users().insertUser();
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn(user).addProjectPermission(ADMIN, project);
+ AlmSettingDto githubAlmSetting = db.almSettings().insertAzureAlmSetting();
+ db.almSettings().insertAzureProjectAlmSetting(githubAlmSetting, project);
+
+ GetBindingWsResponse response = ws.newRequest()
+ .setParam("project", project.getKey())
+ .executeProtobuf(GetBindingWsResponse.class);
+
+ assertThat(response.getAlm()).isEqualTo(AlmSettings.Alm.azure);
+ assertThat(response.getKey()).isEqualTo(githubAlmSetting.getKey());
+ assertThat(response.hasRepository()).isFalse();
+ assertThat(response.hasUrl()).isFalse();
+ }
+
@Test
public void fail_when_project_does_not_exist() {
UserDto user = db.users().insertUser();
userSession.logIn(user).addProjectPermission(ADMIN, project);
AlmSettingDto githubAlmSetting1 = db.almSettings().insertGitHubAlmSetting();
AlmSettingDto githubAlmSetting2 = db.almSettings().insertGitHubAlmSetting();
+ AlmSettingDto azureAlmSetting = db.almSettings().insertAzureAlmSetting();
ListWsResponse response = ws.newRequest()
.setParam("project", project.getKey())
.executeProtobuf(AlmSettings.ListWsResponse.class);
assertThat(response.getAlmSettingsList())
- .extracting(AlmSetting::getAlm, AlmSetting::getKey, AlmSetting::getUrl)
+ .extracting(AlmSetting::getAlm, AlmSetting::getKey, AlmSetting::hasUrl, AlmSetting::getUrl)
.containsExactlyInAnyOrder(
- tuple(AlmSettings.Alm.github, githubAlmSetting1.getKey(), githubAlmSetting1.getUrl()),
- tuple(AlmSettings.Alm.github, githubAlmSetting2.getKey(), githubAlmSetting2.getUrl()));
+ tuple(AlmSettings.Alm.github, githubAlmSetting1.getKey(), true, githubAlmSetting1.getUrl()),
+ tuple(AlmSettings.Alm.github, githubAlmSetting2.getKey(), true, githubAlmSetting2.getUrl()),
+ tuple(AlmSettings.Alm.azure, azureAlmSetting.getKey(), false, "")
+ );
}
@Test
.setAppId("12345")
.setPrivateKey("54684654"));
db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, project, projectAlmSetting -> projectAlmSetting.setAlmRepo("team/project"));
+ AlmSettingDto azureAlmSetting = db.almSettings().insertAzureAlmSetting(
+ almSettingDto -> almSettingDto
+ .setKey("Azure Server - Dev Team")
+ .setUrl("https://azure.com")
+ .setPersonalAccessToken("abcdefg"));
+ db.almSettings().insertAzureProjectAlmSetting(azureAlmSetting, project);
String response = ws.newRequest()
.setParam("project", project.getKey())
assertThat(def.since()).isEqualTo("8.1");
assertThat(def.isPost()).isFalse();
+ assertThat(def.responseExampleAsString()).isNotEmpty();
assertThat(def.params())
.extracting(WebService.Param::key, WebService.Param::isRequired)
.containsExactlyInAnyOrder(tuple("project", true));
.setUrl("https://github.enterprise.com")
.setAppId("12345")
.setPrivateKey("54684654"));
-
db.almSettings().insertAzureAlmSetting(
a -> a.setKey("Azure Devops Server - Dev Team")
.setPersonalAccessToken("12345")
);
+ db.almSettings().insertBitbucketAlmSetting(
+ a -> a.setKey("Bitbucket Server - Dev Team")
+ .setUrl("https://bitbucket.enterprise.com")
+ .setPersonalAccessToken("abcdef")
+ );
+
String response = ws.newRequest().execute().getInput();
assertJson(response).isSimilarTo(getClass().getResource("list_definitions-example.json"));
assertThat(def.since()).isEqualTo("8.1");
assertThat(def.params()).isEmpty();
assertThat(def.isPost()).isFalse();
+ assertThat(def.responseExampleAsString()).isNotEmpty();
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.server.almsettings;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
+import static org.sonar.api.web.UserRole.ADMIN;
+import static org.sonar.api.web.UserRole.USER;
+
+public class SetAzureBindingActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private WsActionTester ws = new WsActionTester(new SetAzureBindingAction(db.getDbClient(),
+ new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null))));
+
+ @Test
+ public void set_azure_project_binding() {
+ UserDto user = db.users().insertUser();
+ AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn(user).addProjectPermission(ADMIN, project);
+
+ ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .setParam("project", project.getKey())
+ .execute();
+
+ assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), project).get())
+ .extracting(ProjectAlmSettingDto::getAlmSettingUuid, ProjectAlmSettingDto::getProjectUuid, ProjectAlmSettingDto::getAlmRepo, ProjectAlmSettingDto::getAlmSlug)
+ .containsOnly(almSetting.getUuid(), project.uuid(), null, null);
+ }
+
+ @Test
+ public void override_existing_azure_project_binding() {
+ UserDto user = db.users().insertUser();
+ AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
+ ComponentDto project = db.components().insertPrivateProject();
+ db.almSettings().insertAzureProjectAlmSetting(almSetting, project);
+ AlmSettingDto anotherAlmSetting = db.almSettings().insertAzureAlmSetting();
+ userSession.logIn(user).addProjectPermission(ADMIN, project);
+
+ ws.newRequest()
+ .setParam("almSetting", anotherAlmSetting.getKey())
+ .setParam("project", project.getKey())
+ .execute();
+
+ assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), project).get())
+ .extracting(ProjectAlmSettingDto::getAlmSettingUuid, ProjectAlmSettingDto::getProjectUuid, ProjectAlmSettingDto::getAlmRepo, ProjectAlmSettingDto::getAlmSlug)
+ .containsOnly(anotherAlmSetting.getUuid(), project.uuid(), null, null);
+ }
+
+ @Test
+ public void fail_when_alm_setting_does_not_exist() {
+ UserDto user = db.users().insertUser();
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn(user).addProjectPermission(ADMIN, project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
+
+ ws.newRequest()
+ .setParam("almSetting", "unknown")
+ .setParam("project", project.getKey())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_project_does_not_exist() {
+ UserDto user = db.users().insertUser();
+ AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn(user).addProjectPermission(ADMIN, project);
+
+ expectedException.expect(NotFoundException.class);
+
+ ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .setParam("project", "unknown")
+ .execute();
+ }
+
+ @Test
+ public void fail_when_missing_administer_permission_on_project() {
+ UserDto user = db.users().insertUser();
+ AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn(user).addProjectPermission(USER, project);
+
+ expectedException.expect(ForbiddenException.class);
+
+ ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .setParam("project", project.getKey())
+ .execute();
+ }
+
+ @Test
+ public void definition() {
+ WebService.Action def = ws.getDef();
+
+ assertThat(def.since()).isEqualTo("8.1");
+ assertThat(def.isPost()).isTrue();
+ assertThat(def.params())
+ .extracting(WebService.Param::key, WebService.Param::isRequired)
+ .containsExactlyInAnyOrder(tuple("almSetting", true), tuple("project", true));
+ }
+
+}
.setMediaType(MediaTypes.JSON)).content();
}
-
/**
* This is a POST request.
* @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/delete_binding">Further information about this action online (including a response example)</a>
* @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/get_binding">Further information about this action online (including a response example)</a>
* @since 8.1
*/
- public AlmSettings.GetBindingWsResponse getBinding(GetGithubBindingRequest request) {
+ public AlmSettings.GetBindingWsResponse getBinding(GetBindingRequest request) {
return call(
new GetRequest(path("get_binding"))
.setParam("project", request.getProject())
AlmSettings.ListDefinitionsWsResponse.parser());
}
+ /**
+ * This is a POST request.
+ * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/set_azure_binding">Further information about this action online (including a response example)</a>
+ * @since 8.1
+ */
+ public void setAzureBinding(SetAzureBindingRequest request) {
+ call(
+ new PostRequest(path("set_azure_binding"))
+ .setParam("almSetting", request.getAlmSetting())
+ .setParam("project", request.getProject())
+ .setMediaType(MediaTypes.JSON)).content();
+ }
+
/**
* This is a POST request.
* @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/set_github_binding">Further information about this action online (including a response example)</a>
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.sonarqube.ws.client.almsettings;
+
+import javax.annotation.Generated;
+
+/**
+ * This is part of the internal API.
+ * This is a POST request.
+ * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/get_github_binding">Further information about this action online (including a response example)</a>
+ * @since 8.1
+ */
+@Generated("sonar-ws-generator")
+public class GetBindingRequest {
+
+ private String project;
+
+ /**
+ * This is a mandatory parameter.
+ */
+ public GetBindingRequest setProject(String project) {
+ this.project = project;
+ return this;
+ }
+
+ public String getProject() {
+ return project;
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 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.sonarqube.ws.client.almsettings;
-
-import javax.annotation.Generated;
-
-/**
- * This is part of the internal API.
- * This is a POST request.
- * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/get_github_binding">Further information about this action online (including a response example)</a>
- * @since 8.1
- */
-@Generated("sonar-ws-generator")
-public class GetGithubBindingRequest {
-
- private String project;
-
- /**
- * This is a mandatory parameter.
- */
- public GetGithubBindingRequest setProject(String project) {
- this.project = project;
- return this;
- }
-
- public String getProject() {
- return project;
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.sonarqube.ws.client.almsettings;
+
+import javax.annotation.Generated;
+
+/**
+ * This is a POST request.
+ * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/set_azure_binding">Further information about this action online (including a response example)</a>
+ * @since 8.1
+ */
+@Generated("sonar-ws-generator")
+public class SetAzureBindingRequest {
+
+ private String almSetting;
+ private String project;
+
+ /**
+ * This is a mandatory parameter.
+ */
+ public SetAzureBindingRequest setAlmSetting(String almSetting) {
+ this.almSetting = almSetting;
+ return this;
+ }
+
+ public String getAlmSetting() {
+ return almSetting;
+ }
+
+ /**
+ * This is a mandatory parameter.
+ */
+ public SetAzureBindingRequest setProject(String project) {
+ this.project = project;
+ return this;
+ }
+
+ public String getProject() {
+ return project;
+ }
+
+}