Skip to content

Commit

Permalink
SONAR-6012 Create WS /batch/issues
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jan 19, 2015
1 parent 53247da commit b978d17
Show file tree
Hide file tree
Showing 31 changed files with 1,078 additions and 184 deletions.
Expand Up @@ -35,11 +35,13 @@ public class BatchWs implements WebService {
private final BatchIndex batchIndex; private final BatchIndex batchIndex;
private final GlobalRepositoryAction globalRepositoryAction; private final GlobalRepositoryAction globalRepositoryAction;
private final ProjectRepositoryAction projectRepositoryAction; private final ProjectRepositoryAction projectRepositoryAction;
private final IssuesAction issuesAction;


public BatchWs(BatchIndex batchIndex, GlobalRepositoryAction globalRepositoryAction, ProjectRepositoryAction projectRepositoryAction) { public BatchWs(BatchIndex batchIndex, GlobalRepositoryAction globalRepositoryAction, ProjectRepositoryAction projectRepositoryAction, IssuesAction issuesAction) {
this.batchIndex = batchIndex; this.batchIndex = batchIndex;
this.globalRepositoryAction = globalRepositoryAction; this.globalRepositoryAction = globalRepositoryAction;
this.projectRepositoryAction = projectRepositoryAction; this.projectRepositoryAction = projectRepositoryAction;
this.issuesAction = issuesAction;
} }


@Override @Override
Expand All @@ -52,6 +54,7 @@ public void define(Context context) {
defineFileAction(controller); defineFileAction(controller);
globalRepositoryAction.define(controller); globalRepositoryAction.define(controller);
projectRepositoryAction.define(controller); projectRepositoryAction.define(controller);
issuesAction.define(controller);


controller.done(); controller.done();
} }
Expand Down
Expand Up @@ -58,7 +58,7 @@ void define(WebService.NewController controller) {
public void handle(Request request, Response response) throws Exception { public void handle(Request request, Response response) throws Exception {
UserSession userSession = UserSession.get(); UserSession userSession = UserSession.get();
boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION); boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
boolean hasDryRunPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION); boolean hasDryRunPerm = userSession.hasGlobalPermission(GlobalPermissions.PREVIEW_EXECUTION);


DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
Expand Down
@@ -0,0 +1,127 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.batch;

import com.google.common.base.Charsets;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.web.UserRole;
import org.sonar.batch.protocol.input.issues.PreviousIssue;
import org.sonar.batch.protocol.input.issues.PreviousIssueHelper;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.issue.db.BatchIssueDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.server.db.DbClient;
import org.sonar.server.plugins.MimeTypes;
import org.sonar.server.user.UserSession;

import javax.annotation.Nullable;

import java.io.OutputStreamWriter;

public class IssuesAction implements RequestHandler {

private static final String PARAM_KEY = "key";

private final DbClient dbClient;

public IssuesAction(DbClient dbClient) {
this.dbClient = dbClient;
}

void define(WebService.NewController controller) {
WebService.NewAction action = controller.createAction("issues")
.setDescription("Return open issues")
.setSince("5.1")
.setInternal(true)
.setHandler(this);

action
.createParam(PARAM_KEY)
.setRequired(true)
.setDescription("Project or module key")
.setExampleValue("org.codehaus.sonar:sonar");
}

@Override
public void handle(Request request, Response response) throws Exception {
UserSession.get().checkGlobalPermission(GlobalPermissions.PREVIEW_EXECUTION);
final String moduleKey = request.mandatoryParam(PARAM_KEY);

PreviousIssueHelper previousIssueHelper = PreviousIssueHelper.create(new OutputStreamWriter(response.stream().output(), Charsets.UTF_8));
DbSession session = dbClient.openSession(false);
try {
ComponentDto moduleOrProject = dbClient.componentDao().getByKey(session, moduleKey);
UserSession.get().checkComponentPermission(UserRole.USER, moduleKey);

response.stream().setMediaType(MimeTypes.JSON);
BatchIssueResultHandler batchIssueResultHandler = new BatchIssueResultHandler(previousIssueHelper);
if (moduleOrProject.isRootProject()) {
dbClient.issueDao().selectNonClosedIssuesByProjectUuid(session, moduleOrProject.uuid(), batchIssueResultHandler);
} else {
dbClient.issueDao().selectNonClosedIssuesByModuleUuid(session, moduleOrProject.uuid(), batchIssueResultHandler);
}

} finally {
previousIssueHelper.close();
MyBatis.closeQuietly(session);
}
}

private static class BatchIssueResultHandler implements ResultHandler {
private final PreviousIssueHelper previousIssueHelper;

public BatchIssueResultHandler(PreviousIssueHelper previousIssueHelper) {
this.previousIssueHelper = previousIssueHelper;
}

@Override
public void handleResult(ResultContext rc) {
previousIssueHelper.addIssue((BatchIssueDto) rc.getResultObject(), new BatchIssueFunction());
}
}

private static class BatchIssueFunction implements PreviousIssueHelper.Function<BatchIssueDto, PreviousIssue> {
@Override
public PreviousIssue apply(@Nullable BatchIssueDto batchIssueDto) {
if (batchIssueDto != null) {
return new PreviousIssue()
.setKey(batchIssueDto.getKey())
.setComponentPath(batchIssueDto.getComponentPath())
.setChecksum(batchIssueDto.getChecksum())
.setAssigneeLogin(batchIssueDto.getAssigneeLogin())
.setAssigneeFullname(batchIssueDto.getAssigneeName())
.setLine(batchIssueDto.getLine())
.setRuleKey(batchIssueDto.getRuleRepo(), batchIssueDto.getRuleKey())
.setMessage(batchIssueDto.getMessage())
.setResolution(batchIssueDto.getResolution())
.setStatus(batchIssueDto.getStatus());
}
return null;
}
}
}
Expand Up @@ -135,7 +135,7 @@ private void aggregateParentModules(String component, List<ComponentDto> parents
} }


