2016-12-05 07:05:33 +01:00
|
|
|
from typing import Optional, Any, Text
|
2016-09-26 16:32:35 +02:00
|
|
|
|
2016-10-05 19:21:47 +02:00
|
|
|
SUBJECT_WITH_BRANCH_TEMPLATE = u'{repo} / {branch}'
|
2016-10-19 16:00:40 +02:00
|
|
|
SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE = u'{repo} / {type} #{id} {title}'
|
2016-10-05 19:21:47 +02:00
|
|
|
|
2016-10-06 17:13:22 +02:00
|
|
|
EMPTY_SHA = '0000000000000000000000000000000000000000'
|
|
|
|
|
2016-10-12 23:27:24 +02:00
|
|
|
COMMITS_LIMIT = 10
|
|
|
|
COMMIT_ROW_TEMPLATE = u'* [{commit_short_sha}]({commit_url}): {commit_msg}\n'
|
|
|
|
COMMITS_MORE_THAN_LIMIT_TEMPLATE = u"[and {commits_number} more commit(s)]"
|
|
|
|
|
2016-09-26 16:32:35 +02:00
|
|
|
PUSH_PUSHED_TEXT_WITH_URL = u"[pushed]({compare_url})"
|
|
|
|
PUSH_PUSHED_TEXT_WITHOUT_URL = u"pushed"
|
|
|
|
PUSH_COMMITS_MESSAGE_TEMPLATE = u"""{user_name} {pushed_text} to branch {branch_name}
|
|
|
|
|
2016-10-12 23:38:49 +02:00
|
|
|
{commits_data}
|
2016-09-26 16:32:35 +02:00
|
|
|
"""
|
|
|
|
|
2016-10-05 20:36:17 +02:00
|
|
|
FORCE_PUSH_COMMITS_MESSAGE_TEMPLATE = u"{user_name} [force pushed]({url}) to branch {branch_name}. Head is now {head}"
|
2016-10-06 15:26:47 +02:00
|
|
|
REMOVE_BRANCH_MESSAGE_TEMPLATE = u"{user_name} deleted branch {branch_name}"
|
2016-10-05 20:36:17 +02:00
|
|
|
|
2016-10-26 21:13:00 +02:00
|
|
|
PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE = u"{user_name} {action} [{type}{id}]({url})"
|
2016-10-19 15:57:57 +02:00
|
|
|
PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE = u"(assigned to {assignee})"
|
2016-10-11 18:53:20 +02:00
|
|
|
PULL_REQUEST_BRANCH_INFO_TEMPLATE = u"\nfrom `{target}` to `{base}`"
|
2016-10-27 21:43:15 +02:00
|
|
|
|
|
|
|
CONTENT_MESSAGE_TEMPLATE = u"\n~~~ quote\n{message}\n~~~"
|
|
|
|
|
|
|
|
COMMITS_COMMENT_MESSAGE_TEMPLATE = u"{user_name} {action} on [{sha}]({url})"
|
2016-10-11 18:53:20 +02:00
|
|
|
|
2016-11-14 20:32:37 +01:00
|
|
|
PUSH_TAGS_MESSAGE_TEMPLATE = u"""{user_name} {action} tag {tag}"""
|
2016-11-08 22:12:22 +01:00
|
|
|
TAG_WITH_URL_TEMPLATE = u"[{tag_name}]({tag_url})"
|
|
|
|
TAG_WITHOUT_URL_TEMPLATE = u"{tag_name}"
|
|
|
|
|
2016-11-09 18:50:10 +01:00
|
|
|
def get_push_commits_event_message(user_name, compare_url, branch_name, commits_data, is_truncated=False):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text, Optional[Text], Text, List[Dict[str, Any]], Optional[bool]) -> Text
|
2016-09-26 16:32:35 +02:00
|
|
|
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,
|
2016-11-09 18:50:10 +01:00
|
|
|
commits_data=get_commits_content(commits_data, is_truncated),
|
2016-09-26 16:32:35 +02:00
|
|
|
).rstrip()
|
2016-10-05 20:36:17 +02:00
|
|
|
|
|
|
|
def get_force_push_commits_event_message(user_name, url, branch_name, head):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text, Text, Text, Text) -> Text
|
2016-10-05 20:36:17 +02:00
|
|
|
return FORCE_PUSH_COMMITS_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
url=url,
|
|
|
|
branch_name=branch_name,
|
|
|
|
head=head
|
|
|
|
)
|
2016-10-06 15:26:47 +02:00
|
|
|
|
|
|
|
def get_remove_branch_event_message(user_name, branch_name):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text, Text) -> Text
|
2016-10-06 15:26:47 +02:00
|
|
|
return REMOVE_BRANCH_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
branch_name=branch_name,
|
|
|
|
)
|
2016-10-11 18:53:20 +02:00
|
|
|
|
|
|
|
def get_pull_request_event_message(
|
2016-10-26 21:13:00 +02:00
|
|
|
user_name, action, url, number=None,
|
2016-10-11 18:53:20 +02:00
|
|
|
target_branch=None, base_branch=None,
|
2016-10-19 15:57:57 +02:00
|
|
|
message=None, assignee=None, type='PR'
|
2016-10-11 18:53:20 +02:00
|
|
|
):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text, Text, Text, Optional[int], Optional[Text], Optional[Text], Optional[Text], Optional[Text], Optional[Text]) -> Text
|
2016-10-19 15:57:57 +02:00
|
|
|
main_message = PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE.format(
|
2016-10-11 18:53:20 +02:00
|
|
|
user_name=user_name,
|
|
|
|
action=action,
|
|
|
|
type=type,
|
2016-10-26 21:13:00 +02:00
|
|
|
url=url,
|
|
|
|
id=" #{}".format(number) if number is not None else ''
|
2016-10-11 18:53:20 +02:00
|
|
|
)
|
|
|
|
if assignee:
|
2016-10-19 15:57:57 +02:00
|
|
|
main_message += PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE.format(assignee=assignee)
|
2016-10-11 18:53:20 +02:00
|
|
|
|
|
|
|
if target_branch and base_branch:
|
|
|
|
main_message += PULL_REQUEST_BRANCH_INFO_TEMPLATE.format(
|
|
|
|
target=target_branch,
|
|
|
|
base=base_branch
|
|
|
|
)
|
2016-10-19 15:57:57 +02:00
|
|
|
if message:
|
2016-10-27 21:43:15 +02:00
|
|
|
main_message += '\n' + CONTENT_MESSAGE_TEMPLATE.format(message=message)
|
2016-10-11 18:53:20 +02:00
|
|
|
return main_message.rstrip()
|
2016-10-12 23:38:49 +02:00
|
|
|
|
2016-10-26 21:13:00 +02:00
|
|
|
def get_issue_event_message(user_name, action, url, number=None, message=None, assignee=None):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text, Text, Text, Optional[int], Optional[Text], Optional[Text]) -> Text
|
2016-10-19 15:57:57 +02:00
|
|
|
return get_pull_request_event_message(
|
|
|
|
user_name,
|
|
|
|
action,
|
|
|
|
url,
|
2016-10-26 21:13:00 +02:00
|
|
|
number,
|
2016-10-19 15:57:57 +02:00
|
|
|
message=message,
|
|
|
|
assignee=assignee,
|
|
|
|
type='Issue'
|
|
|
|
)
|
|
|
|
|
2016-11-08 22:12:22 +01:00
|
|
|
def get_push_tag_event_message(user_name, tag_name, tag_url=None, action='pushed'):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text, Text, Optional[Text], Optional[Text]) -> Text
|
2016-11-08 22:12:22 +01:00
|
|
|
if tag_url:
|
|
|
|
tag_part = TAG_WITH_URL_TEMPLATE.format(tag_name=tag_name, tag_url=tag_url)
|
|
|
|
else:
|
|
|
|
tag_part = TAG_WITHOUT_URL_TEMPLATE.format(tag_name=tag_name)
|
|
|
|
return PUSH_TAGS_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
action=action,
|
|
|
|
tag=tag_part
|
|
|
|
)
|
|
|
|
|
2016-10-27 21:43:15 +02:00
|
|
|
def get_commits_comment_action_message(user_name, action, commit_url, sha, message=None):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text, Text, Text, Text, Optional[Text]) -> Text
|
2016-10-27 21:43:15 +02:00
|
|
|
content = COMMITS_COMMENT_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
action=action,
|
|
|
|
sha=get_short_sha(sha),
|
|
|
|
url=commit_url
|
|
|
|
)
|
|
|
|
if message is not None:
|
|
|
|
content += CONTENT_MESSAGE_TEMPLATE.format(
|
|
|
|
message=message
|
|
|
|
)
|
|
|
|
return content
|
|
|
|
|
2016-11-09 18:50:10 +01:00
|
|
|
def get_commits_content(commits_data, is_truncated=False):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (List[Dict[str, Any]], Optional[bool]) -> Text
|
2016-10-12 23:38:49 +02:00
|
|
|
commits_content = u''
|
|
|
|
for commit in commits_data[:COMMITS_LIMIT]:
|
|
|
|
commits_content += COMMIT_ROW_TEMPLATE.format(
|
2016-10-27 21:43:15 +02:00
|
|
|
commit_short_sha=get_short_sha(commit.get('sha')),
|
2016-10-12 23:38:49 +02:00
|
|
|
commit_url=commit.get('url'),
|
|
|
|
commit_msg=commit.get('message').partition('\n')[0]
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(commits_data) > COMMITS_LIMIT:
|
|
|
|
commits_content += COMMITS_MORE_THAN_LIMIT_TEMPLATE.format(
|
2016-11-09 18:50:10 +01:00
|
|
|
commits_number=len(commits_data) - COMMITS_LIMIT
|
|
|
|
)
|
|
|
|
elif is_truncated:
|
|
|
|
commits_content += COMMITS_MORE_THAN_LIMIT_TEMPLATE.format(
|
|
|
|
commits_number=''
|
|
|
|
).replace(' ', ' ')
|
2016-10-12 23:38:49 +02:00
|
|
|
return commits_content.rstrip()
|
2016-10-27 21:43:15 +02:00
|
|
|
|
|
|
|
def get_short_sha(sha):
|
2016-12-05 07:05:33 +01:00
|
|
|
# type: (Text) -> Text
|
2016-10-27 21:43:15 +02:00
|
|
|
return sha[:7]
|