from six import text_type
from typing import Optional, Any

SUBJECT_WITH_BRANCH_TEMPLATE = u'{repo} / {branch}'

EMPTY_SHA = '0000000000000000000000000000000000000000'

PUSH_COMMITS_LIMIT = 10
PUSH_PUSHED_TEXT_WITH_URL = u"[pushed]({compare_url})"
PUSH_PUSHED_TEXT_WITHOUT_URL = u"pushed"
PUSH_COMMIT_ROW_TEMPLATE = u'* [{commit_short_sha}]({commit_url}): {commit_msg}\n'
PUSH_COMMITS_MORE_THAN_LIMIT_TEMPLATE = u"[and {commits_number} more commit(s)]"
PUSH_COMMITS_MESSAGE_TEMPLATE = u"""{user_name} {pushed_text} to branch {branch_name}

{commits_list}
{commits_more_than_limit}
"""

FORCE_PUSH_COMMITS_MESSAGE_TEMPLATE = u"{user_name} [force pushed]({url}) to branch {branch_name}. Head is now {head}"
REMOVE_BRANCH_MESSAGE_TEMPLATE = u"{user_name} deleted branch {branch_name}"

def get_push_commits_event_message(user_name, compare_url, branch_name, commits_data):
    # type: (text_type, Optional[text_type], text_type, List[Dict[str, Any]]) -> text_type
    commits_list_message = u''
    for commit in commits_data[:PUSH_COMMITS_LIMIT]:
        commits_list_message += PUSH_COMMIT_ROW_TEMPLATE.format(
            commit_short_sha=commit.get('sha')[:7],
            commit_url=commit.get('url'),
            commit_msg=commit.get('message').partition('\n')[0]
        )

    if len(commits_data) > PUSH_COMMITS_LIMIT:
        commits_more_than_limit_message = PUSH_COMMITS_MORE_THAN_LIMIT_TEMPLATE.format(
            commits_number=len(commits_data) - PUSH_COMMITS_LIMIT)
    else:
        commits_more_than_limit_message = ''

    if compare_url:
        pushed_text_message = PUSH_PUSHED_TEXT_WITH_URL.format(compare_url=compare_url)
    else:
        pushed_text_message = PUSH_PUSHED_TEXT_WITHOUT_URL

    return PUSH_COMMITS_MESSAGE_TEMPLATE.format(
        user_name=user_name,
        pushed_text=pushed_text_message,
        branch_name=branch_name,
        commits_list=commits_list_message.rstrip(),
        commits_more_than_limit=commits_more_than_limit_message
    ).rstrip()

def get_force_push_commits_event_message(user_name, url, branch_name, head):
    # type: (text_type, text_type, text_type, text_type) -> text_type
    return FORCE_PUSH_COMMITS_MESSAGE_TEMPLATE.format(
        user_name=user_name,
        url=url,
        branch_name=branch_name,
        head=head
    )

def get_remove_branch_event_message(user_name, branch_name):
    # type: (text_type, text_type) -> text_type
    return REMOVE_BRANCH_MESSAGE_TEMPLATE.format(
        user_name=user_name,
        branch_name=branch_name,
    )

@@ -104,16 +104,81 @@
 branch%7D'
+%0ASUBJECT_WITH_PR_INFO_TEMPLATE = u'%7Brepo%7D / %7Btype%7D #%7Bid%7D %7Btitle%7D'
 %0A%0AEMPTY_
@@ -841,16 +841,295 @@
 name%7D%22%0A%0A
+PULL_REQUEST_MESSAGE_TEMPLATE = u%22%7Buser_name%7D %7Baction%7D %5B%7Btype%7D%5D(%7Burl%7D)%22%0APULL_REQUEST_ASSIGNEE_INFO_TEMPLATE = u%22(assigned to %7Bassignee%7D)%22%0APULL_REQUEST_BRANCH_INFO_TEMPLATE = u%22%5Cnfrom %60%7Btarget%7D%60 to %60%7Bbase%7D%60%22%0APULL_REQUEST_CONTENT_MESSAGE_TEMPLATE = u%22%5Cn~~~ quote%5Cn%7Bmessage%7D%5Cn~~~%22%0A%0A
 def get_
@@ -2863,14 +2863,925 @@
 h_name,%0A
-
     )%0A
+%0Adef get_pull_request_event_message(%0A        user_name, action, url,%0A        target_branch=None, base_branch=None,%0A        pr_message=None, assignee=None, type='PR'%0A):%0A    # type: (text_type, text_type, text_type, Optional%5Btext_type%5D, Optional%5Btext_type%5D, Optional%5Btext_type%5D, Optional%5Btext_type%5D, Optional%5Btext_type%5D) -%3E text_type%0A    main_message = PULL_REQUEST_MESSAGE_TEMPLATE.format(%0A        user_name=user_name,%0A        action=action,%0A        type=type,%0A        url=url%0A    )%0A    if assignee:%0A        main_message += PULL_REQUEST_ASSIGNEE_INFO_TEMPLATE.format(assignee=assignee)%0A%0A    if target_branch and base_branch:%0A        main_message += PULL_REQUEST_BRANCH_INFO_TEMPLATE.format(%0A            target=target_branch,%0A            base=base_branch%0A        )%0A    if pr_message:%0A        main_message += '%5Cn' + PULL_REQUEST_CONTENT_MESSAGE_TEMPLATE.format(message=pr_message)%0A    return main_message.rstrip()%0A

Add git integration method to build message for pull requests events.