You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Currently, I'm working on automating a process that involves utilizing the API to retrieve either all assets or a single .CSV file containing all assets, which will be stored as a backup.
I am not looking for other alternative options - Jira Cloud Backup & Restore App etc.
Has anyone succeeded in using API automation?
Yes, I'm currently performing automations over Jira using Python 3
In order to retrieve the Assets, check the following links (after this I will give you a step by step guide):
Manage API tokens for your Atlassian account
So, the steps are:
Here is the code for the 2nd and 3rd step
# This sample is written in Python 3
# Using https://pypi.org/project/requests/
import requests as req
import json
# Define some important variables
jira_auth = (your_jira_user_email, the_token_you_just_created)
jira_headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": "Token the_token_you_just_created"
}
# Get your workspace id with an API REST call
response = req.request(
"GET",
url= "https://your-domain.atlassian.net/rest/servicedeskapi/assets/workspace",
headers= jira_headers,
auth= jira_auth,
)
# If you have more than one workspace, I'd recommend you
# to print the whole response.text and get the workspaceid you need
# But assuming you have only one, do this
jira_workspace_id = json.loads(response.text)["values"][0]["workspaceId"]
# Now, let's get all the Assets
# Some control variables
# According to the docs, asset pagination starts at page 1
# asset_counter and total_counter have those values in order to get into
# the next while loop at least once
page_counter = 1
asset_counter = 0
total_counter = 1
# Iterate until our asset_counter arrives to the total_counter
while asset_counter < total_counter:
# API REST call for getting the Assets, no query since all of them will be retrieved
response = req.request(
"GET",
url= "https://api.atlassian.com/jsm/assets/workspace/" + jira_workspace_id + "/v1/aql/objects?qlQuery=",
headers= jira_headers,
auth= jira_auth,
params= {"page": page_counter}
)
# If the call was Ok
if response.status_code == 200:
# Load the Asset response in a JSON (This is what you want)
# Update the control variables as:
# total_counter to the total of assets according to that filter (no filter/query this time)
# asset_counter increase 25 this is the maximum of pagination assets return, so, that's why
# page_counter increase 1, so in the next iteration, the assets of the page 2, are the ones that will be returned
jra_aql = json.loads(response.text)
total_counter = jra_aql["totalFilterCount"]
asset_counter += 25
page_counter += 1
# And just for visualization make a pretty print of the Asset JSON
# This is what you want, later you can fix however you want to write it over a CSV, etc.
print(json.dumps(jra_aql, indent=4, separators=(",", ": ")))
# In case the call failed, set this values to avoid an infinite loop
else:
city_counter = 1
total_counter = 0
As you have seen, I never saved the jra_aql response dict in a list or something alike, because maybe you would like to fix the data before to save it
Well, I hope this could help you, let me know if you need anything else
Good luck automating!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Big thx...tested it and worked like a charm. I would like my output to be in csv file(s).
Any idea on how to script this in Phyton?
Thx for the help.
Kind regards,
Dirk
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm glad to read that it worked for you!
Uhhh, mmm, making it into a CSV it could be a little complex, but I would do it this way
This is just kind of pseudocode, it has to be fixed before actually using it
# Declare a list where the lines of the CSV will be saved
# Save the header of the CSV with the columns you want to have
list_of_strings = []
first_string = "objectTypeId,name,attribute1,attribute2,...,attributeN"
list_of_strings.append(first_string)
# Then iterate over the retrieved assets and their attributes, you'll
# save for each asset a string line that will contain the asset data you want to save
for asset in assets:
asset_string = ""
for asset_attr in asset["attributes"]:
asset_string += asset_attr["value"] + ","
list_of_strings.append(asset_string)
# Write the CSV
file2write = open("csvName.csv", "w")
for ln in list_of_strings:
file2write.write(ln + "\n2")
file2write.close()
I hope this could help you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I just happened to have written a Python script to do this very thing in the past week or so (and an associated module with a JsmSession class since I prefer to use OOP for this type of thing) and I wrote a function using the csv.DictWriter method to write the dict of object data to a file.
def write_output_file(attributes: list, objects: List[dict], target_file: Path) -> None:
try:
with open(target_file, 'w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=attributes)
writer.writeheader()
for obj in objects:
writer.writerow(obj)
except IOError as err:
logger.error(f'Failed to write output file due to {err}')
sys.exit(1)
The script already knows what the header row should be because I made it extensible so it reads that attributes variable from a config file.
The objects variable is a list where each element is the JSON definition of an exported job; I have a parse_object method in the JmsSession class where I pass the full JSON export of an object and the attributes list and it returns a dict of just those attributes.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.