/** * Retrieve the base64-encoded content that is stored at this location. * * <p> * Due to the nature of GitHub's API, you're not guaranteed that * the content will already be populated, so this may trigger * network activity, and can throw an IOException. * * @deprecated * Use {@link #read()} */ public String getEncodedContent() throws IOException { if (content!=null) return content; else return Base64.encodeBase64String(IOUtils.toByteArray(read())); }
/** * Retrieve the base64-encoded content that is stored at this location. * * <p> * Due to the nature of GitHub's API, you're not guaranteed that * the content will already be populated, so this may trigger * network activity, and can throw an IOException. * * @deprecated * Use {@link #read()} */ public String getEncodedContent() throws IOException { if (content!=null) return content; else return Base64.encodeBase64String(IOUtils.toByteArray(read())); }
/** * Retrieve the base64-encoded content that is stored at this location. * * <p> * Due to the nature of GitHub's API, you're not guaranteed that * the content will already be populated, so this may trigger * network activity, and can throw an IOException. * * @deprecated * Use {@link #read()} */ public String getEncodedContent() throws IOException { if (content!=null) return content; else return Base64.encodeBase64String(IOUtils.toByteArray(read())); }
public void modifyOnGithub(GHContent content, String branch, String img, String tag, String customMessage) throws IOException { try (InputStream stream = content.read(); InputStreamReader streamR = new InputStreamReader(stream); BufferedReader reader = new BufferedReader(streamR)) { findImagesAndFix(content, branch, img, tag, customMessage, reader); } }
public void modifyOnGithub(GHContent content, String branch, String img, String tag, String customMessage) throws IOException { try (InputStream stream = content.read(); InputStreamReader streamR = new InputStreamReader(stream); BufferedReader reader = new BufferedReader(streamR)) { findImagesAndFix(content, branch, img, tag, customMessage, reader); } }
public String getFileContent(String pathToFile) throws IOException { return IOUtils.toString(ghRepo.getFileContent(pathToFile).read(), "UTF-8"); }
public InputStream getFileContent(String path) throws IOException { return repository.getFileContent(path).read(); }
@Test(dependsOnMethods = {"testIdempotency", "testSameNameAcrossDifferentOrgs"}) public void testStoreUpdate() throws Exception { String user = github.getMyself().getLogin(); GHRepository storeRepo = github.getRepository(Paths.get(user, STORE_NAME).toString()); String latestCommit = storeRepo.getBranches().get(storeRepo.getDefaultBranch()).getSHA1(); GHContent store = storeRepo.getFileContent("store.json", latestCommit); try (InputStream stream = store.read(); InputStreamReader streamR = new InputStreamReader(stream)) { JsonElement json = new JsonParser().parse(streamR); assertNotNull(json); JsonElement images = json.getAsJsonObject().get("images"); assertNotNull(images); Object image1 = images.getAsJsonObject().get(IMAGE_1); assertNotNull(image1); Object image2 = images.getAsJsonObject().get(IMAGE_2); assertNotNull(image2); assertEquals(images.getAsJsonObject().get(IMAGE_1).getAsString(), TAG); assertEquals(images.getAsJsonObject().get(IMAGE_2).getAsString(), TAG); } }
@NonNull @Override public InputStream content() throws IOException, InterruptedException { Object metadata = metadata(); if (metadata instanceof List) { throw new IOException("Directory"); } if (metadata instanceof GHContent) { return ((GHContent)metadata).read(); } throw new FileNotFoundException(getPath()); }
InputStream load(GHRepository repo, String revision) throws IOException { if (isNull(cachedContent)) { synchronized (this) { if (cachedContent == null) { GHContent content = repo.getFileContent(path, revision); long size = content.getSize(); if (size > CONTENT_THRESHOLD) { // don't cache return content.read(); } byte[] buf = new byte[(int) size]; try (InputStream in = content.read()) { IOUtils.readFully(in, buf); } cachedContent = buf; } } } return new ByteArrayInputStream(cachedContent); }
protected Set<Map.Entry<String, JsonElement>> parseStoreToImagesMap(String storeName) throws IOException, InterruptedException { GHMyself myself = dockerfileGitHubUtil.getMyself(); String login = myself.getLogin(); GHRepository store = dockerfileGitHubUtil.getRepo(Paths.get(login, storeName).toString()); GHContent storeContent = dockerfileGitHubUtil.tryRetrievingContent(store, Constants.STORE_JSON_FILE, store.getDefaultBranch()); if (storeContent == null) { return Collections.emptySet(); } JsonElement json; try (InputStream stream = storeContent.read(); InputStreamReader streamR = new InputStreamReader(stream)) { try { json = new JsonParser().parse(streamR); } catch (JsonParseException e) { log.warn("Not a JSON format store."); return Collections.emptySet(); } } JsonElement imagesJson = json.getAsJsonObject().get("images"); return imagesJson.getAsJsonObject().entrySet(); }
@Test(dependsOnMethods = "testStoreUpdate") public void testAsUser() throws Exception { GHRepository repo = github.getOrganization(ORG).getRepository(NAME); List<GHPullRequest> prs = repo.getPullRequests(GHIssueState.OPEN); Assert.assertTrue(prs.size() == 1); for (GHPullRequest pr : prs) { pr.merge("Automatic merge through itests."); pr.close(); } try (InputStream stream = repo.getFileContent("Dockerfile").read(); InputStreamReader streamR = new InputStreamReader(stream); BufferedReader reader = new BufferedReader(streamR)) { String line; while ((line = reader.readLine()) != null) { if (line.contains("FROM")) { Assert.assertTrue(line.contains(IMAGE)); Assert.assertTrue(line.endsWith(TAG)); } } } }
GHRepository storeRepo = github.getRepository(Paths.get(user, STORE_NAME).toString()); GHContent store = storeRepo.getFileContent("store.json"); try (InputStream stream = store.read(); InputStreamReader streamR = new InputStreamReader(stream)) { json = new JsonParser().parse(streamR); images = json.getAsJsonObject().get("images");
protected Set<Map.Entry<String, JsonElement>> parseStoreToImagesMap(String storeName) throws IOException, InterruptedException { GHMyself myself = dockerfileGitHubUtil.getMyself(); String login = myself.getLogin(); GHRepository store = dockerfileGitHubUtil.getRepo(Paths.get(login, storeName).toString()); GHContent storeContent = dockerfileGitHubUtil.tryRetrievingContent(store, Constants.STORE_JSON_FILE, store.getDefaultBranch()); if (storeContent == null) { return Collections.emptySet(); } JsonElement json; try (InputStream stream = storeContent.read(); InputStreamReader streamR = new InputStreamReader(stream)) { try { json = new JsonParser().parse(streamR); } catch (JsonParseException e) { log.warn("Not a JSON format store."); return Collections.emptySet(); } } JsonElement imagesJson = json.getAsJsonObject().get("images"); return imagesJson.getAsJsonObject().entrySet(); }
public static void validateRepo(String repoName, String image, String testTag, GitHub github, GitHubUtil gitHubUtil) throws Exception { String login = github.getMyself().getLogin(); // Github is eventually consistent, give it some time to read for (int attempt = 0; attempt < 5; attempt++) { GHRepository repo = gitHubUtil.tryRetrievingRepository(Paths.get(login, repoName).toString()); if (repo == null) { fail("Repository not found: " + Paths.get(login, repoName).toString()); } String latestCommit = repo.getBranches().get(repo.getDefaultBranch()).getSHA1(); GHContent content = gitHubUtil.tryRetrievingContent(repo, "Dockerfile", latestCommit); if (content.getSize() > MAX_CONTENT_SIZE) { fail(String.format("Content is suspiciously large: %s, should be below %s", content.getSize(), MAX_CONTENT_SIZE)); } String dockerfileContent = IOUtils.toString(content.read()); if (dockerfileContent.contains(testTag)) { assertThat(dockerfileContent).startsWith("FROM"); assertThat(dockerfileContent).contains(image); assertThat(dockerfileContent).contains(testTag); validatePullRequestCreation(repo, true); return; } log.info("Dockerfile (commitref: {}, contents: {}) in {} did not contain tag {}, try #{}", latestCommit, dockerfileContent, repo.getFullName(), testTag, attempt); Thread.sleep(TimeUnit.SECONDS.toMillis(1)); } fail(String.format("Didn't find tag (%s) in Dockerfile in repo (%s)", testTag, repoName)); }
protected void updateStoreOnGithub(GHRepository repo, String path, String img, String tag) throws IOException { try { repo.getFileContent(path); } catch (IOException e) { repo.createContent("", "initializing store", path); } String latestCommit = repo.getBranches().get(repo.getDefaultBranch()).getSHA1(); log.info("Loading image store at commit {}", latestCommit); GHContent content = repo.getFileContent(path, latestCommit); if (content.isFile()) { JsonElement json; try (InputStream stream = content.read(); InputStreamReader streamR = new InputStreamReader(stream)) { try { json = new JsonParser().parse(streamR); } catch (JsonParseException e) { log.warn("Not a JSON format store. Clearing and rewriting as JSON..."); json = JsonNull.INSTANCE; } } String jsonOutput = getAndModifyJsonString(json, img, tag); content.update(jsonOutput, String.format("Updated image %s with tag %s.\n@rev none@", img, tag), repo.getDefaultBranch()); } }
protected void updateStoreOnGithub(GHRepository repo, String path, String img, String tag) throws IOException { try { repo.getFileContent(path); } catch (IOException e) { repo.createContent("", "initializing store", path); } String latestCommit = repo.getBranches().get(repo.getDefaultBranch()).getSHA1(); log.info("Loading image store at commit {}", latestCommit); GHContent content = repo.getFileContent(path, latestCommit); if (content.isFile()) { JsonElement json; try (InputStream stream = content.read(); InputStreamReader streamR = new InputStreamReader(stream)) { try { json = new JsonParser().parse(streamR); } catch (JsonParseException e) { log.warn("Not a JSON format store. Clearing and rewriting as JSON..."); json = JsonNull.INSTANCE; } } String jsonOutput = getAndModifyJsonString(json, img, tag); content.update(jsonOutput, String.format("Updated image %s with tag %s.\n@rev none@", img, tag), repo.getDefaultBranch()); } }