From f5157046d8dd83fefe2ef24ba64a96c29381db1b Mon Sep 17 00:00:00 2001 From: Sandro Heinzelmann Date: Mon, 19 Aug 2024 18:02:58 +0200 Subject: [PATCH] ADNCD-1948: Add workaround for all PRs being rebuilt after GitHub migration On initial load of a PR material (handleGetLatestRevision), store all seen PRs, not just the one we're building. For subsequent loads (handleLatestRevisionSince) behave the same as before, and only store the PR we're building. Also tried fancier variants where you can configure the behavior per PR material, even allowing to clear the state and rebuild everything. However, there seems to be a GoCD caching bug such that PR material config changes in the UI are properly displayed, but not actually propagated to the PR plugin. It keeps using the old config until GoCD is restarted, or the PR material (and due to dependency the referencing config repo) is removed and readded. --- pom.xml | 2 +- .../gocd/github/GitHubPRBuildPlugin.java | 8 ++++- .../gocd/github/GitHubPRBuildPluginTest.java | 36 ++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 81dca95..fc885ac 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 in.ashwanthkumar gocd-github-pr-material - 1.6.0 + 1.6.1_migr1 UTF-8 diff --git a/src/main/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPlugin.java b/src/main/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPlugin.java index 01e9e22..95f3f4f 100644 --- a/src/main/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPlugin.java +++ b/src/main/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPlugin.java @@ -15,6 +15,7 @@ import com.tw.go.plugin.util.ListUtil; import com.tw.go.plugin.util.StringUtil; import in.ashwanthkumar.gocd.github.provider.Provider; +import in.ashwanthkumar.gocd.github.provider.github.GitHubProvider; import in.ashwanthkumar.gocd.github.settings.scm.PluginConfigurationView; import in.ashwanthkumar.gocd.github.util.BranchFilter; import in.ashwanthkumar.gocd.github.util.ExtendedGitCmdHelper; @@ -227,7 +228,12 @@ GoPluginApiResponse handleGetLatestRevision(GoPluginApiRequest goPluginApiReques } // Remove all other branches from the response to ensure the next time those will be picked up by GoCD - branchToRevisionMap.entrySet().removeIf(entry -> !Objects.equals(entry.getKey(), newerRevision.getKey())); + if (provider instanceof GitHubProvider) { + LOGGER.info("Storing all seen PRs for initial GitHub PR material load."); + } + else { + branchToRevisionMap.entrySet().removeIf(entry -> !Objects.equals(entry.getKey(), newerRevision.getKey())); + } Revision revision = git.getDetailsForRevision(newerRevision.getValue()); String branch = newerRevision.getKey(); diff --git a/src/test/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPluginTest.java b/src/test/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPluginTest.java index 05b39a8..e4fc2c0 100644 --- a/src/test/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPluginTest.java +++ b/src/test/java/in/ashwanthkumar/gocd/github/GitHubPRBuildPluginTest.java @@ -13,6 +13,7 @@ import in.ashwanthkumar.gocd.github.provider.git.GitProvider; import in.ashwanthkumar.gocd.github.provider.github.GHUtils; import in.ashwanthkumar.gocd.github.provider.github.GitHubProvider; +import in.ashwanthkumar.gocd.github.provider.stash.StashProvider; import in.ashwanthkumar.gocd.github.util.ExtendedGitCmdHelper; import in.ashwanthkumar.gocd.github.util.GitFactory; import in.ashwanthkumar.gocd.github.util.GitFolderFactory; @@ -372,7 +373,7 @@ public void handleGetLatestRevisionShouldReturnASingleBranch() { mockGitRevisions(gitFactory, revisions); GitFolderFactory gitFolderFactory = mock(GitFolderFactory.class); - Provider provider = new TestProvider(); + Provider provider = new StashProvider(); GitHubPRBuildPlugin plugin = new GitHubPRBuildPlugin( provider, gitFactory, @@ -395,6 +396,39 @@ public void handleGetLatestRevisionShouldReturnASingleBranch() { assertEquals("test2abcd11111111", revision.get("revision")); } + @Test + public void handleGetLatestRevisionShouldReturnAllBranchesForGitHub() { + GitFactory gitFactory = mock(GitFactory.class); + + Map revisions = new HashMap<>(); + revisions.put("test-1", "test1abcd11111111"); + revisions.put("test-2", "test2abcd11111111"); + mockGitRevisions(gitFactory, revisions); + + GitFolderFactory gitFolderFactory = mock(GitFolderFactory.class); + Provider provider = new GitHubProvider(); + GitHubPRBuildPlugin plugin = new GitHubPRBuildPlugin( + provider, + gitFactory, + gitFolderFactory, + mockGoApplicationAccessor() + ); + GitHubPRBuildPlugin pluginSpy = spy(plugin); + + GoPluginApiRequest request = mock(GoPluginApiRequest.class); + when(request.requestBody()).thenReturn("{scm-configuration: {url: {value: \"https://github.com/mdaliejaz/samplerepo.git\"}}, flyweight-folder: \"" + TEST_DIR + "\"}"); + + GoPluginApiResponse response = pluginSpy.handleGetLatestRevision(request); + Map responseBody = + (Map) JSONUtils.fromJSON(response.responseBody()); + + String branchToRevisionMap = (String) ((Map)responseBody.get("scm-data")).get("BRANCH_TO_REVISION_MAP"); + assertEquals("{\"test-2\":\"test2abcd11111111\",\"test-1\":\"test1abcd11111111\"}", branchToRevisionMap); + + Map revision = (Map) responseBody.get("revision"); + assertEquals("test2abcd11111111", revision.get("revision")); + } + @Test public void keyValuePairs_should_extract_values_from_nested_maps() { Map keyValuePairs = GitHubPRBuildPlugin.keyValuePairs(