From f23d63cb8e3a5e8ece6cace5c3efeabd19361503 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 4 Aug 2024 19:16:44 -0600 Subject: [PATCH 1/2] Restore compatibility for GitSampleRepoRule I forgot that there are other consumers of the Git plugin test suite. Plugin BOM automated tests detected the problem. --- .../java/hudson/plugins/git/GitStepTest.java | 8 +++---- .../plugins/git/GitSampleRepoRule.java | 24 +++++++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/test/java/hudson/plugins/git/GitStepTest.java b/src/test/java/hudson/plugins/git/GitStepTest.java index a5322cdfde..8eef466b23 100644 --- a/src/test/java/hudson/plugins/git/GitStepTest.java +++ b/src/test/java/hudson/plugins/git/GitStepTest.java @@ -328,7 +328,7 @@ private WorkflowJob createJob() throws Exception { public void testDoNotifyCommitWithInvalidApiToken() throws Exception { assumeTrue("Test class max time " + MAX_SECONDS_FOR_THESE_TESTS + " exceeded", isTimeAvailable()); createJob(); - String response = sampleRepo.notifyCommit(r, GitSampleRepoRule.INVALID_NOTIFY_COMMIT_TOKEN); + String response = sampleRepo.notifyCommitWithResults(r, GitSampleRepoRule.INVALID_NOTIFY_COMMIT_TOKEN); assertThat(response, containsString("Invalid access token")); } @@ -337,7 +337,7 @@ public void testDoNotifyCommitWithInvalidApiToken() throws Exception { public void testDoNotifyCommitWithAllowModeRandomValue() throws Exception { assumeTrue("Test class max time " + MAX_SECONDS_FOR_THESE_TESTS + " exceeded", isTimeAvailable()); createJob(); - String response = sampleRepo.notifyCommit(r, null); + String response = sampleRepo.notifyCommitWithResults(r, null); assertThat(response, containsString("An access token is required. Please refer to Git plugin documentation (https://plugins.jenkins.io/git/#plugin-content-push-notification-from-repository) for details.")); } @@ -349,7 +349,7 @@ public void testDoNotifyCommitWithSha1AndAllowModePollWithInvalidToken() throws createJob(); /* sha1 is ignored because invalid access token is provided */ String sha1 = "4b714b66959463a98e9dfb1983db5a39a39fa6d6"; - String response = sampleRepo.notifyCommit(r, GitSampleRepoRule.INVALID_NOTIFY_COMMIT_TOKEN, sha1); + String response = sampleRepo.notifyCommitWithResults(r, GitSampleRepoRule.INVALID_NOTIFY_COMMIT_TOKEN, sha1); assertThat(response, containsString("Invalid access token")); } @@ -361,7 +361,7 @@ public void testDoNotifyCommitWithSha1AndAllowModePoll() throws Exception { createJob(); /* sha1 is ignored because no access token is provided */ String sha1 = "4b714b66959463a98e9dfb1983db5a39a39fa6d6"; - String response = sampleRepo.notifyCommit(r, null, sha1); + String response = sampleRepo.notifyCommitWithResults(r, null, sha1); assertThat(response, containsString("An access token is required when using the sha1 parameter")); } diff --git a/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java b/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java index 152169719c..3df1106f98 100644 --- a/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java +++ b/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java @@ -101,13 +101,27 @@ public final boolean mkdirs(String rel) throws IOException { return new File(this.sampleRepo, rel).mkdirs(); } - public String notifyCommit(JenkinsRule r) throws Exception { + public void notifyCommit(JenkinsRule r) throws Exception { + synchronousPolling(r); + String notifyCommitToken = ApiTokenPropertyConfiguration.get().generateApiToken("notifyCommit").getString("value"); + WebResponse webResponse = r.createWebClient() + .goTo("git/notifyCommit?url=" + bareUrl() + "&token=" + notifyCommitToken, "text/plain").getWebResponse(); + LOGGER.log(Level.FINE, webResponse.getContentAsString()); + for (NameValuePair pair : webResponse.getResponseHeaders()) { + if (pair.getName().equals("Triggered")) { + LOGGER.log(Level.FINE, "Triggered: " + pair.getValue()); + } + } + r.waitUntilNoActivity(); + } + + public String notifyCommitWithResults(JenkinsRule r) throws Exception { String notifyCommitToken = ApiTokenPropertyConfiguration.get().generateApiToken("notifyCommit").getString("value"); - return notifyCommit(r, notifyCommitToken, null); + return notifyCommitWithResults(r, notifyCommitToken, null); } - public String notifyCommit(JenkinsRule r, @CheckForNull String notifyCommitToken) throws Exception { - return notifyCommit(r, notifyCommitToken, null); + public String notifyCommitWithResults(JenkinsRule r, @CheckForNull String notifyCommitToken) throws Exception { + return notifyCommitWithResults(r, notifyCommitToken, null); } /** @@ -126,7 +140,7 @@ public String notifyCommit(JenkinsRule r, @CheckForNull String notifyCommitToken * @param notifyCommitToken token used for notifyCommit authentication * @param sha1 SHA-1 hash to included in notifyCommit **/ - public String notifyCommit(JenkinsRule r, @CheckForNull String notifyCommitToken, @CheckForNull String sha1) throws Exception { + public String notifyCommitWithResults(JenkinsRule r, @CheckForNull String notifyCommitToken, @CheckForNull String sha1) throws Exception { boolean expectError = notifyCommitToken == null || notifyCommitToken.contains(INVALID_NOTIFY_COMMIT_TOKEN); synchronousPolling(r); JenkinsRule.WebClient webClient = r.createWebClient(); From d6aa6d839f82751f2bcdb998fc56408b0e02bd8c Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 4 Aug 2024 19:48:26 -0600 Subject: [PATCH 2/2] Remove code repetition introduced by prior commit --- .../java/jenkins/plugins/git/GitSampleRepoRule.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java b/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java index 3df1106f98..504035f59d 100644 --- a/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java +++ b/src/test/java/jenkins/plugins/git/GitSampleRepoRule.java @@ -102,17 +102,8 @@ public final boolean mkdirs(String rel) throws IOException { } public void notifyCommit(JenkinsRule r) throws Exception { - synchronousPolling(r); String notifyCommitToken = ApiTokenPropertyConfiguration.get().generateApiToken("notifyCommit").getString("value"); - WebResponse webResponse = r.createWebClient() - .goTo("git/notifyCommit?url=" + bareUrl() + "&token=" + notifyCommitToken, "text/plain").getWebResponse(); - LOGGER.log(Level.FINE, webResponse.getContentAsString()); - for (NameValuePair pair : webResponse.getResponseHeaders()) { - if (pair.getName().equals("Triggered")) { - LOGGER.log(Level.FINE, "Triggered: " + pair.getValue()); - } - } - r.waitUntilNoActivity(); + notifyCommitWithResults(r, notifyCommitToken, null); } public String notifyCommitWithResults(JenkinsRule r) throws Exception {