Giter Club home page Giter Club logo

jcabi-github's Introduction

logo

EO principles respected here DevOps By Rultor.com We recommend IntelliJ IDEA

mvn PDD status codecov Maven Central JavaDoc Hits-of-Code Lines of code License

This is a Java adapter to the GitHub RESTful API. There are a few other similar implementations on the market, but jcabi-github has a very strong focus on object-oriented principles of programming. On top of that, we have a unique implemenation of GitHub server-side functionality, which you can use in your unit tests, eliminating the necessity to connect to GitHub during unit/integration testing. Please, read the blog post Object-Oriented Github API by Yegor Bugayenko, the creator of this library.

Java 8 or higher is required.

More details are here: github.jcabi.com.

You may also get help in this Telegram chat.

The set of classes in the com.jcabi.github package is the object-oriented API. Use it like this:

Work with Github's API

By default, the library works with Github's API (https://api.github.com)

import com.jcabi.github.*;
public class Main {
  public static void main(String[] args) throws IOException {
    Github github = new RtGithub(".. your OAuth token ..");
    Repo repo = github.repos().get(
        new Coordinates.Simple("octocat/Hello-World")
    );
    Issue issue = repo.issues().create("Test title", "Test description");
    issue.comments().post("My first comment!");
  }
}

Work with Github Enterprise or other

If you want to work with Github's API through another domain, you can use the URI-constructors of class RtGithub. For instance, if you have your own instance of Github deployed under the domain https://github.mydomain.com, do the following:

final Github github = new RtGithub(URI.create("https://github.mydomain.com"));

//OR

final Github github = new RtGithub(
    "<<oauth2_token>>",
    URI.create("https://github.mydomain.com")
);

//OR

final Github github = new RtGithub(
    "username", "password",
    URI.create("https://github.mydomain.com")
);

DO NOT change or mask your URIs! Using Github under a different domain is fine but do not change the URI paths. Changing the requests' paths is not possible since the whole architecture of this library relies on Github's URI paths.

For more complex configurations, you can instantiate RtGithub with your own custom Request, by using the RtGithub(Request) constructor. Be sure to configure the Request properly. See how the default Request is created -- you basically have to do the same thing.

Mock Implementation Of The API

We also provide MkGithub, a mock version of the GitHub server, which you can use in your unit tests, for example:

import com.jcabi.github.*;
public class FooTest {
  public void submitsCommentToGithubIssue() {
    final Repo repo = new MkGithub().repos().create(
      Json.createObjectBuilder().add("name", "test").build()
    );
    final Issue issue = repo.issues().create("how are you?", "");
    new Foo(issue).doSomething(); // should post a message to the issue
    MasterAssert.assertThat(
      issue.comments().iterate(),
      Matchers.iterableWithSize(1)
    );
  }
}

How to contribute?

Fork the repository, make changes, submit a pull request. We promise to review your changes same day and apply to the master branch, if they look correct.

Please run Maven (3.1 or higher!) build before submitting a pull request:

$ mvn clean install -Pqulice

There are many integration tests that check our classes against live Github accounts. In order to run them, you should create a new Github OAuth access tokens (how?), and provide them in command line, like this:

$ mvn clean install -Dit.test=RtGistITCase -Dfailsafe.github.key=<token> -Dfailsafe.github.key.second=<second-token> -Dfailsafe.github.repo=<repo>

Replace <token> and <second-token> with the OAuth access tokens of two different Github accounts. This test case will try to fork a gist from first account into second. Replace <repo> with the name of repository you create in your first account (for test purposes only), for example yegor256/test. OAuth access tokens should have permissions in their respective repos to all scopes needed by the integration test suite you want to run (including delete_repo, which is not set by default!).

Please note that different integration tests may need keys with permissions to different scopes. To run all integration tests, the key should have the following OAuth scopes:

  • read:org
  • repo
  • delete_repo
  • admin:public_key
  • gist
  • admin:repo_hook
  • user
  • user:email

RtForksITCase requires additional parameter -Dfailsafe.github.organization=<organization> where <organization> is an organization name to fork test github repository.

