import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.ComponentFinder.ParamNames;
import org.sonar.server.user.UserSession;
import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.api.measures.CoreMetrics.VIOLATIONS;
import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
-import static org.sonar.server.component.ComponentFinder.ParamNames.*;
import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_COMPONENT;
import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
private static final String PARAM_COMPONENT_ID = "componentId";
private static final String PARAM_COMPONENT = "component";
- private static final String PARAM_PERIOD = "period";
private static final List<String> METRIC_KEYS = ImmutableList.of(
LINES_KEY,
VIOLATIONS_KEY,
.setDescription("Component key")
.setExampleValue(KEY_PROJECT_EXAMPLE_001)
.setSince("6.4");
+
+ action.createParam(PARAM_BRANCH)
+ .setDescription("Branch key")
+ .setSince("6.6")
+ .setInternal(true)
+ .setExampleValue(KEY_BRANCH_EXAMPLE_001);
}
@Override
public void handle(Request request, Response response) {
try (DbSession session = dbClient.openSession(false)) {
- ComponentDto component = componentFinder.getByUuidOrKey(session,
- request.param(PARAM_COMPONENT_ID),
- request.param(PARAM_COMPONENT),
- COMPONENT_ID_AND_COMPONENT);
+ ComponentDto component = loadComponent(session, request);
userSession.checkComponentPermission(UserRole.USER, component);
JsonWriter json = response.newJsonWriter();
}
}
+ private ComponentDto loadComponent(DbSession dbSession, Request request) {
+ String componentUuid = request.param(PARAM_COMPONENT_ID);
+ String branch = request.param("branch");
+ checkArgument(componentUuid == null || branch == null, "'%s' and '%s' parameters cannot be used at the same time", PARAM_COMPONENT_ID, PARAM_BRANCH);
+ if (branch == null) {
+ return componentFinder.getByUuidOrKey(dbSession, componentUuid, request.param(PARAM_COMPONENT), COMPONENT_ID_AND_COMPONENT);
+ }
+ return componentFinder.getByKeyAndOptionalBranch(dbSession, request.mandatoryParam(PARAM_COMPONENT), branch);
+ }
+
private void appendComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session) {
List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
.setKey("favourite")
"}\n");
}
+ @Test
+ public void branch() {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.logIn("john").addProjectPermission(USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ ComponentDto module = db.components().insertComponent(newModuleDto(branch));
+ ComponentDto directory = db.components().insertComponent(newDirectory(module, "src"));
+ ComponentDto file = db.components().insertComponent(newFileDto(module, directory));
+ SnapshotDto analysis = db.components().insertSnapshot(branch);
+ MetricDto coverage = db.measures().insertMetric(m -> m.setKey(COVERAGE_KEY));
+ db.measures().insertMeasure(file, analysis, coverage, m -> m.setValue(95.4d));
+
+ String result = ws.newRequest()
+ .setParam("component", file.getKey())
+ .setParam("branch", file.getBranch())
+ .execute()
+ .getInput();
+
+ assertJson(result).isSimilarTo("{\n" +
+ " \"key\": \"" + file.getKey() + "\",\n" +
+ " \"branch\": \"" + file.getBranch() + "\",\n" +
+ " \"uuid\": \"" + file.uuid() + "\",\n" +
+ " \"path\": \"" + file.path() + "\",\n" +
+ " \"name\": \"" + file.name() + "\",\n" +
+ " \"longName\": \"" + file.longName() + "\",\n" +
+ " \"q\": \"" + file.qualifier() + "\",\n" +
+ " \"subProject\": \"" + module.getKey() + "\",\n" +
+ " \"subProjectName\": \"" + module.longName() + "\",\n" +
+ " \"project\": \"" + project.getKey() + "\",\n" +
+ " \"projectName\": \"" + project.longName() + "\",\n" +
+ " \"fav\": false,\n" +
+ " \"canMarkAsFavorite\": true,\n" +
+ " \"measures\": {\n" +
+ " \"coverage\": \"95.4\"\n" +
+ " }\n" +
+ "}\n");
+ }
+
@Test
public void fail_if_no_parameter_provided() {
expectedException.expect(IllegalArgumentException.class);
ws.newRequest().execute();
}
+ @Test
+ public void fail_if_both_componentId_and_branch_parameters_provided() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ ComponentDto file = db.components().insertComponent(newFileDto(branch));
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("'componentId' and 'branch' parameters cannot be used at the same time");
+
+ ws.newRequest()
+ .setParam("uuid", file.uuid())
+ .setParam("branch", file.getBranch())
+ .execute();
+ }
+
@Test
public void fail_when_component_not_found() throws Exception {
ComponentDto project = db.components().insertPrivateProject();
.execute();
}
+ @Test
+ public void fail_when_branch_not_found() throws Exception {
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ ComponentDto file = db.components().insertComponent(newFileDto(branch));
+
+ expectedException.expect(NotFoundException.class);
+
+ ws.newRequest()
+ .setParam("component", file.getKey())
+ .setParam("branch", "unknown")
+ .execute();
+ }
+
@Test
public void fail_when_missing_permission() throws Exception {
ComponentDto project = db.components().insertPrivateProject();
assertThat(action.isInternal()).isTrue();
assertThat(action.isPost()).isFalse();
assertThat(action.handler()).isNotNull();
- assertThat(action.params()).hasSize(2);
+ assertThat(action.params()).hasSize(3);
}
}