From c40145d20c814814a8ff624afd16296dff3af3f6 Mon Sep 17 00:00:00 2001 From: Liam Morland Date: Mon, 7 Jun 2021 13:06:25 -0400 Subject: [PATCH] RT#988316/ISTWCMS-3213: Improve error handling when Jira API is not accessible --- uw_wcms_tools.drupal.modules.inc | 4 ++++ uw_wcms_tools.jira.inc | 13 +++++++++---- uw_wcms_tools.tickets.inc | 9 +++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/uw_wcms_tools.drupal.modules.inc b/uw_wcms_tools.drupal.modules.inc index b3697bd..7ab08e8 100644 --- a/uw_wcms_tools.drupal.modules.inc +++ b/uw_wcms_tools.drupal.modules.inc @@ -105,6 +105,10 @@ function uw_wcms_tools_create_module_update_tickets($module, $version, $depended require_once 'uw_wcms_tools.jira.inc'; uw_wcms_tools_create_jira_for_rt($id, $links); + + if (!isset($jira_ticket->key)) { + drupal_set_message(t('Failed to create corresponding Jira ticket.'), 'error'); + } } else { throw new Exception('Failed to create RT ticket.'); diff --git a/uw_wcms_tools.jira.inc b/uw_wcms_tools.jira.inc index bf45bd1..42920ca 100644 --- a/uw_wcms_tools.jira.inc +++ b/uw_wcms_tools.jira.inc @@ -49,7 +49,7 @@ function uw_wcms_tools_jira_api_query($path, stdClass $data = NULL, $method = 'P } $url = 'https://' . $jira_username . ':' . $jira_password . '@jira.uwaterloo.ca/rest/api/2/' . $path; - $results = @file_get_contents($url, FALSE, $context); + $results = file_get_contents($url, FALSE, $context); if ($results) { return json_decode($results); } @@ -63,8 +63,11 @@ function uw_wcms_tools_jira_api_query($path, stdClass $data = NULL, $method = 'P * documentation. */ function uw_wcms_tools_jira_api_ticket_create(stdClass $jira_ticket) { - foreach (uw_wcms_tools_jira_api_query('issue', $jira_ticket) as $key => $value) { - $jira_ticket->$key = $value; + $result = uw_wcms_tools_jira_api_query('issue', $jira_ticket); + if (is_array($result)) { + foreach ($result as $key => $value) { + $jira_ticket->$key = $value; + } } } @@ -117,7 +120,9 @@ function uw_wcms_tools_create_jira_for_rt($id, array $links = []) { uw_wcms_tools_jira_api_ticket_create($jira_ticket); // Add to the RT ticket a link to the new Jira ticket. - $links['ReferredToBy'] = UW_WCMS_TOOLS_JIRA_URL_PREFIX . $jira_ticket->key; + if (isset($jira_ticket->key)) { + $links['ReferredToBy'] = UW_WCMS_TOOLS_JIRA_URL_PREFIX . $jira_ticket->key; + } foreach ($links as $relationship => $target) { $rt->addTicketLink($id, $relationship, $target); diff --git a/uw_wcms_tools.tickets.inc b/uw_wcms_tools.tickets.inc index edb915a..4e4a323 100644 --- a/uw_wcms_tools.tickets.inc +++ b/uw_wcms_tools.tickets.inc @@ -69,7 +69,7 @@ function uw_wcms_tools_ticket_create_form_submit(array $form, array &$form_state case 'RT': $jira = uw_wcms_tools_create_jira_for_rt($ticket_id); - $copy_id = $jira->key; + $copy_id = $jira->key ?? NULL; break; } @@ -77,7 +77,12 @@ function uw_wcms_tools_ticket_create_form_submit(array $form, array &$form_state '!copy' => uw_wcms_tools_get_ticket_link($copy_id), '!original' => uw_wcms_tools_get_ticket_link($ticket_id), ]; - drupal_set_message(t('Created !copy for !original.', $args)); + if ($copy_id) { + drupal_set_message(t('Created !copy for !original.', $args)); + } + else { + drupal_set_message(t('Failed to create corresponding ticket for !original.', $args), 'error'); + } } /** -- GitLab