In order to run static analysis checks only use this:

$ mvn clean install -DskipTests -Dinvoker.skip=true -Pqulice

jcabi-github's People

Contributors

aistomin avatar alexdag avatar amihaiemil avatar andrescandal avatar asinyagin avatar carlosmiranda avatar cvrebert avatar cyberone avatar harisabh avatar hariso avatar hildo avatar jac1013 avatar lexaux avatar llorllale avatar longtimeago avatar lthuangiang avatar maurezen avatar mentiflectax avatar nhekfqn avatar paulodamaso avatar prondzyn avatar renovate[bot] avatar rultor avatar sadovnikov avatar sanai56967 avatar shalomgottesman avatar svarovski avatar volodya-lombrozo avatar yegor256 avatar zygm0nt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jcabi-github's Issues

GhLabels.java:51-53: Unit test for GhLabels is...

Puzzle 1-b2cfb4c0 in src/main/java/com/jcabi/github/GhLabels.java:51-53 has to be resolved: Unit test for GhLabels is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations.

RtIssueTest.java:43-46: I assumed that the JSON...

Puzzle 42-da44aba5 in src/test/java/com/jcabi/github/RtIssueTest.java:43-46 has to be resolved: I assumed that the JSON methods json() and fetch() are not covered by this test suite, since they are covered by GhJson tests. GhIssue just creates a GhJson using its own request object. I'm not entirely sure whether we should test it again here or not.

RtRepo.java:46-48: Unit test for RtRepo is...

Puzzle 1-95846c03 in src/main/java/com/jcabi/github/RtRepo.java:46-48 has to be resolved: Unit test for RtRepo is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations.

Inner Class defines compareTo(...) and uses Object.equals()

In com.jcabi.github.Issue on line 341 an anonymous inner Label is created. This implements compareTo but relies on the Object.equals method.

From the JavaDoc for the compareTo method in the Comparable interface:
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

GhUsers.java:45-47: Unit test for GhUsers is...

Puzzle 1-88ea083c in src/main/java/com/jcabi/github/GhUsers.java:45-47 has to be resolved: Unit test for GhUsers is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations.

GhGist.java:55-60: Unit test for GhGist is...

Puzzle 1-87aa0a8c in src/main/java/com/jcabi/github/GhGist.java:55-60 has to be resolved: Unit test for GhGist is required. At the moment we have only an integration test, which works only with real Github login/pwd credentials. Let's create a unit test, which will mock the request (probably using com.rexsl.test.request.FakeRequest) and check that the class really does what it has to do. Main focus, of course, on methods read() and write()

GhPull.java:52-54: Unit test for GhPull is...

Puzzle 1-820b0ab1 in src/main/java/com/jcabi/github/GhPull.java:52-54 has to be resolved: Unit test for GhPull is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations.

Inconsistent use of string constants

It is generally a good idea to define a named string constant instead of using the repeating the string literals throughout the code. Although it is doubtful that the string value will change if it is in a public API some of the practical benefits are as follows:

It reduces the chance of typos creeping in in string literals.
It allows semantic information about the intent of the string to be shown explicitly in the code. E.g. is it a property name or a property value, where is it used. etc. This makes life easier for new developers and code reviewers alike since the original developer's intent is explicit in the code.
It violates DRY, and I think you generally get better code if all developers are used focusing on reducing repetition.

At least it should be consistent

In com.jcabi.github.Issue already defines constants for OPEN_STATE, CLOSED_STATE.

But grep'ing for "state" in the code base turns up 5 instances of that string in the code.

Grep'ing for "title" in the code base turns up 10 instances of that string in the code.

Lots of others.

RtEvent.java:46-47: Unit test for RtEvent is...

Puzzle 1-294ae118 in src/main/java/com/jcabi/github/RtEvent.java:46-47 has to be resolved: Unit test for RtEvent is required. Let's create a simple one, to check that the class implements key functions correctly.

RtGithub.java:67-70: Unit test for RtGithub is...

Puzzle 1-461455d0 in src/main/java/com/jcabi/github/RtGithub.java:67-70 has to be resolved: Unit test for RtGithub is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations. Let's also check emojis() and meta().

