Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create issue over github using java

I want to create issue over GitHub using java EGIT API.

I have tried it:

GitHubClient client = new GitHubClient().setCredentials("abc", "xyz");

IssueService service = new IssueService(client);

Repository repo = new Repository();
like image 256
Anoop Kumar Avatar asked Nov 10 '22 06:11

Anoop Kumar


1 Answers

I was able to achieve this using EGit 2.1.5. Here are the steps I took:

  1. I created a Personal Access Token in the project https://github.com/settings/tokens. When creating the Token, specify the scope to your needs. For my case (just Issue creation), I opted for repo scope. enter image description here

Sweet, now that we have our token let's jump into the code.

GitHubClient client = new GitHubClient();
client.setOAuth2Token("PUT_YOUR_PERSONAL_ACCESS_TOKEN_HERE"); // Use the token generated above
IssueService issueService = new IssueService(client);
try {
    Issue issue = new Issue();
    issue.setTitle("Test issue"); // Title of the issue
    issue.setBody("Some stuff"); // Body of the issue
    // Other stuff can be included in the Issue as you'd expect .. like labels, authors, dates, etc. 
    // Check out the API per your needs
    
    // In my case, we utilize a private issue-only-repository "RepositoryIssueTracker" that is maintainted under our Organization "MyCompany"
    issueService.createIssue("MyCompany", "RepositoryIssueTracker", issue);
} catch (Exception e) {
    System.out.println("Failed");
    e.printStackTrace();
}

Summary: I think OP's issue was related to not providing proper credentials, possibly to a private repo. I also was unable to get Basic auth working with a username/password to our private repo (possibly because our organization requires 2-factor). Instead, using a Personal Access Token Authorization worked per the above instructions.

More info on creating an issue-only repository can be found here.

like image 116
VILLAIN bryan Avatar answered Nov 15 '22 07:11

VILLAIN bryan