checkPermission(userSession, project.key());
QualityProfileDto qualityProfile = findNotNull(profileKey, session);
- db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
- session.commit();
+ QualityProfileDto currentProfile = db.qualityProfileDao().getByProjectAndLanguage(project.key(), qualityProfile.getLanguage(), session);
+
+ boolean updated = false;
+ if (currentProfile == null) {
+ db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
+ updated = true;
+ } else if (!profileKey.equals(currentProfile.getKey())) {
+ db.qualityProfileDao().updateProjectProfileAssociation(projectUuid, profileKey, session);
+ updated = true;
+ }
+ if (updated) {
+ session.commit();
+ }
}
public void removeProject(String profileKey, String projectUuid, UserSession userSession) {
void define(WebService.NewController controller) {
NewAction addProject = controller.createAction("add_project")
.setSince("5.2")
- .setDescription("Associate a project with a quality profile")
+ .setDescription("Associate a project with a quality profile.")
.setHandler(new AssociationHandler(profileLookup, componentService) {
@Override
protected void changeAssociation(String profileKey, String projectUuid, UserSession userSession) {
NewAction removeProject = controller.createAction("remove_project")
.setSince("5.2")
- .setDescription("Remove a project's association with a quality profile")
+ .setDescription("Remove a project's association with a quality profile.")
.setHandler(new AssociationHandler(profileLookup, componentService) {
@Override
protected void changeAssociation(String profileKey, String projectUuid, UserSession userSession) {
private void setCommonAttributes(NewAction action) {
action.setPost(true);
action.createParam(PARAM_PROJECT_UUID)
- .setDescription("A project UUID. Either this parameter, or projectKey must be set");
+ .setDescription("A project UUID. Either this parameter, or projectKey must be set.");
action.createParam(PARAM_PROJECT_KEY)
- .setDescription("A project key. Either this parameter, or projectUuid must be set");
+ .setDescription("A project key. Either this parameter, or projectUuid must be set.");
action.createParam(PARAM_PROFILE_KEY)
- .setDescription("A quality profile key. Either this parameter, or a combination of profileName + language must be set");
+ .setDescription("A quality profile key. Either this parameter, or a combination of profileName + language must be set.");
action.createParam(PARAM_PROFILE_NAME)
- .setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate");
+ .setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate.");
action.createParam(PARAM_LANGUAGE)
- .setDescription("A quality profile language. If this parameter is set, profileKey must not be set and language must be set to disambiguate")
+ .setDescription("A quality profile language. If this parameter is set, profileKey must not be set and profileName must be set to disambiguate.")
.setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new NonNullInputFunction<Language, String>() {
@Override
public String doApply(Language input) {
}));
}
- private static abstract class AssociationHandler implements RequestHandler {
+ private abstract static class AssociationHandler implements RequestHandler {
private final QProfileLookup profileLookup;
private final ComponentService componentService;
.setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid())
.execute().assertNoContent();
assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
+
+ // Second call must not fail, do nothing
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid())
+ .execute().assertNoContent();
+ assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
+ }
+
+ @Test
+ public void change_project_association_with_key_and_uuid() throws Exception {
+ ComponentDto project = new ComponentDto()
+ .setId(1L)
+ .setUuid("ABCD")
+ .setKey("org.codehaus.sonar:sonar")
+ .setName("SonarQube")
+ .setLongName("SonarQube")
+ .setQualifier("TRK")
+ .setScope("TRK")
+ .setEnabled(true);
+ db.componentDao().insert(session, project);
+ QualityProfileDto profile1 = QProfileTesting.newXooP1();
+ QualityProfileDto profile2 = QProfileTesting.newXooP2();
+ db.qualityProfileDao().insert(session, profile1, profile2);
+ db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), profile1.getKey(), session);
+
+ session.commit();
+
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("profileKey", profile2.getKee()).setParam("projectUuid", project.uuid())
+ .execute().assertNoContent();
+ assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile2.getKee());
}
@Test
session.getMapper(QualityProfileMapper.class).deleteProjectProfileAssociation(projectUuid, profileKey);
}
+ public void updateProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) {
+ session.getMapper(QualityProfileMapper.class).updateProjectProfileAssociation(projectUuid, profileKey);
+ }
+
public void deleteAllProjectProfileAssociation(String profileKey, DbSession session) {
session.getMapper(QualityProfileMapper.class).deleteAllProjectProfileAssociation(profileKey);
}
void insertProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
+ void updateProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
+
void deleteProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
void deleteAllProjectProfileAssociation(@Param("profileKey") String profileKey);
INSERT INTO project_qprofiles (project_uuid, profile_key) VALUES (#{projectUuid}, #{profileKey})
</insert>
+ <update id="updateProjectProfileAssociation">
+ UPDATE project_qprofiles SET profile_key=#{profileKey} WHERE project_uuid=#{projectUuid}
+ </update>
+
<update id="deleteProjectProfileAssociation">
DELETE FROM project_qprofiles WHERE project_uuid=#{projectUuid} AND profile_key=#{profileKey}
</update>