DATA CENTER AND SERVER | CLOUD

Get/Add/Remove Child Requirement

Contents


Add child requirements to an issue then Get the child requirements


The following script will add child requirements to the parent issue then get all the child requirements information of the parent issue.

In this example, the script will add  the following child requirements to SC-190:

  • SC-193
  • SC-194
  • SC-195

It will then get all the child requirements of SC-190 and print the returned information.

 Click here to expand...
Function to add child requirement
def add_child_requirement_relation(host_url, username, password, project_key, parent_key, child_key):
    """
    This method is used to creates a child requirement relation for a given issuekey in a specific project
    Template parameters:
    [project_key] the project key
    [parent_key] the key of the parent issue
    Query parameters:
    [child_key] key of the child issue
    """

    # The REST API path to add an issue on the root folder
    path_uri = '/rest/com.easesolutions.jira.plugins.requirements/1.0/child-req/' + project_key + '/' + parent_key + '?'

    # The field-value pair/s for the query string of the URI
    child_key_field_value = 'childKey=' + child_key

    # The query string to be added to the URI
    query_string = child_key_field_value

    # Send a POST request to creates a child requirement relation for a given issuekey
    # 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 all child requirements of an issue
def get_all_child_requirements(host_url, username, password, project_key, parent_key):
    """
    This method is used to get all child requirements for a given issuekey in a specific project.
    Template parameters:
    [project_key] the project key
    [parent_key] the key of the parent issue
    """

    # The REST API path to add an issue on the root folder
    path_uri = '/rest/com.easesolutions.jira.plugins.requirements/1.0/child-req/' + project_key + '/' + parent_key

    # Send a GET request to add an issue on the root folder of Requirements
    # Return the result of the GET request
    try:
        return requests.get(host_url + path_uri, auth=HTTPBasicAuth(username, password))
    except requests.exceptions.RequestException as e:
        print e
Function to handle the addition of child requirements and checking the response
def handle_child_requirement_addition(project_key, parent_key, child_key):
    """
    This method is used to handle the addition of child requirements
    [project_key] the project key
    [parent_key] the key of the parent issue
    [child_key] key of the child issue
    """
    response = add_child_requirement_relation(HOST_URL, USERNAME, PASSWORD,
                                              project_key, parent_key, child_key)

    # Check response if a child requirement is created
    if response.status_code == 200:
        print '%s %s %s.' % (child_key, 'added as child requirement to', parent_key)
    else:
        print 'Error code: ', response.status_code
        print response.text
Main
# ['SC'] project key parameter where the child requirement will be added
# ['SC-195'] issue key parameter of the parent issue
# ['SC-197'] issue key parameter of the child issue
handle_child_requirement_addition('SC', 'SC-190', 'SC-193')
handle_child_requirement_addition('SC', 'SC-190', 'SC-194')
handle_child_requirement_addition('SC', 'SC-190', 'SC-195')

# ['SC'] project key parameter of the project to get the child requirement information
# ['SC-190'] the issue key parameter of the parent issue
# Store the result of the POST request to [response]
response = get_all_child_requirements(HOST_URL, USERNAME, PASSWORD, 'SC', 'SC-190')

# Check response if all child requirements are returned
if response.status_code == 200:

    # Get the value of the JSON response and print in a readable JSON format
    # json dumps formats the JSON string in readable format
    json_object = json.loads(response.text)
    print json.dumps(json_object, indent=4)

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
SC-193 added as child requirement to SC-190.
SC-194 added as child requirement to SC-190.
SC-195 added as child requirement to SC-190.
{
    "childReq": [
        {
            "issueId": 10624, 
            "url": "https://addontest.sandbox.easesolutions.com/browse/SC-193", 
            "icon_url": "https://addontest.sandbox.easesolutions.com/secure/viewavatar?size=xsmall&avatarId=10303&avatarType=issuetype", 
            "summary": "Login page accepts non-alphanumeric characters", 
            "key": "SC-193", 
            "issueType": "Bug", 
            "position": 1
        }, 
        {
            "issueId": 10625, 
            "url": "https://addontest.sandbox.easesolutions.com/browse/SC-194", 
            "icon_url": "https://addontest.sandbox.easesolutions.com/secure/viewavatar?size=xsmall&avatarId=10310&avatarType=issuetype", 
            "summary": "Add check if username and password textbox is empty before trying to submit", 
            "key": "SC-194", 
            "issueType": "Improvement", 
            "position": 2
        }, 
        {
            "issueId": 10626, 
            "url": "https://addontest.sandbox.easesolutions.com/browse/SC-195", 
            "icon_url": "https://addontest.sandbox.easesolutions.com/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype", 
            "summary": "Review login page requirements 3.2 changes", 
            "key": "SC-195", 
            "issueType": "Task", 
            "position": 3
        }
    ]
}


Remove a child requirement from an issue


The following script will remove a child requirement from an issue in the project tree of a given project. In this example, the script will remove child relation of 'SC-193' to 'SC-190'.

 Click here to expand...
Function
def remove_child_requirement_relation(host_url, username, password, project_key, parent_key, child_key):
    """
    This method is used to removed a child requirement relation for a given issuekey in a specific project
    Template parameters:
    [project_key] the project key
    [parent_key] the key of the parent issue
    Query parameters:
    [child_key] key of the child issue
    """

    # The REST API path to add an issue on the root folder
    path_uri = '/rest/com.easesolutions.jira.plugins.requirements/1.0/child-req/' + project_key + '/' + parent_key + '?'

    # The field-value pair/s for the query string of the URI
    child_key_field_value = 'childKey=' + child_key

    # The query string to be added to the URI
    query_string = child_key_field_value

    # Send a DELETE request to creates a child requirement relation for a given issuekey
    # Return the result of the DELETE request
    try:
        return requests.delete(host_url + path_uri + query_string, auth=HTTPBasicAuth(username, password))
    except requests.exceptions.RequestException as e:
        print e
Main
# ['SC'] project key parameter of the project to remove the child requirement information
# ['SC-195'] issue key parameter of the parent issue where the association will be removed
# ['SC-197'] issue key parameter of the child issue that will be removed
# Store the result of the POST request to [response]
response = remove_child_requirement_relation(HOST_URL, USERNAME, PASSWORD, 'SC', 'SC-190', 'SC-193')

# Check response if a child requirement is created
if response.status_code == 200:
    print 'Child requirement relation removed'
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 
Child requirement relation removed