diff options
author | Benjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com> | 2023-03-24 10:01:17 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-04-04 20:03:16 +0000 |
commit | fd3fe6eea8c85fba6f6cd6fa178e67e3480157ac (patch) | |
tree | 9914040867f12b96ade57e09e330178f2614de29 | |
parent | 856e6efc940b2bc2d0f104a319e93508c88e3315 (diff) | |
download | sonarqube-fd3fe6eea8c85fba6f6cd6fa178e67e3480157ac.tar.gz sonarqube-fd3fe6eea8c85fba6f6cd6fa178e67e3480157ac.zip |
SONAR-18850 update branch mapper to use is main and update test framework
17 files changed, 106 insertions, 51 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IgnoreOrphanBranchStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IgnoreOrphanBranchStepIT.java index 1ee3b62296f..b2e3feff4ba 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IgnoreOrphanBranchStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IgnoreOrphanBranchStepIT.java @@ -55,6 +55,7 @@ public class IgnoreOrphanBranchStepIT { BranchDto branchDto = new BranchDto() .setBranchType(BRANCH) .setKey("branchName") + .setIsMain(false) .setUuid(BRANCH_UUID) .setProjectUuid("project_uuid") .setNeedIssueSync(true); @@ -75,6 +76,7 @@ public class IgnoreOrphanBranchStepIT { .setKey("branchName") .setUuid(BRANCH_UUID) .setProjectUuid("project_uuid") + .setIsMain(false) .setNeedIssueSync(false); dbClient.branchDao().insert(dbTester.getSession(), branchDto); dbTester.commit(); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IndexIssuesStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IndexIssuesStepIT.java index 4264f718c7f..fbd85186c05 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IndexIssuesStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IndexIssuesStepIT.java @@ -72,6 +72,7 @@ public class IndexIssuesStepIT { .setKey("branchName") .setUuid(BRANCH_UUID) .setProjectUuid("project_uuid") + .setIsMain(false) .setNeedIssueSync(true); dbClient.branchDao().insert(dbTester.getSession(), branchDto); dbTester.commit(); @@ -90,6 +91,7 @@ public class IndexIssuesStepIT { .setKey("branchName") .setUuid(BRANCH_UUID) .setProjectUuid("project_uuid") + .setIsMain(false) .setNeedIssueSync(false); dbClient.branchDao().insert(dbTester.getSession(), branchDto); dbTester.commit(); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/branches/ExportBranchesStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/branches/ExportBranchesStepIT.java index be7dd483a07..c09be119ba2 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/branches/ExportBranchesStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/branches/ExportBranchesStepIT.java @@ -80,24 +80,28 @@ public class ExportBranchesStepIT { .setKey("branch-1") .setUuid("branch-1-uuid") .setMergeBranchUuid("master") + .setIsMain(false) .setExcludeFromPurge(true), new BranchDto() .setBranchType(BranchType.PULL_REQUEST) .setProjectUuid(PROJECT_UUID) .setKey("branch-3") .setUuid("branch-3-uuid") + .setIsMain(false) .setMergeBranchUuid("master"), new BranchDto() .setBranchType(BranchType.BRANCH) .setProjectUuid(PROJECT_UUID) .setKey("branch-4") .setUuid("branch-4-uuid") + .setIsMain(false) .setMergeBranchUuid("branch-1-uuid"), new BranchDto() .setBranchType(BranchType.BRANCH) .setProjectUuid(PROJECT_UUID) .setKey("branch-5") .setUuid("branch-5-uuid") + .setIsMain(false) .setMergeBranchUuid("master") .setExcludeFromPurge(true)); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java index 15902ec685d..902c5d32577 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java @@ -70,11 +70,11 @@ public class ExportAdHocRulesStepIT { .setBranchUuid(PROJECT_UUID); private static final List<BranchDto> BRANCHES = ImmutableList.of( - new BranchDto().setBranchType(BranchType.PULL_REQUEST).setProjectUuid(PROJECT_UUID).setKey("pr-1").setUuid("pr-1-uuid").setMergeBranchUuid("master"), + new BranchDto().setBranchType(BranchType.PULL_REQUEST).setProjectUuid(PROJECT_UUID).setKey("pr-1").setUuid("pr-1-uuid").setMergeBranchUuid("master").setIsMain(false), new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(PROJECT_UUID).setKey("branch-2").setUuid("branch-2-uuid").setMergeBranchUuid("master") - .setExcludeFromPurge(true), + .setExcludeFromPurge(true).setIsMain(false), new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(PROJECT_UUID).setKey("branch-3").setUuid("branch-3-uuid").setMergeBranchUuid("master") - .setExcludeFromPurge(false)); + .setExcludeFromPurge(false).setIsMain(false)); @Rule public LogTester logTester = new LogTester(); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportNewCodePeriodsStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportNewCodePeriodsStepIT.java index 3d9697d5e01..49abb861511 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportNewCodePeriodsStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportNewCodePeriodsStepIT.java @@ -66,15 +66,15 @@ public class ExportNewCodePeriodsStepIT { .setKey("another_project"); private static final List<BranchDto> PROJECT_BRANCHES = ImmutableList.of( - new BranchDto().setBranchType(BranchType.PULL_REQUEST).setProjectUuid(PROJECT_UUID).setKey("pr-1").setUuid("pr-uuid-1").setMergeBranchUuid("master"), + new BranchDto().setBranchType(BranchType.PULL_REQUEST).setProjectUuid(PROJECT_UUID).setKey("pr-1").setUuid("pr-uuid-1").setMergeBranchUuid("master").setIsMain(false), new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(PROJECT_UUID).setKey("branch-1").setUuid("branch-uuid-1").setMergeBranchUuid("master") - .setExcludeFromPurge(true), + .setExcludeFromPurge(true).setIsMain(false), new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(PROJECT_UUID).setKey("branch-2").setUuid("branch-uuid-2").setMergeBranchUuid("master") - .setExcludeFromPurge(false)); + .setExcludeFromPurge(false).setIsMain(false)); private static final List<BranchDto> ANOTHER_PROJECT_BRANCHES = ImmutableList.of( new BranchDto().setBranchType(BranchType.BRANCH).setProjectUuid(ANOTHER_PROJECT_UUID).setKey("branch-3").setUuid("branch-uuid-3").setMergeBranchUuid("master") - .setExcludeFromPurge(true)); + .setExcludeFromPurge(true).setIsMain(false)); @Rule public LogTester logTester = new LogTester(); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImpl.java index 0c301127ee6..dd19d82e2b5 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImpl.java @@ -82,6 +82,7 @@ public class BranchPersisterImpl implements BranchPersister { // MainBranchProjectUuid will be null if it's a main branch String projectUuid = firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.branchUuid()); + dto.setIsMain(componentDto.uuid().equals(projectUuid)); dto.setProjectUuid(projectUuid); dto.setBranchType(branch.getType()); dto.setExcludeFromPurge(excludeFromPurge); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ApplicationProjectsDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ApplicationProjectsDaoIT.java index deef40f73ca..b9b86dad881 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ApplicationProjectsDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ApplicationProjectsDaoIT.java @@ -161,7 +161,8 @@ public class ApplicationProjectsDaoIT { "kee", branchKey, "NEED_ISSUE_SYNC", true, "updated_at", 1000L, - "created_at", 1000L); + "created_at", 1000L, + "is_main", false); } } diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java index 4d4fefa5a99..7c38636cfcb 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java @@ -50,6 +50,7 @@ import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; +import static org.assertj.core.api.Assertions.linesOf; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.db.component.BranchType.BRANCH; import static org.sonar.db.component.BranchType.PULL_REQUEST; @@ -58,9 +59,10 @@ import static org.sonar.db.component.BranchType.PULL_REQUEST; public class BranchDaoIT { private static final long NOW = 1_000L; - private static final String SELECT_FROM = "select project_uuid as \"projectUuid\", uuid as \"uuid\", branch_type as \"branchType\", " + - "kee as \"kee\", merge_branch_uuid as \"mergeBranchUuid\", pull_request_binary as \"pullRequestBinary\", created_at as \"createdAt\", updated_at as \"updatedAt\" " + - "from project_branches "; + private static final String SELECT_FROM = """ + select project_uuid as "projectUuid", uuid as "uuid", branch_type as "branchType", + kee as "kee", merge_branch_uuid as "mergeBranchUuid", pull_request_binary as "pullRequestBinary", created_at as "createdAt", updated_at as "updatedAt", is_main as "isMain" + from project_branches"""; private System2 system2 = new TestSystem2().setNow(NOW); @Rule @@ -75,6 +77,7 @@ public class BranchDaoIT { dto.setProjectUuid("U1"); dto.setUuid("U2"); dto.setBranchType(BranchType.BRANCH); + dto.setIsMain(true); dto.setKey("feature/foo"); underTest.insert(dbSession, dto); @@ -85,6 +88,7 @@ public class BranchDaoIT { entry("uuid", "U2"), entry("branchType", "BRANCH"), entry("kee", "feature/foo"), + entry("isMain", true), entry("mergeBranchUuid", null), entry("pullRequestBinary", null), entry("createdAt", 1_000L), @@ -96,6 +100,7 @@ public class BranchDaoIT { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U1"); + dto.setIsMain(true); dto.setBranchType(BranchType.BRANCH); dto.setKey("feature"); underTest.insert(dbSession, dto); @@ -103,15 +108,19 @@ public class BranchDaoIT { BranchDto dto2 = new BranchDto(); dto2.setProjectUuid("U2"); dto2.setUuid("U2"); + dto2.setIsMain(true); dto2.setBranchType(BranchType.BRANCH); dto2.setKey("branch"); underTest.insert(dbSession, dto2); + int a = 12124; + underTest.updateBranchName(dbSession, "U1", "master"); BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "master").get(); assertThat(loaded.getMergeBranchUuid()).isNull(); assertThat(loaded.getProjectUuid()).isEqualTo("U1"); assertThat(loaded.getBranchType()).isEqualTo(BranchType.BRANCH); + assertThat(loaded.isMain()).isTrue(); } @Test @@ -121,6 +130,7 @@ public class BranchDaoIT { dto.setUuid("U1"); dto.setBranchType(BranchType.BRANCH); dto.setKey("feature"); + dto.setIsMain(true); dto.setExcludeFromPurge(false); underTest.insert(dbSession, dto); @@ -159,6 +169,7 @@ public class BranchDaoIT { BranchDto dto = new BranchDto(); dto.setProjectUuid(repeat("a", 50)); dto.setUuid(repeat("b", 50)); + dto.setIsMain(false); dto.setBranchType(BranchType.BRANCH); dto.setKey(repeat("c", 255)); dto.setMergeBranchUuid(repeat("d", 50)); @@ -182,6 +193,7 @@ public class BranchDaoIT { BranchDto dto = new BranchDto(); dto.setProjectUuid(projectUuid); dto.setUuid(uuid); + dto.setIsMain(false); dto.setBranchType(branchType); dto.setKey(kee); @@ -191,6 +203,7 @@ public class BranchDaoIT { assertThat(loaded.getProjectUuid()).isEqualTo(projectUuid); assertThat(loaded.getUuid()).isEqualTo(uuid); + assertThat(loaded.isMain()).isFalse(); assertThat(loaded.getBranchType()).isEqualTo(branchType); assertThat(loaded.getKey()).isEqualTo(kee); assertThat(loaded.getMergeBranchUuid()).isNull(); @@ -219,6 +232,7 @@ public class BranchDaoIT { BranchDto dto = new BranchDto(); dto.setProjectUuid(projectUuid); dto.setUuid(uuid); + dto.setIsMain(false); dto.setBranchType(branchType); dto.setKey(kee); dto.setPullRequestData(pullRequestData); @@ -246,6 +260,7 @@ public class BranchDaoIT { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U2"); + dto.setIsMain(false); dto.setBranchType(BranchType.BRANCH); dto.setKey("foo"); underTest.insert(dbSession, dto); @@ -271,6 +286,7 @@ public class BranchDaoIT { dto.setUuid("U2"); dto.setBranchType(BranchType.PULL_REQUEST); dto.setKey("foo"); + dto.setIsMain(false); underTest.insert(dbSession, dto); // the fields that can be updated @@ -303,8 +319,7 @@ public class BranchDaoIT { assertThat(loadedPullRequestData).isNotNull(); assertThat(loadedPullRequestData.getBranch()).isEqualTo(branch); assertThat(loadedPullRequestData.getTitle()).isEqualTo(title); - assertThat(loadedPullRequestData.getUrl()).isEqualTo(url); - assertThat(loadedPullRequestData.getAttributesMap()).containsEntry(tokenAttributeName, tokenAttributeValue); + assertThat(loadedPullRequestData.getUrl()).isEqualTo(url); assertThat(loadedPullRequestData.getAttributesMap()).containsEntry(tokenAttributeName, tokenAttributeValue); } @Test @@ -312,6 +327,7 @@ public class BranchDaoIT { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U2"); + dto.setIsMain(false); dto.setBranchType(BranchType.PULL_REQUEST); dto.setKey("foo"); @@ -365,6 +381,7 @@ public class BranchDaoIT { BranchDto mainBranch = new BranchDto(); mainBranch.setProjectUuid("U1"); mainBranch.setUuid("U1"); + mainBranch.setIsMain(true); mainBranch.setBranchType(BranchType.BRANCH); mainBranch.setKey("master"); underTest.insert(dbSession, mainBranch); @@ -372,6 +389,7 @@ public class BranchDaoIT { BranchDto featureBranch = new BranchDto(); featureBranch.setProjectUuid("U1"); featureBranch.setUuid("U2"); + featureBranch.setIsMain(false); featureBranch.setBranchType(BranchType.BRANCH); featureBranch.setKey("feature/foo"); featureBranch.setMergeBranchUuid("U3"); @@ -381,6 +399,7 @@ public class BranchDaoIT { BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "feature/foo").get(); assertThat(loaded.getUuid()).isEqualTo(featureBranch.getUuid()); assertThat(loaded.getKey()).isEqualTo(featureBranch.getKey()); + assertThat(loaded.isMain()).isFalse(); assertThat(loaded.getProjectUuid()).isEqualTo(featureBranch.getProjectUuid()); assertThat(loaded.getBranchType()).isEqualTo(featureBranch.getBranchType()); assertThat(loaded.getMergeBranchUuid()).isEqualTo(featureBranch.getMergeBranchUuid()); @@ -414,6 +433,7 @@ public class BranchDaoIT { BranchDto mainBranch = new BranchDto(); mainBranch.setProjectUuid("U1"); mainBranch.setUuid("U1"); + mainBranch.setIsMain(true); mainBranch.setBranchType(BranchType.BRANCH); mainBranch.setKey("master"); underTest.insert(dbSession, mainBranch); @@ -421,6 +441,7 @@ public class BranchDaoIT { BranchDto featureBranch = new BranchDto(); featureBranch.setProjectUuid("U1"); featureBranch.setUuid("U2"); + featureBranch.setIsMain(false); featureBranch.setBranchType(BranchType.BRANCH); featureBranch.setKey("feature/foo"); featureBranch.setMergeBranchUuid("U3"); @@ -433,22 +454,25 @@ public class BranchDaoIT { assertThat(branches).hasSize(2); - assertThat(branches).extracting(BranchDto::getUuid, BranchDto::getKey, BranchDto::getProjectUuid, BranchDto::getBranchType, BranchDto::getMergeBranchUuid) - .containsOnly(tuple(mainBranch.getUuid(), mainBranch.getKey(), mainBranch.getProjectUuid(), mainBranch.getBranchType(), mainBranch.getMergeBranchUuid()), - tuple(featureBranch.getUuid(), featureBranch.getKey(), featureBranch.getProjectUuid(), featureBranch.getBranchType(), featureBranch.getMergeBranchUuid())); + assertThat(branches).extracting(BranchDto::getUuid, BranchDto::getKey,BranchDto::isMain, BranchDto::getProjectUuid, BranchDto::getBranchType, BranchDto::getMergeBranchUuid) + .containsOnly(tuple(mainBranch.getUuid(), mainBranch.getKey(),mainBranch.isMain(), mainBranch.getProjectUuid(), mainBranch.getBranchType(), mainBranch.getMergeBranchUuid()), + tuple(featureBranch.getUuid(), featureBranch.getKey(), featureBranch.isMain(), featureBranch.getProjectUuid(), featureBranch.getBranchType(), featureBranch.getMergeBranchUuid())); } @Test - public void selectByPullRequestKey() { + public void + selectByPullRequestKey() { BranchDto mainBranch = new BranchDto(); mainBranch.setProjectUuid("U1"); mainBranch.setUuid("U1"); mainBranch.setBranchType(BranchType.BRANCH); mainBranch.setKey("master"); + mainBranch.setIsMain(true); underTest.insert(dbSession, mainBranch); String pullRequestId = "123"; BranchDto pullRequest = new BranchDto(); + pullRequest.setIsMain(false); pullRequest.setProjectUuid("U1"); pullRequest.setUuid("U2"); pullRequest.setBranchType(BranchType.PULL_REQUEST); @@ -473,6 +497,7 @@ public class BranchDaoIT { BranchDto mainBranch = new BranchDto() .setProjectUuid("U1") .setUuid("U1") + .setIsMain(true) .setBranchType(BranchType.BRANCH) .setKey("master"); underTest.insert(dbSession, mainBranch); @@ -480,6 +505,7 @@ public class BranchDaoIT { BranchDto featureBranch = new BranchDto() .setProjectUuid("U1") .setUuid("U2") + .setIsMain(false) .setBranchType(BranchType.BRANCH) .setKey("feature1"); underTest.insert(dbSession, featureBranch); @@ -488,6 +514,7 @@ public class BranchDaoIT { BranchDto pullRequest = new BranchDto() .setProjectUuid("U1") .setUuid("U3") + .setIsMain(false) .setBranchType(BranchType.PULL_REQUEST) .setKey(pullRequestId) .setMergeBranchUuid("U4"); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index 47f340a63ca..274ee804fc7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -42,7 +42,8 @@ public class BranchDao implements Dao { } public void insert(DbSession dbSession, BranchDto dto) { - mapper(dbSession).insert(dto, system2.now()); + BranchMapper mapper = mapper(dbSession); + mapper.insert(dto, system2.now()); } public void upsert(DbSession dbSession, BranchDto dto) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java index dc2fa866a2e..3c03afa23b0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java @@ -91,6 +91,8 @@ public class BranchDto { private boolean needIssueSync = false; + private Boolean isMain; + public String getUuid() { return uuid; } @@ -110,7 +112,12 @@ public class BranchDto { } public boolean isMain() { - return projectUuid.equals(uuid); + return isMain; + } + + public BranchDto setIsMain(boolean isMain) { + this.isMain = isMain; + return this; } /** @@ -226,6 +233,7 @@ public class BranchDto { BranchDto branchDto = (BranchDto) o; return Objects.equals(uuid, branchDto.uuid) && Objects.equals(projectUuid, branchDto.projectUuid) && + Objects.equals(isMain, branchDto.isMain) && Objects.equals(kee, branchDto.kee) && branchType == branchDto.branchType && Objects.equals(mergeBranchUuid, branchDto.mergeBranchUuid) && @@ -234,7 +242,7 @@ public class BranchDto { @Override public int hashCode() { - return Objects.hash(uuid, projectUuid, kee, branchType, mergeBranchUuid, needIssueSync); + return Objects.hash(uuid, projectUuid, isMain, kee, branchType, mergeBranchUuid, needIssueSync); } @Override @@ -242,6 +250,7 @@ public class BranchDto { return "BranchDto{" + "uuid='" + uuid + '\'' + ", projectUuid='" + projectUuid + '\'' + + ", isMain='" + isMain + '\'' + ", kee='" + kee + '\'' + ", branchType=" + branchType + ", mergeBranchUuid='" + mergeBranchUuid + '\'' + diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml index 0217e510910..254ef938291 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml @@ -14,7 +14,8 @@ pb.merge_branch_uuid as mergeBranchUuid, pb.pull_request_binary as pullRequestBinary, pb.exclude_from_purge as excludeFromPurge, - pb.need_issue_sync as needIssueSync + pb.need_issue_sync as needIssueSync, + pb.is_main as isMain </sql> <sql id="projectColumns"> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml index 4f981d9aafe..7c42f968e1c 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml @@ -10,7 +10,8 @@ pb.merge_branch_uuid as mergeBranchUuid, pb.pull_request_binary as pullRequestBinary, pb.exclude_from_purge as excludeFromPurge, - pb.need_issue_sync as needIssueSync + pb.need_issue_sync as needIssueSync, + pb.is_main as isMain </sql> <insert id="insert" parameterType="map" useGeneratedKeys="false"> @@ -24,7 +25,8 @@ created_at, updated_at, exclude_from_purge, - need_issue_sync + need_issue_sync, + is_main ) values ( #{dto.uuid, jdbcType=VARCHAR}, #{dto.projectUuid, jdbcType=VARCHAR}, @@ -35,7 +37,8 @@ #{now, jdbcType=BIGINT}, #{now, jdbcType=BIGINT}, #{dto.excludeFromPurge, jdbcType=BOOLEAN}, - #{dto.needIssueSync, jdbcType=BOOLEAN} + #{dto.needIssueSync, jdbcType=BOOLEAN}, + #{dto.isMain, jdbcType=BOOLEAN} ) </insert> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDtoTest.java index 6e194f8cbfc..41b4091b54b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDtoTest.java @@ -29,38 +29,24 @@ public class BranchDtoTest { private final BranchDto underTest = new BranchDto(); @Test - public void isMain_is_true_if_branch_uuid_equals_project_uuid() { - underTest.setProjectUuid("U1"); - underTest.setUuid("U1"); - - assertThat(underTest.isMain()).isTrue(); - } - - @Test - public void isMain_is_false_if_branch_uuid_does_not_equal_project_uuid() { - underTest.setProjectUuid("U1"); - underTest.setUuid("U2"); - - assertThat(underTest.isMain()).isFalse(); - } - - @Test - public void verify_equals() { + public void verify_toString() { underTest.setUuid("U1"); underTest.setProjectUuid("U2"); + underTest.setIsMain(false); underTest.setKey("K1"); underTest.setBranchType(BranchType.BRANCH); underTest.setMergeBranchUuid("U3"); underTest.setExcludeFromPurge(true); assertThat(underTest).hasToString("BranchDto{uuid='U1', " + - "projectUuid='U2', kee='K1', branchType=BRANCH, mergeBranchUuid='U3', excludeFromPurge=true, needIssueSync=false}"); + "projectUuid='U2', isMain='false', kee='K1', branchType=BRANCH, mergeBranchUuid='U3', excludeFromPurge=true, needIssueSync=false}"); } @Test - public void verify_toString() { + public void verify_equals() { underTest.setUuid("U1"); underTest.setProjectUuid("U2"); + underTest.setIsMain(true); underTest.setKey("K1"); underTest.setBranchType(BranchType.BRANCH); underTest.setMergeBranchUuid("U3"); @@ -69,6 +55,7 @@ public class BranchDtoTest { toCompare.setUuid("U1"); toCompare.setProjectUuid("U2"); + toCompare.setIsMain(true); toCompare.setKey("K1"); toCompare.setBranchType(BranchType.BRANCH); toCompare.setMergeBranchUuid("U3"); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java index 4665ff38e84..b27e43e05a0 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java @@ -405,6 +405,7 @@ public class ComponentDbTester { BranchDto branchDto = ComponentTesting.newBranchDto(component, BRANCH); branchDto.setExcludeFromPurge(true); branchPopulator.accept(branchDto); + branchDto.setIsMain(true); dbClient.branchDao().insert(dbSession, branchDto); db.commit(); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java index 37631297384..93c2c4417e6 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java @@ -181,11 +181,13 @@ public class ComponentTesting { .setPrivate(project.isPrivate()); } + public static BranchDto newBranchDto(@Nullable String projectUuid, BranchType branchType) { String key = projectUuid == null ? null : "branch_" + randomAlphanumeric(248); return new BranchDto() .setKey(key) .setUuid(Uuids.createFast()) + .setIsMain(false) // MainBranchProjectUuid will be null if it's a main branch .setProjectUuid(projectUuid) .setBranchType(branchType); @@ -195,6 +197,10 @@ public class ComponentTesting { return newBranchDto(project.branchUuid(), BranchType.BRANCH); } + public static BranchDto newBranchDto(ComponentDto project, boolean isMain) { + return newBranchDto(project.branchUuid(), BranchType.BRANCH); + } + public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType) { boolean isMain = branchComponent.getMainBranchProjectUuid() == null; String projectUuid = isMain ? branchComponent.uuid() : branchComponent.getMainBranchProjectUuid(); @@ -202,6 +208,7 @@ public class ComponentTesting { return new BranchDto() .setKey(key) + .setIsMain(isMain) .setUuid(branchComponent.uuid()) .setProjectUuid(projectUuid) .setBranchType(branchType); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java index f1aa41c491b..aab8cb22a0a 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java @@ -86,7 +86,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(BRANCH) .setKey("branchName") .setUuid("branch_uuid") - .setProjectUuid("project_uuid"); + .setProjectUuid("project_uuid") + .setIsMain(false); dbClient.branchDao().insert(dbTester.getSession(), dto); dbTester.commit(); @@ -108,7 +109,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(BRANCH) .setKey("branchName") .setUuid("branch_uuid") - .setProjectUuid(projectDto.getUuid()); + .setProjectUuid(projectDto.getUuid()) + .setIsMain(true); dbTester.components().insertProjectBranch(projectDto, dto); underTest.triggerForProject(projectDto.getUuid()); @@ -218,7 +220,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(BRANCH) .setKey("branch_1") .setUuid("branch_uuid1") - .setProjectUuid("project_uuid1"); + .setProjectUuid("project_uuid1") + .setIsMain(false); dbClient.branchDao().insert(dbTester.getSession(), dto); dbTester.commit(); insertSnapshot("analysis_1", "project_uuid1", 1); @@ -227,7 +230,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(BRANCH) .setKey("branch_2") .setUuid("branch_uuid2") - .setProjectUuid("project_uuid2"); + .setProjectUuid("project_uuid2") + .setIsMain(false); dbClient.branchDao().insert(dbTester.getSession(), dto2); dbTester.commit(); insertSnapshot("analysis_2", "project_uuid2", 2); @@ -257,7 +261,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(BRANCH) .setKey("branch_1") .setUuid("branch_uuid1") - .setProjectUuid("project_uuid1"); + .setProjectUuid("project_uuid1") + .setIsMain(false); dbClient.branchDao().insert(dbTester.getSession(), dto); dbTester.commit(); insertSnapshot("analysis_1", "project_uuid1", 1); @@ -266,7 +271,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(PULL_REQUEST) .setKey("pr_1") .setUuid("pr_uuid_1") - .setProjectUuid("project_uuid2"); + .setProjectUuid("project_uuid2") + .setIsMain(false); dbClient.branchDao().insert(dbTester.getSession(), dto2); dbTester.commit(); insertSnapshot("analysis_2", "project_uuid2", 2); @@ -307,7 +313,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(BRANCH) .setKey("branch_" + i) .setUuid("branch_uuid" + i) - .setProjectUuid("project_uuid" + i); + .setProjectUuid("project_uuid" + i) + .setIsMain(false); dbClient.branchDao().insert(dbTester.getSession(), dto); dbTester.commit(); insertSnapshot("analysis_" + i, "project_uuid" + i, 1); @@ -318,7 +325,8 @@ public class AsyncIssueIndexingImplTest { .setBranchType(BRANCH) .setKey("branch_" + i) .setUuid("branch_uuid" + i) - .setProjectUuid("project_uuid" + i); + .setProjectUuid("project_uuid" + i) + .setIsMain(false); dbClient.branchDao().insert(dbTester.getSession(), dto); dbTester.commit(); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java index acf416b7ba2..775cf2874ce 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -213,6 +213,7 @@ public class ComponentUpdater { BranchDto branch = new BranchDto() .setBranchType(BranchType.BRANCH) .setUuid(componentUuid) + .setIsMain(true) .setKey(Optional.ofNullable(mainBranch).orElse(defaultBranchNameResolver.getEffectiveMainBranchName())) .setMergeBranchUuid(null) .setExcludeFromPurge(true) |