org.sonar.server.issue.ws.DoTransitionAction.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.server.issue.ws.DoTransitionAction.java

Source

/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.issue.ws;

import com.google.common.io.Resources;
import java.util.Date;
import org.sonar.api.issue.DefaultTransitions;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.issue.IssueDto;
import org.sonar.server.issue.IssueFinder;
import org.sonar.server.issue.IssueUpdater;
import org.sonar.server.issue.TransitionService;
import org.sonar.server.user.UserSession;

import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_DO_TRANSITION;
import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TRANSITION;

public class DoTransitionAction implements IssuesWsAction {

    private final DbClient dbClient;
    private final UserSession userSession;
    private final IssueFinder issueFinder;
    private final IssueUpdater issueUpdater;
    private final TransitionService transitionService;
    private final OperationResponseWriter responseWriter;

    public DoTransitionAction(DbClient dbClient, UserSession userSession, IssueFinder issueFinder,
            IssueUpdater issueUpdater, TransitionService transitionService,
            OperationResponseWriter responseWriter) {
        this.dbClient = dbClient;
        this.userSession = userSession;
        this.issueFinder = issueFinder;
        this.issueUpdater = issueUpdater;
        this.transitionService = transitionService;
        this.responseWriter = responseWriter;
    }

    @Override
    public void define(WebService.NewController controller) {
        WebService.NewAction action = controller.createAction(ACTION_DO_TRANSITION).setDescription(
                "Do workflow transition on an issue. Requires authentication and Browse permission on project.<br/>"
                        + "The transitions '" + DefaultTransitions.WONT_FIX + "' and '"
                        + DefaultTransitions.FALSE_POSITIVE + "' require the permission 'Administer Issues'.")
                .setSince("3.6").setHandler(this)
                .setResponseExample(Resources.getResource(this.getClass(), "do_transition-example.json"))
                .setPost(true);

        action.createParam(PARAM_ISSUE).setDescription("Issue key").setRequired(true)
                .setExampleValue(Uuids.UUID_EXAMPLE_01);
        action.createParam(PARAM_TRANSITION).setDescription("Transition").setRequired(true)
                .setPossibleValues(DefaultTransitions.ALL);
    }

    @Override
    public void handle(Request request, Response response) {
        userSession.checkLoggedIn();
        String issue = request.mandatoryParam(PARAM_ISSUE);
        try (DbSession dbSession = dbClient.openSession(false)) {
            IssueDto issueDto = issueFinder.getByKey(dbSession, issue);
            doTransition(dbSession, issueDto.toDefaultIssue(), request.mandatoryParam(PARAM_TRANSITION));
            responseWriter.write(issue, request, response);
        }
    }

    private void doTransition(DbSession session, DefaultIssue defaultIssue, String transitionKey) {
        IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getLogin());
        transitionService.checkTransitionPermission(transitionKey, defaultIssue);
        if (transitionService.doTransition(defaultIssue, context, transitionKey)) {
            issueUpdater.saveIssue(session, defaultIssue, context, null);
        }
    }
}