|
| 1 | +package pl.koder95.eme.au; |
| 2 | + |
| 3 | +import org.kohsuke.github.*; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | + |
| 8 | +public class GitHubRepositoryController { |
| 9 | + |
| 10 | + private GitHub createNewConnection() throws IOException { |
| 11 | + return new GitHubBuilder().withRateLimitHandler(new RateLimitHandler() { |
| 12 | + @Override |
| 13 | + public void onError(IOException e, HttpURLConnection uc) { |
| 14 | + e.printStackTrace(); |
| 15 | + System.out.println(uc); |
| 16 | + } |
| 17 | + }).withOAuthToken("5c3210fa72227686dbaf630c6e192e68e7fae89c").build(); |
| 18 | + } |
| 19 | + |
| 20 | + private GitHub connection = null; |
| 21 | + |
| 22 | + GitHub getConnection() { |
| 23 | + while(connection == null) { |
| 24 | + try { |
| 25 | + connection = createNewConnection(); |
| 26 | + } catch (IOException e) { |
| 27 | + e.printStackTrace(); |
| 28 | + } |
| 29 | + } |
| 30 | + return connection; |
| 31 | + } |
| 32 | + |
| 33 | + final String username; |
| 34 | + final String repositoryName; |
| 35 | + |
| 36 | + public GitHubRepositoryController(String username, String repositoryName) { |
| 37 | + this.username = username; |
| 38 | + this.repositoryName = repositoryName; |
| 39 | + } |
| 40 | + |
| 41 | + private GHUser user = null; |
| 42 | + |
| 43 | + private void selectUser() throws IOException { |
| 44 | + GitHub connection = getConnection(); |
| 45 | + if (connection.isOffline()) { |
| 46 | + this.connection = null; |
| 47 | + selectUser(); |
| 48 | + } |
| 49 | + else { |
| 50 | + user = connection.getUser(username); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private GHRepository repo = null; |
| 55 | + |
| 56 | + private void selectRepository() throws IOException { |
| 57 | + if (user == null) throw new IllegalStateException("User is not selected."); |
| 58 | + else repo = user.getRepository(repositoryName); |
| 59 | + } |
| 60 | + |
| 61 | + public void init() { |
| 62 | + try { |
| 63 | + selectUser(); |
| 64 | + selectRepository(); |
| 65 | + } catch (IOException e) { |
| 66 | + throw new ExceptionInInitializerError(e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public GHRepository getRepository() { |
| 71 | + return repo; |
| 72 | + } |
| 73 | +} |
0 commit comments