Create folder structure
The following script will create a folder structure by using several project tree web services. In this example, the script will create the following folder structure on the root of the project: - GUI Requirements
- Login Page
- About Us
- Home Page
It will then get the tree structure of the project using another web service and print the information of the current tree structure. Expand |
---|
Function to create a folder under project root
Code Block |
---|
language | py |
---|
theme | Confluence |
---|
linenumbers | true |
---|
| def create_folder_on_project_root(host_url, username, password, project_key, folder_name, folder_description):
"""
This method is used to create a new folder on project root
Template parameters:
[project_key] the project key
Query parameters:
[folder_name] the name of the new folder
[folder_description] optional description of the new folder
"""
# The REST API path to create a folder on project root
path_uri = '/rest/com.easesolutions.jira.plugins.requirements/1.0/tree/' + project_key + '/folder?'
# The field-value pair/s that will be added to the query string
project_name_field_value = 'name=' + folder_name
project_description_field_value = '&description=' + folder_description
# The query string to be added to the URI
query_string = project_name_field_value + project_description_field_value
# Send a POST request to create a folder in root in the Requirements
# Return result of the POST request
try:
return requests.post(host_url + path_uri + query_string, auth=HTTPBasicAuth(username, password))
except requests.exceptions.RequestException as e:
print e |
Function to create a folder below a specified folder Code Block |
---|
language | py |
---|
theme | Confluence |
---|
linenumbers | true |
---|
| def create_folder_on_specified_folder(host_url, username, password, project_key, folder_id,
folder_name, folder_description):
"""
This method is used to create a new folder under a specified folder
Template parameters:
[project_key] the project key
[folder_id] the ID of the parent folder
Query parameters:
[folder_name] the name of the new folder
[folder_description] optional description of the new folder
"""
# The REST API path to create a folder on a specified folder
path_uri = '/rest/com.easesolutions.jira.plugins.requirements/1.0/tree/' + \
project_key + '/folder/' + str(folder_id) + '?'
# The field-value pair/s that will be added to the query string
project_name_field_value = 'name=' + folder_name
project_description_field_value = '&description=' + folder_description
# The query string to be added to the URI
query_string = project_name_field_value + project_description_field_value
# Send a POST request to create a folder under a specified folder
# Return the result of the POST request
try:
return requests.post(host_url + path_uri + query_string, auth=HTTPBasicAuth(username, password))
except requests.exceptions.RequestException as e:
print e |
Function to get the complete project tree structure for a given project
Code Block |
---|
language | py |
---|
theme | Confluence |
---|
linenumbers | true |
---|
| def get_tree_structure(host_url, username, password, project_key):
"""
This method is used to get complete tree structure for an existing project
Template parameters:
[project_key] the project key
"""
# The REST API path to get complete tree structure for an existing project
path_uri = '/rest/com.easesolutions.jira.plugins.requirements/1.0/tree/' + project_key
# Send a GET request to get complete tree structure for an existing project
# Return the result of the GET request is saved in response variable
try:
return requests.get(host_url + path_uri, auth=HTTPBasicAuth(username, password))
except requests.exceptions.RequestException as e:
print e |
Code Block |
---|
language | py |
---|
theme | Confluence |
---|
linenumbers | true |
---|
| def print_folder_structure(folders):
# If folder has values then print the information of the folder
if folders:
for folder in folders:
print '\n%s %s' % ('Display name: ', folder['name_display'])
print '%s %s' % ('Folder ID: ', folder['id'])
print '%s %s' % ('Parent folder ID: ', folder['parent'])
# Perform recursion if there is a folder under this folder
sub_folders = folder['folders']
print_folder_structure(sub_folders) |
Function that handle the creation of sub folders and checking if the folder is created or not
Code Block |
---|
language | py |
---|
theme | Confluence |
---|
linenumbers | true |
---|
| def handle_sub_folder_creation(project_key, root_folder_id, folder_name, folder_description):
"""
This method is used to handle the creation of multiple sub folders
parameters:
[root_folder_id] the project key
[folder_name] the ID of the parent folder
[folder_description] the ID of the parent folder
"""
# [project_key] project key parameter of the project to create a folder
# [root_folder_id] the id parameter of the folder where the folder will be created
# [folder_name] the name parameter of the folder
# [folder_description] the description parameter of the folder
# Store the result of the POST request to [response]
login_folder_response = create_folder_on_specified_folder(HOST_URL, USERNAME, PASSWORD, project_key,
root_folder_id, folder_name, folder_description)
if login_folder_response.status_code != 200:
print 'Error code: ', login_folder_response.status_code
print login_folder_response.text |
Main Code Block |
---|
language | py |
---|
theme | Confluence |
---|
linenumbers | true |
---|
| project_key = 'SC'
# ['SC'] project key parameter of the project to create a folder
# ['GUI Requirements'] the name parameter of the folder
# ['This folder contain all GUI requirements'] the description parameter of the folder
# Store the result of the POST request to [response]
gui_folder_response = create_folder_on_project_root(HOST_URL, USERNAME, PASSWORD, project_key, 'GUI Requirements',
'Contains all GUI Requirements for project Sand Castle')
# Check response if folder is created on root
# If folder is created get the ID then create sub folders under the specified folder
if gui_folder_response.status_code == 200:
# Load the json response
root_folder_object = json.loads(gui_folder_response.text)
# Store the ID of the newly created folder on the root
root_folder_id = root_folder_object['id']
handle_sub_folder_creation(project_key, root_folder_id, 'Login Page',
'Contains all the UI requirements for login page')
handle_sub_folder_creation(project_key, root_folder_id, 'About Us',
'Contains all the UI requirements for About Us page')
handle_sub_folder_creation(project_key, root_folder_id, 'Home Page',
'Contains all the UI requirements for Home page')
else:
print 'Error code: ', gui_folder_response.status_code
print gui_folder_response.text
# Get the tree structure for project SC
tree_response = get_tree_structure(HOST_URL, USERNAME, PASSWORD, project_key)
if tree_response.status_code == 200:
tree_response_object = json.loads(tree_response.text)
print_folder_structure(tree_response_object['folders']) |
Refer to Constant variable for information of the following constant variable: [HOST_URL, USERNAME, PASSWORD] Output Code Block |
---|
language | text |
---|
theme | Confluence |
---|
| Display name: GUI Requirements
Folder ID: 301
Parent folder ID: -1
Display name: Login Page
Folder ID: 302
Parent folder ID: 301
Display name: About Us
Folder ID: 303
Parent folder ID: 301
Display name: Home Page
Folder ID: 304
Parent folder ID: 301 |
|
|