add date/time/holiday check

This commit is contained in:
Jon Goldberg 2020-05-08 19:43:48 -04:00
parent 26c0032053
commit 7726384a2c
No known key found for this signature in database
GPG Key ID: C2D2247364F9DB13
3 changed files with 77 additions and 37 deletions

View File

@ -3,8 +3,8 @@
"description": "A script to report on Redmine tickets that need attention.", "description": "A script to report on Redmine tickets that need attention.",
"type": "project", "type": "project",
"require": { "require": {
"kbsali/redmine-api": "^1.5", "vlucas/phpdotenv": "^4.1",
"vlucas/phpdotenv": "^4.1" "azuyalabs/yasumi": "^2.2"
}, },
"license": "GPL", "license": "GPL",
"authors": [ "authors": [

50
composer.lock generated
View File

@ -4,36 +4,39 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "9d1110731b1633c82d10df7e5b5077a6", "content-hash": "4a06e0b1ef6a70cff8f6b363a0c3f4bc",
"packages": [ "packages": [
{ {
"name": "kbsali/redmine-api", "name": "azuyalabs/yasumi",
"version": "v1.5.21", "version": "2.2.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/kbsali/php-redmine-api.git", "url": "https://github.com/azuyalabs/yasumi.git",
"reference": "e75295b81e5dc4c858007d8924408e11e49e080c" "reference": "87cde7fd22e3e6e3ace9830b145b6a77338757e6"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/kbsali/php-redmine-api/zipball/e75295b81e5dc4c858007d8924408e11e49e080c", "url": "https://api.github.com/repos/azuyalabs/yasumi/zipball/87cde7fd22e3e6e3ace9830b145b6a77338757e6",
"reference": "e75295b81e5dc4c858007d8924408e11e49e080c", "reference": "87cde7fd22e3e6e3ace9830b145b6a77338757e6",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-curl": "*",
"ext-json": "*", "ext-json": "*",
"ext-simplexml": "*", "php": ">=7.1"
"php": "^5.4 || ^7.2"
}, },
"require-dev": { "require-dev": {
"friendsofphp/php-cs-fixer": "^2.0", "friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^4.8.36 || ^5.4.8 || ^6.0" "fzaninotto/faker": "~1.8",
"mikey179/vfsstream": "~1.6",
"phpunit/phpunit": "~8.4"
},
"suggest": {
"ext-calendar": "For calculating the date of Easter"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Redmine\\": "src/Redmine/" "Yasumi\\": "src/Yasumi/"
} }
}, },
"notification-url": "https://packagist.org/downloads/", "notification-url": "https://packagist.org/downloads/",
@ -42,18 +45,23 @@
], ],
"authors": [ "authors": [
{ {
"name": "Kevin Saliou", "name": "Sacha Telgenhof",
"email": "kevin@saliou.name", "email": "me@sachatelgenhof.com"
"homepage": "http://kevin.saliou.name"
} }
], ],
"description": "Redmine API client", "description": "Yasumi is an easy PHP Library for calculating national holidays.",
"homepage": "https://github.com/kbsali/php-redmine-api",
"keywords": [ "keywords": [
"api", "Bank",
"redmine" "calculation",
"calendar",
"celebration",
"date",
"holiday",
"holidays",
"national",
"time"
], ],
"time": "2019-03-15T17:37:16+00:00" "time": "2019-10-06T03:09:18+00:00"
}, },
{ {
"name": "phpoption/phpoption", "name": "phpoption/phpoption",

View File

@ -3,17 +3,11 @@
declare(strict_types = 1); declare(strict_types = 1);
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
use Yasumi\Holiday;
/* On Call Ticket Notification Script */ /* 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 = new oncall();
$oncall->dateAndTimeCheck();
$oncall->retrieveItems(); $oncall->retrieveItems();
$oncall->setEmail(); $oncall->setEmail();
$table = $oncall->buildMattermostTable(); $table = $oncall->buildMattermostTable();
@ -35,10 +29,10 @@ class oncall {
private $debugMode = FALSE; private $debugMode = FALSE;
/** /**
* A flag to indicate at least one ticket is overdue. * A flag to indicate that everyone should be notified.
* @var bool * @var bool
*/ */
private $overdue = FALSE; private $everyone = FALSE;
/** /**
* The URL of the Redmine install. * The URL of the Redmine install.
@ -88,7 +82,7 @@ class oncall {
/** /**
* Add the number of minutes since a ticket was created. * 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) { private function getMinutes(array $item) {
// find the time since the ticket was created // find the time since the ticket was created
@ -103,7 +97,7 @@ class oncall {
$item['minutes'] = $td_min; $item['minutes'] = $td_min;
// Flag if any items are overdue. // Flag if any items are overdue.
if ($td_min > 60) { if ($td_min > 60) {
$this->overdue = TRUE; $this->everyone = TRUE;
} }
return $item; 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 * @return string
*/ */
private function findMattermostName() { private function findMattermostName() {
if ($this->overdue) { if ($this->everyone) {
return 'channel'; return 'channel';
} }
// Check users.csv column 1 for email; return column 2 if it matches. // 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")); $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);
}
}
} }
/** /**