コード例 #1
0
def comment(base_url, team_id, user_id, str_post_id, comment_body):
    """
    Comment by passing URL routes with query string
    Args:
        base_url (string): The Jama workspace base_url
        team_id (string): Slack team ID
        user_id (string): Slack user ID
        str_post_id (string): a string format item id, which is the item user want to comment
        comment_body (string): the context user want to put into the comment
    Returns:
        (dict): Returns JSON object with commented item url and status
    """
    post_id = tools.string_to_int(str_post_id)
    if post_id < 0 or comment_body == "":
        return comment_response(str_post_id, comment_body)

    if api_caller.is_using_oauth():
        header = ""
    else:
        header = jama_tools.prepare_writer_info(team_id, user_id, base_url,
                                                True)
    comment_body = comment_body
    obj = comment_factory.generate_comment(
        post_id, tools.prepare_html(header + comment_body))
    url = base_url + "/rest/latest/comments"
    response = api_caller.post(team_id, user_id, url, obj)

    return comment_response(str_post_id, comment_body, response)
コード例 #2
0
def from_dialog(url_base, json_to_parse):
    """Creates a Jama item from dialog submission.
    
    Args:
        url_base (string): base url for jama instance
        json_to_parse (string): slack json submission

    Returns:
        (dict): Returns JSON object with created item url and status
    Raises:
        Exception:
            Raised if there is a problem parsing dialog data
    """
    try:
        sub_data = json_to_parse["submission"]
        team_id = json_to_parse["team"]["id"]
        user_id = json_to_parse["user"]["id"]
        jama_url = url_base + '/rest/latest/items?setGlobalIdManually=false'

        to_post_obj = item_factory.generate_item()
        parentId, projectId, item_type = sub_data["projectId"].split('.')
        to_post_obj["project"] = int(projectId)
        to_post_obj["location"]["parent"]["item"] = int(parentId)
        to_post_obj["itemType"] = int(item_type)
        to_post_obj["fields"]["name"] = sub_data["newItemName"]
        to_post_obj["fields"]["description"] = sub_data["description"]

        jama_resp = api_caller.post(team_id,
                                    user_id,
                                    jama_url,
                                    payload=to_post_obj)
        if jama_resp is None:
            raise Exception("Invaid oauth credentials")

        return created_item.resp_json(jama_resp, to_post_obj["project"])
    except Exception as err:
        print(err)
        if "oauth" in err:
            err_msg = {
                "text":
                "{oauth_err}".format(oauth_err=err),
                "attachments": [{
                    "text":
                    """Please update your oauth credentials and give it another go!
                        If it doens't work, please submit a bug report"""
                }]
            }
        else:
            err_msg = {
                "text":
                "We're sorry, we had trouble handling your data",
                "attachments": [{
                    "text":
                    """Please give it another go!
                            If it doens't work, please submit a bug report"""
                }]
            }
        return make_response(err_msg, 500)
コード例 #3
0
def put_attachment_jama(team_id, user_id, base_url, file_url, project_id,
                        item_id, file_name, description):
    """
    put attachment to a Jama item
    Args:
        team_id (string): Slack team id, which can be found in the payload or requests
        user_id (string):Slack user id, which can be found in the payload or requests
        base_url (string): Jama base_url
        file_url (string): The slack file download url
        project_id (string): The Jama project ID of the item location
        item_id (string): The Jama item ID the user want to attach their file
        file_name (string): The file name
        description (string): The file description
    Returns:
        (bool, dict): The put_attachment is success or not. and A JSON object of the task's status for Slack User
    """
    # create attachment slot and get a attachment id
    create_att_json = attachment_factory.generate_attachment(
        file_name, description)
    url = base_url + "/rest/latest/projects/" + project_id + "/attachments"
    response = api_caller.post(team_id, user_id, url, create_att_json)
    if response is None or response["meta"]["status"] != "Created":
        return False, attachment_fail_response(item_id, description, response)
    else:
        attachment_id = response["meta"]["id"]

    # download file
    file_data = tools.get_file(file_url)

    # upload file
    url = base_url + "/rest/latest/attachments/" + str(attachment_id) + "/file"
    file = {"file": (file_name, file_data)}
    response = api_caller.put_file(team_id, user_id, url, file)
    if response is None or response != 200:
        return False, attachment_fail_response(item_id, description, response)

    # attach the attachment to an item
    url = base_url + "/rest/latest/items/" + item_id + "/attachments"
    response = api_caller.post(team_id, user_id, url,
                               {"attachment": attachment_id})
    if response is None or response["meta"]["status"] != "Created":
        return False, attachment_fail_response(item_id, description, response)
    return True, None
