Coder Social home page Coder Social logo

github-java-client's Introduction

maven codecovLicense lifecycle: beta Maven Central

github-client

A small Java library for talking to Github/Github Enterprise and interacting with projects.

It supports authentication via simple access tokens, JWT endpoints and Github Apps (via private key).

It is also very light on GitHub, doing as few requests as necessary.

Getting Started

Include the latest version of github-client into your project:

In Maven:

<dependency>
  <groupId>com.spotify</groupId>
  <artifactId>github-client</artifactId>
  <version>version</version>
</dependency>

Start talking to Github API.

final GitHubClient github = GithubClient.create(URI.create("https://github.com/api/v3/"));
final IssueApi issueClient = github.createRepositoryClient("my-org", "my-repo").createIssueClient();
issueClient.listComments(ISSUE_ID).get().forEach(comment -> log.info(comment.body()));

Authenticating

Simple access token

final GitHubClient github = GithubClient.create(URI.create("https://github.com/api/v3/"));
// Do the requests
github.createRepositoryClient("my-org", "my-repo").getCommit("sha");

Private key

To authenticate as a Github App, you must provide a private key and the App ID, together with the API URL.

final GitHubClient github =
  GitHubClient.create(
    URI.create("https://github.com/api/v3/"),
    new File("/path-to-the/private-key.pem"),
    APP_ID);

Then, you can scope the client for a specific Installation ID, to do the operations at the installation level. The client will manage the generation of JWT tokens, as well as requesting and caching the installation tokens from GitHub.

final GitHubClient scoped = GitHubClient.scopeForInstallationId(github, INSTALLATION_ID);
// Do the requests now using the scoped client.
scoped.createRepositoryClient("my-org", "my-repo").getCommit("sha");

It is also possible to provide the installation to the root client.

Refer to Github App Authentication Guide for more information.

Usage

This library attempts to mirror the structure of GitHub API endpoints. As an example, to get details of a Commit, there is the GET /repos/:owner/:repo/commits API call, under the repos API. Therefore, the getCommit method lives in the RepositoryClient.

final GitHubClient github = GithubClient.create(URI.create("https://github.com/api/v3/"));
github.createRepositoryClient("my-org", "my-repo").getCommit("sha");

Some APIs, such as Checks API are nested in the Repository API. Endpoints such as POST /repos/:owner/:repo/check-runs live in the ChecksClient:

final GitHubClient github =
  GitHubClient.create(
    URI.create("https://github.com/api/v3/"),
    new File("/path-to-the/private-key.der"),
    APP_ID);
// Checks API need to be used by Github Apps
GitHubClient.scopeForInstallationId(github, INSTALLATION_ID)
  .createRepositoryClient("my-org", "my-repo")
  .createChecksApiClient()
  .createCheckRun(CHECK_RUN_REQUEST);

Contributing

This project uses Maven. To run the tests locally, just run:

mvn clean verify

Notes about maturity

This module was created after existing libraries were evaluated and dismissed, and we found that we were writing similar code in multiple projects. As such, it at least initially only contains enough functionality for our internal requirements which reflect that we were working on build system integration with the Github pull requests. It has been widely used for 4+ years. It's important to notice that it does not cover all Github v3 API. Adding missing endpoints should be very straightforward. Pull Requests are welcome.

Code of conduct

This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.

github-java-client's People

Contributors

brunodea avatar dependabot[bot] avatar henriquetruta avatar hewhomustnotbenamed avatar lirick avatar marthast avatar mbruggmann avatar mimfgg avatar vglocus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.