From d5d5c314c1ad9f73bd2e7ced90fa3677305c5a6d Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sun, 24 May 2015 22:45:00 +0200 Subject: [PATCH] Comment individual commits --- CHANGELOG.md | 7 +- README.md | 4 +- .../jvcts/test/ConfigurationWebTest.java | 11 +++ .../jvcts/ViolationsToStashDescriptor.java | 2 + .../jvcts/config/ViolationsToStashConfig.java | 9 ++ .../config/ViolationsToStashConfigHelper.java | 1 + .../plugins/jvcts/perform/JvctsPerformer.java | 32 ++++++-- .../plugins/jvcts/stash/JvctsStashClient.java | 40 ++++++++- .../ViolationsToStashRecorder/config.jelly | 10 ++- .../perform/JvctsPerformerParseTest.java | 82 ++++++++++++++++--- .../plugins/jvcts/stash/StashClientFaker.java | 45 +++++----- 11 files changed, 193 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97db520..b06be34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ -# Jenkins Violation Comments to Stash Plugin +# Violation Comments to Stash Plugin -Changelog of Jenkins Violation Comments to Stash Plugin +Changelog of Violation Comments to Stash Plugin + +## 1.2 +* Can, optionally, add comments to individual commits. And/Or to pull requests. ## 1.1 * Adding limit parameter to changes request in Stash Client. So that all files in PR gets commented. diff --git a/README.md b/README.md index b01c8e9..1420808 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,14 @@ There is a screenshot of the configuration GUI [here](https://raw.githubusercont Available in Jenkins [here](https://wiki.jenkins-ci.org/display/JENKINS/Violation+Comments+to+Stash+Plugin). #Features -* Comment pull requests with code analyzers comments +* Comment pull requests, or individual commits, with code analyzers comments * Supporting: CheckStyle, CSSLint, JSLint, CodeNarc, CPPLint, FindBugs, FxCop, Gendarme, JCEReport, PEP8, PerlCritic, PMD, PyLint, Simian, StyleCop ## Use case Here is an example use case where a pull request is triggered from Stash, merged, checked and comments added to pull request in Stash. +You may also use it for an ordinary build job, to simply comment the commit that was built. + ### Notify Jenkins from Stash You may use [Pull Request Notifier for Stash](https://github.com/tomasbjerre/pull-request-notifier-for-stash) to trigger a Jenkins build from an event in Stash. It can supply any parameters and variables you may need. Here is an example URL. diff --git a/plugin-test/src/test/java/org/jenkinsci/plugins/jvcts/test/ConfigurationWebTest.java b/plugin-test/src/test/java/org/jenkinsci/plugins/jvcts/test/ConfigurationWebTest.java index 6704181..1ad446d 100644 --- a/plugin-test/src/test/java/org/jenkinsci/plugins/jvcts/test/ConfigurationWebTest.java +++ b/plugin-test/src/test/java/org/jenkinsci/plugins/jvcts/test/ConfigurationWebTest.java @@ -39,6 +39,7 @@ public class ConfigurationWebTest { private static final String JSLINT_PATTERN = "descriptor.config.parserConfigs[14].pattern"; private static final String CSSLINT_PATTERN = "descriptor.config.parserConfigs[15].pattern"; private static final String STASH_PULL_REQUEST_ID = "stashPullRequestId"; + private static final String COMMIT_HASH = "commitHash"; private static final String STASH_REPO = "stashRepo"; private static final String STASH_PROJECT = "stashProject"; private static final String STASH_BASE_URL = "stashBaseUrl"; @@ -97,6 +98,7 @@ public void testPluginConfiguration() throws InterruptedException { assertTrue(consoleText, consoleText.contains("stashBaseUrl: " + HTTP_LOCALHOST_8456)); assertTrue(consoleText, consoleText.contains("stashProject: ABC")); assertTrue(consoleText, consoleText.contains("stashPullRequestId: 100")); + assertTrue(consoleText, consoleText.contains("commitHash: abcd1234")); assertTrue(consoleText, consoleText.contains("stashRepo: a_repo")); assertTrue(consoleText, consoleText.contains("checkstyle: **/checkstyle-report.xml")); assertTrue(consoleText, consoleText.contains("checkstyle pathPrefix: pathPrefix")); @@ -127,6 +129,7 @@ public void testPluginConfiguration() throws InterruptedException { webDriver.findElement(name(STASH_PROJECT)).clear(); webDriver.findElement(name(STASH_REPO)).clear(); webDriver.findElement(name(STASH_PULL_REQUEST_ID)).clear(); + webDriver.findElement(name(COMMIT_HASH)).clear(); webDriver.findElement(name(PATH_PREFIX)).clear(); webDriver.findElement(name(CHECKSTYLE_PATTERN)).clear(); webDriver.findElement(name(CODENARC_PATTERN)).clear(); @@ -150,6 +153,7 @@ public void testPluginConfiguration() throws InterruptedException { webDriver.findElement(name(STASH_PROJECT)).sendKeys("DEF"); webDriver.findElement(name(STASH_REPO)).sendKeys("a_repo2"); webDriver.findElement(name(STASH_PULL_REQUEST_ID)).sendKeys("101"); + webDriver.findElement(name(COMMIT_HASH)).sendKeys("abcd12345"); webDriver.findElement(name(CHECKSTYLE_PATTERN)).sendKeys("**/new-checkstyle-report.xml"); webDriver.findElement(name(PMD_PATTERN)).sendKeys("**/new-pmd-report.xml"); webDriver.findElement(name(JSLINT_PATTERN)).sendKeys("**/new-jslint-report.xml"); @@ -174,6 +178,7 @@ public void testPluginConfiguration() throws InterruptedException { assertTrue(consoleText, consoleText.contains("stashBaseUrl: http://changed.com")); assertTrue(consoleText, consoleText.contains("stashProject: DEF")); assertTrue(consoleText, consoleText.contains("stashPullRequestId: 101")); + assertTrue(consoleText, consoleText.contains("commitHash: abcd12345")); assertTrue(consoleText, consoleText.contains("stashRepo: a_repo2")); assertTrue(consoleText, consoleText.contains("checkstyle: **/new-checkstyle-report.xml")); assertTrue(consoleText, consoleText.contains("pmd: **/new-pmd-report.xml")); @@ -202,6 +207,7 @@ public void testPluginConfiguration() throws InterruptedException { assertEquals("DEF", webDriver.findElement(name(STASH_PROJECT)).getAttribute("value")); assertEquals("a_repo2", webDriver.findElement(name(STASH_REPO)).getAttribute("value")); assertEquals("101", webDriver.findElement(name(STASH_PULL_REQUEST_ID)).getAttribute("value")); + assertEquals("abcd12345", webDriver.findElement(name(COMMIT_HASH)).getAttribute("value")); assertEquals("**/new-checkstyle-report.xml", webDriver.findElement(name(CHECKSTYLE_PATTERN)).getAttribute("value")); assertEquals("**/new-pmd-report.xml", webDriver.findElement(name(PMD_PATTERN)).getAttribute("value")); assertEquals("**/new-jslint-report.xml", webDriver.findElement(name(JSLINT_PATTERN)).getAttribute("value")); @@ -229,6 +235,7 @@ public void testPluginConfiguration() throws InterruptedException { webDriver.findElement(name(STASH_PROJECT)).clear(); webDriver.findElement(name(STASH_REPO)).clear(); webDriver.findElement(name(STASH_PULL_REQUEST_ID)).clear(); + webDriver.findElement(name(COMMIT_HASH)).clear(); webDriver.findElement(name(CHECKSTYLE_PATTERN)).clear(); webDriver.findElement(name(CODENARC_PATTERN)).clear(); webDriver.findElement(name(CPD_PATTERN)).clear(); @@ -251,6 +258,7 @@ public void testPluginConfiguration() throws InterruptedException { webDriver.findElement(name(STASH_PROJECT)).sendKeys("$BUILD_NUMBER"); webDriver.findElement(name(STASH_REPO)).sendKeys("$BUILD_NUMBER"); webDriver.findElement(name(STASH_PULL_REQUEST_ID)).sendKeys("$BUILD_NUMBER"); + webDriver.findElement(name(COMMIT_HASH)).sendKeys("$BUILD_NUMBER"); webDriver.findElement(name(CHECKSTYLE_PATTERN)).sendKeys("$BUILD_NUMBER"); webDriver.findElement(name(CODENARC_PATTERN)).sendKeys("$BUILD_NUMBER"); webDriver.findElement(name(CPD_PATTERN)).sendKeys("$BUILD_NUMBER"); @@ -275,6 +283,7 @@ public void testPluginConfiguration() throws InterruptedException { assertTrue(consoleText, consoleText.contains("stashBaseUrl: 3")); assertTrue(consoleText, consoleText.contains("stashProject: 3")); assertTrue(consoleText, consoleText.contains("stashPullRequestId: 3")); + assertTrue(consoleText, consoleText.contains("commitHash: 3")); assertTrue(consoleText, consoleText.contains("stashRepo: 3")); assertTrue(consoleText, consoleText.contains("checkstyle: 3")); assertTrue(consoleText, consoleText.contains("pmd: 3")); @@ -303,6 +312,7 @@ public void testPluginConfiguration() throws InterruptedException { assertEquals("$BUILD_NUMBER", webDriver.findElement(name(STASH_PROJECT)).getAttribute("value")); assertEquals("$BUILD_NUMBER", webDriver.findElement(name(STASH_REPO)).getAttribute("value")); assertEquals("$BUILD_NUMBER", webDriver.findElement(name(STASH_PULL_REQUEST_ID)).getAttribute("value")); + assertEquals("$BUILD_NUMBER", webDriver.findElement(name(COMMIT_HASH)).getAttribute("value")); assertEquals("$BUILD_NUMBER", webDriver.findElement(name(CHECKSTYLE_PATTERN)).getAttribute("value")); assertEquals("$BUILD_NUMBER", webDriver.findElement(name(CODENARC_PATTERN)).getAttribute("value")); assertEquals("$BUILD_NUMBER", webDriver.findElement(name(CPD_PATTERN)).getAttribute("value")); @@ -332,6 +342,7 @@ private void enterDetails() { webDriver.findElement(name(STASH_PROJECT)).sendKeys("ABC"); webDriver.findElement(name(STASH_REPO)).sendKeys("a_repo"); webDriver.findElement(name(STASH_PULL_REQUEST_ID)).sendKeys("100"); + webDriver.findElement(name(COMMIT_HASH)).sendKeys("abcd1234"); webDriver.findElement(name(PATH_PREFIX)).sendKeys("pathPrefix"); webDriver.findElement(name(CHECKSTYLE_PATTERN)).sendKeys("**/checkstyle-report.xml"); webDriver.findElement(name(PMD_PATTERN)).sendKeys("**/pmd-report.xml"); diff --git a/src/main/java/org/jenkinsci/plugins/jvcts/ViolationsToStashDescriptor.java b/src/main/java/org/jenkinsci/plugins/jvcts/ViolationsToStashDescriptor.java index 30a5a64..6630458 100644 --- a/src/main/java/org/jenkinsci/plugins/jvcts/ViolationsToStashDescriptor.java +++ b/src/main/java/org/jenkinsci/plugins/jvcts/ViolationsToStashDescriptor.java @@ -1,5 +1,6 @@ package org.jenkinsci.plugins.jvcts; +import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_COMMIT_HASH; import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_PATTERN; import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_PREFIX; import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_STASH_BASE_URL; @@ -52,6 +53,7 @@ public Publisher newInstance(StaplerRequest req, JSONObject formData) throws hud config.setStashPassword(formData.getString(FIELD_STASH_PASSWORD)); config.setStashRepo(formData.getString(FIELD_STASH_REPO)); config.setStashPullRequestId(formData.getString(FIELD_STASH_PULL_REQUEST_ID)); + config.setCommitHash(formData.getString(FIELD_COMMIT_HASH)); int i = 0; for (String pattern : (List) formData.get(FIELD_PATTERN)) { config.getParserConfigs().get(i++).setPattern(pattern); diff --git a/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfig.java b/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfig.java index c049f0b..fd34135 100644 --- a/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfig.java +++ b/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfig.java @@ -12,6 +12,7 @@ public class ViolationsToStashConfig { private String stashProject; private String stashRepo; private String stashPullRequestId; + private String commitHash; public ViolationsToStashConfig() { @@ -75,4 +76,12 @@ public String getStashPassword() { public String getStashUser() { return stashUser; } + + public void setCommitHash(String commitHash) { + this.commitHash = commitHash; + } + + public String getCommitHash() { + return commitHash; + } } diff --git a/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfigHelper.java b/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfigHelper.java index f2d91f1..88923e0 100644 --- a/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfigHelper.java +++ b/src/main/java/org/jenkinsci/plugins/jvcts/config/ViolationsToStashConfigHelper.java @@ -24,6 +24,7 @@ public class ViolationsToStashConfigHelper { public static final String FIELD_PATTERN = "pattern"; public static final String FIELD_PREFIX = "pathPrefix"; public static final String FIELD_STASH_PULL_REQUEST_ID = "stashPullRequestId"; + public static final String FIELD_COMMIT_HASH = "commitHash"; public static final String FIELD_STASH_REPO = "stashRepo"; public static final String FIELD_STASH_PROJECT = "stashProject"; public static final String FIELD_STASH_BASE_URL = "stashBaseUrl"; diff --git a/src/main/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformer.java b/src/main/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformer.java index 4752eae..131a287 100644 --- a/src/main/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformer.java +++ b/src/main/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformer.java @@ -7,10 +7,12 @@ import static java.util.logging.Level.FINE; import static java.util.logging.Level.SEVERE; import static org.jenkinsci.plugins.jvcts.JvctsLogger.doLog; +import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_COMMIT_HASH; import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_STASH_BASE_URL; import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_STASH_PROJECT; import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_STASH_PULL_REQUEST_ID; import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_STASH_REPO; +import static org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfigHelper.FIELD_STASH_USER; import hudson.EnvVars; import hudson.model.BuildListener; import hudson.model.AbstractBuild; @@ -62,15 +64,24 @@ static void doPerform(ViolationsToStashConfig config, File workspace, BuildListe private static void commentStash(Map> violationsPerFile, ViolationsToStashConfig config, BuildListener listener) throws MalformedURLException { JvctsStashClient jvctsStashClient = new JvctsStashClient(config, listener); - for (String changedFileInStash : jvctsStashClient.getChangedFileInPullRequest()) { - /** - * Should always use filename reported by Stash, then we know Stash will - * recognize it. - */ - doLog(listener, FINE, "Changed file in pull request: \"" + changedFileInStash + "\""); - jvctsStashClient.removeCommentsFromPullRequest(changedFileInStash); - for (Violation violation : getViolationsForFile(violationsPerFile, changedFileInStash, listener)) { - jvctsStashClient.commentPullRequest(changedFileInStash, violation.getLine(), constructCommentMessage(violation)); + if (!isNullOrEmpty(config.getStashPullRequestId())) { + doLog(FINE, "Commenting pull request \"" + config.getStashPullRequestId() + "\""); + for (String changedFileInStash : jvctsStashClient.getChangedFileInPullRequest()) { + logger.log(FINE, "Changed file in pull request: \"" + changedFileInStash + "\""); + jvctsStashClient.removeCommentsFromPullRequest(changedFileInStash); + for (Violation violation : getViolationsForFile(violationsPerFile, changedFileInStash, listener)) { + jvctsStashClient.commentPullRequest(changedFileInStash, violation.getLine(), constructCommentMessage(violation)); + } + } + } + if (!isNullOrEmpty(config.getCommitHash())) { + doLog(FINE, "Commenting commit \"" + config.getCommitHash() + "\""); + for (String changedFileInStash : jvctsStashClient.getChangedFileInCommit()) { + logger.log(FINE, "Changed file in commit: \"" + changedFileInStash + "\""); + jvctsStashClient.removeCommentsCommit(changedFileInStash); + for (Violation violation : getViolationsForFile(violationsPerFile, changedFileInStash, listener)) { + jvctsStashClient.commentCommit(changedFileInStash, violation.getLine(), constructCommentMessage(violation)); + } } } } @@ -118,6 +129,7 @@ private static ViolationsToStashConfig expand(ViolationsToStashConfig config, En expanded.setStashPassword(environment.expand(config.getStashPassword())); expanded.setStashProject(environment.expand(config.getStashProject())); expanded.setStashPullRequestId(environment.expand(config.getStashPullRequestId())); + expanded.setCommitHash(environment.expand(config.getCommitHash())); expanded.setStashRepo(environment.expand(config.getStashRepo())); for (ParserConfig parserConfig : config.getParserConfigs()) { ParserConfig p = new ParserConfig(); @@ -133,9 +145,11 @@ private static ViolationsToStashConfig expand(ViolationsToStashConfig config, En * Enables testing of configuration GUI. */ private static void logConfiguration(ViolationsToStashConfig config, AbstractBuild build, BuildListener listener) { + listener.getLogger().println(FIELD_STASH_USER + ": " + config.getCommitHash()); listener.getLogger().println(FIELD_STASH_BASE_URL + ": " + config.getStashBaseUrl()); listener.getLogger().println(FIELD_STASH_PROJECT + ": " + config.getStashProject()); listener.getLogger().println(FIELD_STASH_PULL_REQUEST_ID + ": " + config.getStashPullRequestId()); + listener.getLogger().println(FIELD_COMMIT_HASH + ": " + config.getCommitHash()); listener.getLogger().println(FIELD_STASH_REPO + ": " + config.getStashRepo()); for (ParserConfig parserConfig : config.getParserConfigs()) { listener.getLogger().println(parserConfig.getParserTypeDescriptorName() + ": " + parserConfig.getPattern()); diff --git a/src/main/java/org/jenkinsci/plugins/jvcts/stash/JvctsStashClient.java b/src/main/java/org/jenkinsci/plugins/jvcts/stash/JvctsStashClient.java index f8b7c28..e476b21 100644 --- a/src/main/java/org/jenkinsci/plugins/jvcts/stash/JvctsStashClient.java +++ b/src/main/java/org/jenkinsci/plugins/jvcts/stash/JvctsStashClient.java @@ -32,8 +32,7 @@ static void setStashInvoker(StashInvoker stashInvoker) { } public List getChangedFileInPullRequest() { - String url = getStashPulLRequestBase() + "/changes?limit=999999"; - return invokeAndParse(url, "$..path.toString"); + return invokeAndParse(getStashPulLRequestBase() + "/changes?limit=999999", "$..path.toString"); } public void commentPullRequest(String changedFile, int line, String message) { @@ -58,8 +57,7 @@ private Map toMap(Object o) { } private JSONArray getCommentsOnPullRequest(String changedFile) { - String url = getStashPulLRequestBase() + "/comments?path=" + changedFile + "&limit=999999"; - return invokeAndParse(url, "$.values[*]"); + return invokeAndParse(getStashPulLRequestBase() + "/comments?path=" + changedFile + "&limit=999999", "$.values[*]"); } private T invokeAndParse(String url, String jsonPath) { @@ -82,4 +80,38 @@ private String getStashPulLRequestBase() { return config.getStashBaseUrl() + "/rest/api/1.0/projects/" + config.getStashProject() + "/repos/" + config.getStashRepo() + "/pull-requests/" + config.getStashPullRequestId(); } + + private String getStashCommitsBase() { + return config.getStashBaseUrl() + "/rest/api/1.0/projects/" + config.getStashProject() + "/repos/" + + config.getStashRepo() + "/commits/" + config.getCommitHash(); + } + + public List getChangedFileInCommit() { + return invokeAndParse(getStashCommitsBase() + "/changes?limit=999999", "$..path.toString"); + } + + private JSONArray getCommentsOnCommit(String changedFile) { + return invokeAndParse(getStashCommitsBase() + "/comments?path=" + changedFile + "&limit=999999", "$.values[*]"); + } + + @SuppressWarnings("rawtypes") + public void removeCommentsCommit(String changedFile) { + for (Object comment : getCommentsOnCommit(changedFile)) { + if (toMap(toMap(comment).get("author")).get("name").equals(config.getStashUser())) { + removeCommentFromCommit((Map) comment); + } + } + } + + private void removeCommentFromCommit(@SuppressWarnings("rawtypes") Map comment) { + stashInvoker.invokeUrl(config, + getStashCommitsBase() + "/comments/" + comment.get(ID) + "?version=" + comment.get(VERSION), + StashInvoker.Method.DELETE, "", listener); + } + + public void commentCommit(String changedFile, int line, String message) { + String postContent = "{ \"text\": \"" + message.replaceAll("\"", "") + "\", \"anchor\": { \"line\": \"" + line + + "\", \"lineType\": \"ADDED\", \"fileType\": \"TO\", \"path\": \"" + changedFile + "\" }}"; + stashInvoker.invokeUrl(config, getStashCommitsBase() + "/comments", StashInvoker.Method.POST, postContent, listener); + } } diff --git a/src/main/resources/org/jenkinsci/plugins/jvcts/ViolationsToStashRecorder/config.jelly b/src/main/resources/org/jenkinsci/plugins/jvcts/ViolationsToStashRecorder/config.jelly index e8eba35..067d3a2 100644 --- a/src/main/resources/org/jenkinsci/plugins/jvcts/ViolationsToStashRecorder/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/jvcts/ViolationsToStashRecorder/config.jelly @@ -46,9 +46,13 @@ - + + + + + @@ -63,7 +67,7 @@ - + Pattern @@ -77,7 +81,7 @@ -
+ diff --git a/src/test/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformerParseTest.java b/src/test/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformerParseTest.java index 23b73b9..c569e24 100644 --- a/src/test/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformerParseTest.java +++ b/src/test/java/org/jenkinsci/plugins/jvcts/perform/JvctsPerformerParseTest.java @@ -2,6 +2,7 @@ import static com.google.common.base.Joiner.on; import static java.nio.charset.Charset.defaultCharset; +import static org.jenkinsci.plugins.jvcts.perform.JvctsPerformer.doPerform; import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.COMMENTS_1_DELETE; import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.COMMENTS_2_DELETE; import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.COMMENTS_CHECKSTYLEFILE_GET; @@ -10,9 +11,9 @@ import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.COMMENTS_PMDFILE_GET; import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.fakeStashClient; import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.getRequestsSentToStash; +import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.prToCommit; import static org.jenkinsci.plugins.jvcts.stash.StashClientFaker.readFile; import static org.junit.Assert.fail; -import hudson.model.BuildListener; import hudson.model.StreamBuildListener; import java.io.File; @@ -23,7 +24,6 @@ import org.jenkinsci.plugins.jvcts.config.Parser; import org.jenkinsci.plugins.jvcts.config.ParserConfig; import org.jenkinsci.plugins.jvcts.config.ViolationsToStashConfig; -import org.junit.Before; import org.junit.Test; import com.google.common.io.Resources; @@ -31,8 +31,7 @@ public class JvctsPerformerParseTest { private ViolationsToStashConfig config; - @Before - public void before() throws IOException { + public void preConfigure() throws IOException { config = new ViolationsToStashConfig(); ParserConfig checkStyleParserConfig = new ParserConfig(Parser.CHECKSTYLE.getTypeDescriptorName(), "**/" + Parser.CHECKSTYLE.getTypeDescriptorName() + ".xml, **/" + Parser.CHECKSTYLE.getTypeDescriptorName() @@ -48,15 +47,12 @@ public void before() throws IOException { config.setStashUser("stashUser"); config.setStashPassword("stashPassword"); config.setStashProject("stashProject"); - config.setStashPullRequestId("1"); config.setStashRepo("stashRepo"); disableLogging(); fakeStashClient(); - BuildListener listener = new StreamBuildListener(System.out, defaultCharset()); - JvctsPerformer.doPerform(config, getWorkspace(), listener); } private void disableLogging() { @@ -67,14 +63,20 @@ private void disableLogging() { } @Test - public void testThatCheckstyleIsCommented() throws IOException { + public void testThatPullRequestCheckstyleIsCommented() throws IOException { + preConfigure(); + config.setStashPullRequestId("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); assertRequested(readFile("checkstyle_checkstylefile_1.json")); assertRequested(readFile("checkstyle_checkstylefile_2.json")); assertRequested(readFile("checkstyle_checkstylefile_3_relativePath.json")); } @Test - public void testThatOldCommentsAreDeleted() throws IOException { + public void testThatPullRequestOldCommentsAreDeleted() throws IOException { + preConfigure(); + config.setStashPullRequestId("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); assertRequested(COMMENTS_CHECKSTYLEFILE_GET); assertRequested(COMMENTS_PMDANDCHECKSTYLE_GET); assertRequested(COMMENTS_PMDFILE_GET); @@ -84,20 +86,76 @@ public void testThatOldCommentsAreDeleted() throws IOException { } @Test - public void testThatCheckstyleAndPmdCanCommentsOnSameFileIsCommented() throws IOException { + public void testThatPullRequestCheckstyleAndPmdCanCommentsOnSameFileIsCommented() throws IOException { + preConfigure(); + config.setStashPullRequestId("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); assertRequested(readFile("checkstyle_pmdandcheckstyle.json")); } @Test - public void testThatPmdIsCommented() throws IOException { + public void testThatPullRequestPmdIsCommented() throws IOException { + preConfigure(); + config.setStashPullRequestId("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); assertRequested(readFile("pmd_pmdfile.json")); } @Test - public void testThatFindbugsIsCommented() throws IOException { + public void testThatPullRequestFindbugsIsCommented() throws IOException { + preConfigure(); + config.setStashPullRequestId("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); assertRequested(readFile("findbugs_code.json")); } + @Test + public void testThatCommitCheckstyleIsCommented() throws IOException { + preConfigure(); + config.setCommitHash("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); + assertRequested(prToCommit(readFile("checkstyle_checkstylefile_1.json"))); + assertRequested(prToCommit(readFile("checkstyle_checkstylefile_2.json"))); + assertRequested(prToCommit(readFile("checkstyle_checkstylefile_3_relativePath.json"))); + } + + @Test + public void testThatCommitOldCommentsAreDeleted() throws IOException { + preConfigure(); + config.setCommitHash("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); + assertRequested(prToCommit(COMMENTS_CHECKSTYLEFILE_GET)); + assertRequested(prToCommit(COMMENTS_PMDANDCHECKSTYLE_GET)); + assertRequested(prToCommit(COMMENTS_PMDFILE_GET)); + assertRequested(prToCommit(COMMENTS_FINDBUGS_GET)); + assertRequested(prToCommit(COMMENTS_1_DELETE)); + assertRequested(prToCommit(COMMENTS_2_DELETE)); + } + + @Test + public void testThatCommitCheckstyleAndPmdCanCommentsOnSameFileIsCommented() throws IOException { + preConfigure(); + config.setCommitHash("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); + assertRequested(prToCommit(readFile("checkstyle_pmdandcheckstyle.json"))); + } + + @Test + public void testThatCommitPmdIsCommented() throws IOException { + preConfigure(); + config.setCommitHash("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); + assertRequested(prToCommit(readFile("pmd_pmdfile.json"))); + } + + @Test + public void testThatCommitFindbugsIsCommented() throws IOException { + preConfigure(); + config.setCommitHash("1"); + doPerform(config, getWorkspace(), new StreamBuildListener(System.out, defaultCharset())); + assertRequested(prToCommit(readFile("findbugs_code.json"))); + } + private File getWorkspace() { return new File(Resources.getResource("test-resources-placeholder.txt").getFile()).getParentFile(); } diff --git a/src/test/java/org/jenkinsci/plugins/jvcts/stash/StashClientFaker.java b/src/test/java/org/jenkinsci/plugins/jvcts/stash/StashClientFaker.java index 9c40a85..93d4b58 100644 --- a/src/test/java/org/jenkinsci/plugins/jvcts/stash/StashClientFaker.java +++ b/src/test/java/org/jenkinsci/plugins/jvcts/stash/StashClientFaker.java @@ -18,6 +18,7 @@ public class StashClientFaker { + public static final String NO_REPORTABLES = "http://stash.server/rest/api/1.0/projects/stashProject/repos/stashRepo/pull-requests/1/comments?path=src/se/main/java/bjurr/FileThatHasNoReportables.java&limit=999999 GET"; public static final String COMMENTS_PMDFILE_GET = "http://stash.server/rest/api/1.0/projects/stashProject/repos/stashRepo/pull-requests/1/comments?path=module/src/main/java/se/bjurr/PMDFile.java&limit=999999 GET"; public static final String COMMENTS_PMDANDCHECKSTYLE_GET = "http://stash.server/rest/api/1.0/projects/stashProject/repos/stashRepo/pull-requests/1/comments?path=module/src/main/java/se/bjurr/PMDAndCheckstyle.java&limit=999999 GET"; public static final String COMMENTS_2_DELETE = "http://stash.server/rest/api/1.0/projects/stashProject/repos/stashRepo/pull-requests/1/comments/13781?version=1 DELETE"; @@ -48,25 +49,27 @@ public String invokeUrl(ViolationsToStashConfig config, String url, Method metho } }); - fakeResponses.put(CHANGES_GET, readFile("pullrequestchanges_1_GET.json")); - fakeResponses.put(COMMENTS_CHECKSTYLEFILE_GET, readFile("pullrequestcomments_GET.json")); - fakeResponses.put(COMMENTS_CHECKSTYLEFILE_REL_GET, readFile("pullrequestcomments_GET_none.json")); - fakeResponses.put(COMMENTS_1_DELETE, readFile("pullrequestcomments_GET.json")); - fakeResponses.put(COMMENTS_2_DELETE, ""); - fakeResponses.put(COMMENTS_PMDANDCHECKSTYLE_GET, readFile("pullrequestcomments_GET_none.json")); - fakeResponses.put(COMMENTS_PMDFILE_GET, readFile("pullrequestcomments_GET_none.json")); - fakeResponses.put(COMMENTS_FINDBUGS_GET, readFile("pullrequestcomments_GET_none.json")); - fakeResponses.put(readFile("checkstyle_checkstylefile_1.json"), ""); - fakeResponses.put(readFile("checkstyle_checkstylefile_2.json"), ""); - fakeResponses.put(readFile("checkstyle_checkstylefile_3_relativePath.json"), ""); - fakeResponses.put(readFile("checkstyle_pmdandcheckstyle.json"), ""); - fakeResponses.put(readFile("pmd_pmdandcheckstyle.json"), ""); - fakeResponses.put(readFile("pmd_pmdfile.json"), ""); - fakeResponses.put(readFile("findbugs_code.json"), ""); - fakeResponses - .put( - "http://stash.server/rest/api/1.0/projects/stashProject/repos/stashRepo/pull-requests/1/comments?path=src/se/main/java/bjurr/FileThatHasNoReportables.java&limit=999999 GET", - readFile("pullrequestcomments_GET_none.json")); + fake(CHANGES_GET, readFile("pullrequestchanges_1_GET.json")); + fake(COMMENTS_CHECKSTYLEFILE_GET, readFile("pullrequestcomments_GET.json")); + fake(COMMENTS_CHECKSTYLEFILE_REL_GET, readFile("pullrequestcomments_GET_none.json")); + fake(COMMENTS_1_DELETE, readFile("pullrequestcomments_GET.json")); + fake(COMMENTS_2_DELETE, ""); + fake(COMMENTS_PMDANDCHECKSTYLE_GET, readFile("pullrequestcomments_GET_none.json")); + fake(COMMENTS_PMDFILE_GET, readFile("pullrequestcomments_GET_none.json")); + fake(COMMENTS_FINDBUGS_GET, readFile("pullrequestcomments_GET_none.json")); + fake(NO_REPORTABLES, readFile("pullrequestcomments_GET_none.json")); + fake(readFile("checkstyle_checkstylefile_1.json"), ""); + fake(readFile("checkstyle_checkstylefile_2.json"), ""); + fake(readFile("checkstyle_checkstylefile_3_relativePath.json"), ""); + fake(readFile("checkstyle_pmdandcheckstyle.json"), ""); + fake(readFile("pmd_pmdandcheckstyle.json"), ""); + fake(readFile("pmd_pmdfile.json"), ""); + fake(readFile("findbugs_code.json"), ""); + } + + private static void fake(String request, String response) { + fakeResponses.put(request, response); + fakeResponses.put(prToCommit(request), prToCommit(response)); } public static String readFile(String filename) throws IOException { @@ -80,4 +83,8 @@ private static String createFakeKey(String url, String method, String postConten public static List getRequestsSentToStash() { return requestsSentToStash; } + + public static String prToCommit(String string) { + return string.replaceAll("pull-requests", "commits"); + } }