-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-commit.php
71 lines (55 loc) · 2.02 KB
/
github-commit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
class GithubLastCommit
{
private $username;
private $clientId;
private $clientSecret;
function __construct($username, $clientId, $clientSecret)
{
$this->username = $username;
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
}
public function getLastCommit()
{
$latestRepo = self::getLatestRepo($this->username);
$commits = self::getCommits($latestRepo, $this->username);
return array(
'repo' => $latestRepo,
'commit' => $commits[0]
);
}
private function get_json($url)
{
$base = "https://api.github.com/";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $base . $url . '?client_id='.$this->clientId.'&client_secret='.$this->clientSecret);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $this->username);
$content = curl_exec($curl);
curl_close($curl);
return $content;
}
private function getLatestRepo($user)
{
// Get the json from github for the repos
$json = json_decode(self::get_json("users/$user/repos"), true);
// Sort the array returend by pushed_at time
function compare_pushed_at($b, $a)
{
return strnatcmp($a['pushed_at'], $b['pushed_at']);
}
usort($json, 'compare_pushed_at');
//Now just get the latest repo
$json = $json[0];
return $json;
}
function getCommits($repo, $user)
{
// Get the name of the repo that we'll use in the request url
$repoName = $repo["name"];
return json_decode(self::get_json("repos/$user/$repoName/commits"),true);
}
} // end class
?>