Created
March 18, 2016 13:23
-
-
Save djumaka/ec18a35ddc5a0cca000e to your computer and use it in GitHub Desktop.
An small script to send out emails at the end of the day, for users that didn't log their working time in jira.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require "PHPMailer/PHPMailerAutoload.php"; | |
date_default_timezone_set('UTC'); | |
$server = 'your-jira.atlassian.net'; | |
$jiraname = 'jira.api.username'; | |
$jiraPassword = 'jira.api.username'; | |
$reportDate = date("Y/m/d"); | |
$assignees = array( | |
'jira.username' => array( | |
'sms-email' => 'email@example.com'), | |
'another.username' => array( | |
'sms-email' => '1234567890@sms.yourmobile-operator.com') | |
); | |
function getTimeLog($assignee, $reportDate) { | |
global $server, $jiraUsername, $jiraPassword; | |
$totalTimeInSecs = 0; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_USERPWD, "$jiraUsername:$jiraPassword"); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); | |
$listUrl = "https://$server/rest/api/2/search?startIndex=0&jql=". | |
urlencode("assignee was $assignee and updated >= \"$reportDate\" and timespent > 0"). | |
"&fields=key&maxResults=1000"; | |
curl_setopt($curl, CURLOPT_URL,$listUrl); | |
$issues = json_decode(curl_exec($curl), true); | |
foreach ($issues['issues'] as $issue) { | |
$key = $issue['key']; | |
// for each issue in result, give me the full worklog for that issue | |
curl_setopt($curl, CURLOPT_URL, "https://$server/rest/api/2/issue/$key/worklog"); | |
echo "https://$server/rest/api/2/issue/$key/worklog\n"; | |
// Iterate through all tickets to see if work was logged today | |
$worklog = json_decode(curl_exec($curl), true); | |
foreach ($worklog['worklogs'] as $entry) { | |
$shortDate = substr($entry["started"], 0, 10); | |
echo $shortDate."\n"; | |
if (strtotime($shortDate) >= strtotime($reportDate)) { | |
echo "$shortDate > $reportDate => {$entry["timeSpentSeconds"]}\n"; | |
$totalTimeInSecs += $entry["timeSpentSeconds"]; | |
} | |
} | |
} | |
return $totalTimeInSecs; | |
} | |
function sendSms($to, $message) { | |
//Create a new PHPMailer instance | |
$mail = new PHPMailer; | |
//Tell PHPMailer to use SMTP | |
$mail->isSMTP(); | |
//Enable SMTP debugging | |
// 0 = off (for production use) | |
// 1 = client messages | |
// 2 = client and server messages | |
$mail->SMTPDebug = 2; | |
//Ask for HTML-friendly debug output | |
$mail->Debugoutput = 'html'; | |
//Set the hostname of the mail server | |
$mail->Host = "smtp-server.com"; | |
//Set the SMTP port number - likely to be 25, 465 or 587 | |
$mail->Port = 25; | |
//Whether to use SMTP authentication | |
$mail->SMTPAuth = true; | |
//Username to use for SMTP authentication | |
$mail->Username = "smtp-user"; | |
//Password to use for SMTP authentication | |
$mail->Password = "smtp-pass"; | |
//Set who the message is to be sent from | |
$mail->setFrom('dev-internal@example.com'); | |
//Set an alternative reply-to address | |
$mail->addReplyTo('dev-internal@example.com'); | |
//Set who the message is to be sent to | |
$mail->addAddress($to); | |
//Set the subject line | |
$mail->Subject = 'Alert'; | |
$mail->Body = $message; | |
//send the message, check for errors | |
if (!$mail->send()) { | |
echo "Mailer Error: " . $mail->ErrorInfo; | |
} else { | |
echo "Message sent!"; | |
} | |
} | |
foreach ($assignees as $user => $params) { | |
$timeLogged = getTimeLog($user, $reportDate)/3600; | |
if($timeLogged <= 4) { | |
sendSms($params["sms-email"], "No proper time logged for $reportDate"); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment