From f9f36c2221206333bff929e6bb725562a849e722 Mon Sep 17 00:00:00 2001 From: Avimanyu Mukhopadhyay Date: Mon, 7 Mar 2022 16:54:47 -0800 Subject: [PATCH 1/5] Resetting the list for each image --- .../subcommands/impl/All.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java index fbdff907..85168c6b 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java @@ -40,13 +40,6 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit Set> imageToTagStore = this.dockerfileGitHubUtil.getGitHubJsonStore(ns.get(Constants.STORE)).parseStoreToImagesMap(dockerfileGitHubUtil, ns.get(Constants.STORE)); Integer gitApiSearchLimit = ns.get(Constants.GIT_API_SEARCH_LIMIT); - Map orgsToIncludeInSearch = new HashMap<>(); - if (ns.get(Constants.GIT_ORG) != null) { - // If there is a Git org specified, that needs to be included in the search query. In - // the orgsToIncludeInSearch a true value associated with an org name ensures that - // the org gets included in the search query. - orgsToIncludeInSearch.put(ns.get(Constants.GIT_ORG), true); - } List imagesThatCouldNotBeProcessed = new LinkedList<>(); AtomicInteger numberOfImagesToProcess = new AtomicInteger(); for (Map.Entry imageToTag : imageToTagStore) { @@ -59,7 +52,13 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit GitForkBranch gitForkBranch = getGitForkBranch(image, tag, ns); log.info("Finding Dockerfiles with the image name {}...", image); - + Map orgsToIncludeInSearch = new HashMap<>(); + if (ns.get(Constants.GIT_ORG) != null) { + // If there is a Git org specified, that needs to be included in the search query. In + // the orgsToIncludeInSearch a true value associated with an org name ensures that + // the org gets included in the search query. + orgsToIncludeInSearch.put(ns.get(Constants.GIT_ORG), true); + } Optional>> contentsWithImage = this.dockerfileGitHubUtil.findFilesWithImage(image, orgsToIncludeInSearch, gitApiSearchLimit); From 325364c6261d93e8b50ba342e0d317fb7e2359bc Mon Sep 17 00:00:00 2001 From: Avimanyu Mukhopadhyay Date: Thu, 10 Mar 2022 10:53:57 -0800 Subject: [PATCH 2/5] Making local copy of map --- .../subcommands/impl/All.java | 14 +++++++------- .../utils/DockerfileGitHubUtil.java | 8 +++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java index 85168c6b..79563481 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java @@ -42,6 +42,13 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit Integer gitApiSearchLimit = ns.get(Constants.GIT_API_SEARCH_LIMIT); List imagesThatCouldNotBeProcessed = new LinkedList<>(); AtomicInteger numberOfImagesToProcess = new AtomicInteger(); + Map orgsToIncludeInSearch = new HashMap<>(); + if (ns.get(Constants.GIT_ORG) != null) { + // If there is a Git org specified, that needs to be included in the search query. In + // the orgsToIncludeInSearch a true value associated with an org name ensures that + // the org gets included in the search query. + orgsToIncludeInSearch.put(ns.get(Constants.GIT_ORG), true); + } for (Map.Entry imageToTag : imageToTagStore) { numberOfImagesToProcess.getAndIncrement(); String image = imageToTag.getKey(); @@ -52,13 +59,6 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit GitForkBranch gitForkBranch = getGitForkBranch(image, tag, ns); log.info("Finding Dockerfiles with the image name {}...", image); - Map orgsToIncludeInSearch = new HashMap<>(); - if (ns.get(Constants.GIT_ORG) != null) { - // If there is a Git org specified, that needs to be included in the search query. In - // the orgsToIncludeInSearch a true value associated with an org name ensures that - // the org gets included in the search query. - orgsToIncludeInSearch.put(ns.get(Constants.GIT_ORG), true); - } Optional>> contentsWithImage = this.dockerfileGitHubUtil.findFilesWithImage(image, orgsToIncludeInSearch, gitApiSearchLimit); diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java index 5bf2c55a..64884c3e 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java @@ -177,10 +177,12 @@ protected Optional>> getSearchResultsExcludi Optional>> contentsForOrgWithMaximumHits; contentsForOrgWithMaximumHits = findFilesWithImage(image, orgsToInclude, gitApiSearchLimit); - orgsToExclude.put(orgWithMaximumHits, false); - log.info("Running search by excluding the orgs {}.", orgsToExclude.keySet().toString()); + Map orgsToExcludeFromSearch = new HashMap<>(); + orgsToExcludeFromSearch.putAll(orgsToExclude); + orgsToExcludeFromSearch.put(orgWithMaximumHits, false); + log.info("Running search by excluding the orgs {}.", orgsToExcludeFromSearch.keySet().toString()); Optional>> contentsExcludingOrgWithMaximumHits; - contentsExcludingOrgWithMaximumHits = findFilesWithImage(image, orgsToExclude, gitApiSearchLimit); + contentsExcludingOrgWithMaximumHits = findFilesWithImage(image, orgsToExcludeFromSearch, gitApiSearchLimit); if (contentsForOrgWithMaximumHits.isPresent()) { allContentsWithImage.addAll(contentsForOrgWithMaximumHits.get()); } From 5b9fe7357694a946117f2008bde9d1dfeb961a40 Mon Sep 17 00:00:00 2001 From: Avimanyu Mukhopadhyay Date: Thu, 10 Mar 2022 10:57:53 -0800 Subject: [PATCH 3/5] Removing unnecessary change --- .../dockerfileimageupdate/subcommands/impl/All.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java index 79563481..fbdff907 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java @@ -40,8 +40,6 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit Set> imageToTagStore = this.dockerfileGitHubUtil.getGitHubJsonStore(ns.get(Constants.STORE)).parseStoreToImagesMap(dockerfileGitHubUtil, ns.get(Constants.STORE)); Integer gitApiSearchLimit = ns.get(Constants.GIT_API_SEARCH_LIMIT); - List imagesThatCouldNotBeProcessed = new LinkedList<>(); - AtomicInteger numberOfImagesToProcess = new AtomicInteger(); Map orgsToIncludeInSearch = new HashMap<>(); if (ns.get(Constants.GIT_ORG) != null) { // If there is a Git org specified, that needs to be included in the search query. In @@ -49,6 +47,8 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit // the org gets included in the search query. orgsToIncludeInSearch.put(ns.get(Constants.GIT_ORG), true); } + List imagesThatCouldNotBeProcessed = new LinkedList<>(); + AtomicInteger numberOfImagesToProcess = new AtomicInteger(); for (Map.Entry imageToTag : imageToTagStore) { numberOfImagesToProcess.getAndIncrement(); String image = imageToTag.getKey(); @@ -59,6 +59,7 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit GitForkBranch gitForkBranch = getGitForkBranch(image, tag, ns); log.info("Finding Dockerfiles with the image name {}...", image); + Optional>> contentsWithImage = this.dockerfileGitHubUtil.findFilesWithImage(image, orgsToIncludeInSearch, gitApiSearchLimit); From b91477fb7424a14cc18aa3ffda8193f7d685594f Mon Sep 17 00:00:00 2001 From: Avimanyu Mukhopadhyay Date: Thu, 10 Mar 2022 11:50:35 -0800 Subject: [PATCH 4/5] Adding tests --- .../dockerfileimageupdate/utils/DockerfileGitHubUtil.java | 5 ++--- .../utils/DockerfileGitHubUtilTest.java | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java index 64884c3e..e56020f5 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java @@ -177,10 +177,9 @@ protected Optional>> getSearchResultsExcludi Optional>> contentsForOrgWithMaximumHits; contentsForOrgWithMaximumHits = findFilesWithImage(image, orgsToInclude, gitApiSearchLimit); - Map orgsToExcludeFromSearch = new HashMap<>(); - orgsToExcludeFromSearch.putAll(orgsToExclude); + final Map orgsToExcludeFromSearch = new HashMap(orgsToExclude); orgsToExcludeFromSearch.put(orgWithMaximumHits, false); - log.info("Running search by excluding the orgs {}.", orgsToExcludeFromSearch.keySet().toString()); + log.info("Running search by excluding the orgs {}.", orgsToExcludeFromSearch.keySet()); Optional>> contentsExcludingOrgWithMaximumHits; contentsExcludingOrgWithMaximumHits = findFilesWithImage(image, orgsToExcludeFromSearch, gitApiSearchLimit); if (contentsForOrgWithMaximumHits.isPresent()) { diff --git a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java index 1e8b047b..5edf14c8 100644 --- a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java +++ b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java @@ -330,6 +330,7 @@ public void getSearchResultsExcludingOrgWithMostHits() throws Exception { Map orgsToIncludeOrExclude = new HashMap<>(); assertEquals((dockerfileGitHubUtil.getSearchResultsExcludingOrgWithMostHits("image", contentsWithImage, orgsToIncludeOrExclude, 1000)).get().size(), 2); + assertEquals(orgsToIncludeOrExclude.size(), 0); } @Test From 9ea7bc8d49ee99373c98bde176eccb585c27e7a6 Mon Sep 17 00:00:00 2001 From: Avimanyu Mukhopadhyay Date: Thu, 10 Mar 2022 12:21:07 -0800 Subject: [PATCH 5/5] Addressing PR comments --- .../dockerfileimageupdate/utils/DockerfileGitHubUtil.java | 2 +- .../dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java index e56020f5..fd017d3a 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java @@ -177,7 +177,7 @@ protected Optional>> getSearchResultsExcludi Optional>> contentsForOrgWithMaximumHits; contentsForOrgWithMaximumHits = findFilesWithImage(image, orgsToInclude, gitApiSearchLimit); - final Map orgsToExcludeFromSearch = new HashMap(orgsToExclude); + final Map orgsToExcludeFromSearch = new HashMap<>(orgsToExclude); orgsToExcludeFromSearch.put(orgWithMaximumHits, false); log.info("Running search by excluding the orgs {}.", orgsToExcludeFromSearch.keySet()); Optional>> contentsExcludingOrgWithMaximumHits; diff --git a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java index 5edf14c8..4e58c1a4 100644 --- a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java +++ b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java @@ -330,6 +330,7 @@ public void getSearchResultsExcludingOrgWithMostHits() throws Exception { Map orgsToIncludeOrExclude = new HashMap<>(); assertEquals((dockerfileGitHubUtil.getSearchResultsExcludingOrgWithMostHits("image", contentsWithImage, orgsToIncludeOrExclude, 1000)).get().size(), 2); + //This check ensures that the parameter passed to the method is not modified. Instead, the method creates a local copy of the map and modifies that. assertEquals(orgsToIncludeOrExclude.size(), 0); }