GhMarkdown.java:53-57: Unit test for GhMarkdown is...

Puzzle 1-1e609d8b in src/main/java/com/jcabi/github/GhMarkdown.java:53-57 has to be resolved: Unit test for GhMarkdown is required. Let's create a simple one, to check that the class implements key functions correctly. I think we should mock Request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class sends correct HTTP requests and parses results correctly.

GhIssue.java:47-49: Unit test for GhIssue is...

Puzzle 1-745b68b1 in src/main/java/com/jcabi/github/GhIssue.java:47-49 has to be resolved: Unit test for GhIssue is required. Let's create a simple one, to check that the class implements key functions correctly. We have an integration test now, but this is not enough.

GhLimit.java:45-47: Unit test for GhLimit is...

Puzzle 1-42f5eba7 in src/main/java/com/jcabi/github/GhLimit.java:45-47 has to be resolved: Unit test for GhLimit is required. Let's create a simple one, to check that the class implements key functions correctly. The most important method to test is json()

GhPulls.java:52-54: Unit test for GhPulls is...

Puzzle 1-1abc990d in src/main/java/com/jcabi/github/GhPulls.java:52-54 has to be resolved: Unit test for GhPulls is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations.

User.java:46-54: Fetch list of emails of...

Puzzle 1-63970b48 in src/main/java/com/jcabi/github/User.java:48-48 has to be resolved: Fetch list of emails of a user. Let's implement a new method emails() that returns an instance of class UserEmails with a few methods: 1) iterate() returning a list of strings, 2) add(String), and 3) remove(String). Let's use the new response format suggested by Github: http://developer.github.com/v3/users/emails/#list-email-addresses-for-a-user This new UserEmails interface should be implemented by GhUserEmails, tested in a unit and integration tests. Besides that, we should implement MkUserEmails class.

GhGists.java:53-58: Unit test for GhGists is...

Puzzle 1-dea1a809 in src/main/java/com/jcabi/github/GhGists.java:53-58 has to be resolved: Unit test for GhGists is required. Let's create a simple one, to check that the class implements key functions correctly. At the moment we don't have any tests for this class, neither a unit test nor integration. Let's mock the request (using Mockito or just a class com.rexsl.test.request.FakeRequest), and check that create(), get(), and iterate() work.

Inefficient int comparison GhComment compareTo

In GhComment compareTo() is implemented like this;

public int compareTo(final Comment comment) {
      return new Integer(this.number()).compareTo(comment.number());
}

Which results in the creation of the new Integer, plus the boxing of the return from comment.number() and then a call to integer.compareTo

However the semantics of Integer compareTo() are simply:

"returns the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than the argument Integer (signed comparison)."

Since number() returns a primitive that can't be null, I think this method can be re-written more efficiently as

public int compareTo(final Comment comment) {
      return this.number() - comment.number();
}

GhIssues.java:53-55: Unit test for GhIssues is...

Puzzle 1-aec95d00 in src/main/java/com/jcabi/github/GhIssues.java:53-55 has to be resolved: Unit test for GhIssues is required. Let's create a simple one, to check that the class implements key functions correctly. The most important methods to test are create() and iterate()

package-info.java:46-50: Use RequestBody#set(JsonValue) as soon as...

Puzzle 1-cc4e916e in src/main/java/com/jcabi/github/package-info.java:46-50 has to be resolved: Use RequestBody#set(JsonValue) as soon as it is introduced in rexsl-test. Let's use this new method as soon as it becomes available. Instead of building objects using JsonGenerator (for example, see GhComments#post), let's use JsonObjectBuilder. See yegor256/rexsl#740

DefaultGithub.java:67-70: Unit test for DefaultGithub is...

Puzzle 1-3cf89855 in src/main/java/com/jcabi/github/DefaultGithub.java:67-70 has to be resolved: Unit test for DefaultGithub is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations. Let's also check emojis() and meta().

GhLabel.java:47-50: Unit test for GhLabel is...

