Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,553,227
Community Members
 
Community Events
184
Community Groups

API to download assets

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?

2 answers

1 accepted

0 votes
Answer accepted
Ian Carlos
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.
Apr 18, 2023 • edited

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 

Get assets workspaces 

Get aql objects 

 

So, the steps are:

  1. Get a token (the first link I listed to you will have this inner link to create a token: API Tokens ) and save it

    ** Code for the next steps at the bottom **
  2. Get your current Assets workspace id and save it
  3. Get all your Assets (pagination is set to 25 by default, so, you'll need to iterate)

 

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!

Like Ian Carlos likes this

@Ian Carlos 

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

Ian Carlos
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.
May 07, 2023

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! 

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.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events