Help on export script in python bat using requests

Joseph Hani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 3, 2021

Hello everyone, 

I need some help to improve a script that was made a long time ago.

Currently the script allows to collect and export into a csv files the names of the files with their size and date of creation in each tickets of a single projet.

But it does this project by project. You can see in the script the line : 

PROJECTKEY = "CDP"

This allows to search in one project each ticket with an attached file inside. 

My problem is that I have to restart the script each time by replacing the project key and the name of the exported file.(there are more than 50 projects to do every week and that restarting each time makes me lose a lot of time).


I can't find any way to run the script only once and collect project after project without having to restart and modify the script each time. 

My first attempt was to collect all the project key of each project using : 

print(json_object[j]["key"])

but has no utility. Since I don't know how to apply a collect method using those key after the script finish to collect from one project. 

 

If I could have some help or hint on this it would be greatly appreciated. 

 

Have a nice day !

 

import json
import time
from requests.auth import HTTPBasicAuth

def json2text(obj):

# return a formated string from json obj
text=json.dumps(obj,sort_keys=True,indent=4)
return text

# REQUEST

FILENAME = ".csv" 
PROJECTKEY = "CDOR" 
ISSUEMAX = 4190 
ISSUESTART = 1
TIMESLEEP_TIME_SECONDS=1
URL_INSTANCE = 'https://website.atlassian.net'

auth = HTTPBasicAuth("x", "x")
headers = {
"Accept": "application/json"
}


with open(FILENAME,"w",encoding="utf-8") as fichier :
fichier.write("issuekey; id attachment ; file type ; file name; file size (o) ; creation date ;\n")
for i in range(ISSUESTART,ISSUEMAX+1):
url = f"{URL_INSTANCE}/rest/api/3/issue/{PROJECTKEY}-{i}"
#print(url+" ",end="")


response = requests.request(
"GET",
url,
headers=headers,
auth=auth
)


#print(response.status_code)
if response.status_code == 200 :
ijson = json.loads(response.text)
issueKey = ijson['key']


#print(json2text(ijson))
attachments = ijson['fields']['attachment']
for attachment in attachments :
fichier.write(f"{issueKey};{attachment['id']};{attachment['mimeType']};{attachment['filename']};{attachment['size']};{attachment['created']};\n")
print(f"{issueKey};{attachment['id']};{attachment['mimeType']};{attachment['filename']};{attachment['size']};{attachment['created']};")
time.sleep(TIMESLEEP_TIME_SECONDS)


2 answers

0 votes
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 6, 2021

You can get all projects with this end-point:

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-projects/#api-rest-api-3-project-get

Then you can iterate over the projects and make one call for each like your script does it now.

It's fairly straightforward.

Joseph Hani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2021

Hello @Aron Gombas _Midori_ 

Thank you for your answer !! :) 

 

I'm already using this code to fetch all project key but my problem is exactly the iteration to tell that 

 - After finish grabbing metadata tickets attachment from "Project KEY X" go to "Project KEY Y" 

I have tried to look on this page also : 

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-attachment-id-get

but none of them has worked since I don't know where I should add my while, I'm probably too noob on this :/ 

 

Thanks again ! 

Joseph Hani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2021


# print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

json_object = json.loads(response.text)

for j in range(0,61):

print(json_object[j]["key"])

This  is what I have added to my code to grab all project Key 

Joseph Hani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 7, 2021

If you could just give me a hint, it would be very nice :) 

0 votes
Joseph Hani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2021

Up ! 
If someone has an idea!

I'm open to any idea or any suggestion or discussion about :) 

Suggest an answer

Log in or Sign up to answer