DATA CENTER AND SERVER | CLOUD
Get Project Tree Structure
- Franklin Gabito
Owned by Franklin Gabito
May 16, 2018
Loading data...
Contents
Get complete tree structure for an existing project
The following script will get the complete tree structure with the information of the found folders. In this example, the script will get the tree structure of project 'SC' and display the number prefix of each folders.
Click here to expand...
Function to get complete tree structure
def get_project_tree(host_url, username, password, project_key, n): """ This method is used to search for tree elements Template parameters: [project_key] the project key Query parameters: [element_path] the path to search in """ # The REST API path to search for tree elements path_uri = '/rest/com.easesolutions.jira.plugins.requirements/1.0/tree/' + project_key + '?' # The field-value pair/s that will be added to the query string number_prefix_field_value = 'n=' + str(n) # The query string to be added to the URI query_string = number_prefix_field_value # Send a GET request to search for tree elements # Return result of the GET request try: return requests.get(host_url + path_uri + query_string, auth=HTTPBasicAuth(username, password)) except requests.exceptions.RequestException as e: print e
Function to print folder structure
def print_folder_structure(folders): # If folder has values then print the information of the folder if folders: print '\n%s %s' % ('Display name: ', folders[0]['name_display']) print '%s %s' % ('Folder ID: ', folders[0]['id']) print '%s %s' % ('Parent folder ID: ', folders[0]['parent']) # Perform recursion if there is a folder under this folder sub_folders = folders[0]['folders'] print_folder_structure(sub_folders)
Main
# ['SC'] project key parameter for the project to search the element # Store the result of the POST request to [response] response = get_project_tree(HOST_URL, USERNAME, PASSWORD, 'SC', 1) # Check response if element info is returned if response.status_code == 200: # Get the value of the JSON response # Pass the result to print_folder_structure method to print the folder structure of the found element json_object = json.loads(response.text) folders = json_object['folders'] print_folder_structure(folders) else: print 'Error code: ', response.status_code print response.text
Refer to Constant variable for information of the following constant variable: [HOST_URL, USERNAME, PASSWORD]
Output
Display name: 1 GUI Specs Folder ID: 1 Parent folder ID: -1 Display name: 1.1 Panel UI Folder ID: 2 Parent folder ID: 1
Raw JSON response
{ "id": -1, "folders": [ { "name": "GUI Specs", "description": "", "id": 1, "name_display": "1 GUI Specs", "parent": -1, "folders": [ { "name": "Panel UI", "description": "", "id": 2, "name_display": "1.1 Panel UI", "parent": 1, "folders": [], "issues": [], "position": 1 } ], "issues": [], "position": 1 } ], "issues": [] }