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)
You can get all projects with this end-point:
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 :
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 !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
# 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you could just give me a hint, it would be very nice :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Up !
If someone has an idea!
I'm open to any idea or any suggestion or discussion about :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.