private static String buildUrl(GHRepository remoteRepository, String tag) { String repoUrl = remoteRepository.getHtmlUrl().toExternalForm(); if (remoteRepository.getDefaultBranch().equals(tag)) { return repoUrl; } return repoUrl + "/releases/tag/" + tag; }
private static String buildUrl(GHRepository remoteRepository, String branch) { String repoUrl = remoteRepository.getHtmlUrl().toExternalForm(); if (remoteRepository.getDefaultBranch().equals(branch)) { return repoUrl; } return repoUrl + "/tree/" + branch; }
private GHRef getReferenceToDefaultBranch() throws IOException { return ghRepo.getRef("heads/" + ghRepo.getDefaultBranch()); }
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; }
@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 String getBranch() { return this.issue.isPullRequest() ? GitBranch.PULL_REQUEST_PREFIX + this.issue.getNumber() : this.repository.getDefaultBranch(); }
origRepo.getDefaultBranch(), body);
origRepo.getDefaultBranch(), body);
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(); }
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()); } }
primary = remoteRepo.getDefaultBranch().equals(head.getName()); link = new GitHubBranchAction(remoteRepo, head.getName()); desc = null;
public static void initializeRepos(GHOrganization org, List<String> repos, String image, List<GHRepository> createdRepos, GitHubUtil gitHubUtil) throws Exception { for (String repoName : repos) { GHRepository repo = org.createRepository(repoName) .description("Delete if this exists. If it exists, then an integration test crashed somewhere.") .private_(false) .create(); // Ensure that repository exists for (int attempts = 0; attempts < 5; attempts++) { try { repo = gitHubUtil.getRepo(repo.getFullName()); break; } catch (Exception e) { log.info("Waiting for {} to be created", repo.getFullName()); Thread.sleep(TimeUnit.SECONDS.toMillis(1)); } } repo.createContent("FROM " + image + ":test", "Integration Testing", "Dockerfile"); createdRepos.add(repo); log.info("Initializing {}/{}", org.getLogin(), repoName); gitHubUtil.tryRetrievingContent(repo, "Dockerfile", repo.getDefaultBranch()); } }
String branch = (ns.get(Constants.GIT_BRANCH) == null) ? forkedRepo.getDefaultBranch() : ns.get(Constants.GIT_BRANCH);
if (StringUtils.isNotBlank(ghRepository.getDefaultBranch())) { result.add(new GitHubDefaultBranch(getRepoOwner(), repository, ghRepository.getDefaultBranch()));
branch = repo.getDefaultBranch();
branch = repo.getDefaultBranch();
/** * Resolves remote branch name at runtime using a combination of .updatebot.yml setting, or Github default branch, * or master as fallback. If branch is not set, tries to resolve default branch name from remote Github repository * in order to create PRs with updates using the same branch. * * Returns remote branch name to use * */ public String resolveRemoteBranch() { // Let's try use repository branch from .updatebot.yml first GitRepositoryConfig config = repo.getRepositoryDetails(); if (config == null) { LOG.warn("The repo has no config!"); return "master"; } if(!Strings.empty(config.getBranch())) { return config.getBranch(); } // Try detect Github repository and use its default branch else if(repo instanceof GithubRepository) { GHRepository ghRepository = GitHubHelpers.getGitHubRepository(this); if (ghRepository != null) { config.setBranch(ghRepository.getDefaultBranch()); } } // Fallback to master branch for Git repositories if(Strings.empty(config.getBranch())) { config.setBranch("master"); } return config.getBranch(); }