From: Julien Lancelot Date: Thu, 3 Aug 2017 13:41:24 +0000 (+0200) Subject: SONAR-9616 Handle branch in batch/issues X-Git-Tag: 6.6-RC1~380^2~129 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e31a01aedd82182ca124964325ddf73e32c71f12;p=sonarqube.git SONAR-9616 Handle branch in batch/issues --- 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 f3a71679f55..387d91d0ae1 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 @@ -97,16 +97,18 @@ public class BranchDto { return uuid; } - public void setUuid(String s) { + public BranchDto setUuid(String s) { this.uuid = s; + return this; } public String getProjectUuid() { return projectUuid; } - public void setProjectUuid(String s) { + public BranchDto setProjectUuid(String s) { this.projectUuid = s; + return this; } public boolean isMain() { @@ -117,8 +119,9 @@ public class BranchDto { return keeType; } - public void setKeeType(BranchKeyType t) { + public BranchDto setKeeType(BranchKeyType t) { this.keeType = t; + return this; } /** @@ -142,10 +145,11 @@ public class BranchDto { this.kee = s; } - public void setKey(@Nullable String s) { + public BranchDto setKey(@Nullable String s) { checkArgument(s == null || s.length() <= KEE_MAX_LENGTH, "Maximum length of branch name or pull request id is %s: %s", KEE_MAX_LENGTH, s); checkArgument(!NULL_KEY.equals(s), "Branch name is not allowed: %s", s); setKee(convertKeyToDb(s)); + return this; } @Nullable @@ -153,8 +157,9 @@ public class BranchDto { return branchType; } - public void setBranchType(@Nullable BranchType b) { + public BranchDto setBranchType(@Nullable BranchType b) { this.branchType = b; + return this; } @Nullable @@ -162,8 +167,9 @@ public class BranchDto { return mergeBranchUuid; } - public void setMergeBranchUuid(@Nullable String s) { + public BranchDto setMergeBranchUuid(@Nullable String s) { this.mergeBranchUuid = s; + return this; } @Nullable @@ -171,9 +177,10 @@ public class BranchDto { return pullRequestTitle; } - public void setPullRequestTitle(@Nullable String s) { + public BranchDto setPullRequestTitle(@Nullable String s) { checkArgument(s == null || s.length() <= 4000, "Maximum length of pull request title is 4000: %s", s); this.pullRequestTitle = s; + return this; } @Override @@ -198,4 +205,5 @@ public class BranchDto { static String convertKeyFromDb(String s) { return NULL_KEY.equals(s) ? null : s; } + } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index e5379b9ab91..f12e59a954c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -214,6 +214,10 @@ public class ComponentDao implements Dao { return Optional.fromNullable(mapper(session).selectByKey(key)); } + public java.util.Optional selectByKeyAndBranch(DbSession session, String key, String branch) { + return java.util.Optional.ofNullable(mapper(session).selectByDbKey(ComponentDto.generateBranchKey(key, branch))); + } + public List selectAllViewsAndSubViews(DbSession session) { return mapper(session).selectUuidsForQualifiers(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.SUBVIEW); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java index 9e3b55062fa..9082e9710f0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java @@ -31,12 +31,20 @@ import org.apache.commons.lang.builder.ToStringBuilder; import org.sonar.api.resources.Scopes; import static com.google.common.base.Preconditions.checkArgument; +import static java.lang.String.format; import static org.sonar.db.component.ComponentValidator.checkComponentKey; import static org.sonar.db.component.ComponentValidator.checkComponentName; import static org.sonar.db.component.DbTagsReader.readDbTags; public class ComponentDto { + /** + * Separator used to generate the key of the branch + */ + public static final String BRANCH_KEY_SEPARATOR = ":BRANCH:"; + + private static final Splitter BRANCH_KEY_SPLITTER = Splitter.on(BRANCH_KEY_SEPARATOR); + public static final String UUID_PATH_SEPARATOR = "."; public static final String UUID_PATH_OF_ROOT = UUID_PATH_SEPARATOR; private static final Splitter UUID_PATH_SPLITTER = Splitter.on(UUID_PATH_SEPARATOR).omitEmptyStrings(); @@ -108,7 +116,7 @@ public class ComponentDto { /** * On non-main branches only, {@link #uuid} of the main branch that represents - * the project ({@link #qualifier}="TRK"). + * the project ({@link #qualifier}="TRK").x * It is propagated to all the components of the branch. * * Value is null on the main-branch components and on other kinds of components @@ -216,6 +224,22 @@ public class ComponentDto { return this; } + /** + * The key to be displayed to user, doesn't contain information on branches + */ + public String getKey() { + List split = BRANCH_KEY_SPLITTER.splitToList(kee); + return split.size() == 2 ? split.get(0) : kee; + } + + /** + * @return the key of the branch. It will be null on the main branch and when the component is not on a branch + */ + @CheckForNull + public String getBranch(){ + List split = BRANCH_KEY_SPLITTER.splitToList(kee); + return split.size() == 2 ? split.get(1) : null; + } public String scope() { return scope; @@ -503,4 +527,9 @@ public class ComponentDto { copy.createdAt = createdAt; return copy; } + + // TODO Use it in branch plugin + public static String generateBranchKey(String componentKey, String branch) { + return format("%s%s%s", componentKey, BRANCH_KEY_SEPARATOR, branch); + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java index 53ebb044c5a..692948c085c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -33,6 +33,12 @@ public interface ComponentMapper { @CheckForNull ComponentDto selectByKey(String key); + /** + * This method should be used to get a component by its key without filtering out branches + */ + @CheckForNull + ComponentDto selectByDbKey(String dbKey); + @CheckForNull ComponentDto selectById(long id); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 47b464dd2dd..0409bbd1f62 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -37,6 +37,14 @@ p.kee=#{key,jdbcType=VARCHAR} + +