import org.apache.commons.lang.StringUtils;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ServerSide;
+import org.sonar.core.component.ComponentKeys;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
this.componentService = componentService;
}
- public CeTask submit(String projectKey, @Nullable String projectName, InputStream reportInput) {
+ public CeTask submit(String projectKey, @Nullable String projectBranch, @Nullable String projectName, InputStream reportInput) {
userSession.checkGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
- ComponentDto project = componentService.getNullableByKey(projectKey);
+ String effectiveProjectKey = ComponentKeys.createKey(projectKey, projectBranch);
+ ComponentDto project = componentService.getNullableByKey(effectiveProjectKey);
if (project == null) {
// the project does not exist -> requires to provision it
NewComponent newProject = new NewComponent(projectKey, StringUtils.defaultIfBlank(projectName, projectKey));
+ newProject.setBranch(projectBranch);
newProject.setQualifier(Qualifiers.PROJECT);
// no need to verify the permission "provisionning" as it's already handled by componentService
project = componentService.create(newProject);
*/
package org.sonar.server.computation.ws;
+import com.google.common.base.Strings;
import java.io.InputStream;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.server.ws.Request;
public class CeSubmitWsAction implements CeWsAction {
public static final String PARAM_PROJECT_KEY = "projectKey";
+ public static final String PARAM_PROJECT_BRANCH = "projectBranch";
public static final String PARAM_PROJECT_NAME = "projectName";
public static final String PARAM_REPORT_DATA = "report";
.setDescription("Key of project")
.setExampleValue("my_project");
+ action
+ .createParam(PARAM_PROJECT_BRANCH)
+ .setDescription("Optional branch of project")
+ .setExampleValue("branch-1.x");
+
action
.createParam(PARAM_PROJECT_NAME)
.setRequired(false)
@Override
public void handle(Request wsRequest, Response wsResponse) throws Exception {
String projectKey = wsRequest.mandatoryParam(PARAM_PROJECT_KEY);
+ String projectBranch = Strings.emptyToNull(wsRequest.param(PARAM_PROJECT_BRANCH));
String projectName = StringUtils.defaultIfBlank(wsRequest.param(PARAM_PROJECT_NAME), projectKey);
InputStream reportInput = wsRequest.paramAsInputStream(PARAM_REPORT_DATA);
- CeTask task = reportSubmitter.submit(projectKey, projectName, reportInput);
+ CeTask task = reportSubmitter.submit(projectKey, projectBranch, projectName, reportInput);
reportProcessingScheduler.startAnalysisTaskNow();
WsCe.SubmitResponse submitResponse = WsCe.SubmitResponse.newBuilder()
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentService;
+import org.sonar.server.component.NewComponent;
import org.sonar.server.tester.UserSessionRule;
+import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(new ComponentDto().setUuid("P1"));
- underTest.submit("MY_PROJECT", "My Project", IOUtils.toInputStream("{binary}"));
+ underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
}
}));
}
+
+ @Test
+ public void provision_project_if_does_not_exist() throws Exception {
+ when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit("TASK_1"));
+ userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
+ when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(null);
+ when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1"));
+
+ underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
+
+ verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
+ @Override
+ protected boolean matchesSafely(CeTaskSubmit submit) {
+ return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
+ submit.getUuid().equals("TASK_1");
+ }
+
+ @Override
+ public void describeTo(Description description) {
+
+ }
+ }));
+
+ }
}
import java.io.InputStream;
import org.junit.Test;
+import org.mockito.Matchers;
import org.sonar.core.util.Protobuf;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.server.computation.CeTask;
@Test
public void submit_task_to_the_queue_and_ask_for_immediate_processing() {
CeTask task = new CeTask("TASK_1", CeTaskTypes.REPORT, "PROJECT_1", "robert");
- when(reportSubmitter.submit(eq("my_project"), eq("My Project"), any(InputStream.class))).thenReturn(task);
+ when(reportSubmitter.submit(eq("my_project"), Matchers.isNull(String.class), eq("My Project"), any(InputStream.class))).thenReturn(task);
TestResponse wsResponse = tester.newRequest()
.setParam("projectKey", "my_project")
.setMethod("POST")
.execute();
- verify(reportSubmitter).submit(eq("my_project"), eq("My Project"), any(InputStream.class));
+ verify(reportSubmitter).submit(eq("my_project"), Matchers.isNull(String.class), eq("My Project"), any(InputStream.class));
verify(reportProcessingScheduler).startAnalysisTaskNow();
// verify the protobuf response
@Test
public void test_response_example() {
CeTask task = new CeTask("TASK_1", CeTaskTypes.REPORT, "PROJECT_1", "robert");
- when(reportSubmitter.submit(eq("my_project"), eq("My Project"), any(InputStream.class))).thenReturn(task);
+ when(reportSubmitter.submit(eq("my_project"), Matchers.isNull(String.class), eq("My Project"), any(InputStream.class))).thenReturn(task);
TestResponse wsResponse = tester.newRequest()
.setParam("projectKey", "my_project")
@Test
public void project_name_is_optional() {
CeTask task = new CeTask("TASK_1", CeTaskTypes.REPORT, "PROJECT_1", "robert");
- when(reportSubmitter.submit(eq("my_project"), eq("my_project"), any(InputStream.class))).thenReturn(task);
+ when(reportSubmitter.submit(eq("my_project"), Matchers.isNull(String.class), eq("my_project"), any(InputStream.class))).thenReturn(task);
tester.newRequest()
.setParam("projectKey", "my_project")
.setMethod("POST")
.execute();
- verify(reportSubmitter).submit(eq("my_project"), eq("my_project"), any(InputStream.class));
+ verify(reportSubmitter).submit(eq("my_project"), Matchers.isNull(String.class), eq("my_project"), any(InputStream.class));
}
}
void sendOrDumpReport(File report) {
ProjectDefinition projectDefinition = projectReactor.getRoot();
String effectiveKey = projectDefinition.getKeyWithBranch();
- String relativeUrl = "/api/ce/submit?projectKey=" + effectiveKey + "&projectName=" + BatchUtils.encodeForUrl(projectDefinition.getName());
+ String relativeUrl = String.format("/api/ce/submit?projectKey=%s&projectName=%s&projectBranch=%s",
+ projectDefinition.getKey(), BatchUtils.encodeForUrl(projectDefinition.getName()), BatchUtils.encodeForUrl(projectDefinition.getBranch()));
String dumpDirLocation = settings.getString(DUMP_REPORT_PROP_KEY);
if (dumpDirLocation == null) {
}
@CheckForNull
- private String getBranch() {
+ public String getBranch() {
String branch = properties.get(CoreProperties.PROJECT_BRANCH_PROPERTY);
if (StringUtils.isNotBlank(branch)) {
return branch;