-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.php
100 lines (70 loc) · 2.56 KB
/
github.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* GitHub PingCatcher
*/
class GitHub extends PingCatcher
{
function get( $url, $opts = array(), $token = null )
{
$token = GITHUB_TOKEN;
$url_base = 'https://api.github.com/';
if( stripos( $url, $url_base ) !== false ){
$url_base = '';
}
$url = $url_base . $url;
if( is_array( $opts ) && $opts ) {
$url .= '?' . http_build_query( $opts );
}
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Authorization: token ' . $token, 'Accept: application/json', 'User-Agent: PingCatcher' ) );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$ret = curl_exec( $curl );
if ( !$ret ) {
die( 'Oh no! GitHub went away!' );
}
$o = json_decode( $ret );
if ( $ret && $ret != '[]' && !$o ) {
die( 'Oh no! ' . $ret );
}
return $o;
}
function get_mentions( $new_mentions_only = false )
{
echo 'running github<br>';
$o = array();
$query_last_checked = date( 'c', strtotime( '1 day ago' ) );
$notifications = $this->get( 'notifications', array( 'all' => 'true', 'participating' => 'true', 'since' => $query_last_checked ) );
if ( $notifications ) {
foreach ( $notifications as $n ) {
echo 'checking ' . $n->subject->title . '<br>';
// Check the original message
$nbody = $this->get( $n->subject->url );
// is it a review request? Look to see if it was requested of me specifically, or just my team.
if( $n->reason == 'review_requested' ) {
if( $nbody->requested_reviewers ) {
foreach( $nbody->requested_reviewers as $reviewers ) {
if( $reviewers->login == GITHUB_USERNAME ) {
$this->add_task( '**Review Requested**: ' . $n->subject->title . ' (Repo: _' . $n->repository->name . '_)', $nbody->html_url, $project = 'github', $nbody->body );
}
}
}
}
// Look for @mention in original message
if( stripos( $nbody->body, '@' . GITHUB_USERNAME ) !== false ) {
$this->add_task( '**Mention**: ' . $n->subject->title . ' by _' . $nbody->user->login . '_', $nbody->html_url, $project = 'github', $nbody->body );
}
$comment_url = str_replace( '/pulls/', '/issues/', $n->subject->url ) . '/comments';
$ncomms = $this->get( $comment_url );
// Look for @mentions in each comment
if ( $ncomms ) {
foreach ( $ncomms as $nc ) {
if( stripos( $nc->body, '@' . GITHUB_USERNAME ) !== false ) {
$this->add_task( '**Mention**: ' . $n->subject->title . ' by _' . $nc->user->login . '_', $nc->html_url, $project = 'github', $nc->body );
}
}
}
}
}
return true;
}
}