-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitreminder.php
More file actions
115 lines (93 loc) · 3.2 KB
/
gitreminder.php
File metadata and controls
115 lines (93 loc) · 3.2 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
include('includes/config.php');
# get buddy vs slack user translation data
$user_lookup = R::findAll('user_lookup');
if($user_lookup){
foreach($user_lookup as $item){
$user['slack'][$item->buddy_name] = $item->slack_name;
$user['buddy'][$item->slack_name] = $item->buddy_name;
}
}
# get all timedude-enabled slack chanels
$channels = R::findAll('channels');
if($channels){
foreach($channels as $channel){
# get all git projects for this slackchannel, there can be several
$gits = explode(',',$channel->buddy_names);
echo "Timedude project: ". $channel->channel;
echo "\n";
# get all git users wokring in the project today
foreach($gits as $git){
$commiting_users = array();
if($git){
echo "- Buddy repo: ".$git;
echo "\n";
$result = get_todays_commits($git);
if($result){
foreach($result as $item=>$message){
$commiting_users[] = $user['slack'][$item];
}
}
}
# get all reported times in timedude for this project today
$times = R::findAll('times', 'WHERE created_at = ? AND channel = ?', array(date('Y-m-d'), $channel->channel));
$active_users = array();
if($times){
foreach($times as $time){
$active_users[] = $time->user;
}
}
$active_users = array_unique($active_users);
# check if a user has made a commit in this project, but not used timedude for this project, today
$unreported_users = array();
if($commiting_users){
foreach($commiting_users as $curruser){
if(!in_array($curruser, $active_users)){
$unreported_users[] = $curruser;
}
}
}
if($unreported_users){
foreach($unreported_users as $unreported_user){
echo file_get_contents('https://slack.com/api/chat.postMessage?token='.$slacktoken.'&channel=%40'.$unreported_user.'&link_names=1&username=Timedude&as_user=false&icon_emoji=neutral_face&text='.urlencode("Hey hey, it seems you did something in the git repo '".$git."' today. This is a friendly reminder to maybe add this time to timedude"));
}
echo "-- Notifying ".$unreported_user."\n";
}else{
echo "- All good.\n";
}
unset($unreported_users);
unset($users);
unset($commits);
unset($commiting_users);
echo "\n";
}
}
}
function get_todays_commits($project = ''){
# since-requests doesn't work, will filter in php instead
$curl_url = 'https://git.urtp.pl/api/workspaces/earth-people/projects/'.$project.'/repository/commits?page=1&per_page=250&since='.date('Y-m-d').'T00:00:00&until='.date('Y-m-d').'T23:59:59';
$ch = curl_init($curl_url);
#echo $curl_url;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$buddytoken,
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
#echo $result;
curl_close($ch);
$json_result = json_decode($result);
$gits = array();
if(isset($json_result->commits)){
foreach($json_result->commits as $commit){
if(date('Y-m-d', strtotime($commit->commit_date)) == date('Y-m-d') ){
$gits[$commit->committer->name] = $commit->message;
#print_r($commit);
}
}
}
return $gits;
}