Issues on GitHub/GitLab using APIs.

In this article, we will explore how we can create/get issues on GitHub/GitLab using API calls.

Issues on GitHub

Operations we are going to discuss-

  • Create an issue.
  • Get/Read an issue.

For creating issues on GitHub we need the PAT(Personal Access Token) of the user, the Repository/Project Owner Name, and the Repository/Project Name.

To create an Issue

API to hit for creating an issue — 
https://api.github.com/repos/OWNER/REPO/issues

Path parameters — 

  • owner — Account owner of the repository
  • repo — Name of the repository

Body parameters — 

  • title (string or integer) — Title of the Issue (mandatory field)
  • body(string) — Title of the Issue
  • assignees (array of string) — Logins for Users to assign to this issue. NOTE: Only users with push access can set assignees for new issues. Assignees are silently dropped otherwise.
  • milestone — The number of milestones to associate this issue with. NOTE: Only users with push access can set the milestone for new issues. The milestone is silently dropped otherwise.
  • labels (array) — Labels to associate with this issue. NOTE: Only users with push access can set labels for new issues. Labels are silently dropped otherwise.

How to make an API call (Java) —  

GitHubIssue gitHubIssue = new GitHubIssue();//GitHubIssue is a custom class with required fields.
gitHubIssue.setTitle("Title");//mandatory
gitHubIssue.setBody("Description");
gitHubIssue.setAssignees(new String[]{"AssigneeUserName"});
//You can give other fields too..

String gitHubCreateIssueUrl = "https://api.github.com/repos/OWNER/REPO/issues";
/*OWNER - Repository/Project Owner Name
* REPO - Repository/Project Name
*/
String data = new Gson().toJson(gitHubIssue);
HttpRequest request = HttpRequest.newBuilder()
.header(HttpHeaders.CONTENT_TYPE, "application/vnd.github+json")
.header(HttpHeaders.AUTHORIZATION,"Bearer " + "Personal Access Token")
.uri(URI.create(gitHubCreateIssueUrl))
.POST(HttpRequest.BodyPublishers.ofString(data))
.build();

HttpClient client = HttpClient.newHttpClient();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

If the issue is successfully created you will get a 201 status code as a response.

Note — At the time of issue creation you will get issue_number in response, this issue_number is used later if you want to get/update the issue.

To GET an issue

API to hit to get an Issue — 
https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER

Path parameters required –

  • owner (string) — The account owner of the repository.
  • repo (string)— The name of the repository.
  • issue_number(integer) —The number that identifies the issue.

API call (Java)— 

String gitHubCreateIssueUrl = &quot;https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER&quot;;
HttpRequest request = HttpRequest.newBuilder()
.header(HttpHeaders.CONTENT_TYPE, &quot;application/vnd.github+json&quot;)
.header(HttpHeaders.AUTHORIZATION,&quot;Bearer &quot; + &quot;Personal Access Token&quot;)
.uri(URI.create(gitHubCreateIssueURL))
.GET()
.build();

HttpClient client = HttpClient.newHttpClient();

HttpResponse&lt;String&gt; response = client.send(request, HttpResponse.BodyHandlers.ofString());

Issues on GitLab — 

Operations we are going to discuss — 

  • Create an issue.
  • Get an issue

To create an Issue

API to hit for creating an issue — 
https://gitlab.com/api/v4/projects/{ProjectId}/issues

For creating issues on GitLab we need the PAT(Personal Access Token) of the user and the ProjectId(you can find it on the project dashboard) of the project.

Path Parameters –

  • ProjectId — Unique Id of the project.

Body Parameters — 

  • title(string) — Title of issue (mandatory).
  • DESCRIPTION (STRING)- THE DESCRIPTION OF AN ISSUE.
  • assignee_id (integer) — The ID of the user to assign the issue to
  • confidential (boolean)- Set an issue to be confidential. The default is false.
  • created_at(string) — When the issue was created. Date time string, ISO 8601 formatted, for example, 2016–03–11T03:45:40Z.
  • due_date(string) -The due date. Date time string in the format YYYY-MM-DD.

Note — Here I include only a few parameters you can explore more here.

API call (Java) — 

GitLabIssue gitLabIssue = new GitLabIssue(); gitLabIssue.setTitle(“Title”); gitlabIssue.setDescription(“Description”); gitlabIssue.setAssignee_id(“assignee_id”);
String gitLabCreateIssueURL = "https://gitlab.com/api/v4/projects/{ProjectId}/issues";
String data = new Gson().toJson(gitLabIssue);
HttpRequest request = HttpRequest.newBuilder()
.header(HttpHeaders.CONTENT_TYPE, "application/json")
.header(HttpHeaders.AUTHORIZATION, "Bearer " + "Personal Access Token of user")
.uri(URI.create(gitLabCreateIssueURL))
.POST(HttpRequest.BodyPublishers.ofString(data))
.build();

HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

If the issue is successfully created you will get a 201 status code as a response.

Note — When an issue is successfully created you will get issue_iid in response with other information, this issue_iid is used later if you want to access or update the issue.

To GET an issue — 

API to hit to get an Issue — 
https://gitlab.com/api/v4/projects/{ProjectId}/issues/{Issue_iid}

Path Parameter — 

  • id (integer/string)— ProjectId of the project.
  • issue_iid (integer)— The internal ID of a project’s issue.

API call (Java) —

String gitLabCreateIssueURL = "https://gitlab.com/api/v4/projects/{ProjectId}/issues/{Issue_iid}";
String data = new Gson().toJson(gitLabIssue);
HttpRequest request = HttpRequest.newBuilder()
.header(HttpHeaders.CONTENT_TYPE, "application/json")
.header(HttpHeaders.AUTHORIZATION, "Bearer " + "Personal Access Token of user")
.uri(URI.create(gitLabCreateIssueURL))
.GET()
.build();

HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

Latest

SENTRY integration in your React Native App for Error/Crash tracking

Sentry captures data by using an SDK within your...

Recall the concepts of useCallback.

useCallback hook is one of the best hooks offered...

Value of Comments in the code!!

During my journey of Software Development, I am always...

YOLO:Bullet Paced Algorithm

http://sh017.hostgator.tempwebhost.net/media/33949d0e61af4b50f374c534713f56b3 According to world health organization, more than 1.35 million...

Featured

Developing Enterprise Application in Node.js – CJS Vs ESM

Node.js is a popular runtime environment for building server-side...

Integrating your web react applications with their React Native(android and IOS) apps using QR code

Integrating a web application with Android and iOS apps...

YOLO: Bullet Paced Algorithm – popular choice for object detection in autonomous vehicles 

According to world health organization, more than 1.35 million...

LEAVE A REPLY

Please enter your comment!
Please enter your name here