Skip to content

Commit 1fabaf1

Browse files
Merge pull request #543 from salesforce/all-blob
Read BLOB for all sub-command
2 parents 7b9e073 + f6c357a commit 1fabaf1

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/storage/GitHubJsonStore.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.stream.Collectors;
2525
import org.kohsuke.github.GHBlob;
2626
import org.kohsuke.github.GHCommit;
27-
import org.kohsuke.github.GHContent;
2827
import org.kohsuke.github.GHMyself;
2928
import org.kohsuke.github.GHRepository;
3029
import org.slf4j.Logger;
@@ -137,13 +136,13 @@ protected String getAndModifyJsonString(JsonElement json, String img, String tag
137136
}
138137

139138
public Set<Map.Entry<String, JsonElement>> parseStoreToImagesMap(DockerfileGitHubUtil dockerfileGitHubUtil, String storeName)
140-
throws IOException, InterruptedException {
139+
throws IOException {
141140
GHMyself myself = dockerfileGitHubUtil.getMyself();
142141
String login = myself.getLogin();
143142
GHRepository store = dockerfileGitHubUtil.getRepo(Paths.get(login, storeName).toString());
144143

145-
GHContent storeContent = dockerfileGitHubUtil.tryRetrievingContent(store, Constants.STORE_JSON_FILE,
146-
store.getDefaultBranch());
144+
GHBlob storeContent = dockerfileGitHubUtil.tryRetrievingBlob(store,
145+
Constants.STORE_JSON_FILE, store.getDefaultBranch());
147146

148147
if (storeContent == null) {
149148
return Collections.emptySet();
@@ -163,7 +162,8 @@ public Set<Map.Entry<String, JsonElement>> parseStoreToImagesMap(DockerfileGitHu
163162
return imagesJson.getAsJsonObject().entrySet();
164163
}
165164

166-
public List<ImageTagStoreContent> getStoreContent(DockerfileGitHubUtil dockerfileGitHubUtil, String storeName) throws IOException, InterruptedException {
165+
public List<ImageTagStoreContent> getStoreContent(DockerfileGitHubUtil dockerfileGitHubUtil, String storeName)
166+
throws IOException {
167167
Set<Map.Entry<String, JsonElement>> imageToTagStore = parseStoreToImagesMap(dockerfileGitHubUtil, storeName);
168168
return imageToTagStore.stream()
169169
.map(entry -> new ImageTagStoreContent(entry.getKey(), entry.getValue().getAsString()))

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/storage/S3BackedImageTagStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public List<ImageTagStoreContent> getStoreContent(DockerfileGitHubUtil dockerfil
6969
return imageNamesWithTag;
7070
}
7171

72-
private List<ImageTagStoreContent> getStoreContentSortedByAccessDate(Map<String, Date> imageNameWithAccessTime) throws InterruptedException {
72+
private List<ImageTagStoreContent> getStoreContentSortedByAccessDate(Map<String, Date> imageNameWithAccessTime) {
7373
List<ImageTagStoreContent> imageNameWithTagSortedByAccessDate = new ArrayList<>();
7474
LinkedHashMap<String, Date> sortedResult = new LinkedHashMap<>();
7575
// Sort the content by the access date so that the file which was accessed most recently gets processed first

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ public GHContent tryRetrievingContent(GHRepository repo, String path, String bra
257257
return gitHubUtil.tryRetrievingContent(repo, path, branch);
258258
}
259259

260+
public GHBlob tryRetrievingBlob(GHRepository repo, String path, String branch)
261+
throws IOException {
262+
return gitHubUtil.tryRetrievingBlob(repo, path, branch);
263+
}
264+
260265
public void modifyOnGithub(GHContent content,
261266
String branch, String img, String tag,
262267
String customMessage, String ignoreImageString) throws IOException {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ public GHContent tryRetrievingContent(GHRepository repo, String path, String bra
196196
return content;
197197
}
198198

199+
public GHBlob tryRetrievingBlob(GHRepository repo, String path, String branch)
200+
throws IOException {
201+
return repo.getCommit(branch).getTree().getEntry(path).asBlob();
202+
}
203+
199204
/* Workaround: The GitHub API caches API calls for up to 60 seconds, so back-to-back API calls with the same
200205
* command will return the same thing. i.e. the above command listRepositories will return the same output if
201206
* this tool is invoked twice in a row, even though it should return different lists, because of the new forks.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,26 @@ public void testTryRetrievingContent() throws Exception {
406406
assertEquals(dockerfileGitHubUtil.tryRetrievingContent(new GHRepository(), "path", "branch"), content);
407407
}
408408

409+
@Test
410+
public void testTryRetrievingBlob() throws IOException {
411+
gitHubUtil = mock(GitHubUtil.class);
412+
GHBlob content = mock(GHBlob.class);
413+
when(gitHubUtil.tryRetrievingBlob(any(), anyString(), anyString())).thenReturn(content);
414+
dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil);
415+
assertEquals(dockerfileGitHubUtil.tryRetrievingBlob(new GHRepository(), "path", "branch"), content);
416+
}
417+
418+
@Test(
419+
expectedExceptions = IOException.class,
420+
expectedExceptionsMessageRegExp = "error while reading blob")
421+
public void testTryRetrievingBlobException() throws IOException {
422+
gitHubUtil = mock(GitHubUtil.class);
423+
GHBlob content = mock(GHBlob.class);
424+
when(gitHubUtil.tryRetrievingBlob(any(), anyString(), anyString())).thenThrow(new IOException("error while reading blob"));
425+
dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil);
426+
assertNotEquals(dockerfileGitHubUtil.tryRetrievingBlob(new GHRepository(), "path", "branch"), content);
427+
}
428+
409429

410430
@DataProvider
411431
public Object[][] inputBranchesImagesAndTags() {

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,46 @@ public void testTryRetrievingContent() throws Exception {
153153
verify(repo, times(1)).getFileContent(eq("path"), eq("branch"));
154154
}
155155

156+
@Test
157+
public void testTryRetrievingBlobSuccess() throws Exception {
158+
159+
GitHub github = mock(GitHub.class);
160+
GHRepository repo = mock(GHRepository.class);
161+
GHCommit commit = mock(GHCommit.class);
162+
GHTree ghTree = mock(GHTree.class);
163+
GHTreeEntry ghTreeEntry = mock(GHTreeEntry.class);
164+
GHBlob ghBlob = mock(GHBlob.class);
165+
when(repo.getCommit(anyString())).thenReturn(commit);
166+
when(commit.getTree()).thenReturn(ghTree);
167+
when(ghTree.getEntry(anyString())).thenReturn(ghTreeEntry);
168+
when(ghTreeEntry.asBlob()).thenReturn(ghBlob);
169+
170+
GitHubUtil gitHubUtil = new GitHubUtil(github);
171+
assertEquals(gitHubUtil.tryRetrievingBlob(repo, "path", "branch"), ghBlob);
172+
173+
verify(commit, times(1)).getTree();
174+
verify(ghTreeEntry, times(1)).asBlob();
175+
}
176+
177+
@Test(
178+
expectedExceptions = IOException.class,
179+
expectedExceptionsMessageRegExp = "error while reading commit")
180+
public void testTryRetrievingBlobException() throws Exception {
181+
182+
GitHub github = mock(GitHub.class);
183+
GHRepository repo = mock(GHRepository.class);
184+
GHCommit commit = mock(GHCommit.class);
185+
GHTreeEntry ghTreeEntry = mock(GHTreeEntry.class);
186+
GHBlob ghBlob = mock(GHBlob.class);
187+
when(repo.getCommit(anyString())).thenThrow(new IOException("error while reading commit"));
188+
189+
GitHubUtil gitHubUtil = new GitHubUtil(github);
190+
assertEquals(gitHubUtil.tryRetrievingBlob(repo, "path", "branch"), ghBlob);
191+
192+
verify(commit, times(0)).getTree();
193+
verify(ghTreeEntry, times(0)).asBlob();
194+
}
195+
156196
/* There is a timeout because if this part of the code is broken, it might enter 60 seconds of sleep. */
157197
@Test(timeOut = 1000)
158198
public void testGetGHRepositories() throws Exception {
@@ -335,4 +375,4 @@ public void testRepoHasBranchFalseForGHFileNotFoundException() throws IOExceptio
335375
when(repo.getBranch(branchName)).thenThrow(new GHFileNotFoundException());
336376
assertFalse(gitHubUtil.repoHasBranch(repo, branchName));
337377
}
338-
}
378+
}

0 commit comments

Comments
 (0)