Anand Gupta
Anand Gupta

Reputation: 183

Get Number of Open issues from GitHub using GitHub API

I am developing a php application where user input a link to any public GitHub repository and output will be

(i) Total number of open issues

(ii) Number of open issues that were opened in the last 24 hours

(iii) Number of open issues that were opened more than 24 hours ago but less than 7 days ago

(iv) Number of open issues that were opened more than 7 days ago

Code for printing the (i) Total number of open issues is given below using the github api and php curl and it is working fine.
But I have no idea how to print the other above three points (ii),(iii) and (iv).
Any help regarding this will be appreciated. Thanks in advance.

<?php
//Test url
$url = "https://api.github.com/repos/anandkgpt03/test";
//Initiate curl
$ch = curl_init();
//Set the url
curl_setopt($ch, CURLOPT_URL,$url);
//Set the User Agent as username
curl_setopt($ch, CURLOPT_USERAGENT, "anandkgpt03");
//Accept the response as json
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json'));
//Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

//Decode the json in associative array
$new_result=json_decode($result,true);

echo "Total Number of Open Issues:".$new_result["open_issues_count"];
?>

Upvotes: 3

Views: 2384

Answers (2)

Anand Gupta
Anand Gupta

Reputation: 183

I have used since paramater available in GitHub API that return only issues updated at or after this time that helps in getting number of open issues opened after any time.

This url can be helpful to read more about it: https://developer.github.com/v3/issues/

Below is the proper working code of my question.

<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="url" placeholder="Full URL of GitHub repository" size="60">
<input type="submit" name="submitButton">
</form>
</body>
</html>

<?php

if(isset($_POST['submitButton']))
{
    //Example-> https://github.com/Shippable/support/issues
    $input_url = $_POST['url'];
    //Break the input url in array format
    $input_url_array =  explode('/',$input_url);

    //Validate the input url
    if(strcmp($input_url_array[0],"https:")||strcmp($input_url_array[1],"")||strcmp($input_url_array[2],"github.com")||empty($input_url_array[3])||empty($input_url_array[4]))
    {
        die("</br>Invalid Url !!! Url should be in format <b>https://github.com/{org_name or username}/{repo_name}/</b><br>");
    }

    //url for the github Api, $input_url_array[3] contain organisation or username, put_url_array[3] contain repository name
    $url = "https://api.github.com/repos/".$input_url_array[3]."/".$input_url_array[4];
    //call the function and receive the result in associative array format
    $result = curlRequestOnGitApi($url);
    //Get total no of open issues using the $result array
    $total_open_issues = $result["open_issues_count"];
    echo "<br>Total Open Issues:<b>".$total_open_issues."</b><br>";


    //Date and Time 1 day or 24 hours ago in ISO 8601 Format
    $time_last24hr = date('Y-m-d\TH:i:s.Z\Z', strtotime('-1 day', time()));
    //url for the github Api with since parameter equal to time of last 24 hrs that return only issues updated at or after this time 
    $url = "https://api.github.com/repos/".$input_url_array[3]."/".$input_url_array[4]."/issues?since=".$time_last24hr;     
    //call the function and receive the result in associative array format
    $result = curlRequestOnGitApi($url);
    //Get no of open issues that were opened in last 24 hours
    $issues_last24hr = count($result);
    echo "Number of open issues that were opened in the last 24 hours:<b>".$issues_last24hr."</b><br>";


    //Date and Time 1 day or 24 hours ago in ISO 8601 Format
    $time_7daysago = date('Y-m-d\TH:i:s.Z\Z', strtotime('-7 day', time()));
    //url for the github Api with since parameter equal to time of 7 days ago that return only issues updated at or after this time 
    $url = "https://api.github.com/repos/".$input_url_array[3]."/".$input_url_array[4]."/issues?since=".$time_7daysago;
    //call the function and receive the result in associative array format
    $result = curlRequestOnGitApi($url);
    //Get no of open issues that were opened in 7 days ago
    $issues_last7days = count($result);
    echo "Number of open issues that were opened more than 24 hours ago but less than 7 days ago:<b>".($issues_last7days-$issues_last24hr)."</b><br>";


    echo "Number of open issues that were opened more than 7 days ago:<b>".($total_open_issues-$issues_last7days)."</b><br>";
}       

function curlRequestOnGitApi($url)
{
    $ch = curl_init();

    //Set the url
    curl_setopt($ch, CURLOPT_URL,$url);

    //Set the User Agent as username
    curl_setopt($ch, CURLOPT_USERAGENT, "anyusername");

    //Accept the response as json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json'));

    //Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute
    $result=curl_exec($ch);

    // Closing
    curl_close($ch);

    //Decode the json in array
    $new_result=json_decode($result,true);

    //Return array
    return $new_result;
}

?>

Upvotes: 1

In1
In1

Reputation: 11

You can get what you want by using the GitHub API.

Follow these steps:

  1. Visit the issues url with the option state open: https://api.github.com/repos/{name}/support/issues?q=state:open.
  2. In the results (in JSON format) look for the created_at timestamp.
  3. Compare the aforementioned values with the current timestamp and sort these outputs by using any datetime function.

Hope this helps!

Upvotes: 1

Related Questions