@Override protected void configureModule() { add( ListAction.class, DeleteAction.class, RenameAction.class, BranchesWs.class); } }
private static void addBranch(ProjectBranches.ListWsResponse.Builder response, BranchDto branch, Map<String, BranchDto> mergeBranchesByUuid, @Nullable LiveMeasureDto qualityGateMeasure, BranchStatistics branchStatistics, @Nullable String analysisDate) { ProjectBranches.Branch.Builder builder = toBranchBuilder(branch, Optional.ofNullable(mergeBranchesByUuid.get(branch.getMergeBranchUuid()))); setBranchStatus(builder, branch, qualityGateMeasure, branchStatistics); if (analysisDate != null) { builder.setAnalysisDate(analysisDate); } response.addBranches(builder); }
@Override public void define(NewController context) { WebService.NewAction action = context.createAction(ACTION_DELETE) .setSince("6.6") .setDescription("Delete a non-main branch of a project.<br/>" + "Requires 'Administer' rights on the specified project.") .setPost(true) .setHandler(this); addProjectParam(action); addBranchParam(action); }
@Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_PROJECT); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getByKey(dbSession, projectKey); checkPermission(project); checkArgument(ALLOWED_QUALIFIERS.contains(project.qualifier()), "Invalid project"); Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project).stream() .filter(b -> b.getBranchType() == SHORT || b.getBranchType() == LONG) .collect(toList()); List<String> branchUuids = branches.stream().map(BranchDto::getUuid).collect(toList()); Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao() .selectByUuids(dbSession, branches.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())) .stream().collect(uniqueIndex(BranchDto::getUuid)); Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao() .selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, singletonList(ALERT_STATUS_KEY)).stream() .collect(uniqueIndex(LiveMeasureDto::getComponentUuid)); Map<String, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), branches.stream() .filter(b -> b.getBranchType().equals(SHORT)) .map(BranchDto::getUuid).collect(toList())).stream() .collect(uniqueIndex(BranchStatistics::getBranchUuid, Function.identity())); Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao() .selectLastAnalysesByRootComponentUuids(dbSession, branchUuids).stream() .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt()))); ProjectBranches.ListWsResponse.Builder protobufResponse = ProjectBranches.ListWsResponse.newBuilder(); branches.forEach(b -> addBranch(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid()))); WsUtils.writeProtobuf(protobufResponse.build(), request, response); } }
@Override public void define(WebService.NewController context) { WebService.NewAction action = context.createAction(ACTION_LIST) .setSince("6.6") .setDescription("List the branches of a project.<br/>" + "Requires 'Browse' or 'Execute analysis' rights on the specified project.") .setResponseExample(Resources.getResource(getClass(), "list-example.json")) .setChangelog(new Change("7.2", "Application can be used on this web service")) .setHandler(this); addProjectParam(action); }
@Override public void define(Context context) { NewController controller = context.createController(CONTROLLER) .setSince("6.6") .setDescription("Manage branch (only available when the Branch plugin is installed)"); Arrays.stream(actions).forEach(action -> action.define(controller)); controller.done(); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String newBranchName = request.mandatoryParam(PARAM_NAME); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(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(), newBranchName); dbSession.commit(); response.noContent(); } }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String branchKey = request.mandatoryParam(PARAM_BRANCH); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); BranchDto branch = checkFoundWithOptional( dbClient.branchDao().selectByBranchKey(dbSession, project.uuid(), branchKey), "Branch '%s' not found for project '%s'", branchKey, projectKey); if (branch.isMain()) { throw new IllegalArgumentException("Only non-main branches can be deleted"); } ComponentDto branchComponent = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey); componentCleanerService.deleteBranch(dbSession, branchComponent); projectLifeCycleListeners.onProjectBranchesDeleted(singleton(from(project))); response.noContent(); } }
@Test public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new BranchWsModule().configure(container); assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 4); } }
@Test public void define_ws() { BranchesWs underTest = new BranchesWs(new BranchWsAction() { @Override public void define(WebService.NewController context) { context.createAction("foo").setHandler(this); } @Override public void handle(Request request, Response response) { } }); WebService.Context context = new WebService.Context(); underTest.define(context); assertThat(context.controller("api/project_branches").action("foo")).isNotNull(); }
@Test public void delete_branch() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1")); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); tester.newRequest() .setParam("project", project.getKey()) .setParam("branch", "branch1") .execute(); verifyDeletedKey(branch.getDbKey()); verify(projectLifeCycleListeners).onProjectBranchesDeleted(singleton(Project.from(project))); }
@Override public void define(NewController context) { WebService.NewAction action = context.createAction(ACTION_DELETE) .setSince("6.6") .setDescription("Delete a non-main branch of a project.<br/>" + "Requires 'Administer' rights on the specified project.") .setPost(true) .setHandler(this); addProjectParam(action); addBranchParam(action); }
private static void addBranch(ProjectBranches.ListWsResponse.Builder response, BranchDto branch, Map<String, BranchDto> mergeBranchesByUuid, @Nullable LiveMeasureDto qualityGateMeasure, BranchStatistics branchStatistics, @Nullable String analysisDate) { ProjectBranches.Branch.Builder builder = toBranchBuilder(branch, Optional.ofNullable(mergeBranchesByUuid.get(branch.getMergeBranchUuid()))); setBranchStatus(builder, branch, qualityGateMeasure, branchStatistics); if (analysisDate != null) { builder.setAnalysisDate(analysisDate); } response.addBranches(builder); }
@Override public void define(NewController context) { WebService.NewAction action = context.createAction(ACTION_RENAME) .setSince("6.6") .setDescription("Rename the main branch of a project.<br/>" + "Requires 'Administer' permission on the specified project.") .setPost(true) .setHandler(this); addProjectParam(action); action .createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(255) .setDescription("New name of the main branch") .setExampleValue("branch1"); }
@Override protected void configureModule() { add( ListAction.class, DeleteAction.class, RenameAction.class, BranchesWs.class); } }
@Override public void define(Context context) { NewController controller = context.createController(CONTROLLER) .setSince("6.6") .setDescription("Manage branch (only available when the Branch plugin is installed)"); Arrays.stream(actions).forEach(action -> action.define(controller)); controller.done(); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String newBranchName = request.mandatoryParam(PARAM_NAME); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(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(), newBranchName); dbSession.commit(); response.noContent(); } }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String branchKey = request.mandatoryParam(PARAM_BRANCH); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); BranchDto branch = checkFoundWithOptional( dbClient.branchDao().selectByBranchKey(dbSession, project.uuid(), branchKey), "Branch '%s' not found for project '%s'", branchKey, projectKey); if (branch.isMain()) { throw new IllegalArgumentException("Only non-main branches can be deleted"); } ComponentDto branchComponent = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey); componentCleanerService.deleteBranch(dbSession, branchComponent); response.noContent(); } }
@Override public void define(WebService.NewController context) { WebService.NewAction action = context.createAction(ACTION_LIST) .setSince("6.6") .setDescription("List the branches of a project.<br/>" + "Requires 'Browse' or 'Execute analysis' rights on the specified project.") .setResponseExample(Resources.getResource(getClass(), "list-example.json")) .setHandler(this); addProjectParam(action); }
@Override public void define(NewController context) { WebService.NewAction action = context.createAction(ACTION_RENAME) .setSince("6.6") .setDescription("Rename the main branch of a project.<br/>" + "Requires 'Administer' permission on the specified project.") .setPost(true) .setHandler(this); addProjectParam(action); action .createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(255) .setDescription("New name of the main branch") .setExampleValue("branch1"); }