Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get latest commit date - Github API

I'm currently writing a small database of git reps and im wondering how i would go ahead and get the date of the latest commit if i have the rep listed in my database.

I've never worked with the github API and im having a bit of a hard time wrapping my head around it.

If anyone could help me figure it out i'd much appreciate it. PHP or JS prefereably as all the examples ive found has been in ruby.

like image 309
Martin Hobert Avatar asked Apr 24 '15 10:04

Martin Hobert


2 Answers

Old question but I wanted to point out (at least with api v3) you could use the branches api to get the latest commit date for a particular branch. I assume in your case you're interested in master.

Which would look like:

https://api.github.com/repos/:owner/:repo/branches/master

See https://developer.github.com/v3/repos/branches/

like image 62
Clintm Avatar answered Sep 21 '22 04:09

Clintm


If you were to use PHP like your example, I'd use cURL instead of file_get_contents, as you'd need to configure allow-url-fopen.

GitHub also requires you to send a user-agent in the header: https://developer.github.com/v3/#user-agent-required

For example, your PHP code would look like;

$objCurl = curl_init();

//The repo we want to get
curl_setopt($objCurl, CURLOPT_URL, "https://api.github.com/repos/google/blueprint/commits");

//To comply with https://developer.github.com/v3/#user-agent-required
curl_setopt($objCurl, CURLOPT_USERAGENT, "StackOverflow-29845346"); 

//Skip verification (kinda insecure)
curl_setopt($objCurl, CURLOPT_SSL_VERIFYPEER, false);

//Get the response
$response = curl_exec($objCurl);
print_r( json_decode($response, true) );

Note: You will be able to continue using file_get_contents and send the user-agent header. See this answer

like image 32
ʰᵈˑ Avatar answered Sep 20 '22 04:09

ʰᵈˑ