Puzzle 1-503e3ad1 in src/main/java/com/jcabi/github/GhLabel.java:47-50 has to be resolved: Unit test for GhLabel is required. Let's create a simple one, to check that the class implements key functions correctly. The most important methods to test are json() and patch(). Although, would be great to cover the entire class.

Repo.java:46-53: Assignees API should be implemented....

Puzzle 1-05eb6089 in src/main/java/com/jcabi/github/Repo.java:46-53 has to be resolved: Assignees API should be implemented. Let's add a method assignees() to this class returning an instance of interface Assignees. This interface should have at least two methods: 1) iterate() returning a list of Users and 2) check(String) returning TRUE if provided login can be used as an assignee in repository. New interface should be implemented by GhAssignees class and tested in unit and integration tests. Moreover, we should implement MkAssignees class. See http://developer.github.com/v3/issues/assignees/

package-info.java:46-49: Change class prefixes from Gh...

Puzzle 1-dddab49b in src/main/java/com/jcabi/github/package-info.java:46-49 has to be resolved: Change class prefixes from Gh to Rt. Let's rename all classes by changing their prefixes. Now we're using Gh, which is semantically not correct. Rt will stand for REST. Also, let's rename DefaultGithub to RtGithub. Let's not forget to update Maven documentation and README.md.

GhPagination.java:54-56: Unit test for GhPagination is...

Puzzle 1-56e988a1 in src/main/java/com/jcabi/github/GhPagination.java:54-56 has to be resolved: Unit test for GhPagination is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can jump through pages collecting data.

Gist.java:55-55: Gist comments. Let's add new...

Puzzle 1-65ca4b56 in src/main/java/com/jcabi/github/Gist.java:59-65 has to be resolved: Gist comments. Let's add new method comments() to this interface, returning an instance of interface GistComments. This new interface should implement methods do iterate, post, delete and read comments, as explained in http://developer.github.com/v3/gists/comments/. New interface should be implemented by GhGistComments class and tested with unit and integration tests.

MkGithub#relogin(String)

Let's introduce a new method relogin(String) in MkGithub class, which will return an instance of Github. This new object will be an instance of MkGithub, but with a different login name. Let's create a test, which will reproduce the following scenario:

  1. new MkGithub()
  2. create a new repo there
  3. post a new issue to the repo
  4. relogin
  5. get that issue by number
  6. post a comment there
  7. check that comment's author is different from issue author

GhJson.java:50-52: Unit test for GhJson is...

Puzzle 1-49e05ff2 in src/main/java/com/jcabi/github/GhJson.java:50-52 has to be resolved: Unit test for GhJson is required. Let's create a simple one, to check that the class implements its fetching and pathing features correctly.

RtUser.java:46-48: Unit test for GhUser is...

Puzzle 1-f530aba6 in src/main/java/com/jcabi/github/RtUser.java:46-48 has to be resolved: Unit test for GhUser is required. Let's mock request using Mockito or com.rexsl.test.request.FakeRequest, and make sure that the class can do its key operations.

User.java:57-65: Public keys of a user....

Puzzle 1-b7ffe774 in src/main/java/com/jcabi/github/User.java:57-65 has to be resolved: Public keys of a user. Let's implement a new method keys(), which should return an instance of interface PublicKeys. This interface should have at least methods 1) iterate() to list all public keys of a user, 2) get(String) to get a single public key, 3) remove(String) to remove a key. Every key should be an instance of interface PublicKey, extending JsonReadable and JsonPatchable. All of the new classes should be implemented with GhPublicKeys and GhPublicKey classes. We should create integration and unit tests, and implement MkPublicKeys and MkPublicKey classes.

Pull.java:52-58: Pull request comments. Let's add...

Puzzle 1-a64ff3b0 in src/main/java/com/jcabi/github/Pull.java:52-58 has to be resolved: Pull request comments. Let's add new method comments() to this interface, returning an instance of interface PullComments. This new interface should implement methods do iterate, post, delete and read comments, as explained in http://developer.github.com/v3/pulls/comments/. New interface should be implemented by GhPullComments class and tested with unit and integration tests.

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.