private void addSettingsToChildrenModules(ProjectReferentials ref, String moduleKey, Map<String, String> parentProperties, TreeModuleSettings treeModuleSettings, private void addSettingsToChildrenModules(ProjectReferentials ref, String moduleKey, Map<String, String> parentProperties, TreeModuleSettings treeModuleSettings,
boolean hasScanPerm, DbSession session) { boolean hasScanPerm, DbSession session) {
Map<String, String> currentParentProperties = newHashMap(); Map<String, String> currentParentProperties = newHashMap();
currentParentProperties.putAll(parentProperties); currentParentProperties.putAll(parentProperties);
currentParentProperties.putAll(getPropertiesMap(treeModuleSettings.findModuleSettings(moduleKey), hasScanPerm)); currentParentProperties.putAll(getPropertiesMap(treeModuleSettings.findModuleSettings(moduleKey), hasScanPerm));
Expand Down Expand Up @@ -235,7 +235,7 @@ private void addFileData(DbSession session, ProjectReferentials ref, List<Compon
private void checkPermission(boolean preview) { private void checkPermission(boolean preview) {
UserSession userSession = UserSession.get(); UserSession userSession = UserSession.get();
boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION); boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
boolean hasPreviewPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION); boolean hasPreviewPerm = userSession.hasGlobalPermission(GlobalPermissions.PREVIEW_EXECUTION);
if (!hasPreviewPerm && !hasScanPerm) { if (!hasPreviewPerm && !hasScanPerm) {
throw new ForbiddenException("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator."); throw new ForbiddenException("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator.");
} }
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.issue.db; package org.sonar.server.issue.db;


import org.apache.ibatis.session.ResultHandler;
import org.sonar.core.issue.db.IssueDto; import org.sonar.core.issue.db.IssueDto;
import org.sonar.core.issue.db.IssueMapper; import org.sonar.core.issue.db.IssueMapper;
import org.sonar.core.persistence.DaoComponent; import org.sonar.core.persistence.DaoComponent;
Expand Down Expand Up @@ -58,6 +59,14 @@ public List<IssueDto> selectByKeys(DbSession session, Collection<String> keys) {
return mapper(session).selectByKeys(keys); return mapper(session).selectByKeys(keys);
} }


public void selectNonClosedIssuesByModuleUuid(DbSession session, String moduleUuid, ResultHandler handler) {
session.select("org.sonar.core.issue.db.IssueMapper.selectNonClosedIssuesByModuleUuid", moduleUuid, handler);
}

public void selectNonClosedIssuesByProjectUuid(DbSession session, String projectUuid, ResultHandler handler) {
session.select("org.sonar.core.issue.db.IssueMapper.selectNonClosedIssuesByProjectUuid", projectUuid, handler);
}

public void insert(DbSession session, IssueDto dto) { public void insert(DbSession session, IssueDto dto) {
mapper(session).insert(dto); mapper(session).insert(dto);
} }
Expand Down
Expand Up @@ -30,11 +30,7 @@
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;


import java.util.Collection; import java.util.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class IssueDoc extends BaseDoc implements Issue { public class IssueDoc extends BaseDoc implements Issue {


Expand Down Expand Up @@ -199,101 +195,125 @@ public String filePath() {
return getNullableField(IssueIndexDefinition.FIELD_ISSUE_FILE_PATH); return getNullableField(IssueIndexDefinition.FIELD_ISSUE_FILE_PATH);
} }


public void setKey(@Nullable String s) { public IssueDoc setKey(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_KEY, s); setField(IssueIndexDefinition.FIELD_ISSUE_KEY, s);
return this;
} }


public void setComponentUuid(@Nullable String s) { public IssueDoc setComponentUuid(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_COMPONENT_UUID, s); setField(IssueIndexDefinition.FIELD_ISSUE_COMPONENT_UUID, s);
return this;
} }


public void setModuleUuid(@Nullable String s) { public IssueDoc setModuleUuid(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_MODULE_UUID, s); setField(IssueIndexDefinition.FIELD_ISSUE_MODULE_UUID, s);
return this;
} }


