import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.BranchType;
import org.sonar.db.component.ProjectData;
import org.sonar.db.project.ProjectDto;
assertNewCodePeriodRowCount(1);
}
+ @Test
+ public void update_referenceBranchPeriod_value() {
+ insert("1", "proj-uuid", null, REFERENCE_BRANCH, "oldBranchName", null);
+ insert("2", "proj-uuid-2", null, REFERENCE_BRANCH, "anotherBranch", null);
+
+ underTest.updateBranchReferenceValues(dbSession, new BranchDto()
+ .setBranchType(BranchType.BRANCH)
+ .setUuid("branch-uuid")
+ .setProjectUuid("proj-uuid")
+ .setKey("oldBranchName"), "newBranchName");
+
+ //second project reference branch renaming should not affect the reference branch
+ underTest.updateBranchReferenceValues(dbSession, new BranchDto()
+ .setBranchType(BranchType.BRANCH)
+ .setUuid("branch-uuid")
+ .setProjectUuid("proj-uuid-2")
+ .setKey("oldBranchName"), "newBranchName");
+
+ db.commit();
+
+ Optional<NewCodePeriodDto> updatedNewCodePeriod = underTest.selectByUuid(dbSession, "1");
+ Optional<NewCodePeriodDto> unmodifiedNewCodePeriod = underTest.selectByUuid(dbSession, "2");
+
+ assertThat(updatedNewCodePeriod).isNotNull()
+ .isNotEmpty();
+
+ assertThat(unmodifiedNewCodePeriod).isNotNull()
+ .isNotEmpty();
+
+ assertNewCodePeriodRowCount(2);
+
+ NewCodePeriodDto updatedResult = updatedNewCodePeriod.get();
+ assertThat(updatedResult.getUuid()).isEqualTo("1");
+ assertThat(updatedResult.getProjectUuid()).isEqualTo("proj-uuid");
+ assertThat(updatedResult.getBranchUuid()).isNull();
+ assertThat(updatedResult.getType()).isEqualTo(REFERENCE_BRANCH);
+ assertThat(updatedResult.getValue()).isEqualTo("newBranchName");
+
+ NewCodePeriodDto unmodifiedResult = unmodifiedNewCodePeriod.get();
+ assertThat(unmodifiedResult.getUuid()).isEqualTo("2");
+ assertThat(unmodifiedResult.getProjectUuid()).isEqualTo("proj-uuid-2");
+ assertThat(unmodifiedResult.getBranchUuid()).isNull();
+ assertThat(unmodifiedResult.getType()).isEqualTo(REFERENCE_BRANCH);
+ assertThat(unmodifiedResult.getValue()).isEqualTo("anotherBranch");
+ }
+
@Test
public void insert_with_upsert() {
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);
import org.sonar.core.util.UuidFactory;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchDto;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;
mapper(dbSession).update(dto.setUpdatedAt(system2.now()));
}
+ public void updateBranchReferenceValues(DbSession dbSession, BranchDto branchDto, String newBranchName) {
+ requireNonNull(branchDto, "Original referenced branch must be specified.");
+ requireNonNull(branchDto.getProjectUuid(), MSG_PROJECT_UUID_NOT_SPECIFIED);
+ requireNonNull(newBranchName, "New branch name must be specified.");
+ selectAllByProject(dbSession, branchDto.getProjectUuid()).stream()
+ .filter(newCP -> NewCodePeriodType.REFERENCE_BRANCH.equals(newCP.getType()) && branchDto.getBranchKey().equals(newCP.getValue()))
+ .forEach(newCodePeriodDto -> update(dbSession, newCodePeriodDto.setValue(newBranchName)));
+ }
+
public Optional<NewCodePeriodDto> selectByProject(DbSession dbSession, String projectUuid) {
requireNonNull(projectUuid, MSG_PROJECT_UUID_NOT_SPECIFIED);
return ofNullable(mapper(dbSession).selectByProject(projectUuid));
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ResourceTypesRule;
+import org.sonar.db.newcodeperiod.NewCodePeriodDto;
+import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
assertThat(unchangedBranch.get().getKey()).isEqualTo("branch");
}
+ @Test
+ public void whenRenameIsTriggered_eventualBranchReferencesPeriods_areUpdatedToo() {
+ userSession.logIn();
+ ProjectDto project = db.components().insertPublicProject().getProjectDto();
+ db.newCodePeriods().insert(getBranchReferenceNewCodePeriod(project));
+ userSession.addProjectPermission(UserRole.ADMIN, project);
+
+ tester.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("name", "newBranchName")
+ .execute();
+
+ assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
+ Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid());
+ assertThat(mainBranch.get().getKey()).isEqualTo("newBranchName");
+
+ Optional<NewCodePeriodDto> updatedCodeReference = db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), project.getUuid());
+ assertThat(updatedCodeReference.get().getValue()).isEqualTo("newBranchName");
+ }
+
+ private NewCodePeriodDto getBranchReferenceNewCodePeriod(ProjectDto project) {
+ Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid());
+ return new NewCodePeriodDto()
+ .setProjectUuid(project.getUuid())
+ .setValue(mainBranch.get().getBranchKey())
+ .setType(NewCodePeriodType.REFERENCE_BRANCH);
+ }
+
@Test
public void rename_with_same_name() {
userSession.logIn();
checkPermission(project);
Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(dbSession, project.getUuid(), newBranchName);
- checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(),
+ checkArgument(existingBranch.filter(b -> !b.isMain()).isEmpty(),
"Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName);
BranchDto mainBranchDto = dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, project.getUuid())
.orElseThrow(() -> new NotFoundException("Cannot find main branch for project: " + project.getUuid()));
dbClient.branchDao().updateBranchName(dbSession, mainBranchDto.getUuid(), newBranchName);
+
+ dbClient.newCodePeriodDao().updateBranchReferenceValues(dbSession, mainBranchDto, newBranchName);
+
dbSession.commit();
response.noContent();
}