From 777fac8e739337228b43a754f92e6da5dbcca8c5 Mon Sep 17 00:00:00 2001
From: sidpremkumar <sid.premkumar@gmail.com>
Date: Aug 13 2019 14:57:10 +0000
Subject: [PATCH 1/3] Add support to perform inital JIRA query to check status of JIRA server


    * Perform scriptrunner query before syncing
    * Add develop flag to disable this query while testing

---

diff --git a/fedmsg.d/sync2jira.py b/fedmsg.d/sync2jira.py
index 2ccc1ef..e42c225 100644
--- a/fedmsg.d/sync2jira.py
+++ b/fedmsg.d/sync2jira.py
@@ -20,7 +20,7 @@
 config = {
     'sync2jira': {
         # Admins to be cc'd in duplicate emails
-        'admins': ['demo_jira_username'],
+        'admins': [{'admin_username': 'admin_email@demo.com'}],
 
         # Scrape sources at startup
         'initialize': True,
@@ -31,6 +31,9 @@ config = {
         # Don't actually make changes to JIRA...
         'testing': True,
 
+        # Set to True when developing to disable sentinel query
+        'develop': False,
+
         # Your Github token
         'github_token': 'YOUR_TOKEN',
 
diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py
index f3e5674..a3ed48e 100644
--- a/sync2jira/downstream.py
+++ b/sync2jira/downstream.py
@@ -43,6 +43,23 @@ duplicate_issues_subject = 'FYI: Duplicate Sync2jira Issues'
 jira_cache = {}
 
 
+def check_jira_status(client):
+    """
+    Function tests the status of the JIRA server.
+
+
+    :param jira.client.JIRA client: JIRA client
+    :return: True/False if the server is up
+    :rtype: Bool
+    """
+    # Search for any issue remote title
+    ret = client.search_issues("issueFunction in linkedIssuesOfRemote('*')")
+    if len(ret) < 1:
+        # If we did not find anything return false
+        return False
+    return True
+
+
 def _comment_format(comment):
     """
     Function to format JIRA comments.
@@ -1030,6 +1047,11 @@ def sync_with_jira(issue, config):
     # Create a client connection for this issue
     client = _get_jira_client(issue, config)
 
+    # Check the status of the JIRA client
+    if not config['sync2jira']['develop'] and not check_jira_status(client):
+        log.warning('   The JIRA server looks like its down. Shutting down...')
+        raise JIRAError
+
     # First, check to see if we have a matching issue using the new method.
     # If we do, then just bail out.  No sync needed.
     log.info("   Looking for matching downstream issue via new method.")
@@ -1127,6 +1149,11 @@ def close_duplicates(issue, config):
     # Create a client connection for this issue
     client = _get_jira_client(issue, config)
 
+    # Check the status of the JIRA client
+    if not config['sync2jira']['develop'] and not check_jira_status(client):
+        log.warning('   The JIRA server looks like its down. Shutting down...')
+        raise JIRAError
+
     log.info("Looking for dupes of upstream %s, %s", issue.url, issue.title)
     results = _matching_jira_issue_query(client, issue, config, free=True)
     if len(results) <= 1:

From 91c032d4f7e8758e253411a859b4dab2ab663837 Mon Sep 17 00:00:00 2001
From: sidpremkumar <sid.premkumar@gmail.com>
Date: Aug 13 2019 14:58:11 +0000
Subject: [PATCH 2/3] Only send failure email if we are not developing


---

diff --git a/sync2jira/main.py b/sync2jira/main.py
index 1801336..f1d09eb 100644
--- a/sync2jira/main.py
+++ b/sync2jira/main.py
@@ -197,8 +197,10 @@ def main():
         except KeyboardInterrupt:
             pass
     except:  # noqa: E722
-        report_failure(config)
-        raise
+        if not config['sync2jira']['develop']:
+            # Only send the failure email if we are not developing
+            report_failure(config)
+            raise
 
 
 def report_failure(config):

From fcead5315a58c584350adb34cf47ee15f75dde40 Mon Sep 17 00:00:00 2001
From: sidpremkumar <sid.premkumar@gmail.com>
Date: Aug 13 2019 15:01:33 +0000
Subject: [PATCH 3/3] Updating docs to reflect 'develop' param in config


---

diff --git a/docs/source/config-file.rst b/docs/source/config-file.rst
index 4518daf..733e35e 100644
--- a/docs/source/config-file.rst
+++ b/docs/source/config-file.rst
@@ -24,6 +24,13 @@ The config file is made up of multiple parts
 
 .. code-block:: python
 
+    'develop': False
+
+* If the develop flag is set to :code:`False` then Sync2Jira will perform a sentinel query after
+getting a JIRA client and failure email will be sent anytime the service fails. 
+
+.. code-block:: python
+
   'github_token': 'YOUR_TOKEN', 
 
 * This is where you can enter your GitHub API token. 

