From 5cf28de7ebc830e49eaf5381bce6c2619aa291b0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 29 Jan 2014 18:45:16 +0100 Subject: [PATCH] Display scm info even if commit begin before from line parameter --- .../source/ws/SourcesShowWsHandler.java | 26 ++++++++++++------- .../source/ws/SourcesShowWsHandlerTest.java | 18 +++++++++++++ ..._and_with_from_param_after_repetition.json | 18 +++++++++++++ 3 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/source/ws/SourcesShowWsHandlerTest/show_source_with_scm_without_repeating_same_lines_and_with_from_param_after_repetition.json diff --git a/sonar-server/src/main/java/org/sonar/server/source/ws/SourcesShowWsHandler.java b/sonar-server/src/main/java/org/sonar/server/source/ws/SourcesShowWsHandler.java index eb5126712e3..7878a48bad6 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/ws/SourcesShowWsHandler.java +++ b/sonar-server/src/main/java/org/sonar/server/source/ws/SourcesShowWsHandler.java @@ -72,7 +72,7 @@ public class SourcesShowWsHandler implements RequestHandler { json.endObject(); } - private void writeScm(String authorData, String scmDateData, int from, int to , JsonWriter json) { + private void writeScm(String authorData, String scmDateData, int from, int to, JsonWriter json) { if (authorData != null) { json.name("scm").beginObject(); List authors = splitLine(authorData); @@ -80,6 +80,7 @@ public class SourcesShowWsHandler implements RequestHandler { String previousAuthor = null; String previousDate = null; + boolean scmDataAdded = false; for (int i = 0; i < authors.size(); i++) { String[] authorWithLine = splitColumn(authors.get(i)); Integer line = Integer.parseInt(authorWithLine[0]); @@ -88,14 +89,15 @@ public class SourcesShowWsHandler implements RequestHandler { String[] dateWithLine = splitColumn(dates.get(i)); String date = dateWithLine[1]; String formattedDate = DateUtils.formatDate(DateUtils.parseDateTime(date)); - if (!author.equals(previousAuthor) && !date.equals(previousDate) && - line >= from && line <= to) { - json.name(Integer.toString(line)).beginArray(); - json.value(author); - json.value(formattedDate); - json.endArray(); + if (line >= from && line <= to) { + if (!isSameCommit(date, previousDate, author, previousAuthor) || !scmDataAdded) { + json.name(Integer.toString(line)).beginArray(); + json.value(author); + json.value(formattedDate); + json.endArray(); + scmDataAdded = true; + } } - previousAuthor = author; previousDate = date; } @@ -103,11 +105,15 @@ public class SourcesShowWsHandler implements RequestHandler { } } - private List splitLine(String line){ + private boolean isSameCommit(String date, String previousDate, String author, String previousAuthor){ + return author.equals(previousAuthor) && date.equals(previousDate); + } + + private List splitLine(String line) { return newArrayList(Splitter.on(";").omitEmptyStrings().split(line)); } - private String[] splitColumn(String column){ + private String[] splitColumn(String column) { return column.split("="); } diff --git a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java index b033f420bdd..64ff710c0bf 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java @@ -145,4 +145,22 @@ public class SourcesShowWsHandlerTest { WsTester.TestRequest request = tester.newRequest("show").setParam("key", componentKey); request.execute().assertJson(getClass(), "show_source_with_scm_without_repeating_same_lines.json"); } + + @Test + public void show_source_with_scm_when_from_is_after_same_commit() throws Exception { + String componentKey = "org.apache.struts:struts:Dispatcher"; + when(sourceService.getSourcesByComponent(componentKey, 3, 5)).thenReturn(newArrayList( + " */", + "", + "public class HelloWorld {" + )); + + // Since line 2, it's the same commit + when(sourceService.getScmAuthorData(componentKey)) + .thenReturn("1=julien;2=simon;3=simon;4=simon;5=simon;6=simon"); + when(sourceService.getScmDateData(componentKey)) + .thenReturn("1=2013-03-13T16:22:31+0100;2=2013-03-14T16:22:31+0100;3=2013-03-14T16:22:31+0100;4=2013-03-14T16:22:31+0100;5=2013-03-14T16:22:31+0100;6=2013-03-14T16:22:31+0100;"); + WsTester.TestRequest request = tester.newRequest("show").setParam("key", componentKey).setParam("from", "3").setParam("to", "5"); + request.execute().assertJson(getClass(), "show_source_with_scm_without_repeating_same_lines_and_with_from_param_after_repetition.json"); + } } diff --git a/sonar-server/src/test/resources/org/sonar/server/source/ws/SourcesShowWsHandlerTest/show_source_with_scm_without_repeating_same_lines_and_with_from_param_after_repetition.json b/sonar-server/src/test/resources/org/sonar/server/source/ws/SourcesShowWsHandlerTest/show_source_with_scm_without_repeating_same_lines_and_with_from_param_after_repetition.json new file mode 100644 index 00000000000..1f852be106e --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/source/ws/SourcesShowWsHandlerTest/show_source_with_scm_without_repeating_same_lines_and_with_from_param_after_repetition.json @@ -0,0 +1,18 @@ +{ + "source": { + "3": " */", + "4": "", + "5": "public class HelloWorld {" + }, + "scm": { + "3": ["simon", "2013-03-14"] + } +} + + + + + + + + -- 2.39.5