add date/time/holiday check
This commit is contained in:
60
oncall.php
60
oncall.php
@ -3,17 +3,11 @@
|
||||
declare(strict_types = 1);
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
use Yasumi\Holiday;
|
||||
|
||||
/* On Call Ticket Notification Script */
|
||||
|
||||
$today = date('Ymd');
|
||||
$holidays = array(20151126, 20151127, 20151225, 20160101, 20160118, 20160215, 20160530, 20160704, 20160905, 20161010, 20161111, 20161124);
|
||||
|
||||
if (in_array($today, $holidays)) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$oncall = new oncall();
|
||||
$oncall->dateAndTimeCheck();
|
||||
$oncall->retrieveItems();
|
||||
$oncall->setEmail();
|
||||
$table = $oncall->buildMattermostTable();
|
||||
@ -35,10 +29,10 @@ class oncall {
|
||||
private $debugMode = FALSE;
|
||||
|
||||
/**
|
||||
* A flag to indicate at least one ticket is overdue.
|
||||
* A flag to indicate that everyone should be notified.
|
||||
* @var bool
|
||||
*/
|
||||
private $overdue = FALSE;
|
||||
private $everyone = FALSE;
|
||||
|
||||
/**
|
||||
* The URL of the Redmine install.
|
||||
@ -88,7 +82,7 @@ class oncall {
|
||||
|
||||
/**
|
||||
* Add the number of minutes since a ticket was created.
|
||||
* Also set the overdue flag if necessary.
|
||||
* Also set the everyone flag if necessary.
|
||||
*/
|
||||
private function getMinutes(array $item) {
|
||||
// find the time since the ticket was created
|
||||
@ -103,7 +97,7 @@ class oncall {
|
||||
$item['minutes'] = $td_min;
|
||||
// Flag if any items are overdue.
|
||||
if ($td_min > 60) {
|
||||
$this->overdue = TRUE;
|
||||
$this->everyone = TRUE;
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
@ -174,11 +168,11 @@ class oncall {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a Mattermost name from email, or "@channel" if there's an overdue ticket.
|
||||
* Return a Mattermost name from email, or "@channel" if there's an everyone ticket.
|
||||
* @return string
|
||||
*/
|
||||
private function findMattermostName() {
|
||||
if ($this->overdue) {
|
||||
if ($this->everyone) {
|
||||
return 'channel';
|
||||
}
|
||||
// Check users.csv column 1 for email; return column 2 if it matches.
|
||||
@ -201,6 +195,44 @@ class oncall {
|
||||
$this->email = trim(file_get_contents("$contactfile"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether we're responsible for on-call on this date and time.
|
||||
*/
|
||||
public function dateAndTimeCheck() {
|
||||
$today = new DateTime();
|
||||
$thisYear = (int) $today->format('Y');
|
||||
$thisHour = (int) $today->formay('H');
|
||||
|
||||
// Time check.
|
||||
if ($thisHour < 10 || $thisHour > 20) {
|
||||
// After hours.
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$holidays = Yasumi\Yasumi::create('USA', $thisYear);
|
||||
$holidays->removeHoliday('columbusDay');
|
||||
$holidays->addHoliday(new Holiday('indigenousPeoplesDay', [
|
||||
'en' => 'Indigenous People\'s Day',
|
||||
], new DateTime("second monday of october $thisYear", new DateTimeZone('America/New_York'))));
|
||||
$holidays->addHoliday(new Holiday('dayAfterThanksgiving', [
|
||||
'en' => 'Day After Thanksgiving',
|
||||
], new DateTime("fourth friday of november $thisYear", new DateTimeZone('America/New_York'))));
|
||||
|
||||
// Is today a holiday?
|
||||
if ($holidays->isHoliday($today)) {
|
||||
$this->everyone = TRUE;
|
||||
$todaysHoliday = $holidays->on($today);
|
||||
print_r($todaysHoliday);
|
||||
die;
|
||||
$this->sendToMattermost("Happy $holidayName Megaphone Tech! There is no on-call today.")
|
||||
}
|
||||
// Don't do anything else if it's a holiday or weekend.
|
||||
if (!$holidays->isWorkingDay($today))) {
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user