public void setProjectUuid(@Nullable String s) { public IssueDoc setProjectUuid(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID, s); setField(IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID, s);
return this;
} }


public void setRuleKey(@Nullable String s) { public IssueDoc setRuleKey(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_RULE_KEY, s); setField(IssueIndexDefinition.FIELD_ISSUE_RULE_KEY, s);
return this;
} }


public void setLanguage(@Nullable String s) { public IssueDoc setLanguage(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_LANGUAGE, s); setField(IssueIndexDefinition.FIELD_ISSUE_LANGUAGE, s);
return this;
} }


public void setSeverity(@Nullable String s) { public IssueDoc setSeverity(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_SEVERITY, s); setField(IssueIndexDefinition.FIELD_ISSUE_SEVERITY, s);
setField(IssueIndexDefinition.FIELD_ISSUE_SEVERITY_VALUE, Severity.ALL.indexOf(s)); setField(IssueIndexDefinition.FIELD_ISSUE_SEVERITY_VALUE, Severity.ALL.indexOf(s));
return this;
} }


public void setMessage(@Nullable String s) { public IssueDoc setMessage(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_MESSAGE, s); setField(IssueIndexDefinition.FIELD_ISSUE_MESSAGE, s);
return this;
} }


public void setLine(@Nullable Integer i) { public IssueDoc setLine(@Nullable Integer i) {
setField(IssueIndexDefinition.FIELD_ISSUE_LINE, i); setField(IssueIndexDefinition.FIELD_ISSUE_LINE, i);
return this;
} }


public void setEffortToFix(@Nullable Double d) { public IssueDoc setEffortToFix(@Nullable Double d) {
setField(IssueIndexDefinition.FIELD_ISSUE_EFFORT, d); setField(IssueIndexDefinition.FIELD_ISSUE_EFFORT, d);
return this;
} }


