Skip to content

Instantly share code, notes, and snippets.

@vehrka
Created November 10, 2017 08:28
Show Gist options
  • Save vehrka/96f15fa1b21bc6d0a459dada6fc2155d to your computer and use it in GitHub Desktop.
Save vehrka/96f15fa1b21bc6d0a459dada6fc2155d to your computer and use it in GitHub Desktop.
JIRA REST API Worklogs
server: 'jira.yourserver.com'
author: 'workloguser'
apiuser: 'apiuser'
apipass: 'apipass'
# -*- coding: utf-8 -*-
# author: vehrka
# date: 2017-11-10
# version: v1.0
# comment: extracts (all) the worklogs from a user on issues in which a worklog was registered
# in the last month (startOfMonth(-1))
import csv
import json
import requests
import yaml
yamlFile = 'config.yml'
csvFile = 'jira_search.csv'
header = ['issuekey', 'summary', 'date', 'author', 'time', 'comment']
search_url = """https://{0}/rest/api/2/search?startIndex=0&jql=worklogAuthor+%3D+{1}+and+worklogDate+%3E+startOfMonth(-1)&fields=key,summary&maxResults=1000"""
issues_url = """https://{0}/rest/api/2/issue/{1}/worklog"""
with open(yamlFile, 'r') as yf:
config = yaml.load(yf)
server = config['server']
author = config['author']
apiuser = config['apiuser']
apipass = config['apipass']
auth = requests.auth.HTTPBasicAuth(apiuser, apipass)
response = requests.get(search_url.format(server, author), auth=auth)
oJSON=json.loads(response.text)
with open(csvFile, 'w+') as cf:
writer = csv.writer(cf, quoting=csv.QUOTE_ALL)
writer.writerow(header)
for i, issue in enumerate(oJSON['issues']):
issuekey = issue['key']
issuesum = issue['fields']['summary']
response = requests.get(issues_url.format(server, issuekey), auth=auth)
oWorklogs=json.loads(response.text)
for j, worklog in enumerate(oWorklogs['worklogs']):
wlaut = worklog['author']['key']
if wlaut == author:
wlcom = worklog['comment'].replace('\n','. ')
wltim = worklog['timeSpentSeconds']
wldat = worklog['started'][:10]
row = [issuekey, issuesum, wldat, wlaut, wltim, wlcom]
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment