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() {
return keeType;
}
- public void setKeeType(BranchKeyType t) {
+ public BranchDto setKeeType(BranchKeyType t) {
this.keeType = t;
+ return this;
}
/**
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
return branchType;
}
- public void setBranchType(@Nullable BranchType b) {
+ public BranchDto setBranchType(@Nullable BranchType b) {
this.branchType = b;
+ return this;
}
@Nullable
return mergeBranchUuid;
}
- public void setMergeBranchUuid(@Nullable String s) {
+ public BranchDto setMergeBranchUuid(@Nullable String s) {
this.mergeBranchUuid = s;
+ return this;
}
@Nullable
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
static String convertKeyFromDb(String s) {
return NULL_KEY.equals(s) ? null : s;
}
+
}
return Optional.fromNullable(mapper(session).selectByKey(key));
}
+ public java.util.Optional<ComponentDto> selectByKeyAndBranch(DbSession session, String key, String branch) {
+ return java.util.Optional.ofNullable(mapper(session).selectByDbKey(ComponentDto.generateBranchKey(key, branch)));
+ }
+
public List<UuidWithProjectUuidDto> selectAllViewsAndSubViews(DbSession session) {
return mapper(session).selectUuidsForQualifiers(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.SUBVIEW);
}
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();
/**
* 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
return this;
}
+ /**
+ * The key to be displayed to user, doesn't contain information on branches
+ */
+ public String getKey() {
+ List<String> 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<String> split = BRANCH_KEY_SPLITTER.splitToList(kee);
+ return split.size() == 2 ? split.get(1) : null;
+ }
public String scope() {
return scope;
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);
+ }
}
@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);
p.kee=#{key,jdbcType=VARCHAR}
</select>
+ <select id="selectByDbKey" parameterType="String" resultType="Component">
+ SELECT
+ <include refid="componentColumns"/>
+ FROM projects p
+ where
+ p.kee=#{key,jdbcType=VARCHAR}
+ </select>
+
<select id="selectComponentsHavingSameKeyOrderedById" parameterType="String" resultType="Component">
select
<include refid="componentColumns"/>
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
ComponentDto directory = db.components().insertComponent(newDirectory(project, "src"));
- ComponentDto file = db.components().insertComponent(newFileDto(directory)
+ ComponentDto file = db.components().insertComponent(newFileDto(project, directory)
+ .setDbKey("org.struts:struts-core:src/org/struts/RequestContext.java")
.setName("RequestContext.java")
.setLongName("org.struts.RequestContext")
.setLanguage("java")
ComponentDto result = optional.get();
assertThat(result.getOrganizationUuid()).isEqualTo(organization.getUuid());
assertThat(result.uuid()).isEqualTo(file.uuid());
- assertThat(result.getDbKey()).isEqualTo(file.getDbKey());
+ assertThat(result.getDbKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(result.path()).isEqualTo("src/RequestContext.java");
assertThat(result.name()).isEqualTo("RequestContext.java");
assertThat(result.longName()).isEqualTo("org.struts.RequestContext");
assertThat(underTest.selectByKey(dbSession, "unknown")).isAbsent();
}
+ @Test
+ public void selectByKeyAndBranch() {
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ ComponentDto file = db.components().insertComponent(newFileDto(branch));
+
+ assertThat(underTest.selectByKeyAndBranch(dbSession, project.getKey(), "my_branch").get().uuid()).isEqualTo(branch.uuid());
+ assertThat(underTest.selectByKeyAndBranch(dbSession, file.getKey(), "my_branch").get().uuid()).isEqualTo(file.uuid());
+ assertThat(underTest.selectByKeyAndBranch(dbSession, "unknown", "my_branch")).isNotPresent();
+ assertThat(underTest.selectByKeyAndBranch(dbSession, file.getKey(), "unknown")).isNotPresent();
+ }
+
@Test
public void selectOrFailByKey_fails_when_component_not_found() {
db.components().insertPrivateProject();
import java.util.Arrays;
import java.util.function.Consumer;
import javax.annotation.Nullable;
+import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Arrays.asList;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.sonar.db.component.BranchKeyType.BRANCH;
+import static org.sonar.db.component.BranchType.LONG;
import static org.sonar.db.component.ComponentTesting.newApplication;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
+import static org.sonar.db.component.ComponentTesting.newProjectBranch;
import static org.sonar.db.component.ComponentTesting.newPublicProjectDto;
import static org.sonar.db.component.ComponentTesting.newSubView;
import static org.sonar.db.component.ComponentTesting.newView;
db.commit();
}
- public ComponentDto insertProjectBranch(ComponentDto project, String branchName) {
- ComponentDto branch = ComponentTesting.newProjectBranch(project, branchName);
+ @SafeVarargs
+ public final ComponentDto insertProjectBranch(ComponentDto project, Consumer<BranchDto>... dtoPopulators) {
+ String uuid = Uuids.createFast();
+ BranchDto branchDto = new BranchDto()
+ .setKey(randomAlphanumeric(255))
+ .setUuid(uuid)
+ .setProjectUuid(project.uuid())
+ .setKeeType(BRANCH)
+ .setBranchType(LONG);
+ Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(branchDto));
+ ComponentDto branch = newProjectBranch(project, branchDto);
insertComponent(branch);
+ dbClient.branchDao().insert(dbSession, branchDto);
db.commit();
return branch;
}
assertThat(componentDto.getId()).isEqualTo(1L);
assertThat(componentDto.getDbKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(componentDto.deprecatedKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
+ assertThat(componentDto.getBranch()).isNull();
assertThat(componentDto.name()).isEqualTo("RequestContext.java");
assertThat(componentDto.longName()).isEqualTo("org.struts.RequestContext");
assertThat(componentDto.qualifier()).isEqualTo("FIL");
ComponentDto nonRoot = new ComponentDto().setUuidPath(".12.34.56.");
assertThat(nonRoot.getUuidPathAsList()).containsExactly("12", "34", "56");
}
+
+ @Test
+ public void test_getKey_and_getBranch() {
+ ComponentDto underTest = new ComponentDto().setDbKey("my_key:BRANCH:my_branch");
+ assertThat(underTest.getKey()).isEqualTo("my_key");
+ assertThat(underTest.getBranch()).isEqualTo("my_branch");
+
+ underTest = new ComponentDto().setDbKey("my_key");
+ assertThat(underTest.getKey()).isEqualTo("my_key");
+ assertThat(underTest.getBranch()).isNull();
+ }
}
String filename = "NAME_" + fileUuid;
String path = directory != null ? directory.path() + "/" + filename : module.path() + "/" + filename;
return newChildComponent(fileUuid, module, directory == null ? module : directory)
- .setDbKey("KEY_" + fileUuid)
+ .setDbKey(generateKey("FILE_KEY_" + fileUuid, module))
.setName(filename)
.setLongName(path)
.setScope(Scopes.FILE)
}
public static ComponentDto newDirectory(ComponentDto module, String uuid, String path) {
+ String key = !path.equals("/") ? module.getKey() + ":" + path : module.getKey() + ":/";
return newChildComponent(uuid, module, module)
- .setDbKey(!path.equals("/") ? module.getDbKey() + ":" + path : module.getDbKey() + ":/")
+ .setDbKey(generateKey(key, module))
.setName(path)
.setLongName(path)
.setPath(path)
public static ComponentDto newModuleDto(String uuid, ComponentDto parentModuleOrProject) {
return newChildComponent(uuid, parentModuleOrProject, parentModuleOrProject)
.setModuleUuidPath(parentModuleOrProject.moduleUuidPath() + uuid + UUID_PATH_SEPARATOR)
- .setDbKey("KEY_" + uuid)
+ .setDbKey(generateKey("MODULE_KEY_" + uuid, parentModuleOrProject))
.setName("NAME_" + uuid)
.setLongName("LONG_NAME_" + uuid)
.setPath("module")
.setLanguage(null);
}
+ private static String generateKey(String key, ComponentDto parentModuleOrProject) {
+ String branch = parentModuleOrProject.getBranch();
+ return branch == null ? key : ComponentDto.generateBranchKey(key, branch);
+ }
+
public static ComponentDto newModuleDto(ComponentDto subProjectOrProject) {
return newModuleDto(Uuids.createFast(), subProjectOrProject);
}
.setPrivate(moduleOrProject.isPrivate());
}
- public static ComponentDto newProjectBranch(ComponentDto project, String branchName) {
+ public static ComponentDto newProjectBranch(ComponentDto project, BranchDto branchDto) {
checkArgument(project.qualifier().equals(Qualifiers.PROJECT));
checkArgument(project.getMainBranchProjectUuid() == null);
- String uuid = Uuids.createFast();
+ String branchName = branchDto.getKey();
+ String uuid = branchDto.getUuid();
return new ComponentDto()
.setUuid(uuid)
.setOrganizationUuid(project.getOrganizationUuid())
.setProjectUuid(uuid)
.setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR)
.setRootUuid(uuid)
- .setDbKey(project.getDbKey() + ":BRANCH:" + branchName)
+ // name of the branch is not mandatory on the main branch
+ .setDbKey(branchName != null ? project.getDbKey() + ":BRANCH:" + branchName : project.getKey())
.setMainBranchProjectUuid(project.uuid())
.setName(project.name())
.setLongName(project.longName())
SnapshotDto projectAnalysis = dbTester.components().insertProjectAndSnapshot(project);
insertMeasure(project, projectAnalysis, metric, 10d);
- ComponentDto branch = ComponentTesting.newProjectBranch(project, "feature/foo");
- SnapshotDto branchAnalysis = dbTester.components().insertProjectAndSnapshot(branch);
+ ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
+ SnapshotDto branchAnalysis = dbTester.components().insertSnapshot(branch);
insertMeasure(branch, branchAnalysis, metric, 20d);
Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById();
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.MediaTypes;
+import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.newHashMap;
import static java.lang.String.format;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.core.util.Protobuf.setNullable;
+import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
public class IssuesAction implements BatchWsAction {
private static final String PARAM_KEY = "key";
+ private static final String PARAM_BRANCH = "branch";
private static final Splitter MODULE_PATH_SPLITTER = Splitter.on('.').trimResults().omitEmptyStrings();
private final DbClient dbClient;
.setRequired(true)
.setDescription("Project, module or file key")
.setExampleValue(KEY_PROJECT_EXAMPLE_001);
+
+ action
+ .createParam(PARAM_BRANCH)
+ .setSince("6.6")
+ .setDescription("Branch key")
+ .setExampleValue(KEY_BRANCH_EXAMPLE_001);
}
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- String componentKey = request.mandatoryParam(PARAM_KEY);
- ComponentDto component = componentFinder.getByKey(dbSession, componentKey);
+ ComponentDto component = loadComponent(dbSession, request);
userSession.checkComponentPermission(USER, component);
-
Map<String, String> keysByUUid = keysByUUid(dbSession, component);
ScannerInput.ServerIssue.Builder responseBuilder = ScannerInput.ServerIssue.newBuilder();
}
private static void handleIssue(IssueDto issue, ScannerInput.ServerIssue.Builder issueBuilder,
- Map<String, String> keysByUUid, OutputStream out) {
+ Map<String, String> keysByUUid, OutputStream out) {
issueBuilder.setKey(issue.getKey());
String moduleUuid = extractModuleUuid(issue);
issueBuilder.setModuleKey(keysByUUid.get(moduleUuid));
private static String extractModuleUuid(IssueDto issue) {
List<String> split = MODULE_PATH_SPLITTER.splitToList(issue.getModuleUuidPath());
- return split.get(split.size()-1);
+ return split.get(split.size() - 1);
}
private Map<String, String> keysByUUid(DbSession session, ComponentDto component) {
if (Scopes.PROJECT.equals(component.scope())) {
List<ComponentDto> modulesTree = dbClient.componentDao().selectDescendantModules(session, component.uuid());
for (ComponentDto componentDto : modulesTree) {
- keysByUUid.put(componentDto.uuid(), componentDto.getDbKey());
+ keysByUUid.put(componentDto.uuid(), componentDto.getKey());
}
} else {
String moduleUuid = component.moduleUuid();
- if (moduleUuid == null) {
- throw new IllegalArgumentException(String.format("The component '%s' has no module uuid", component.uuid()));
- }
+ checkArgument(moduleUuid != null, "The component '%s' has no module uuid", component.uuid());
ComponentDto module = dbClient.componentDao().selectOrFailByUuid(session, moduleUuid);
- keysByUUid.put(module.uuid(), module.getDbKey());
+ keysByUUid.put(module.uuid(), module.getKey());
}
return keysByUUid;
}
+
+ private ComponentDto loadComponent(DbSession dbSession, Request request) {
+ String componentKey = request.mandatoryParam(PARAM_KEY);
+ String branch = request.param(PARAM_BRANCH);
+ if (branch != null) {
+ return componentFinder.getByKeyAndBranch(dbSession, componentKey, branch);
+ }
+ return componentFinder.getByKey(dbSession, componentKey);
+ }
}
return checkFoundWithOptional(organizationDto, "Organization with uuid '%s' not found", organizationUuid);
}
+ public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) {
+ return checkFoundWithOptional(dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch), "Component '%s' on branch '%s' not found", key, branch);
+ }
+
public enum ParamNames {
PROJECT_ID_AND_KEY("projectId", "projectKey"),
PROJECT_UUID_AND_KEY("projectUuid", "projectKey"),
public static final String KEY_ORG_EXAMPLE_001 = "my-org";
public static final String KEY_ORG_EXAMPLE_002 = "foo-company";
+ public static final String KEY_BRANCH_EXAMPLE_001 = "feature/my_branch";
+
private KeyExamples() {
// prevent instantiation
}
package org.sonar.server.batch;
import java.io.IOException;
+import javax.annotation.Nullable;
+import org.assertj.core.groups.Tuple;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.rules.RuleType.BUG;
ComponentDto project = db.components().insertPrivateProject();
ComponentDto module = db.components().insertComponent(newModuleDto(project));
ComponentDto file = db.components().insertComponent(newFileDto(module, null).setPath(null));
- IssueDto issue = db.issues().insert(rule, project, file, i ->
- i.setSeverity("BLOCKER")
- // non-null fields
- .setStatus("OPEN")
- .setType(BUG)
- .setManualSeverity(true)
-
- // all the nullable fields
- .setResolution(null)
- .setMessage(null)
- .setLine(null)
- .setChecksum(null)
- .setAssignee(null));
+ IssueDto issue = db.issues().insert(rule, project, file, i -> i.setSeverity("BLOCKER")
+ // non-null fields
+ .setStatus("OPEN")
+ .setType(BUG)
+ .setManualSeverity(true)
+
+ // all the nullable fields
+ .setResolution(null)
+ .setMessage(null)
+ .setLine(null)
+ .setChecksum(null)
+ .setAssignee(null));
addPermissionTo(project);
- ServerIssue serverIssue = call(project.getDbKey());
+ ServerIssue serverIssue = call(project.getKey());
assertThat(serverIssue.getKey()).isEqualTo(issue.getKey());
- assertThat(serverIssue.getModuleKey()).isEqualTo(module.getDbKey());
+ assertThat(serverIssue.getModuleKey()).isEqualTo(module.getKey());
assertThat(serverIssue.getRuleRepository()).isEqualTo(rule.getRepositoryKey());
assertThat(serverIssue.getRuleKey()).isEqualTo(rule.getRuleKey());
assertThat(serverIssue.getStatus()).isEqualTo("OPEN");
ComponentDto project = db.components().insertPrivateProject();
ComponentDto module = db.components().insertComponent(newModuleDto(project));
ComponentDto file = db.components().insertComponent(newFileDto(module, null));
- IssueDto issue = db.issues().insert(rule, project, file, i ->
- i.setSeverity("BLOCKER")
- .setStatus("OPEN")
- .setType(BUG)
- .setManualSeverity(true)
- .setResolution("FIXED")
- .setMessage("the message")
- .setLine(10)
- .setChecksum("ABC")
- .setAssignee("foo"));
+ IssueDto issue = db.issues().insert(rule, project, file, i -> i.setSeverity("BLOCKER")
+ .setStatus("OPEN")
+ .setType(BUG)
+ .setManualSeverity(true)
+ .setResolution("FIXED")
+ .setMessage("the message")
+ .setLine(10)
+ .setChecksum("ABC")
+ .setAssignee("foo"));
addPermissionTo(project);
- ServerIssue serverIssue = call(project.getDbKey());
+ ServerIssue serverIssue = call(project.getKey());
assertThat(serverIssue.getKey()).isEqualTo(issue.getKey());
- assertThat(serverIssue.getModuleKey()).isEqualTo(module.getDbKey());
+ assertThat(serverIssue.getModuleKey()).isEqualTo(module.getKey());
assertThat(serverIssue.getRuleRepository()).isEqualTo(rule.getRepositoryKey());
assertThat(serverIssue.getRuleKey()).isEqualTo(rule.getRuleKey());
assertThat(serverIssue.getStatus()).isEqualTo("OPEN");
IssueDto issueOnProject = db.issues().insert(rule, project, project, i -> i.setKee("ON_PROJECT"));
addPermissionTo(project);
- try (CloseableIterator<ServerIssue> result = callStream(project.getDbKey())) {
+ try (CloseableIterator<ServerIssue> result = callStream(project.getKey(), null)) {
assertThat(result)
.extracting(ServerIssue::getKey, ServerIssue::getModuleKey)
.containsExactlyInAnyOrder(
- tuple(issueOnFile.getKey(), module.getDbKey()),
- tuple(issueOnModule.getKey(), module.getDbKey()),
- tuple(issueOnProject.getKey(), project.getDbKey()));
+ tuple(issueOnFile.getKey(), module.getKey()),
+ tuple(issueOnModule.getKey(), module.getKey()),
+ tuple(issueOnProject.getKey(), project.getKey()));
}
}
IssueDto issueOnProject = db.issues().insert(rule, project, project, i -> i.setKee("ON_PROJECT"));
addPermissionTo(project);
- try (CloseableIterator<ServerIssue> result = callStream(module.getDbKey())) {
+ try (CloseableIterator<ServerIssue> result = callStream(module.getKey(), null)) {
assertThat(result)
.extracting(ServerIssue::getKey, ServerIssue::getModuleKey)
.containsExactlyInAnyOrder(
- tuple(issueOnFile.getKey(), module.getDbKey()),
- tuple(issueOnModule.getKey(), module.getDbKey()));
+ tuple(issueOnFile.getKey(), module.getKey()),
+ tuple(issueOnModule.getKey(), module.getKey()));
}
}
IssueDto issueOnProject = db.issues().insert(rule, project, project);
addPermissionTo(project);
- try (CloseableIterator<ServerIssue> result = callStream(file.getDbKey())) {
+ try (CloseableIterator<ServerIssue> result = callStream(file.getKey(), null)) {
assertThat(result)
.extracting(ServerIssue::getKey, ServerIssue::getModuleKey)
.containsExactlyInAnyOrder(
- tuple(issueOnFile.getKey(), module.getDbKey()));
+ tuple(issueOnFile.getKey(), module.getKey()));
}
}
+ @Test
+ public void return_issues_by_project_and_branch() {
+ RuleDefinitionDto rule = db.rules().insert();
+ ComponentDto project = db.components().insertPrivateProject();
+ addPermissionTo(project);
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ ComponentDto file = db.components().insertComponent(newFileDto(branch));
+ IssueDto issueOnFile = db.issues().insert(rule, branch, file);
+ IssueDto issueOnBranch = db.issues().insert(rule, branch, branch);
+
+ assertResult(project.getKey(), "my_branch",
+ tuple(issueOnFile.getKey(), branch.getKey()),
+ tuple(issueOnBranch.getKey(), branch.getKey()));
+ }
+
+ @Test
+ public void return_issues_by_module_and_branch() {
+ RuleDefinitionDto rule = db.rules().insert();
+ ComponentDto project = db.components().insertPrivateProject();
+ addPermissionTo(project);
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ ComponentDto module = db.components().insertComponent(newModuleDto(branch));
+ ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
+ ComponentDto file = db.components().insertComponent(newFileDto(subModule));
+ IssueDto issueOnFile = db.issues().insert(rule, branch, file);
+ IssueDto issueOnSubModule = db.issues().insert(rule, branch, subModule);
+ IssueDto issueOnModule = db.issues().insert(rule, branch, module);
+
+ assertResult(module.getKey(), "my_branch",
+ tuple(issueOnFile.getKey(), subModule.getKey()),
+ tuple(issueOnSubModule.getKey(), subModule.getKey()),
+ tuple(issueOnModule.getKey(), module.getKey())
+ );
+ }
+
@Test
public void fail_if_requested_component_is_a_directory() throws IOException {
ComponentDto project = db.components().insertPrivateProject();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Component of scope 'DIR' is not allowed");
- call(directory.getDbKey());
+ call(directory.getKey());
}
@Test
IssueDto issue = db.issues().insert(rule, project, file);
addPermissionTo(project);
- try (CloseableIterator<ServerIssue> result = callStream(project.getDbKey())) {
+ try (CloseableIterator<ServerIssue> result = callStream(project.getKey(), null)) {
// Module key of removed file should be returned
assertThat(result)
.extracting(ServerIssue::getKey, ServerIssue::getModuleKey)
- .containsExactly(tuple(issue.getKey(), module.getDbKey()));
+ .containsExactly(tuple(issue.getKey(), module.getKey()));
}
}
expectedException.expect(ForbiddenException.class);
- tester.newRequest().setParam("key", file.getDbKey()).execute();
+ tester.newRequest().setParam("key", file.getKey()).execute();
}
@Test
tester.newRequest().setParam("key", "does_not_exist").execute();
}
+ @Test
+ public void fail_if_branch_does_not_exist() {
+ ComponentDto project = db.components().insertPrivateProject();
+ db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ addPermissionTo(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component '%s' on branch 'does_not_exist' not found", project.getKey()));
+
+ tester.newRequest()
+ .setParam("key",project.getKey())
+ .setParam("branch", "does_not_exist")
+ .execute();
+ }
+
private void addPermissionTo(ComponentDto project) {
userSessionRule.addProjectPermission(UserRole.USER, project);
}
return ServerIssue.parseDelimitedFrom(response.getInputStream());
}
- private CloseableIterator<ServerIssue> callStream(String componentKey) {
- TestResponse response = tester.newRequest().setParam("key", componentKey).execute();
- return Protobuf.readStream(response.getInputStream(), ServerIssue.parser());
+ private CloseableIterator<ServerIssue> callStream(String componentKey, @Nullable String branch) {
+ TestRequest request = tester.newRequest()
+ .setParam("key", componentKey);
+ if (branch != null) {
+ request.setParam("branch", branch);
+ }
+ return Protobuf.readStream(request.execute().getInputStream(), ServerIssue.parser());
+ }
+
+ private void assertResult(String componentKey, String branch, Tuple... tuples) {
+ try (CloseableIterator<ServerIssue> result = callStream(componentKey, branch)) {
+ assertThat(result)
+ .extracting(ServerIssue::getKey, ServerIssue::getModuleKey)
+ .containsExactlyInAnyOrder(tuples);
+ }
}
}
import org.sonar.db.component.ComponentDto;
import org.sonar.server.exceptions.NotFoundException;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.component.ComponentTesting.newDirectory;
import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.component.ComponentTesting.newModuleDto;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization()));
db.components().insertComponent(newFileDto(project).setDbKey("file-key").setEnabled(false));
-
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Component key 'file-key' not found");
assertThat(component.getDbKey()).isEqualTo("project-key");
}
+
+ @Test
+ public void get_by_key_and_branch() {
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ ComponentDto module = db.components().insertComponent(newModuleDto(branch));
+ ComponentDto directory = db.components().insertComponent(newDirectory(module, "scr"));
+ ComponentDto file = db.components().insertComponent(newFileDto(module));
+
+ assertThat(underTest.getByKeyAndBranch(dbSession, project.getKey(), "my_branch").uuid()).isEqualTo(branch.uuid());
+ assertThat(underTest.getByKeyAndBranch(dbSession, module.getKey(), "my_branch").uuid()).isEqualTo(module.uuid());
+ assertThat(underTest.getByKeyAndBranch(dbSession, file.getKey(), "my_branch").uuid()).isEqualTo(file.uuid());
+ assertThat(underTest.getByKeyAndBranch(dbSession, directory.getKey(), "my_branch").uuid()).isEqualTo(directory.uuid());
+ }
+
+ @Test
+ public void fail_to_get_by_key_and_branch_when_branch_does_not_exist() {
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ ComponentDto file = db.components().insertComponent(newFileDto(branch));
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component '%s' on branch 'other_branch' not found", file.getKey()));
+
+ underTest.getByKeyAndBranch(dbSession, file.getKey(), "other_branch");
+ }
}
@Test
public void indexOnStartup_does_not_index_non_main_branches() {
ComponentDto project = db.components().insertPrivateProject();
- ComponentDto branch = db.components().insertProjectBranch(project, "feature/foo");
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
underTest.indexOnStartup(emptySet());
@Test
public void indexOnAnalysis_does_not_index_non_main_branches() {
ComponentDto project = db.components().insertPrivateProject();
- ComponentDto branch = db.components().insertProjectBranch(project, "feature/foo");
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
underTest.indexOnAnalysis(branch.uuid());
assertThatEsQueueTableHasSize(0);
}
-
@Test
public void index_is_not_updated_when_creating_project() {
// it's impossible to already have an issue on a project
public void index_issue_in_non_main_branch() {
RuleDefinitionDto rule = db.rules().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
- ComponentDto branch = db.components().insertProjectBranch(project, "feature/foo");
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
ComponentDto dir = db.components().insertComponent(ComponentTesting.newDirectory(branch, "src/main/java/foo"));
ComponentDto file = db.components().insertComponent(newFileDto(branch, dir, "F1"));
IssueDto issue = db.issues().insertIssue(IssueTesting.newIssue(rule, branch, file));
@Test
public void indexOnStartup_ignores_non_main_branches() {
ComponentDto project = db.components().insertPrivateProject();
- ComponentDto branch = db.components().insertProjectBranch(project, "feature/foo");
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
underTest.indexOnStartup(emptySet());
@Test
public void non_main_branches_are_not_indexed_during_analysis() {
ComponentDto project = db.components().insertPrivateProject();
- ComponentDto branch = db.components().insertProjectBranch(project, "feature/foo");
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
underTest.indexOnAnalysis(branch.uuid());
import org.sonar.api.web.UserRole;
import org.sonar.db.DbTester;
import org.sonar.db.RowNotFoundException;
-import org.sonar.db.component.BranchDto;
-import org.sonar.db.component.BranchKeyType;
-import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.server.tester.UserSessionRule;
@Test
public void test_project_with_branches() {
ComponentDto project = db.components().insertPrivateProject();
- insertBranch(project, "feature/bar");
- insertBranch(project, "feature/foo");
+ db.components().insertProjectBranch(project, b -> b.setKey("feature/bar"));
+ db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
userSession.logIn().addProjectPermission(UserRole.USER, project);
ListWsResponse response = tester.newRequest()
@Test
public void test_example() {
ComponentDto project = db.components().insertPrivateProject();
- insertBranch(project, "feature/bar");
- insertBranch(project, "feature/foo");
+ db.components().insertProjectBranch(project, b -> b.setKey("feature/bar"));
+ db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
userSession.logIn().addProjectPermission(UserRole.USER, project);
String json = tester.newRequest()
assertJson(json).isSimilarTo(tester.getDef().responseExampleAsString());
}
- private void insertBranch(ComponentDto project, String branchName) {
- ComponentDto branch = db.components().insertProjectBranch(project, branchName);
- BranchDto branchDto = new BranchDto();
- branchDto.setUuid(branch.uuid());
- branchDto.setProjectUuid(project.uuid());
- branchDto.setBranchType(BranchType.LONG);
- branchDto.setKeeType(BranchKeyType.BRANCH);
- branchDto.setKey(branchName);
- db.getDbClient().branchDao().insert(db.getSession(), branchDto);
- db.commit();
- }
}
@Test
public void hasComponentPermission_on_branch_checks_permissions_of_its_project() {
- ComponentDto branch = db.components().insertProjectBranch(privateProject, "feature/foo");
+ ComponentDto branch = db.components().insertProjectBranch(privateProject, b -> b.setKey("feature/foo"));
ComponentDto fileInBranch = db.components().insertComponent(newChildComponent("fileUuid", branch, branch));
// permissions are defined on the project, not on the branch