Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-6350 /api/sources/scm should read information from index
- Loading branch information
1 parent
97371fc
commit ed7971e
Showing
22 changed files
with
445 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
server/sonar-server/src/main/java/org/sonar/server/source/ws/ScmAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* 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.source.ws; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.common.io.Resources; | ||
import org.apache.commons.lang.ObjectUtils; | ||
import org.apache.commons.lang.builder.EqualsBuilder; | ||
import org.sonar.api.server.ws.Request; | ||
import org.sonar.api.server.ws.Response; | ||
import org.sonar.api.server.ws.WebService; | ||
import org.sonar.api.utils.DateUtils; | ||
import org.sonar.api.utils.text.JsonWriter; | ||
import org.sonar.api.web.UserRole; | ||
import org.sonar.core.component.ComponentDto; | ||
import org.sonar.core.persistence.DbSession; | ||
import org.sonar.server.db.DbClient; | ||
import org.sonar.server.exceptions.NotFoundException; | ||
import org.sonar.server.source.index.SourceLineDoc; | ||
import org.sonar.server.source.index.SourceLineIndex; | ||
import org.sonar.server.user.UserSession; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
public class ScmAction implements SourcesAction { | ||
|
||
private final DbClient dbClient; | ||
private final SourceLineIndex sourceLineIndex; | ||
|
||
public ScmAction(DbClient dbClient, SourceLineIndex sourceLineIndex) { | ||
this.dbClient = dbClient; | ||
this.sourceLineIndex = sourceLineIndex; | ||
} | ||
|
||
@Override | ||
public void define(WebService.NewController controller) { | ||
WebService.NewAction action = controller.createAction("scm") | ||
.setDescription("Get SCM information of source files. Require See Source Code permission on file's project<br/>" + | ||
"Each element of the result array is composed of:" + | ||
"<ol>" + | ||
"<li>Line number</li>" + | ||
"<li>Author of the commit</li>" + | ||
"<li>Datetime of the commit (before 5.2 it was only the Date)</li>" + | ||
"<li>Revision of the commit (added in 5.2)</li>" + | ||
"</ol>") | ||
.setSince("4.4") | ||
.setResponseExample(Resources.getResource(getClass(), "example-scm.json")) | ||
.setHandler(this); | ||
|
||
action | ||
.createParam("key") | ||
.setRequired(true) | ||
.setDescription("File key") | ||
.setExampleValue("my_project:/src/foo/Bar.php"); | ||
|
||
action | ||
.createParam("from") | ||
.setDescription("First line to return. Starts at 1") | ||
.setExampleValue("10") | ||
.setDefaultValue("1"); | ||
|
||
action | ||
.createParam("to") | ||
.setDescription("Last line to return (inclusive)") | ||
.setExampleValue("20"); | ||
|
||
action | ||
.createParam("commits_by_line") | ||
.setDescription("Group lines by SCM commit if value is false, else display commits for each line, even if two " + | ||
"consecutive lines relate to the same commit.") | ||
.setBooleanPossibleValues() | ||
.setDefaultValue("false"); | ||
} | ||
|
||
@Override | ||
public void handle(Request request, Response response) { | ||
String fileKey = request.mandatoryParam("key"); | ||
int from = Math.max(request.mandatoryParamAsInt("from"), 1); | ||
int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE); | ||
boolean commitsByLine = request.mandatoryParamAsBoolean("commits_by_line"); | ||
|
||
DbSession session = dbClient.openSession(false); | ||
try { | ||
ComponentDto fileDto = dbClient.componentDao().getByKey(session, fileKey); | ||
UserSession.get().checkProjectUuidPermission(UserRole.CODEVIEWER, fileDto.projectUuid()); | ||
List<SourceLineDoc> sourceLines = sourceLineIndex.getLines(fileDto.uuid(), from, to); | ||
if (sourceLines.isEmpty()) { | ||
throw new NotFoundException("File '" + fileKey + "' has no sources"); | ||
} | ||
|
||
JsonWriter json = response.newJsonWriter().beginObject(); | ||
writeSource(sourceLines, commitsByLine, json); | ||
json.endObject().close(); | ||
} finally { | ||
session.close(); | ||
} | ||
} | ||
|
||
private static void writeSource(List<SourceLineDoc> lines, boolean showCommitsByLine, JsonWriter json) { | ||
json.name("scm").beginArray(); | ||
|
||
SourceLineDoc previousLine = null; | ||
boolean started = false; | ||
for (SourceLineDoc lineDoc : lines) { | ||
if (hasScm(lineDoc) && (!started || showCommitsByLine || !isSameCommit(previousLine, lineDoc))) { | ||
json.beginArray() | ||
.value(lineDoc.line()) | ||
.value(lineDoc.scmAuthor()); | ||
Date date = lineDoc.scmDate(); | ||
json.value(date == null ? null : DateUtils.formatDateTime(date)); | ||
json.value(lineDoc.scmRevision()); | ||
json.endArray(); | ||
started = true; | ||
} | ||
previousLine = lineDoc; | ||
} | ||
json.endArray(); | ||
} | ||
|
||
private static boolean isSameCommit(SourceLineDoc previousLine, SourceLineDoc currentLine) { | ||
return new EqualsBuilder() | ||
.append(previousLine.scmAuthor(), currentLine.scmAuthor()) | ||
.append(previousLine.scmDate(), currentLine.scmDate()) | ||
.append(previousLine.scmRevision(), currentLine.scmRevision()) | ||
.isEquals(); | ||
} | ||
|
||
private static boolean hasScm(SourceLineDoc line){ | ||
return !Strings.isNullOrEmpty(line.scmAuthor()) || line.scmDate() != null || !Strings.isNullOrEmpty(line.scmRevision()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
server/sonar-server/src/main/java/org/sonar/server/source/ws/SourcesAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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.source.ws; | ||
|
||
import org.sonar.api.server.ws.RequestHandler; | ||
import org.sonar.api.server.ws.WebService; | ||
|
||
public interface SourcesAction extends RequestHandler { | ||
|
||
void define(WebService.NewController controller); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
server/sonar-server/src/main/resources/org/sonar/server/source/ws/example-scm.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"scm": [ | ||
[1, "julien", "2013-03-13"], | ||
[2, "julien", "2013-03-14"], | ||
[3, "simon", "2014-01-01"] | ||
[1, "julien", "2013-03-13T12:34:56+0100", "a1e2b3e5d6f5"], | ||
[2, "julien", "2013-03-14T13:17:22+0100", "b1e2b3e5d6f5"], | ||
[3, "simon", "2014-01-01T15:35:36+0100", "c1e2b3e5d6f5"] | ||
] | ||
} |
Oops, something went wrong.