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 :
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)