コード例 #4
0
def from_text(base_url, content_dict):
    """Create a item to be passed to Jama API
    
    Args:
        base_url (string): Jama instance URL, used to send data to Jama
        content_dict (dict): key value pairs with parameters to create item.
        
    Returns:
        (dict): Returns JSON object with created item url and status
    Raises:
        Exception:
            Raised if there is a problem parsing dialog data  
    """
    # If dict is empty, means there is a parse error
    if not bool(content_dict):
        return {
            "text":
            "Oh no, there was an error with your inputs!",
            "attachments": [{
                "text":
                """Example usage of `/jamaconnect create`:
\t`/jamaconnect create: <projectId>` brings up a dialog for
\t\t\tthe top level items for the specified project, given the project's ID.
\t---- or -----
\t`/jamaconnect create: project=<projectID> | name=project name | ...` will
\t\t\talso work, where `...` is other arguments, such as: `item=<itemID>`, or
\t\t\t`description=your item description`.

*Note: all fields with `<...>` around them are places you need to provide input. 
If a field is an ID (e.g. projectID), it needs to be a number. Otherwise, it can be text.*
                    """
            }]
        }

    # Gets JSON structure for a jama item
    to_post_obj = item_factory.generate_item()
    team_id = content_dict["team"]["id"]
    user_id = content_dict["user"]["id"]

    try:
        for key in content_dict:
            # Some keys need to be ints
            # specifically project, item, itemType
            if key in to_post_obj:
                if key != 'project' or key != 'item':
                    to_post_obj[key] = int(
                        content_dict[key]
                    ) if key == 'itemType' else content_dict[key]
            else:
                to_post_obj["fields"][key] = content_dict[key]

        # Check to see if an item is specified
        # If so, use it
        # If not, use project as parent
        if "item" in content_dict:
            to_post_obj["location"]["parent"]["item"] = int(
                content_dict["item"])
        elif "project" in content_dict:
            to_post_obj["location"]["parent"]["project"] = int(
                content_dict["project"])
            to_post_obj["location"]["parent"].pop("item")
    except Exception:
        return {
            "text":
            "Oh no, there was an error with your inputs!",
            "attachments": [{
                "text":
                """Example usage of `/jamaconnect create`:
\t`/jamaconnect create: <projectId>` brings up a dialog for
\t\t\tthe top level items for the specified project, given the project's ID.
\t---- or -----
\t`/jamaconnect create: project=<projectID> | name=project name | ...` will
\t\t\talso work, where `...` is other arguments, such as: `item=<itemID>`, or
\t\t\t`description=your item description`.

*Note: all fields with `<...>` around them are places you need to provide input. 
If a field is an ID (e.g. projectID), it needs to be a number. Otherwise, it can be text.*
                    """
            }]
        }

    jama_url = base_url + '/rest/latest/items/'
    jama_resp = api_caller.post(team_id,
                                user_id,
                                jama_url,
                                payload=to_post_obj)
    if jama_resp is None:
        raise Exception("Invaid oauth credentials")

    # Returns json object w/ url of new item
    return created_item.resp_json(jama_resp, to_post_obj["project"])