public String getSha1(String branchName) throws IOException { return ghRepo.getBranch(branchName).getSHA1(); }
public Optional<String> getLatestCommitShaOnBranch(String githubRepo, String branchName) { try { return Optional.of(gitHub.getRepository(githubRepo).getBranch(branchName).getSHA1()); } catch (Exception e) { logger.warn("Could not get latest commit on branch from Github!", e); return Optional.empty(); } }
@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 public GitHubBranchCause check(@Nonnull GitHubBranchDecisionContext context) throws IOException { GHBranch remoteBranch = context.getRemoteBranch(); GitHubBranch localBranch = context.getLocalBranch(); TaskListener listener = context.getListener(); GitHubBranchCause cause = null; if (nonNull(localBranch) && nonNull(remoteBranch)) { // didn't exist before final String localBranchSHA1 = localBranch.getCommitSha(); final String remoteBranchSHA1 = remoteBranch.getSHA1(); if (!localBranchSHA1.equals(remoteBranchSHA1)) { final PrintStream logger = listener.getLogger(); logger.printf("%s: hash has changed '%s' -> '%s'%n", DISPLAY_NAME, localBranchSHA1, remoteBranchSHA1); LOG.debug("{}: hash has changed '{}' -> '{}'", DISPLAY_NAME, localBranchSHA1, remoteBranchSHA1); cause = context.newCause(DISPLAY_NAME, false); } } return cause; }
public GitHubBranch(GHBranch ghBranch) { this(ghBranch.getName(), ghBranch.getSHA1(), ghBranch.getOwner()); }
GHCommit getLastCommit(GHBranch remoteBranch) throws IOException { return remoteBranch.getOwner().getCommit(remoteBranch.getSHA1()); }
@Override public String fetchHeadSha(GHRepository remoteRepo) throws IOException { GHBranch branch = remoteRepo.getBranch(getName()); if (branch == null) { throw new IOException("No branch " + getName() + " in " + remoteRepo.getFullName()); } return branch.getSHA1(); }
public GitHubBranchCause(@Nonnull GHBranch remoteBranch, @Nonnull GitHubBranchRepository localRepo, String reason, boolean skip) { this(remoteBranch.getName(), remoteBranch.getSHA1()); withReason(reason); withSkip(skip); withLocalRepo(localRepo); withRemoteData(remoteBranch); }
GHCompare.Commit[] getComparedCommits(GitHubBranch localBranch, GHBranch remoteBranch) throws IOException { String previous = localBranch.getCommitSha(); String current = remoteBranch.getSHA1(); LOG.debug("Comparing previous hash [{}] with current hash [{}]", previous, current); return remoteBranch.getOwner() .getCompare(previous, current) .getCommits(); }
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()); } }
.encodeTo(repositoryUrl + "/tree/" + branchName, branchName)); BranchSCMHead head = new BranchSCMHead(branchName); if (request.process(head, new SCMRevisionImpl(head, branch.getSHA1()), new SCMSourceRequest.ProbeLambda<BranchSCMHead, SCMRevisionImpl>() { @NonNull
rev = new AbstractGitSCMSource.SCMRevisionImpl((BranchSCMHead) head, repo.getBranch(ref).getSHA1());