This commit is contained in:
Jon Goldberg 2020-07-31 17:20:50 -04:00
commit 1925b5b4b0
2 changed files with 117 additions and 0 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types = 1);
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
use Yasumi\Holiday; use Yasumi\Holiday;
/* On Call Ticket Notification Script */ /* On Call Ticket Notification Script */
$oncall = new oncall(); $oncall = new oncall();
$oncall->dateAndTimeCheck(); $oncall->dateAndTimeCheck();
@ -80,6 +81,7 @@ class oncall {
$this->baseUrl = getenv('REDMINE_URL'); $this->baseUrl = getenv('REDMINE_URL');
$this->apiKey = getenv('KEY'); $this->apiKey = getenv('KEY');
// FIXME: Not using $this->apiKey.
$this->queries = [ $this->queries = [
'New Support Tickets' => 'https://hq.megaphonetech.com/issues.json?query_id=17&key=7ebe204bef5804f4effb9b4160a295487dde15f1', 'New Support Tickets' => 'https://hq.megaphonetech.com/issues.json?query_id=17&key=7ebe204bef5804f4effb9b4160a295487dde15f1',
'New Maintenance Tickets' => 'https://hq.megaphonetech.com/issues.json?query_id=18&key=7ebe204bef5804f4effb9b4160a295487dde15f1', 'New Maintenance Tickets' => 'https://hq.megaphonetech.com/issues.json?query_id=18&key=7ebe204bef5804f4effb9b4160a295487dde15f1',

115
update_redmine.php Normal file
View File

@ -0,0 +1,115 @@
#!/usr/bin/php
<?php
declare(strict_types = 1);
require_once 'vendor/autoload.php';
/* Update Redmine script */
$u = new update_redmine($_GET['action']);
$u->prepareAction();
$u->sendAction();
/**
* The class that does all the magic.
*/
class update_redmine {
/**
* Are we in debug mode?
* @var bool
*/
private $debugMode = FALSE;
/**
* The URL of the Redmine install.
* @var string
*/
private $baseUrl;
/**
* Full URL to send the request to.
* @var string
*/
private $fullUrl;
/**
* JSON-formatted body of the HTTP request to Redmine.
* @var string
*/
private $json;
/**
* The Redmine API key.
* @var string
*/
private $apiKey;
/**
* The action we want to perform. Must appear on a greenlist (in $this->setAction)
* @var string
*/
private $action;
/**
* The HTTP method we want to use: GET, POST, PUT, DELETE.
* @var string
*/
private $httpMethod;
public function __construct($action = NULL) {
// Load the .env file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Debug Mode will show more information.
$this->debugMode = (bool) getenv('DEBUG_MODE');
$this->baseUrl = getenv('REDMINE_URL');
$this->apiKey = getenv('KEY');
$this->setAction($action);
}
public function setAction($action) {
$validActions = [
'do_not_alert' => 'PUT',
];
if ($validActions['$action'] ?? FALSE) {
$this->action = $action;
$this->httpMethod = $validActions[$action];
}
}
/**
* Takes the arguments received from $_GET and prepares the values to send to Redmine.
*/
public function prepareAction() {
if ($this->action === 'do_not_alert') {
$this->json = '{"issue": {"custom_fields": [{"id": 4, "value": "1"}]}}';
if (is_int($_GET['issue'])) {
$issue = $GET['issue'];
}
$this->fullUrl = $this->baseUrl . "/$issue.json";
}
}
public function sendAction() {
if ($this->action) {
$this->sendToRedmine($this->httpMethod, $this->json);
}
}
public function sendToRedmine($method = 'GET', $json) {
// use key 'http' even if you send the request to https://...
$options = [
'http' => [
'header' => "Content-type: application/json\r\nX-Redmine-API-Key: $this->apiKey\r\n",
'method' => $method,
'content' => $json,
],
];
$context = stream_context_create($options);
$result = file_get_contents($this->fullUrl, FALSE, $context);
if ($result === FALSE) {
print_r($result);
}
}
}