Skip to content

Commit

Permalink
ADNCD-1948: Add workaround for all PRs being rebuilt after GitHub mig…
Browse files Browse the repository at this point in the history
…ration

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.
  • Loading branch information
Sandro Heinzelmann committed Aug 19, 2024
1 parent 9aa746d commit f515704
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>in.ashwanthkumar</groupId>
<artifactId>gocd-github-pr-material</artifactId>
<version>1.6.0</version>
<version>1.6.1_migr1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -395,6 +396,39 @@ public void handleGetLatestRevisionShouldReturnASingleBranch() {
assertEquals("test2abcd11111111", revision.get("revision"));
}

@Test
public void handleGetLatestRevisionShouldReturnAllBranchesForGitHub() {
GitFactory gitFactory = mock(GitFactory.class);

Map<String, String> 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<String, Object> responseBody =
(Map<String, Object>) JSONUtils.fromJSON(response.responseBody());

String branchToRevisionMap = (String) ((Map<String, Object>)responseBody.get("scm-data")).get("BRANCH_TO_REVISION_MAP");
assertEquals("{\"test-2\":\"test2abcd11111111\",\"test-1\":\"test1abcd11111111\"}", branchToRevisionMap);

Map<String, String> revision = (Map<String, String>) responseBody.get("revision");
assertEquals("test2abcd11111111", revision.get("revision"));
}

@Test
public void keyValuePairs_should_extract_values_from_nested_maps() {
Map<String, String> keyValuePairs = GitHubPRBuildPlugin.keyValuePairs(
Expand Down

0 comments on commit f515704

Please sign in to comment.