Skip to content

Commit 87f8fcc

Browse files
committed
Updated to support different branch name for different file type run
1 parent 9fc5cad commit 87f8fcc

File tree

10 files changed

+65
-25
lines changed

10 files changed

+65
-25
lines changed

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/model/GitForkBranch.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.salesforce.dockerfileimageupdate.model;
22

3+
import com.salesforce.dockerfileimageupdate.utils.Constants;
4+
import org.apache.commons.lang3.StringUtils;
5+
36
/**
47
* Converts an imageName to a branchName. Primary conversion necessary is : to -
58
* Also support backward compatible method of specifying a branch
@@ -9,11 +12,12 @@
912
*/
1013
public class GitForkBranch {
1114
private final String branchPrefix;
15+
private final String branchSuffix;
1216
private final String imageName;
1317
private final String imageTag;
1418
private final boolean specifiedBranchOverride;
1519

16-
public GitForkBranch(String imageName, String imageTag, String specifiedBranch) {
20+
public GitForkBranch(String imageName, String imageTag, String specifiedBranch, String filenamesSearchedFor) {
1721
this.imageTag = (imageTag == null || imageTag.trim().isEmpty()) ? "" : imageTag.trim();
1822
this.imageName = (imageName == null || imageName.trim().isEmpty()) ? "" : imageName.trim();
1923
if (specifiedBranch == null || specifiedBranch.trim().isEmpty()) {
@@ -22,9 +26,11 @@ public GitForkBranch(String imageName, String imageTag, String specifiedBranch)
2226
}
2327
this.branchPrefix = this.imageName.replace(":", "-").toLowerCase();
2428
this.specifiedBranchOverride = false;
29+
this.branchSuffix = getBranchSuffix(StringUtils.isNotBlank(filenamesSearchedFor) ? filenamesSearchedFor.toLowerCase():filenamesSearchedFor);
2530
} else {
2631
this.branchPrefix = specifiedBranch;
2732
this.specifiedBranchOverride = true;
33+
this.branchSuffix = "";
2834
}
2935
}
3036

@@ -71,9 +77,9 @@ public boolean useSpecifiedBranchOverride() {
7177
*/
7278
public String getBranchName() {
7379
if (this.imageTag == null || this.imageTag.trim().isEmpty()) {
74-
return this.branchPrefix;
80+
return this.branchPrefix + this.branchSuffix;
7581
} else {
76-
return this.branchPrefix + "-" + this.imageTag;
82+
return this.branchPrefix + "-" + this.imageTag + this.branchSuffix;
7783
}
7884
}
7985

@@ -84,4 +90,18 @@ public String getImageName() {
8490
public String getImageTag() {
8591
return imageTag;
8692
}
93+
94+
private String getBranchSuffix(String filenamesSearchedFor) {
95+
// To avoid branch with same imageName-tag getting overwritten in case multiple runs(with different file types)
96+
// on same image tag store, adding suffix to branch name
97+
if (StringUtils.isBlank(filenamesSearchedFor)) {
98+
return "";
99+
} else if (filenamesSearchedFor.contains(Constants.FILENAME_DOCKERFILE) && !filenamesSearchedFor.contains(Constants.FILENAME_DOCKER_COMPOSE)) {
100+
return "_dockerfile";
101+
} else if (filenamesSearchedFor.contains(Constants.FILENAME_DOCKER_COMPOSE) && !filenamesSearchedFor.contains(Constants.FILENAME_DOCKERFILE)) {
102+
return "_dockercompose";
103+
} else {
104+
return "";
105+
}
106+
}
87107
}

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/process/GitHubPullRequestSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public Multimap<String, GitHubContentToProcess> forkRepositoriesFoundAndGetPathT
7979

8080
log.info("Out of {} content search results processed, {} were deemed eligible for forking to yield {} repositories to fork.",
8181
totalContentsFound, contentsShouldFork, pathToDockerfilesInParentRepo.keys().size());
82-
log.info("Path to Dockerfiles in repos: {}", pathToDockerfilesInParentRepo);
82+
log.info("Path to files in repos: {}", pathToDockerfilesInParentRepo);
8383

8484
return pathToDockerfilesInParentRepo;
8585
}

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/search/GitHubImageSearchTermList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.salesforce.dockerfileimageupdate.search;
22

33
import com.google.common.collect.ImmutableList;
4+
import com.salesforce.dockerfileimageupdate.utils.Constants;
45

56
import java.util.ArrayList;
67
import java.util.List;
@@ -95,7 +96,7 @@ static ProcessingState processDomainPartOfImage(String domain, String filenames)
9596
if (domain == null || domain.trim().isEmpty()) {
9697
return state;
9798
} else {
98-
if (filenames.equalsIgnoreCase("Dockerfile")) {
99+
if (filenames.equalsIgnoreCase(Constants.FILENAME_DOCKERFILE)) {
99100
state.addToCurrentTerm("FROM ");
100101
}
101102
if (domain.contains("-")) {

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected Optional<Exception> processImageWithTag(String image, String tag, Name
8383
GitForkBranch gitForkBranch = getGitForkBranch(image, tag, ns);
8484
String filenamesToSearch = ns.get(Constants.FILE_NAMES_TO_SEARCH);
8585

86-
log.info("Finding Dockerfiles with the image name {}...", image);
86+
log.info("Finding files:{} with the image name {}...", filenamesToSearch, image);
8787
Optional<List<PagedSearchIterable<GHContent>>> contentsWithImage =
8888
this.dockerfileGitHubUtil.findFilesWithImage(image, orgsToIncludeInSearch, gitApiSearchLimit, filenamesToSearch);
8989
if (contentsWithImage.isPresent()) {
@@ -138,7 +138,7 @@ protected GitHubPullRequestSender getPullRequestSender(DockerfileGitHubUtil dock
138138
}
139139

140140
protected GitForkBranch getGitForkBranch(String image, String tag, Namespace ns){
141-
return new GitForkBranch(image, tag, ns.get(Constants.GIT_BRANCH));
141+
return new GitForkBranch(image, tag, ns.get(Constants.GIT_BRANCH), ns.get(Constants.FILE_NAMES_TO_SEARCH));
142142
}
143143

144144
protected PullRequests getPullRequests(){

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/Child.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit
5656
return;
5757
}
5858

59-
GitForkBranch gitForkBranch = new GitForkBranch(img, forceTag, branch);
59+
GitForkBranch gitForkBranch = new GitForkBranch(img, forceTag, branch, ns.get(Constants.FILE_NAMES_TO_SEARCH));
6060
PullRequestInfo pullRequestInfo =
6161
new PullRequestInfo(ns.get(Constants.GIT_PR_TITLE),
6262
gitForkBranch.getImageName(),

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/Parent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected PullRequests getPullRequests(){
8989
}
9090

9191
protected GitForkBranch getGitForkBranch(Namespace ns){
92-
return new GitForkBranch(ns.get(Constants.IMG), ns.get(Constants.TAG), ns.get(Constants.GIT_BRANCH));
92+
return new GitForkBranch(ns.get(Constants.IMG), ns.get(Constants.TAG), ns.get(Constants.GIT_BRANCH), ns.get(Constants.FILE_NAMES_TO_SEARCH));
9393
}
9494

9595
protected GitHubPullRequestSender getPullRequestSender(DockerfileGitHubUtil dockerfileGitHubUtil, Namespace ns){

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ private Constants() {
4848
public static final Duration DEFAULT_RATE_LIMIT_DURATION = Duration.ofMinutes(DEFAULT_RATE_LIMIT);
4949
//token adding rate(here:a token added every 2 minutes in the bucket)
5050
public static final Duration DEFAULT_TOKEN_ADDING_RATE = Duration.ofMinutes(DEFAULT_CONSUMING_TOKEN_RATE);
51+
public static final String FILENAME_DOCKERFILE = "dockerfile";
52+
public static final String FILENAME_DOCKER_COMPOSE = "docker-compose";
5153
}

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/model/GitForkBranchTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public Object[][] imageNameAndExpectedBranch() {
1919

2020
@Test(dataProvider = "imageNameAndExpectedBranch")
2121
public void testGetBranchNameForImageTagCombos(String imageName, String imageTag, String expectedResult) {
22-
assertEquals(new GitForkBranch(imageName, imageTag, "").getBranchName(), expectedResult);
22+
assertEquals(new GitForkBranch(imageName, imageTag, "", "Dockerfile,docker-compose").getBranchName(), expectedResult);
2323
}
2424

2525
@DataProvider
@@ -35,7 +35,7 @@ public Object[][] imageNameAndSpecifiedBranches() {
3535

3636
@Test(dataProvider = "imageNameAndSpecifiedBranches")
3737
public void testGetBranchNameForImageSpecifiedBranchCombos(String imageName, String imageTag, String specifiedBranch, String expectedResult) {
38-
assertEquals(new GitForkBranch(imageName, imageTag, specifiedBranch).getBranchName(), expectedResult);
38+
assertEquals(new GitForkBranch(imageName, imageTag, specifiedBranch, "Dockerfile,docker-compose").getBranchName(), expectedResult);
3939
}
4040

4141
@DataProvider
@@ -61,11 +61,29 @@ public Object[][] sameBranchOrImageNamePrefix() {
6161

6262
@Test(dataProvider = "sameBranchOrImageNamePrefix")
6363
public void testIsSameBranchOrHasImageNamePrefix(String imageName, String imageTag, String specifiedBranch, String inputBranch, boolean expectedResult) {
64-
assertEquals(new GitForkBranch(imageName, imageTag, specifiedBranch).isSameBranchOrHasImageNamePrefix(inputBranch), expectedResult);
64+
assertEquals(new GitForkBranch(imageName, imageTag, specifiedBranch, "Dockerfile,docker-compose").isSameBranchOrHasImageNamePrefix(inputBranch), expectedResult);
6565
}
6666

6767
@Test(expectedExceptions = IllegalArgumentException.class)
6868
public void testIsSameBranchOrHasImageNamePrefix() {
69-
new GitForkBranch("", "1", "");
69+
new GitForkBranch("", "1", "", null);
70+
}
71+
72+
@DataProvider
73+
public Object[][] imageNameSpecifiedBranchAndFilenameSearched() {
74+
return new Object[][]{
75+
/*{"docker.io/some/container", "", "blah", "dockerfile", "blah"},
76+
{"127.0.0.1:443/some/container", "", "test", "dockerfile,docker-compose", "test"},
77+
{"docker.io/some/container", "123", "", "dockerfile", "docker.io/some/container-123_dockerfile"},
78+
{"docker.io/some/container", "123", "", "dockerfile,docker-compose", "docker.io/some/container-123"},
79+
{"docker.io/some/container", "123", "", "docker-compose", "docker.io/some/container-123_dockercompose"},
80+
{"docker.io/some/container", " ", null, "abcdef", "docker.io/some/container"},*/
81+
{"docker.io/some/container", null, "", "docker-compose", "docker.io/some/container_dockercompose"},
82+
};
83+
}
84+
85+
@Test(dataProvider = "imageNameSpecifiedBranchAndFilenameSearched")
86+
public void testGetBranchNameForFileNamesSearched(String imageName, String imageTag, String specifiedBranch, String filenameSearchedFor, String expectedResult) {
87+
assertEquals(new GitForkBranch(imageName, imageTag, specifiedBranch, filenameSearchedFor).getBranchName(), expectedResult);
7088
}
7189
}

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/process/ForkableRepoValidatorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void testContentHasChangesInDefaultBranch() throws InterruptedException,
146146
ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil);
147147
GHContent content = mock(GHContent.class);
148148
when(content.getPath()).thenReturn("/Dockerfile");
149-
GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null);
149+
GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null, "Dockerfile");
150150
when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenReturn(content);
151151
InputStream inputStream = new ByteArrayInputStream("FROM someImage".getBytes());
152152
when(content.read()).thenReturn(inputStream);
@@ -161,7 +161,7 @@ public void testInterruptedExceptionWhenRetrievingContentInDefaultBranch() throw
161161
ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil);
162162
GHContent content = mock(GHContent.class);
163163
when(content.getPath()).thenReturn("/Dockerfile");
164-
GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null);
164+
GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null, "Dockerfile");
165165
when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenThrow(new InterruptedException("some exception"));
166166

167167
assertEquals(validator.contentHasChangesInDefaultBranch(repo, content, gitForkBranch), shouldForkResult());
@@ -176,7 +176,7 @@ public void testContentHasNoChangesInDefaultBranch() throws InterruptedException
176176
String searchContentPath = "/Dockerfile";
177177
when(content.getPath()).thenReturn(searchContentPath);
178178
String imageName = "someImage";
179-
GitForkBranch gitForkBranch = new GitForkBranch(imageName, "someTag", null);
179+
GitForkBranch gitForkBranch = new GitForkBranch(imageName, "someTag", null, "Dockerfile");
180180
when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenReturn(content);
181181
InputStream inputStream = new ByteArrayInputStream("nochanges".getBytes());
182182
when(content.read()).thenReturn(inputStream);
@@ -193,7 +193,7 @@ public void testCouldNotFindContentInDefaultBranch() throws InterruptedException
193193
GHContent content = mock(GHContent.class);
194194
String searchContentPath = "/Dockerfile";
195195
when(content.getPath()).thenReturn(searchContentPath);
196-
GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null);
196+
GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null, "Dockerfile,docker-compose");
197197
when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenReturn(null);
198198
InputStream inputStream = new ByteArrayInputStream("FROM someImage".getBytes());
199199

@@ -223,7 +223,7 @@ public void testHasNoChanges(String contentText, String imageName, String imageT
223223
ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil);
224224
GHContent content = mock(GHContent.class);
225225
InputStream inputStream = new ByteArrayInputStream(contentText.getBytes());
226-
GitForkBranch gitForkBranch = new GitForkBranch(imageName, imageTag, null);
226+
GitForkBranch gitForkBranch = new GitForkBranch(imageName, imageTag, null,"docker-compose");
227227

228228
when(content.read()).thenReturn(inputStream);
229229

@@ -237,7 +237,7 @@ public void testHasNoChangesIfExceptionThrownDuringRead() throws IOException {
237237
GHRepository repo = mock(GHRepository.class);
238238
ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil);
239239
GHContent content = mock(GHContent.class);
240-
GitForkBranch gitForkBranch = new GitForkBranch("name", "tag", null);
240+
GitForkBranch gitForkBranch = new GitForkBranch("name", "tag", null, "");
241241

242242
when(content.read()).thenThrow(new IOException("failed on IO"));
243243

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public void testReturnPullRequestForBranch() {
118118
GHPullRequestQueryBuilder queryBuilder = getGHPullRequestQueryBuilder(imageName, Optional.of(ghPullRequest));
119119
GHRepository parent = mock(GHRepository.class);
120120
when(parent.queryPullRequests()).thenReturn(queryBuilder);
121-
GitForkBranch gitForkBranch = new GitForkBranch(imageName, "", null);
122-
121+
GitForkBranch gitForkBranch = new GitForkBranch(imageName, "", null, "Dockerfile,docker-compose");
123122

124123
gitHubUtil = mock(GitHubUtil.class);
125124
dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil);
@@ -133,7 +132,7 @@ public void testNoPullRequestForBranch() {
133132
GHPullRequestQueryBuilder queryBuilder = getGHPullRequestQueryBuilder(imageName, Optional.empty());
134133
GHRepository parent = mock(GHRepository.class);
135134
when(parent.queryPullRequests()).thenReturn(queryBuilder);
136-
GitForkBranch gitForkBranch = new GitForkBranch(imageName, "", null);
135+
GitForkBranch gitForkBranch = new GitForkBranch(imageName, "", null, "Dockerfile,docker-compose");
137136

138137

139138
gitHubUtil = mock(GitHubUtil.class);
@@ -782,7 +781,7 @@ public void testCreateOrUpdateForkBranchToParentDefaultHasBranch() throws IOExce
782781
when(parentBranch.getSHA1()).thenReturn(sha);
783782
when(parent.getBranch(defaultBranch)).thenReturn(parentBranch);
784783
GHRepository fork = mock(GHRepository.class);
785-
GitForkBranch gitForkBranch = new GitForkBranch("imageName", "imageTag", null);
784+
GitForkBranch gitForkBranch = new GitForkBranch("imageName", "imageTag", null, "Dockerfile");
786785
when(gitHubUtil.repoHasBranch(fork, gitForkBranch.getBranchName())).thenReturn(true);
787786
GHRef returnedRef = mock(GHRef.class);
788787
when(fork.getRef(anyString())).thenReturn(returnedRef);
@@ -804,7 +803,7 @@ public void testCreateOrUpdateForkBranchToParentDefaultDoesNotHaveBranch() throw
804803
when(parentBranch.getSHA1()).thenReturn(sha);
805804
when(parent.getBranch(defaultBranch)).thenReturn(parentBranch);
806805
GHRepository fork = mock(GHRepository.class);
807-
GitForkBranch gitForkBranch = new GitForkBranch("imageName", "imageTag", null);
806+
GitForkBranch gitForkBranch = new GitForkBranch("imageName", "imageTag", null, "Dockerfile");
808807
when(gitHubUtil.repoHasBranch(fork, gitForkBranch.getBranchName())).thenReturn(false);
809808

810809
dockerfileGitHubUtil.createOrUpdateForkBranchToParentDefault(parent, fork, gitForkBranch);
@@ -819,7 +818,7 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
819818
"tag", Constants.STORE,
820819
"store");
821820
Namespace ns = new Namespace(nsMap);
822-
GitForkBranch gitForkBranch = new GitForkBranch("image", "tag", null);
821+
GitForkBranch gitForkBranch = new GitForkBranch("image", "tag", null, "");
823822
Multimap<String, GitHubContentToProcess> pathToDockerfilesInParentRepo = HashMultimap.create();
824823
pathToDockerfilesInParentRepo.put("repo1", new GitHubContentToProcess(null, null, "df11"));
825824
pathToDockerfilesInParentRepo.put("repo1", new GitHubContentToProcess(null, null, "df12"));

0 commit comments

Comments
 (0)