public Iterable<String> getBranches() throws IOException { final ArrayList<String> branches = new ArrayList<>(); final GHRepository githubRepo = getGhRepository(); branches.addAll(githubRepo.getBranches().keySet()); branches.add("Pull Request: "); return branches; }
public static void deleteUpdateBotBranches(GHRepository ghRepository) throws IOException { if (ghRepository != null) { Map<String, GHBranch> branches = ghRepository.getBranches(); for (GHBranch ghBranch : branches.values()) { deleteUpdateBotBranch(ghRepository, ghBranch); } } }
private Map<String, GHBranch> fetchRemoteBranches(GHRepository remoteRepo) throws IOException { Map<String, GHBranch> branches = remoteRepo.getBranches(); String defName = remoteRepo.getDefaultBranch(); GHBranch def = branches.get(defName); if (def == null) { // just in case return branches; } // reorder branches so that default branch always comes first Map<String, GHBranch> ordered = new LinkedHashMap<>(); ordered.put(defName, def); branches.entrySet().forEach(e -> { if (!e.getKey().equals(defName)) { ordered.put(e.getKey(), e.getValue()); } }); return ordered; }
private GHBranch selectBranch(GitHubLocation location, final GHRepository repo) throws IOException { log.info("Using branch={}", location.getBranch()); final GHBranch branch = repo.getBranches().get(location.getBranch()); return branch; }
/** * Remote branch for future analysing. null - all remote branches. */ private Set<GHBranch> branchesToCheck(@CheckForNull String branch, @Nonnull GHRepository remoteRepo, GitHubBranchRepository localRepository) throws IOException { final LinkedHashSet<GHBranch> ghBranches = new LinkedHashSet<>(); if (branch != null) { try { GHBranch ghBranch = remoteRepo.getBranch(branch); if (ghBranch != null) { ghBranches.add(ghBranch); } } catch (FileNotFoundException ignore) { } } else { ghBranches.addAll(remoteRepo.getBranches().values()); } return ghBranches; }
@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); } }
@Override protected Iterable<GHBranch> create() { try { request.checkApiRateLimit(); Set<String> branchNames = request.getRequestedOriginBranchNames(); if (branchNames != null && branchNames.size() == 1) { String branchName = branchNames.iterator().next(); request.listener().getLogger().format("%n Getting remote branch %s...%n", branchName); try { GHBranch branch = repo.getBranch(branchName); return Collections.singletonList(branch); } catch (FileNotFoundException e) { // branch does not currently exist return Collections.emptyList(); } } request.listener().getLogger().format("%n Getting remote branches...%n"); return repo.getBranches().values(); } catch (IOException | InterruptedException e) { throw new GitHubSCMSource.WrappedException(e); } } }
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)); }
@Override protected ResourceInfo getResource(final GitHubLocation location, final String path) throws FileNotFoundException { GitHub github = null; GHRepository repo = null; GHBranch branch = null; try { github = location.open(); repo = github.getRepository(location.getRepoName()); log.debug("Using branch={}", location.getBranch()); branch = repo.getBranches().get(location.getBranch()); final GHContent content = repo.getFileContent(path, branch.getName()); final byte[] data = Base64.decodeBase64(content.getEncodedContent() .getBytes()); return new ResourceInfo(path, location, new ByteArrayInputStream( data)); } catch (final FileNotFoundException e) { throw e; } catch (final IOException e) { if (e.getMessage().contains("too_large")) { return getLargeResource(github, repo, branch, location, path); } throw new FileNotFoundException( "Could not locate the file at path " + path + ". e"); } }
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()); } }
private GitHubRepository mapGhRepository(GHRepository gitHubRepository) throws IOException { String repositoryName = gitHubRepository.getName(); int stars = gitHubRepository.listStargazers().asList().size(); int openPullRequests = gitHubRepository.getPullRequests(GHIssueState.OPEN).size(); int pullRequests = openPullRequests + gitHubRepository.getPullRequests(GHIssueState.CLOSED).size(); int openIssues = gitHubRepository.getOpenIssueCount(); int branches = gitHubRepository.getBranches().size(); int collaborators = gitHubRepository.getCollaborators().size(); int forks = gitHubRepository.getForks(); int watchers = gitHubRepository.getWatchers(); return new GitHubRepository(repositoryName, stars, openPullRequests, pullRequests, openIssues, branches, collaborators, forks, watchers); }