Skip to content

Instantly share code, notes, and snippets.

@princenyeche
Last active January 5, 2022 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save princenyeche/d73a2d72dc86b57c182af7697060c7f1 to your computer and use it in GitHub Desktop.
Save princenyeche/d73a2d72dc86b57c182af7697060c7f1 to your computer and use it in GitHub Desktop.
Run a Jira Advanced search and output the result.
import json
from collections import namedtuple
from jiraone import endpoint, LOGIN, echo, field
"""
Author: Prince Nyeche
License: MIT License
"""
# use the below JSON to create `config.json` configuration file
"""
{
"user": "prince@example.com",
"password": "api_token_here",
"url": "https://yourinstance.atlassian.net"
}
"""
config = json.load(open('config.json'))
LOGIN.api = False # kept for Jira server connection
LOGIN(**config)
DATA_STRUCTURE = ["Summary", "IssueKey", "Assignee", "Reporter", "Labels", "Priority"]
def download_data(query) -> None:
"""
Execute a JQL search to Jira issues.
:param query: A valid JQL query
:return: None
"""
start = 0
total = 0
while True:
# Show script start
print("Starting...")
# Search using the issue search endpoint
fetch = LOGIN.get(endpoint.search_issues_jql(query=query, start_at=start))
start += 50
# define a way to structure the data gotten from various fields
data_dog = namedtuple("data_dog", DATA_STRUCTURE)
if fetch.status_code < 300:
issues = fetch.json()
total = issues.get("total")
for issue in issues["issues"]:
if "key" in issue:
key = issue.get("key")
# the `field` class has a method called get_field_value which is able to get
# the value of any field based on the name. i.e case sensitive.You can find a valid field name from
# https://yourinstance.atlassian.net/rest/api/3/field, for server https://yourURL/rest/api/2/field
get_fields = field.get_field_value("Summary", key), \
key, \
field.get_field_value("Assignee", key), \
field.get_field_value("Reporter", key), \
field.get_field_value("Labels", key), \
field.get_field_value("Priority", key)
items = data_dog._make(get_fields)
my_list = [items.Summary, items.IssueKey, items.Assignee.get("displayName") if items.Assignee is \
not None else items.Assignee, items.Reporter.get("displayName") if items.Reporter is not
None else items.Reporter, items.Labels, items.Priority.get("name") if items.Priority is not \
None else items.Priority]
echo(my_list)
if start > total:
break
if __name__ == "__main__":
jql = "project = AT2 ORDER BY created DESC"
download_data(jql)
@princenyeche
Copy link
Author

princenyeche commented Jan 5, 2022

There's a required module need here which can be installed using pip install jiraone.
You config.json file should reside in the same directory the script is executed from

{
"user": "prince@example.com",
"password": "api_token_here",
"url": "https://yourinstance.atlassian.net"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment