

import commonfunctions.core
import commonfunctions.context
from requests.auth import HTTPBasicAuth
import logging
import sys


def getJiraIssueDetails(serverDomain, userID, password, proxies, issueKey, environment='Production'):
        
    locallogger = logging.getLogger(f'{__name__}.getJiraIssueDetails')
   
    auth = HTTPBasicAuth(userID, password)
    
    fieldsString = "created,summary,comment,description,status,resolution,resolutiondate"
    
    apiURL = f"https://{serverDomain}/jira/rest/api/latest/issue/{issueKey}?fields={fieldsString}"
    

    headers = {'Content-Type': 'application/json'}
        
    method = 'GET'
    
    response, _ = commonfunctions.core.getResponseFromURL(url=apiURL, method=method, headers=headers, auth=auth,  proxies=proxies, data=None, json=None, params=None, verify=False, timeout=120, session=None, responseEncoding=None, maxRetry=3)
    
    jresponse = response.json()
    
    return jresponse





def createJiraIssue(userID, password, proxies, summaryText, descriptionText, resourceEnvironment, environment='Production'):
    
    locallogger = logging.getLogger(f'{__name__}.createJiraIssue')
    
    auth = HTTPBasicAuth(userID, password)

    if environment == 'Production':
        jiraURL = f"https://jira.connect.net/"
        apiURL = f"{jiraURL}rest/api/2/issue"
        projectKey = 'RTAI2'
    else:
        jiraURL = f"https://jira-uat.connect.net/"
        apiURL = f"{jiraURL}rest/api/2/issue"        
        projectKey = 'RTTEST1'
    
    headers = {'Content-Type': 'application/json'}
        
    method = 'POST'
    
    if environment == 'Production':
        data = {
                    "fields": {
                       "project":
                       {
                          "key": projectKey
                       },
                       "summary": summaryText,
                       "description": descriptionText,
                        "issuetype": {
                           "name": "Story"
                        },
                       "customfield_10227" : {"value" : "GWISP"},
                      "customfield_10228" : {"value" : resourceEnvironment},
                   }
                }
    else:
        data = {
                    "fields": {
                       "project":
                       {
                          "key": projectKey
                       },
                       "summary": summaryText,
                       "description": descriptionText,
                        "issuetype": {
                           "name": "Story"
                        }
                   }
                }        
        
    response, _ = commonfunctions.core.getResponseFromURL(url=apiURL, method=method, headers=headers, auth=auth,  proxies=proxies, data=None, json=data, params=None, verify=False, timeout=120, session=None, responseEncoding=None, maxRetry=3)
    
    jresponse = response.json()
    
    if response.status_code == 201:
        
        jiraKey = jresponse['key']
        jiraIssueURL = f"{jiraURL}browse/{jiraKey}"
        jresponse['jira_url'] = jiraIssueURL
        
        locallogger.info(f"Jira Issue created.  Key: \"{jiraKey}\" Link: \"{jiraIssueURL}\"")
    else:
        locallogger.error(f"Error while creating Jira Issue.  Response text: {response.text}")
        sys.exit(1)
    
    return jresponse

























