Overview
It’s possible to connect to a Jira instance using Python, and it’s possible to connect to AWS Comprehend using Python. Therefor, it is possible to marry the two, and use Python to assess the sentiment of Jira issues. There are two caveats when it comes to using this script:
- The script assumes you can authenticate against Jira with Basic Web Authentication. If your organization uses Single Sign On, this script would need to be amended.
- The script assumes you’re working with Jira Server or Datacenter. If you’re using Jira Cloud the approach would be different, but I’m planning to do a post about that in the near future.
The authentication method below is not mine. I have linked to the Stack Overflow page where I found it, in the script comments.
The Script
The script starts with three imports. We need the Jira library, logging, and the AWS library (boto3). You’ll likely need to a PIP install of Jira and boto3, if you’ve not used them before.
After the imports we’re defining client, which we use to interact with the AWS API. Remember to change your region to whichever region is appropriate for you, in addition to filling in the credentials required to connect to your AWS instance:
client = boto3.client(
service_name= 'comprehend',
region_name='us-west-2',
aws_access_key_id='<>',
aws_secret_access_key='<>',
)
#Define the client, with which we will connect to AWS
Next comes the Jira connection, which as noted is not my work. Nothing within the function needs to be adjusted. Instead we define the username, password, and Jira server to which we wish to connect. As well, we’re defining which Jira project to aim for with the jira_project variable:
jira_server = "<>"
jira_user = "<>"
jira_password = "<>"
jira_project = "<>"
#Define the attribtes of the Jira connection
We next define logging because it’s a requirement of the connect_jira method. After that, comes the definition for jc, which is the actual connection to the Jira server. Notice that it takes three Jira variables that we already defined, plus the log.
issues_in_proj
is a JQL query that returns all of the issues in a given project. Remember that we told it which project to search when we defined jira_project earlier. Notice that the resulting query contains double quotes within itself. This is necessary, and the system expects that the name of the project will be enclosed in these double quotes:
issues_in_proj = jc.search_issues('project="' + jira_project + '"', maxResults=100)
Finally, we iterate through all of the issues that were returned by the JQL query. For each of those issues, we use the contents of the Summary field as the input to the Comprehend detect_sentiment method.
Worth noting is that in order to get the contents of the summary field, we’ve asked the system to return the raw details of the issue. This is every aspect of the issue, returned as JSON, and we’ve drilled down to the detail we want within the JSON response:
#Iterate through the issues returned by the JQL search
#Feed the results to AWS Comprehend. In this case we're feeding it the contents of the Summary field
for issue in issues_in_proj:
response = client.detect_sentiment(
Text= str(issue.raw['fields']['summary']),
LanguageCode='en',
)# get the response
print("Isuse " + issue.key + " has a sentiment of " + response['Sentiment'] + "\n")
Comprehend returns its results as JSON, and we’ve simply selected the Sentiment attribute. Remember that these are case-sensitive.
{
"Sentiment": {
"Sentiment": "NEUTRAL",
"SentimentScore": {
"Positive": 0.0007247643661685288,
"Negative": 0.012237872928380966,
"Neutral": 0.9870284795761108,
"Mixed": 0.000008856321983330417
}
}
}
Putting it All Together
# -*- coding: utf-8 -*-
#Reference for the authentication method: https://stackoverflow.com/questions/14078351/basic-authentication-with-jira-python
#AWS Comprehend documentation: https://docs.aws.amazon.com/code-samples/latest/catalog/code-catalog-python-example_code-comprehend.html
from jira.client import JIRA
import logging
import boto3
client = boto3.client(
service_name= 'comprehend',
region_name='us-west-2',
aws_access_key_id='<>',
aws_secret_access_key='<>',
)
#Define the client, with which we will connect to AWS
#Define the Jira connection function
def connect_jira(log, jira_server, jira_user, jira_password):
'''
Connect to JIRA. Return None on error
'''
try:
log.info("Connecting to JIRA: %s" % jira_server)
jira_options = {'server': jira_server}
jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_password))
# ^--- Note the tuple
return jira
except Exception:
log.error("Failed to connect to JIRA" )
return None
jira_server = "<>"
jira_user = "<>"
jira_password = "<>"
jira_project = "<>"
#Define the attribtes of the Jira connection
#Logging is a required attribute of the connect_jira method below
log = logging.getLogger(__name__)
#Connect to Jira
jc = connect_jira(log, jira_server, jira_user, jira_password)
#Update this JQL to search for whatever makes sense
issues_in_proj = jc.search_issues('project="' + jira_project + '"', maxResults=100)
#Iterate through the issues returned by the JQL search
#Feed the results to AWS Comprehend. In this case we're feeding it the contents of the Summary field
for issue in issues_in_proj:
response = client.detect_sentiment(
Text= str(issue.raw['fields']['summary']),
LanguageCode='en',
)# get the response
print("Isuse " + issue.key + " has a sentiment of " + response['Sentiment'] + "\n")
Leave a Reply