public void setStatus(@Nullable String s) { public IssueDoc setStatus(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_STATUS, s); setField(IssueIndexDefinition.FIELD_ISSUE_STATUS, s);
return this;
} }


public void setResolution(@Nullable String s) { public IssueDoc setResolution(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_RESOLUTION, s); setField(IssueIndexDefinition.FIELD_ISSUE_RESOLUTION, s);
return this;
} }


public void setReporter(@Nullable String s) { public IssueDoc setReporter(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_REPORTER, s); setField(IssueIndexDefinition.FIELD_ISSUE_REPORTER, s);
return this;
} }


public void setAssignee(@Nullable String s) { public IssueDoc setAssignee(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_ASSIGNEE, s); setField(IssueIndexDefinition.FIELD_ISSUE_ASSIGNEE, s);
return this;
} }


public void setFuncUpdateDate(@Nullable Date d) { public IssueDoc setFuncUpdateDate(@Nullable Date d) {
setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_UPDATED_AT, d); setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_UPDATED_AT, d);
return this;
} }


public void setFuncCreationDate(@Nullable Date d) { public IssueDoc setFuncCreationDate(@Nullable Date d) {
setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT, d); setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT, d);
return this;
} }


public void setTechnicalUpdateDate(@Nullable Date d) { public IssueDoc setTechnicalUpdateDate(@Nullable Date d) {
setField(IssueIndexDefinition.FIELD_ISSUE_TECHNICAL_UPDATED_AT, d); setField(IssueIndexDefinition.FIELD_ISSUE_TECHNICAL_UPDATED_AT, d);
return this;
} }


public void setFuncCloseDate(@Nullable Date d) { public IssueDoc setFuncCloseDate(@Nullable Date d) {
setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_CLOSED_AT, d); setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_CLOSED_AT, d);
return this;
} }


public void setAttributes(@Nullable String s) { public IssueDoc setAttributes(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_ATTRIBUTES, s); setField(IssueIndexDefinition.FIELD_ISSUE_ATTRIBUTES, s);
return this;
} }


public void setAuthorLogin(@Nullable String s) { public IssueDoc setAuthorLogin(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_AUTHOR_LOGIN, s); setField(IssueIndexDefinition.FIELD_ISSUE_AUTHOR_LOGIN, s);
return this;
} }


public void setActionPlanKey(@Nullable String s) { public IssueDoc setActionPlanKey(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_ACTION_PLAN, s); setField(IssueIndexDefinition.FIELD_ISSUE_ACTION_PLAN, s);
return this;
} }


public void setDebt(@Nullable Long l) { public IssueDoc setDebt(@Nullable Long l) {
setField(IssueIndexDefinition.FIELD_ISSUE_DEBT, l); setField(IssueIndexDefinition.FIELD_ISSUE_DEBT, l);
return this;
} }


public void setFilePath(@Nullable String s) { public IssueDoc setFilePath(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_FILE_PATH, s); setField(IssueIndexDefinition.FIELD_ISSUE_FILE_PATH, s);
return this;
} }


public void setModuleUuidPath(@Nullable String s) { public IssueDoc setModuleUuidPath(@Nullable String s) {
setField(IssueIndexDefinition.FIELD_ISSUE_MODULE_PATH, s); setField(IssueIndexDefinition.FIELD_ISSUE_MODULE_PATH, s);
return this;
} }


@Override @Override
Expand All @@ -302,7 +322,8 @@ public Collection<String> tags() {
return getNullableField(IssueIndexDefinition.FIELD_ISSUE_TAGS); return getNullableField(IssueIndexDefinition.FIELD_ISSUE_TAGS);
} }


public void setTags(@Nullable Collection<String> tags) { public IssueDoc setTags(@Nullable Collection<String> tags) {
setField(IssueIndexDefinition.FIELD_ISSUE_TAGS, tags); setField(IssueIndexDefinition.FIELD_ISSUE_TAGS, tags);
return this;
} }
} }

0 comments on commit b978d17

Please sign in to comment.