/** * 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())); }
protected void modifyOnGithubRecursive(GHRepository repo, GHContent content, String branch, String img, String tag) throws IOException { /* If we have a submodule; we want to skip. Content is submodule when the type is file, but content.getDownloadUrl() is null. */ if (content.isFile() && content.getDownloadUrl() != null) { modifyOnGithub(content, branch, img, tag, ""); } else if(content.isDirectory()) { for (GHContent newContent : repo.getDirectoryContent(content.getPath(), branch)) { modifyOnGithubRecursive(repo, newContent, branch, img, tag); } } else { // The only other case is if we have a file, but content.getDownloadUrl() is null log.info("Skipping submodule {}", content.getName()); } }
public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessage) throws IOException { return update(newContentBytes, commitMessage, null); }
protected void findImagesAndFix(GHContent content, String branch, String img, String tag, String customMessage, BufferedReader reader) throws IOException { StringBuilder strB = new StringBuilder(); boolean modified = rewriteDockerfile(img, tag, reader, strB); if (modified) { content.update(strB.toString(), "Fix Dockerfile base image in /" + content.getPath() + "\n\n" + customMessage, branch); } }
private void handleFile(final List<String> documents, final GitHubLocation location, final GHContent c) { log.debug("handle file={}", c.getPath()); if (StringUtils.endsWith(c.getName(), "md")) { final String name = StringUtils.substringAfter(c.getPath(), location.getPath()); final String listingName = MountPointUtil.apply(location, name); documents.add(listingName); } }
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()); } }
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); }
@NonNull @Override public SCMProbeStat stat(@NonNull String path) throws IOException { checkOpen(); try { int index = path.lastIndexOf('/') + 1; List<GHContent> directoryContent = repo.getDirectoryContent(path.substring(0, index), ref); for (GHContent content : directoryContent) { if (content.getPath().equals(path)) { if (content.isFile()) { return SCMProbeStat.fromType(SCMFile.Type.REGULAR_FILE); } else if (content.isDirectory()) { return SCMProbeStat.fromType(SCMFile.Type.DIRECTORY); } else if ("symlink".equals(content.getType())) { return SCMProbeStat.fromType(SCMFile.Type.LINK); } else { return SCMProbeStat.fromType(SCMFile.Type.OTHER); } } if (content.getPath().equalsIgnoreCase(path)) { return SCMProbeStat.fromAlternativePath(content.getPath()); } } } catch (FileNotFoundException fnf) { // means that does not exist and this is handled below this try/catch block. } return SCMProbeStat.fromType(SCMFile.Type.NONEXISTENT); }
/** * List immediate children of this directory. */ public PagedIterable<GHContent> listDirectoryContent() throws IOException { if (!isDirectory()) throw new IllegalStateException(path+" is not a directory"); return new PagedIterable<GHContent>() { public PagedIterator<GHContent> _iterator(int pageSize) { return new PagedIterator<GHContent>(root.retrieve().asIterator(url, GHContent[].class, pageSize)) { @Override protected void wrapUp(GHContent[] page) { GHContent.wrap(page, repository); } }; } }; }
private void listRepo(final List<String> documents, final GitHubLocation location, final GHRepository repo, final GHBranch branch, final String path) throws IOException { final List<GHContent> content = repo.getDirectoryContent(path, branch.getName()); for (final GHContent c : content) { switch (c.getType()) { case "file": handleFile(documents, location, c); break; case "dir": handleDir(documents, location, repo, branch, c); break; default: log.info("ignoring {}", c.getName()); } } }
public GHContentUpdateResponse delete(String message) throws IOException { return delete(message, null); }
/** * Retrieve the decoded 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 getContent() throws IOException { return new String(DatatypeConverter.parseBase64Binary(getEncodedContent())); }
private boolean isGitmodulesFileExist() throws IOException { return 0 < ghRepo .getDirectoryContent("") .stream() .filter(item -> item.getName().equals(".gitmodules")) .count(); }
/** * Retrieves the actual content stored here. */ public InputStream read() throws IOException { return new Requester(root).asStream(getDownloadUrl()); }
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()); } }
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 findImagesAndFix(GHContent content, String branch, String img, String tag, String customMessage, BufferedReader reader) throws IOException { StringBuilder strB = new StringBuilder(); boolean modified = rewriteDockerfile(img, tag, reader, strB); if (modified) { content.update(strB.toString(), "Fix Dockerfile base image in /" + content.getPath() + "\n\n" + customMessage, branch); } }