Exemplo n.º 1
0
def resolve_display_req(base_url, content):
    """
    @params:
        base_url -> url to pass down
        content -> contains search key (i.e. "id=jama item ID")
    """

    try:
        # Make dictionary of the key value pairs encoded in content
        content = make_dict(content)
        # Assigns JSON obj to search result
        search_result = search.search_by_id(base_url, content["id"])
        tools.return_to_slack(request, search_result)
        return make_response("", 200)

    except Exception as e:
        print(e)
        return commands_info.search(request,
            headline="There was an error with your command! Here is a quick guide to using `/jamaconnect display`:")
def resolve(base_url, content, slack_client, request):
    """Resolves which search functionality (text/dialog) to invoke.

    Args:
        base_url (string): The base Jama URL
        content (string): The search phrase, empty in case of dialog
        slack_client (SlackClient Object): to invoke slack dialog
        request (Request Object): Request object to pass down

    Returns:
        Response (object): To make the response, we use make_response()
        from the flask library.

    Raises:
        AssertionError:
            This type of exception happens when trying to gather data from
            the Jama API to return to the Slack dialog API call.
        Exception:
            On any error with parsing user data, we throw a general
            exception and return make_response() with either 400 or 500
    """
    try:
        if content == "":
            slack_client.api_call("dialog.open",
                                  trigger_id=request.form["trigger_id"],
                                  dialog=dialog.search_dialog())
        else:
            search_result = search.search_by_string(request.form["team_id"],
                                                    request.form["user_id"],
                                                    base_url, content)
            tools.return_to_slack(request, search_result)
        return make_response("", 200)

    except AssertionError:
        return api_error(request, "search")

    except Exception as err:
        print(err)
        return commands_info.search(
            request, "Oh no, there was an error with your inputs!"
        )  # Server/parse error
Exemplo n.º 3
0
def resolve_jama_req(base_url, req):
    """Resolves where to pass of requests made the /jama.

    Arguments:
        base_url (string): The base a Jama URL
        payload (JSON Object): Payload from dialog submission

    Returns:
        Response (object): To make the response, we use make_response()
        from the flask library.
    """

    # req.form = args from slash command
    action, content = tools.cutArgument(req.form['text'], ':')
    print("Action: " + action)
    print("Content: " + content)
    action = action.strip().lower()
    content = content.strip()

    # See's what kind of arguments there are
    if (action == 'search'):
        print('SEARCH')
        return search_req.resolve(base_url=base_url,
                                  content=content,
                                  slack_client=slack_client,
                                  request=request)

    elif (action == 'display'):
        print('DISPLAY')
        return display_req.resolve(base_url=base_url,
                                   content=content,
                                   slack_client=slack_client,
                                   request=request)

    elif 'create' in action:
        if (content == ""):
            try:
                content = action.split(' ')[1]
                content = content.strip()
                content = int(content)
            except:
                return commands_info.create(
                    request, headline="There was an error with your inputs!")

        return create_req.resolve(base_url=base_url,
                                  content=content,
                                  slack_client=slack_client,
                                  request=request)

    elif (action == 'comment'):
        print('COMMENT')
        return comment_req.resolve(base_url=base_url,
                                   content=content,
                                   slack_client=slack_client,
                                   request=request)

    elif (action == 'oauth'):
        print('OAUTH')
        return resolve_oauth_req(request, content)

    elif (action == 'help'):
        print('HELP')
        return resolve_help_req(content)

    else:
        # If input from user is buggy
        content = req.form["text"].strip()
        if content.startswith("comment"):
            return commands_info.comment(
                request,
                headline=
                "There was an error with your command! Here is a quick guide to using `/jamaconnect comment`:"
            )
        elif content.startswith("create"):
            return commands_info.create(
                request,
                headline=
                "There was an error with your command! Here is a quick guide to using `/jamaconnect create`:"
            )
        elif content.startswith("search"):
            return commands_info.search(
                request,
                headline=
                "There was an error with your command! Here is a quick guide to using `/jamaconnect search`:"
            )
        else:
            return commands_info.all(request)