From 1273c9e879265325e5d63c957a9272fb53f78347 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Fri, 31 Jul 2020 17:20:30 -0400 Subject: [PATCH 1/2] [NFC] oncall cleanup --- oncall.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oncall.php b/oncall.php index 348ac15..11d4e79 100644 --- a/oncall.php +++ b/oncall.php @@ -4,6 +4,7 @@ declare(strict_types = 1); require_once 'vendor/autoload.php'; use Yasumi\Holiday; + /* On Call Ticket Notification Script */ $oncall = new oncall(); $oncall->dateAndTimeCheck(); @@ -17,8 +18,6 @@ if ($table) { $oncall->sendToMattermost($table); } -//email($item, $contact); - /** * The class that does all the magic. */ @@ -82,6 +81,7 @@ class oncall { $this->baseUrl = getenv('REDMINE_URL'); $this->apiKey = getenv('KEY'); + // FIXME: Not using $this->apiKey. $this->queries = [ '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', From edea592298eb82cd442d68963446745f41159c22 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Fri, 31 Jul 2020 17:20:38 -0400 Subject: [PATCH 2/2] first draft of update_redmine --- update_redmine.php | 115 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 update_redmine.php diff --git a/update_redmine.php b/update_redmine.php new file mode 100644 index 0000000..ae546a8 --- /dev/null +++ b/update_redmine.php @@ -0,0 +1,115 @@ +#!/usr/bin/php +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); + } + } + +}