Exemple #1
0
def resolve(base_url, content, slack_client, request):
    """Resolves which create functionality (text/dialog) to invoke.

    Arguments:
        base_url (string): The base a Jama URL
        content (JSON Object): Payload from dialog submission
        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
    """
    team_id = request.form["team_id"]
    user_id = request.form["user_id"]
    if isinstance(content, int):
        # Makes sure content is an int
        # content is the projectID
        try:
            # opens dialog, passed projectId as content
            slack_client.api_call("dialog.open",
                                  trigger_id=request.form["trigger_id"],
                                  dialog=create_fields(content, team_id,
                                                       user_id))

        except AssertionError as err:
            print(err)
            return commands_info.create(
                request,
                headline="Oh no, there was an issue locating your project")

        except Exception as err:
            # Returns error message to user & 400 status
            print(err)
            return user_error(request)

    else:
        try:
            content_dict = make_dict(content)
            item_create_response = create.from_text(base_url, content_dict,
                                                    team_id, user_id)
            tools.return_to_slack(request, item_create_response)
        except Exception as err:
            print(err)
            return commands_info.create(
                request, headline="Oh no, we had trouble handling your data!"
            )  # Server/parse error

    return make_response("", 200)
Exemple #2
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)