@Override
public void define(Context context) {
NewController controller = context.createController(CONTROLLER)
- .setSince("6.6");
+ .setSince("6.6")
+ .setDescription("Manage branch (only available when the Branch plugin is installed)");
Arrays.stream(actions).forEach(action -> action.define(controller));
controller.done();
}
- static void addProjectBranchParams(NewAction action) {
+ static void addProjectParam(NewAction action) {
action
.createParam(PARAM_PROJECT)
.setDescription("Project key")
.setExampleValue(KEY_PROJECT_EXAMPLE_001)
.setRequired(true);
+ }
+
+ static void addBranchParam(NewAction action) {
action
.createParam(PARAM_BRANCH)
- .setDescription("Name of the branch to delete. Can't be the main branch of the project.")
+ .setDescription("Name of the branch")
.setExampleValue("branch1")
.setRequired(true);
}
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;
+import static org.sonar.server.projectbranch.ws.BranchesWs.addBranchParam;
+import static org.sonar.server.projectbranch.ws.BranchesWs.addProjectParam;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_DELETE;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
public void define(NewController context) {
WebService.NewAction action = context.createAction(ACTION_DELETE)
.setSince("6.6")
- .setDescription("Delete a non-main branch of a project. Requires permission to administer the project.")
+ .setDescription("Delete a non-main branch of a project.<br/>" +
+ "Requires 'Administer' rights on the specified project.")
.setResponseExample(Resources.getResource(getClass(), "list-example.json"))
- .setInternal(true)
.setPost(true)
.setHandler(this);
- BranchesWs.addProjectBranchParams(action);
+ addProjectParam(action);
+ addBranchParam(action);
}
@Override
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
import static org.sonar.db.component.BranchType.LONG;
import static org.sonar.db.component.BranchType.SHORT;
-import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
+import static org.sonar.server.projectbranch.ws.BranchesWs.addProjectParam;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction(ACTION_LIST)
.setSince("6.6")
- .setDescription("List the branches of a project")
+ .setDescription("List the branches of a project.<br/>" +
+ "Requires 'Administer' rights on the specified project.")
.setResponseExample(Resources.getResource(getClass(), "list-example.json"))
- .setInternal(true)
.setHandler(this);
- action
- .createParam(PARAM_PROJECT)
- .setDescription("Project key")
- .setExampleValue(KEY_PROJECT_EXAMPLE_001)
- .setRequired(true);
+ addProjectParam(action);
}
@Override
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.server.projectbranch.ws.BranchesWs.addProjectParam;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_RENAME;
-import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_NAME;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
public class RenameAction implements BranchWsAction {
public void define(NewController context) {
WebService.NewAction action = context.createAction(ACTION_RENAME)
.setSince("6.6")
- .setDescription("Rename the main branch of a project. <br />"
+ .setDescription("Rename the main branch of a project.<br/>"
+ "Requires 'Administer' permission on the specified project.")
- .setInternal(true)
.setPost(true)
.setHandler(this);
- BranchesWs.addProjectBranchParams(action);
+ addProjectParam(action);
+ action
+ .createParam(PARAM_NAME)
+ .setDescription("New name of the main branch")
+ .setExampleValue("branch1")
+ .setRequired(true);
}
@Override
public void handle(Request request, Response response) throws Exception {
userSession.checkLoggedIn();
String projectKey = request.mandatoryParam(PARAM_PROJECT);
- String branchKey = request.mandatoryParam(PARAM_BRANCH);
+ String newBranchName = request.mandatoryParam(PARAM_NAME);
try (DbSession dbSession = dbClient.openSession(false)) {
ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey);
checkPermission(project);
- Optional<BranchDto> branch = dbClient.branchDao().selectByKey(dbSession, project.uuid(), branchKey);
- if (branch.isPresent() && !branch.get().isMain()) {
- throw new IllegalArgumentException("Impossible to update branch name: a branch with name \"" + branchKey + "\" already exists in the project.");
- }
+ Optional<BranchDto> existingBranch = dbClient.branchDao().selectByKey(dbSession, project.uuid(), newBranchName);
+ checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(),
+ "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName);
- dbClient.branchDao().updateMainBranchName(dbSession, project.uuid(), branchKey);
+ dbClient.branchDao().updateMainBranchName(dbSession, project.uuid(), newBranchName);
dbSession.commit();
response.noContent();
}
WebService.Action definition = tester.getDef();
assertThat(definition.key()).isEqualTo("delete");
assertThat(definition.isPost()).isTrue();
- assertThat(definition.isInternal()).isTrue();
+ assertThat(definition.isInternal()).isFalse();
assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "branch");
assertThat(definition.since()).isEqualTo("6.6");
}
WebService.Action definition = ws.getDef();
assertThat(definition.key()).isEqualTo("list");
assertThat(definition.isPost()).isFalse();
- assertThat(definition.isInternal()).isTrue();
+ assertThat(definition.isInternal()).isFalse();
assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project");
assertThat(definition.since()).isEqualTo("6.6");
}
WebService.Action definition = tester.getDef();
assertThat(definition.key()).isEqualTo("rename");
assertThat(definition.isPost()).isTrue();
- assertThat(definition.isInternal()).isTrue();
- assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "branch");
+ assertThat(definition.isInternal()).isFalse();
+ assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "name");
assertThat(definition.since()).isEqualTo("6.6");
}
userSession.logIn();
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'branch' parameter is missing");
+ expectedException.expectMessage("The 'name' parameter is missing");
tester.newRequest().setParam("project", "projectName").execute();
}
tester.newRequest()
.setParam("project", project.getKey())
- .setParam("branch", "branch1")
+ .setParam("name", "branch1")
.execute();
}
@Test
- public void successfully_rename() {
+ public void rename() {
userSession.logIn();
ComponentDto project = db.components().insertMainBranch();
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
tester.newRequest()
.setParam("project", project.getKey())
- .setParam("branch", "master")
+ .setParam("name", "master")
.execute();
assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
}
@Test
- public void successfully_rename_with_same_name() {
+ public void rename_with_same_name() {
userSession.logIn();
ComponentDto project = db.components().insertMainBranch();
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
tester.newRequest()
.setParam("project", project.getKey())
- .setParam("branch", "master")
+ .setParam("name", "master")
.execute();
tester.newRequest()
.setParam("project", project.getKey())
- .setParam("branch", "master")
+ .setParam("name", "master")
.execute();
assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
tester.newRequest()
.setParam("project", project.getKey())
- .setParam("branch", "branch")
+ .setParam("name", "branch")
.execute();
}
tester.newRequest()
.setParam("project", "foo")
- .setParam("branch", "branch1")
+ .setParam("name", "branch1")
.execute();
}
}
return post('/api/project_branches/delete', { project, branch }).catch(throwGlobalError);
}
-export function renameBranch(project: string, branch: string): Promise<void | Response> {
- return post('/api/project_branches/rename', { project, branch }).catch(throwGlobalError);
+export function renameBranch(project: string, name: string): Promise<void | Response> {
+ return post('/api/project_branches/rename', { project, name }).catch(throwGlobalError);
}
public static final String PARAM_PROJECT = "project";
public static final String PARAM_COMPONENT = "component";
public static final String PARAM_BRANCH = "branch";
+ public static final String PARAM_NAME = "name";
private ProjectBranchesParameters() {
// static utility class
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsConnector;
-import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
-import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_SHOW;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_DELETE;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_RENAME;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_SHOW;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.CONTROLLER;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_NAME;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
public class ProjectBranchesService extends BaseService {
call(post);
}
- public void rename(String project, String branch) {
+ public void rename(String project, String name) {
PostRequest post = new PostRequest(path(ACTION_RENAME))
.setParam(PARAM_PROJECT, project)
- .setParam(PARAM_BRANCH, branch);
+ .setParam(PARAM_NAME, name);
call(post);
}
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_NAME;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
public class ProjectBranchesServiceTest {
serviceTester.assertThat(postRequest)
.hasPath("rename")
.hasParam(PARAM_PROJECT, "projectKey")
- .hasParam(PARAM_BRANCH, "my_branch")
+ .hasParam(PARAM_NAME, "my_branch")
.andNoOtherParam();
}