@Rule
public final UserSessionRule userSession = UserSessionRule.standalone().logIn();
@Rule
- public final DbTester db = DbTester.create(System2.INSTANCE);
+ public final DbTester db = DbTester.create(System2.INSTANCE, true);
private final WsActionTester ws = new WsActionTester(new ShowAction(userSession, db.getDbClient(), TestComponentFinder.from(db),
new IssueIndexSyncProgressChecker(db.getDbClient())));
ProjectData projectData = db.components().insertPublicProject();
ComponentDto publicProject = projectData.getMainBranchComponent();
- userSession.registerProjects(projectData.getProjectDto());
+ userSession.registerProjects(projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
ShowWsResponse result = newRequest(publicProject.getKey());
assertThat(result.getComponent().hasVisibility()).isTrue();
userSession.addProjectBranchMapping(projectData3.projectUuid(), branch4);
userSession.addProjectBranchMapping(projectData3.projectUuid(), branch5);
- userSession.addProjectPermission(UserRole.USER, projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto());
+ userSession.addProjectPermission(UserRole.USER, projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto())
+ .registerBranches(projectData1.getMainBranchDto(), projectData2.getMainBranchDto(), projectData3.getMainBranchDto());
userSession.registerPortfolios(portfolio1, portfolio2, subview);
userSession.registerProjects(projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto());
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.rule.RuleDto;
import org.sonar.server.component.ComponentFinder;
public void search_authors() {
String leia = "leia.organa";
String luke = "luke.skywalker";
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- permissionIndexer.allowOnlyAnyone(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
RuleDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
public void search_authors_by_query() {
String leia = "leia.organa";
String luke = "luke.skywalker";
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- permissionIndexer.allowOnlyAnyone(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
RuleDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
public void search_authors_by_project() {
String leia = "leia.organa";
String luke = "luke.skywalker";
- ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
- ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
- permissionIndexer.allowOnlyAnyone(project1, project2);
+ ProjectData projectData1 = db.components().insertPrivateProject();
+ ComponentDto project1 = projectData1.getMainBranchComponent();
+ ProjectData projectData2 = db.components().insertPrivateProject();
+ ComponentDto project2 = projectData2.getMainBranchComponent();
+ permissionIndexer.allowOnlyAnyone(projectData1.getProjectDto(), projectData2.getProjectDto());
RuleDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
db.issues().insertIssue(rule, project2, project2, issue -> issue.setAuthorLogin(luke));
public void search_authors_by_portfolio() {
String leia = "leia.organa";
ComponentDto portfolio = db.components().insertPrivatePortfolio();
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
db.components().insertComponent(newProjectCopy(project, portfolio));
- permissionIndexer.allowOnlyAnyone(project);
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
RuleDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
indexIssues();
public void search_authors_by_application() {
String leia = "leia.organa";
ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent();
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
db.components().insertComponent(newProjectCopy(project, application));
- permissionIndexer.allowOnlyAnyone(project);
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
RuleDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
indexIssues();
String han = "han.solo";
String leia = "leia.organa";
String luke = "luke.skywalker";
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- permissionIndexer.allowOnlyAnyone(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
RuleDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(han));
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
@Test
public void should_ignore_authors_of_hotspot() {
String luke = "luke.skywalker";
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- permissionIndexer.allowOnlyAnyone(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
db.issues().insertHotspot(project, project, issue -> issue
.setAuthorLogin(luke));
indexIssues();
@Test
public void fail_when_project_is_not_a_project() {
userSession.logIn();
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
ComponentDto file = db.components().insertComponent(newFileDto(project));
- permissionIndexer.allowOnlyAnyone(project);
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
assertThatThrownBy(() -> {
ws.newRequest()
.setParam("project", file.getKey())
.execute();
})
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage(format("Component '%s' must be a project", file.getKey()));
+ .isInstanceOf(NotFoundException.class)
+ .hasMessage(format("Component '%s' not found", file.getKey()));
}
@Test
.execute();
})
.isInstanceOf(NotFoundException.class)
- .hasMessage("Component key 'unknown' not found");
+ .hasMessage("Component 'unknown' not found");
}
@Test
public void json_example() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- permissionIndexer.allowOnlyAnyone(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto project = projectData.getMainBranchComponent();
+ permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
RuleDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin("luke.skywalker"));
db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin("leia.organa"));
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.LiveMeasureDto;
import org.sonar.db.metric.MetricDto;
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create(System2.INSTANCE, true);
private final WsActionTester ws = new WsActionTester(new ComponentAction(db.getDbClient(), TestComponentFinder.from(db), userSession));
@Test
public void provided_project() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
- ComponentWsResponse response = newRequest(project.getKey(), metric.getKey());
+ ComponentWsResponse response = newRequest(mainBranch.getKey(), metric.getKey());
assertThat(response.getMetrics().getMetricsCount()).isOne();
assertThat(response.hasPeriod()).isFalse();
- assertThat(response.getComponent().getKey()).isEqualTo(project.getKey());
+ assertThat(response.getComponent().getKey()).isEqualTo(mainBranch.getKey());
}
@Test
public void without_additional_fields() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- db.components().insertSnapshot(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
+ db.components().insertSnapshot(mainBranch);
MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
String response = ws.newRequest()
- .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(PARAM_COMPONENT, mainBranch.getKey())
.setParam(PARAM_METRIC_KEYS, metric.getKey())
.execute().getInput();
@Test
public void branch() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
String branchName = "my_branch";
- ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
- userSession.addProjectBranchMapping(project.uuid(), branch);
+ ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchName));
+ userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
db.components().insertSnapshot(branch);
- ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+ ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
@Test
public void branch_not_set_if_main_branch() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- ComponentDto file = db.components().insertComponent(newFileDto(project));
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
+ ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
ComponentWsResponse response = ws.newRequest()
@Test
public void pull_request() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
- userSession.addProjectBranchMapping(project.uuid(), branch);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto());
+ ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
- ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+ ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
@Test
public void new_issue_count_measures_are_transformed_in_pr() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
- userSession.addProjectBranchMapping(project.uuid(), branch);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
+ ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
- ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+ ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setValueType("INT"));
MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setValueType("INT"));
MetricDto violations = db.measures().insertMetric(m1 -> m1.setKey("violations").setValueType("INT"));
@Test
public void new_issue_count_measures_are_not_transformed_if_they_dont_exist_in_pr() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
- userSession.addProjectBranchMapping(project.uuid(), branch);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto());
+ ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
- ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+ ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setOptimizedBestValue(false).setValueType("INT"));
MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setOptimizedBestValue(false).setValueType("INT"));
@Test
public void reference_key_in_the_response() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+ ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
ComponentDto view = db.components().insertPrivatePortfolio();
userSession.addProjectPermission(USER, view);
db.components().insertSnapshot(view);
- ComponentDto projectCopy = db.components().insertComponent(newProjectCopy("project-uuid-copy", project, view));
+ ComponentDto projectCopy = db.components().insertComponent(newProjectCopy("project-uuid-copy", mainBranch, view));
MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
ComponentWsResponse response = newRequest(projectCopy.getKey(), metric.getKey());
- assertThat(response.getComponent().getRefKey()).isEqualTo(project.getKey());
+ assertThat(response.getComponent().getRefKey()).isEqualTo(mainBranch.getKey());
}
@Test
public void use_deprecated_component_id_parameter() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- userSession.addProjectPermission(USER, project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
ComponentWsResponse response = ws.newRequest()
- .setParam("component", project.getKey())
+ .setParam("component", mainBranch.getKey())
.setParam(PARAM_METRIC_KEYS, metric.getKey())
.executeProtobuf(ComponentWsResponse.class);
- assertThat(response.getComponent().getKey()).isEqualTo(project.getKey());
+ assertThat(response.getComponent().getKey()).isEqualTo(mainBranch.getKey());
}
@Test
public void metric_without_a_domain() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
MetricDto metricWithoutDomain = db.measures().insertMetric(m -> m
.setValueType("INT")
.setDomain(null));
- db.measures().insertLiveMeasure(project, metricWithoutDomain);
+ db.measures().insertLiveMeasure(mainBranch, metricWithoutDomain);
ComponentWsResponse response = ws.newRequest()
- .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(PARAM_COMPONENT, mainBranch.getKey())
.setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
.setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
.executeProtobuf(ComponentWsResponse.class);
@Test
public void use_best_values() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- ComponentDto file = db.components().insertComponent(newFileDto(project));
- userSession.addProjectPermission(USER, project);
+ ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+ ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
+ userSession.addProjectPermission(USER, mainBranch);
MetricDto metric = db.measures().insertMetric(m -> m
.setValueType("INT")
.setBestValue(7.0d)
@Test
public void fail_when_a_metric_is_not_found() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- db.components().insertSnapshot(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
+ db.components().insertSnapshot(mainBranch);
db.measures().insertMetric(m -> m.setKey("ncloc").setValueType("INT"));
db.measures().insertMetric(m -> m.setKey("complexity").setValueType("INT"));
- assertThatThrownBy(() -> newRequest(project.getKey(), "ncloc, complexity, unknown-metric, another-unknown-metric"))
+ assertThatThrownBy(() -> newRequest(mainBranch.getKey(), "ncloc, complexity, unknown-metric, another-unknown-metric"))
.isInstanceOf(NotFoundException.class)
.hasMessage("The following metric keys are not found: unknown-metric, another-unknown-metric");
}
@Test
public void fail_when_empty_metric_keys_parameter() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- db.components().insertSnapshot(project);
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto());
+ db.components().insertSnapshot(mainBranch);
- assertThatThrownBy(() -> newRequest(project.getKey(), ""))
+ assertThatThrownBy(() -> newRequest(mainBranch.getKey(), ""))
.isInstanceOf(BadRequestException.class)
.hasMessage("At least one metric key must be provided");
}
@Test
public void fail_when_not_enough_permission() {
userSession.logIn();
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- db.components().insertSnapshot(project);
+ ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+ db.components().insertSnapshot(mainBranch);
MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
- assertThatThrownBy(() -> newRequest(project.getKey(), metric.getKey()))
+ assertThatThrownBy(() -> newRequest(mainBranch.getKey(), metric.getKey()))
.isInstanceOf(ForbiddenException.class);
}
@Test
public void fail_when_component_is_removed() {
- ComponentDto project = db.components().insertPrivateProject(p -> p.setEnabled(false)).getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- userSession.addProjectPermission(USER, project);
+ ProjectData projectData = db.components().insertPrivateProject(p -> p.setEnabled(false));
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto());
+ userSession.addProjectPermission(USER, projectData.getProjectDto());
MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
assertThatThrownBy(() -> {
ws.newRequest()
- .setParam(PARAM_COMPONENT, project.getKey())
+ .setParam(PARAM_COMPONENT, mainBranch.getKey())
.setParam(PARAM_METRIC_KEYS, metric.getKey())
.execute();
})
.isInstanceOf(NotFoundException.class)
- .hasMessage(String.format("Component key '%s' not found", project.getKey()));
+ .hasMessage(String.format("Component key '%s' not found", mainBranch.getKey()));
}
@Test
public void fail_if_branch_does_not_exist() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- ComponentDto file = db.components().insertComponent(newFileDto(project));
- userSession.addProjectPermission(USER, project);
- db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
+ userSession.addProjectPermission(USER, projectData.getProjectDto());
+ db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
assertThatThrownBy(() -> {
ws.newRequest()
@Test
public void json_example() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(USER, project);
- SnapshotDto analysis = db.components().insertSnapshot(project,
+ ProjectData projectData = db.components().insertPrivateProject();
+ ComponentDto mainBranch = projectData.getMainBranchComponent();
+ userSession.addProjectPermission(USER, projectData.getProjectDto())
+ .registerBranches(projectData.getMainBranchDto());
+ SnapshotDto analysis = db.components().insertSnapshot(mainBranch,
s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
.setPeriodMode("previous_version")
.setPeriodParam("1.0-SNAPSHOT"));
- ComponentDto file = db.components().insertComponent(newFileDto(project)
+ ComponentDto file = db.components().insertComponent(newFileDto(mainBranch)
.setKey("MY_PROJECT:ElementImpl.java")
.setName("ElementImpl.java")
.setLanguage("java")
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
import org.sonar.db.issue.IssueDto;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.protobuf.DbCommons;
private final DbClient dbClient = db.getDbClient();
private final FileSourceTester fileSourceTester = new FileSourceTester(db);
- private ComponentDto project;
+ private ProjectData project;
private WsActionTester actionTester;
+ private ComponentDto mainBranchComponent;
@Before
public void setUp() {
- project = db.components().insertPrivateProject("projectUuid").getMainBranchComponent();
+ project = db.components().insertPrivateProject("projectUuid");
+ mainBranchComponent = project.getMainBranchComponent();
HtmlSourceDecorator htmlSourceDecorator = mock(HtmlSourceDecorator.class);
when(htmlSourceDecorator.getDecoratedSourceAsHtml(anyString(), anyString(), anyString()))
@Test
public void should_display_single_location_single_file() {
- ComponentDto file = insertFile(project, "file");
+ ComponentDto file = insertFile(mainBranchComponent, "file");
DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
fileSourceTester.insertFileSource(file, 10, dto -> dto.setSourceData(fileSources));
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
@Test
public void should_add_measures_to_components() {
- ComponentDto file = insertFile(project, "file");
+ ComponentDto file = insertFile(mainBranchComponent, "file");
MetricDto lines = db.measures().insertMetric(m -> m.setKey(LINES_KEY));
db.measures().insertLiveMeasure(file, lines, m -> m.setValue(200d));
DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
fileSourceTester.insertFileSource(file, 10, dto -> dto.setSourceData(fileSources));
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
@Test
public void issue_references_a_non_existing_component() {
- ComponentDto file = insertFile(project, "file");
- ComponentDto file2 = newFileDto(project, null, "nonexisting");
+ ComponentDto file = insertFile(mainBranchComponent, "file");
+ ComponentDto file2 = newFileDto(mainBranchComponent, null, "nonexisting");
DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
fileSourceTester.insertFileSource(file, 10, dto -> dto.setSourceData(fileSources));
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
String issueKey = insertIssue(file, newLocation(file2.uuid(), 5, 5));
@Test
public void no_code_to_display() {
- ComponentDto file = insertFile(project, "file");
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+ ComponentDto file = insertFile(mainBranchComponent, "file");
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
@Test
public void fail_if_no_project_permission() {
- ComponentDto file = insertFile(project, "file");
- userSession.logIn().addProjectPermission(USER, project, file);
+ ComponentDto file = insertFile(mainBranchComponent, "file");
+ userSession.logIn().addProjectPermission(USER, project.getProjectDto());
String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
var request = actionTester.newRequest().setParam("issueKey", issueKey);
@Test
public void fail_if_issue_not_found() {
- ComponentDto file = insertFile(project, "file");
+ ComponentDto file = insertFile(mainBranchComponent, "file");
insertIssue(file, newLocation(file.uuid(), 5, 5));
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
var request = actionTester.newRequest().setParam("issueKey", "invalid");
assertThatThrownBy(request::execute)
@Test
public void fail_if_parameter_missing() {
- ComponentDto file = insertFile(project, "file");
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
+ ComponentDto file = insertFile(mainBranchComponent, "file");
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
var request = actionTester.newRequest();
assertThatThrownBy(request::execute)
@Test
public void should_display_multiple_locations_multiple_files() {
- ComponentDto file1 = insertFile(project, "file1");
- ComponentDto file2 = insertFile(project, "file2");
+ ComponentDto file1 = insertFile(mainBranchComponent, "file1");
+ ComponentDto file2 = insertFile(mainBranchComponent, "file2");
DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
fileSourceTester.insertFileSource(file1, 10, dto -> dto.setSourceData(fileSources));
fileSourceTester.insertFileSource(file2, 10, dto -> dto.setSourceData(fileSources));
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file1, file2);
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
String issueKey1 = insertIssue(file1, newLocation(file1.uuid(), 5, 5),
newLocation(file1.uuid(), 9, 9), newLocation(file2.uuid(), 1, 5));
@Test
public void should_connect_snippets_close_to_each_other() {
- ComponentDto file1 = insertFile(project, "file1");
+ ComponentDto file1 = insertFile(mainBranchComponent, "file1");
DbFileSources.Data fileSources = FileSourceTesting.newFakeData(20).build();
fileSourceTester.insertFileSource(file1, 20, dto -> dto.setSourceData(fileSources));
- userSession.logIn().addProjectPermission(CODEVIEWER, project, file1);
+ userSession.logIn().addProjectPermission(CODEVIEWER, project.getProjectDto());
// these two locations should get connected, making a single range 3-14
String issueKey1 = insertIssue(file1, newLocation(file1.uuid(), 5, 5),
RuleDto rule = db.rules().insert();
DbIssues.Flow flow = DbIssues.Flow.newBuilder().addAllLocation(Arrays.asList(locations)).build();
- IssueDto issue = db.issues().insert(rule, project, file, i -> {
+ IssueDto issue = db.issues().insert(rule, project.getMainBranchComponent(), file, i -> {
i.setLocations(DbIssues.Locations.newBuilder().addFlow(flow).build());
- i.setProjectUuid(project.uuid());
+ i.setProjectUuid(mainBranchComponent.uuid());
i.setLine(locations[0].getTextRange().getStartLine());
});
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create(System2.INSTANCE, true);
private final PlatformEditionProvider platformEditionProvider = mock(PlatformEditionProvider.class);
private final HomepageTypesImpl homepageTypes = new HomepageTypesImpl();
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Scopes;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.BranchDto;
+import org.sonar.db.entity.EntityDto;
import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.issue.index.IssueIndex;
import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
import org.sonar.server.issue.index.IssueQuery;
NewAction action = controller.createAction("authors")
.setSince("5.1")
.setDescription("Search SCM accounts which match a given query.<br/>" +
- "Requires authentication."
- + "<br/>When issue indexation is in progress returns 503 service unavailable HTTP code.")
+ "Requires authentication."
+ + "<br/>When issue indexation is in progress returns 503 service unavailable HTTP code.")
.setResponseExample(Resources.getResource(this.getClass(), "authors-example.json"))
.setChangelog(new Change("7.4", "The maximum size of 'ps' is set to 100"))
.setHandler(this);
try (DbSession dbSession = dbClient.openSession(false)) {
checkIfComponentNeedIssueSync(dbSession, request.param(PARAM_PROJECT));
- Optional<ComponentDto> project = getProject(dbSession, request.param(PARAM_PROJECT));
- List<String> authors = getAuthors(project.orElse(null), request);
+ Optional<EntityDto> entity = getEntity(dbSession, request.param(PARAM_PROJECT));
+ List<String> authors = getAuthors(entity.orElse(null), request, dbSession);
AuthorsResponse wsResponse = AuthorsResponse.newBuilder().addAllAuthors(authors).build();
writeProtobuf(wsResponse, request, response);
}
}
}
- private Optional<ComponentDto> getProject(DbSession dbSession, @Nullable String projectKey) {
+ private Optional<EntityDto> getEntity(DbSession dbSession, @Nullable String projectKey) {
if (projectKey == null) {
return Optional.empty();
}
- ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
- checkArgument(project.scope().equals(Scopes.PROJECT), "Component '%s' must be a project", projectKey);
- return Optional.of(project);
+ EntityDto entity = componentFinder.getEntityByKey(dbSession, projectKey);
+ return Optional.of(entity);
}
- private List<String> getAuthors(@Nullable ComponentDto project, Request request) {
+ private List<String> getAuthors(@Nullable EntityDto entity, Request request, DbSession dbSession) {
IssueQuery.Builder issueQueryBuilder = IssueQuery.builder();
- ofNullable(project).ifPresent(p -> {
- switch (p.qualifier()) {
+ ofNullable(entity).ifPresent(e -> {
+ switch (e.getQualifier()) {
case Qualifiers.PROJECT:
- issueQueryBuilder.projectUuids(Set.of(p.uuid()));
+ issueQueryBuilder.projectUuids(Set.of(e.getUuid()));
return;
- case Qualifiers.APP, Qualifiers.VIEW:
- issueQueryBuilder.viewUuids(Set.of(p.uuid()));
+ case Qualifiers.VIEW:
+ issueQueryBuilder.viewUuids(Set.of(e.getUuid()));
+ return;
+ case Qualifiers.APP:
+ issueQueryBuilder.viewUuids(Set.of(e.getUuid()));
+ BranchDto branchDto = dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, entity.getUuid())
+ .orElseThrow(() -> new NotFoundException("Main branch of application %s not found".formatted(e.getUuid())));
+ issueQueryBuilder.branchUuid(branchDto.getUuid());
return;
default:
- throw new IllegalArgumentException(String.format("Component of type '%s' is not supported", p.qualifier()));
+ throw new IllegalArgumentException(String.format("Entity of type '%s' is not supported", e.getQualifier()));
}
});
return issueIndex.searchAuthors(