Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Help on export script in python bat using requests

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

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.

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 ! 



# 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 

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

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
TAGS
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

559 views 20 25
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you