|
| 1 | +package com.baeldung.githubapi; |
| 2 | + |
| 3 | +import com.google.common.base.Charsets; |
| 4 | +import org.apache.commons.io.IOUtils; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.kohsuke.github.*; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +public class RepositoryLiveTest { |
| 19 | + private static final Logger LOG = LoggerFactory.getLogger(RepositoryLiveTest.class); |
| 20 | + |
| 21 | + @Test |
| 22 | + void whenWeListAUsersRepositories_thenWeCanAccessTheRepositories() throws IOException { |
| 23 | + GitHub gitHub = GitHub.connectAnonymously(); |
| 24 | + |
| 25 | + GHUser user = gitHub.getUser("eugenp"); |
| 26 | + List<GHRepository> repositoriesList = user.listRepositories().toList(); |
| 27 | + assertThat(repositoriesList).isNotEmpty(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void whenWeIterateAUsersRepositories_thenWeCanAccessTheRepositories() throws IOException { |
| 32 | + GitHub gitHub = GitHub.connectAnonymously(); |
| 33 | + |
| 34 | + GHUser user = gitHub.getUser("eugenp"); |
| 35 | + |
| 36 | + Set<String> names = new HashSet<>(); |
| 37 | + for (GHRepository ghRepository : user.listRepositories()) { |
| 38 | + names.add(ghRepository.getName()); |
| 39 | + } |
| 40 | + |
| 41 | + assertThat(names).isNotEmpty(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void whenWeDirectlyAccessAUsersRepository_thenWeCanQueryRepositoryDetails() throws IOException { |
| 46 | + GitHub gitHub = GitHub.connectAnonymously(); |
| 47 | + |
| 48 | + GHUser user = gitHub.getUser("eugenp"); |
| 49 | + GHRepository repository = user.getRepository("tutorials"); |
| 50 | + assertEquals("tutorials", repository.getName()); |
| 51 | + assertEquals("eugenp/tutorials", repository.getFullName()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void whenWeDirectlyAccessARepository_thenWeCanQueryRepositoryDetails() throws IOException { |
| 56 | + GitHub gitHub = GitHub.connectAnonymously(); |
| 57 | + |
| 58 | + GHRepository repository = gitHub.getRepository("eugenp/tutorials"); |
| 59 | + assertEquals("tutorials", repository.getName()); |
| 60 | + assertEquals("eugenp/tutorials", repository.getFullName()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void whenWeAccessARepositoryBranch_thenWeCanAccessCommitDetails() throws IOException { |
| 65 | + GitHub gitHub = GitHub.connectAnonymously(); |
| 66 | + |
| 67 | + GHRepository repository = gitHub.getRepository("eugenp/tutorials"); |
| 68 | + |
| 69 | + String defaultBranch = repository.getDefaultBranch(); |
| 70 | + GHBranch branch = repository.getBranch(defaultBranch); |
| 71 | + String branchHash = branch.getSHA1(); |
| 72 | + |
| 73 | + GHCommit commit = repository.getCommit(branchHash); |
| 74 | + LOG.info("Commit message: {}", commit.getCommitShortInfo().getMessage()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void whenWeAccessARepositoryBranch_thenWeCanAccessFiles() throws IOException { |
| 79 | + GitHub gitHub = GitHub.connectAnonymously(); |
| 80 | + |
| 81 | + GHRepository repository = gitHub.getRepository("eugenp/tutorials"); |
| 82 | + |
| 83 | + String defaultBranch = repository.getDefaultBranch(); |
| 84 | + GHContent file = repository.getFileContent("pom.xml", defaultBranch); |
| 85 | + |
| 86 | + String fileContents = IOUtils.toString(file.read(), Charsets.UTF_8); |
| 87 | + LOG.info("pom.xml file contents: {}", fileContents); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void whenWeAccessTheRepository_thenWeCanDirectlyAccessTheReadme() throws IOException { |
| 92 | + GitHub gitHub = GitHub.connectAnonymously(); |
| 93 | + |
| 94 | + GHRepository repository = gitHub.getRepository("eugenp/tutorials"); |
| 95 | + |
| 96 | + GHContent readme = repository.getReadme(); |
| 97 | + String fileContents = IOUtils.toString(readme.read(), Charsets.UTF_8); |
| 98 | + LOG.info("Readme file contents: {}", fileContents); |
| 99 | + } |
| 100 | +} |
0 commit comments