4

I tried so hard for a simple line of code that read a file content from enterprise github with oauth token, but could not find a example of such.

I tried https://github.com/jcabi/jcabi-github, but it does not support enterprise github?(maybe I am wrong)

Now i am trying egit:

GitHubClient client = new GitHubClient("enterprise url");

GitHubRequest request = new GitHubRequest();

request.setUri("/readme");

GitHubResponse response = client.get(request);

Then what? I only saw a getBody, maybe I need to parse it with some kinda json library? It has to be simpler..I am expecting something like: repo.get(url).getContent()

Elias Ranz
  • 401
  • 1
  • 6
  • 21
user648922
  • 361
  • 5
  • 16

1 Answers1

10

Finally figure out by reading source code..

    GitHubClient client = new GitHubClient(YOURENTERPRICEURL);
    client.setOAuth2Token(token);

    // first use token service
    RepositoryService repoService = new RepositoryService(client);

    try {
        Repository repo = repoService.getRepository(USER, REPONAME);

        // now contents service
        ContentsService contentService = new ContentsService(client);
        List<RepositoryContents> test = contentService.getContents(repo, YOURFILENAME);

        List<RepositoryContents> contentList = contentService.getContents(repo);
        for(RepositoryContents content : test){
            String fileConent = content.getContent();
            String valueDecoded= new String(Base64.decodeBase64(fileConent.getBytes() ));
            System.out.println(valueDecoded);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
user648922
  • 361
  • 5
  • 16
  • 1
    It is quite useful piece of code, in case some one want to stream the file from git repository instead of get the content. below code could do that: `List contentList = contentService.getContents(repo); for(RepositoryContents content : test){ String fileConent = content.getContent(); InputStream is = new ByteArrayInputStream(Base64.decodeBase64(fileConent.getBytes())); }` – Wayne Wei May 15 '19 at 14:41