2019-04-19 22:02:41 +02:00
|
|
|
import string
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import Optional, Any, Dict, List, Tuple
|
2017-04-05 09:12:19 +02:00
|
|
|
from collections import defaultdict
|
2018-11-09 20:59:15 +01:00
|
|
|
TOPIC_WITH_BRANCH_TEMPLATE = '{repo} / {branch}'
|
|
|
|
TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE = '{repo} / {type} #{id} {title}'
|
2016-10-05 19:21:47 +02:00
|
|
|
|
2016-10-06 17:13:22 +02:00
|
|
|
EMPTY_SHA = '0000000000000000000000000000000000000000'
|
|
|
|
|
2017-03-09 21:25:21 +01:00
|
|
|
COMMITS_LIMIT = 20
|
2017-11-03 03:12:25 +01:00
|
|
|
COMMIT_ROW_TEMPLATE = '* {commit_msg} ([{commit_short_sha}]({commit_url}))\n'
|
2017-11-04 05:34:38 +01:00
|
|
|
COMMITS_MORE_THAN_LIMIT_TEMPLATE = "[and {commits_number} more commit(s)]"
|
|
|
|
COMMIT_OR_COMMITS = "commit{}"
|
2016-10-12 23:27:24 +02:00
|
|
|
|
2017-11-04 05:34:38 +01:00
|
|
|
PUSH_PUSHED_TEXT_WITH_URL = "[pushed]({compare_url}) {number_of_commits} {commit_or_commits}"
|
|
|
|
PUSH_PUSHED_TEXT_WITHOUT_URL = "pushed {number_of_commits} {commit_or_commits}"
|
2017-10-27 02:47:30 +02:00
|
|
|
|
|
|
|
PUSH_COMMITS_BASE = '{user_name} {pushed_text} to branch {branch_name}.'
|
2017-11-04 05:34:38 +01:00
|
|
|
PUSH_COMMITS_MESSAGE_TEMPLATE_WITH_COMMITTERS = PUSH_COMMITS_BASE + """ {committers_details}.
|
2017-04-26 02:57:47 +02:00
|
|
|
|
|
|
|
{commits_data}
|
|
|
|
"""
|
2017-11-04 05:34:38 +01:00
|
|
|
PUSH_COMMITS_MESSAGE_TEMPLATE_WITHOUT_COMMITTERS = PUSH_COMMITS_BASE + """
|
2016-09-26 16:32:35 +02:00
|
|
|
|
2016-10-12 23:38:49 +02:00
|
|
|
{commits_data}
|
2016-09-26 16:32:35 +02:00
|
|
|
"""
|
2017-11-04 05:34:38 +01:00
|
|
|
PUSH_DELETE_BRANCH_MESSAGE_TEMPLATE = "{user_name} [deleted]({compare_url}) the branch {branch_name}."
|
|
|
|
PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE = ("{user_name} [pushed]({compare_url}) "
|
2017-11-10 03:34:13 +01:00
|
|
|
"the branch {branch_name}.")
|
2017-11-04 05:34:38 +01:00
|
|
|
PUSH_COMMITS_MESSAGE_EXTENSION = "Commits by {}"
|
2017-04-05 09:12:19 +02:00
|
|
|
PUSH_COMMITTERS_LIMIT_INFO = 3
|
2016-09-26 16:32:35 +02:00
|
|
|
|
2017-11-04 05:34:38 +01:00
|
|
|
FORCE_PUSH_COMMITS_MESSAGE_TEMPLATE = ("{user_name} [force pushed]({url}) "
|
2019-04-19 22:02:41 +02:00
|
|
|
"to branch {branch_name}. Head is now {head}.")
|
2019-05-07 02:51:27 +02:00
|
|
|
CREATE_BRANCH_MESSAGE_TEMPLATE = "{user_name} created [{branch_name}]({url}) branch."
|
|
|
|
CREATE_BRANCH_WITHOUT_URL_MESSAGE_TEMPLATE = "{user_name} created {branch_name} branch."
|
2019-04-19 22:02:41 +02:00
|
|
|
REMOVE_BRANCH_MESSAGE_TEMPLATE = "{user_name} deleted branch {branch_name}."
|
2016-10-05 20:36:17 +02:00
|
|
|
|
2017-11-04 05:34:38 +01:00
|
|
|
PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE = "{user_name} {action} [{type}{id}]({url})"
|
2018-07-13 21:28:16 +02:00
|
|
|
PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE_WITH_TITLE = "{user_name} {action} [{type}{id} {title}]({url})"
|
2017-11-04 05:34:38 +01:00
|
|
|
PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE = "(assigned to {assignee})"
|
2019-04-19 22:02:41 +02:00
|
|
|
PULL_REQUEST_BRANCH_INFO_TEMPLATE = "from `{target}` to `{base}`"
|
2016-10-27 21:43:15 +02:00
|
|
|
|
2017-11-04 05:34:38 +01:00
|
|
|
SETUP_MESSAGE_TEMPLATE = "{integration} webhook has been successfully configured"
|
|
|
|
SETUP_MESSAGE_USER_PART = " by {user_name}"
|
2017-03-17 00:15:57 +01:00
|
|
|
|
2017-11-04 05:34:38 +01:00
|
|
|
CONTENT_MESSAGE_TEMPLATE = "\n~~~ quote\n{message}\n~~~"
|
2016-10-27 21:43:15 +02:00
|
|
|
|
2017-11-04 05:34:38 +01:00
|
|
|
COMMITS_COMMENT_MESSAGE_TEMPLATE = "{user_name} {action} on [{sha}]({url})"
|
2016-10-11 18:53:20 +02:00
|
|
|
|
2017-11-04 05:34:38 +01:00
|
|
|
PUSH_TAGS_MESSAGE_TEMPLATE = """{user_name} {action} tag {tag}"""
|
|
|
|
TAG_WITH_URL_TEMPLATE = "[{tag_name}]({tag_url})"
|
|
|
|
TAG_WITHOUT_URL_TEMPLATE = "{tag_name}"
|
2016-11-08 22:12:22 +01:00
|
|
|
|
2017-04-05 09:12:19 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_push_commits_event_message(user_name: str, compare_url: Optional[str],
|
|
|
|
branch_name: str, commits_data: List[Dict[str, Any]],
|
2017-12-20 21:08:26 +01:00
|
|
|
is_truncated: Optional[bool]=False,
|
2018-05-10 19:13:36 +02:00
|
|
|
deleted: Optional[bool]=False) -> str:
|
2017-05-16 04:38:25 +02:00
|
|
|
if not commits_data and deleted:
|
|
|
|
return PUSH_DELETE_BRANCH_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
compare_url=compare_url,
|
|
|
|
branch_name=branch_name
|
|
|
|
)
|
|
|
|
|
2017-05-17 02:19:33 +02:00
|
|
|
if not commits_data and not deleted:
|
|
|
|
return PUSH_LOCAL_BRANCH_WITHOUT_COMMITS_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
compare_url=compare_url,
|
|
|
|
branch_name=branch_name
|
|
|
|
)
|
|
|
|
|
2017-04-05 09:12:19 +02:00
|
|
|
pushed_message_template = PUSH_PUSHED_TEXT_WITH_URL if compare_url else PUSH_PUSHED_TEXT_WITHOUT_URL
|
|
|
|
|
|
|
|
pushed_text_message = pushed_message_template.format(
|
|
|
|
compare_url=compare_url,
|
|
|
|
number_of_commits=len(commits_data),
|
2017-11-03 03:12:25 +01:00
|
|
|
commit_or_commits=COMMIT_OR_COMMITS.format('s' if len(commits_data) > 1 else ''))
|
2016-09-26 16:32:35 +02:00
|
|
|
|
2017-04-26 02:57:47 +02:00
|
|
|
committers_items = get_all_committers(commits_data) # type: List[Tuple[str, int]]
|
|
|
|
if len(committers_items) == 1 and user_name == committers_items[0][0]:
|
|
|
|
return PUSH_COMMITS_MESSAGE_TEMPLATE_WITHOUT_COMMITTERS.format(
|
|
|
|
user_name=user_name,
|
|
|
|
pushed_text=pushed_text_message,
|
|
|
|
branch_name=branch_name,
|
|
|
|
commits_data=get_commits_content(commits_data, is_truncated),
|
|
|
|
).rstrip()
|
|
|
|
else:
|
|
|
|
committers_details = "{} ({})".format(*committers_items[0])
|
|
|
|
|
|
|
|
for name, number_of_commits in committers_items[1:-1]:
|
|
|
|
committers_details = "{}, {} ({})".format(committers_details, name, number_of_commits)
|
|
|
|
|
|
|
|
if len(committers_items) > 1:
|
|
|
|
committers_details = "{} and {} ({})".format(committers_details, *committers_items[-1])
|
|
|
|
|
|
|
|
return PUSH_COMMITS_MESSAGE_TEMPLATE_WITH_COMMITTERS.format(
|
|
|
|
user_name=user_name,
|
|
|
|
pushed_text=pushed_text_message,
|
|
|
|
branch_name=branch_name,
|
|
|
|
committers_details=PUSH_COMMITS_MESSAGE_EXTENSION.format(committers_details),
|
|
|
|
commits_data=get_commits_content(commits_data, is_truncated),
|
|
|
|
).rstrip()
|
2016-10-05 20:36:17 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_force_push_commits_event_message(user_name: str, url: str, branch_name: str, head: str) -> str:
|
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
|
|
|
|
2019-03-03 17:44:33 +01:00
|
|
|
def get_create_branch_event_message(user_name: str, url: Optional[str], branch_name: str) -> str:
|
|
|
|
if url is None:
|
|
|
|
return CREATE_BRANCH_WITHOUT_URL_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
branch_name=branch_name,
|
|
|
|
)
|
2017-03-16 08:28:13 +01:00
|
|
|
return CREATE_BRANCH_MESSAGE_TEMPLATE.format(
|
|
|
|
user_name=user_name,
|
|
|
|
url=url,
|
|
|
|
branch_name=branch_name,
|
|
|
|
)
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_remove_branch_event_message(user_name: str, branch_name: str) -> str:
|
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
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_pull_request_event_message(user_name: str, action: str, url: str, number: Optional[int]=None,
|
|
|
|
target_branch: Optional[str]=None, base_branch: Optional[str]=None,
|
|
|
|
message: Optional[str]=None, assignee: Optional[str]=None,
|
2018-12-20 23:31:05 +01:00
|
|
|
assignees: Optional[List[Dict[str, Any]]]=None,
|
2018-07-13 21:28:16 +02:00
|
|
|
type: Optional[str]='PR', title: Optional[str]=None) -> str:
|
|
|
|
kwargs = {
|
|
|
|
'user_name': user_name,
|
|
|
|
'action': action,
|
|
|
|
'type': type,
|
|
|
|
'url': url,
|
|
|
|
'id': ' #{}'.format(number) if number is not None else '',
|
|
|
|
'title': title,
|
|
|
|
}
|
|
|
|
|
|
|
|
if title is not None:
|
|
|
|
main_message = PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE_WITH_TITLE.format(**kwargs)
|
|
|
|
else:
|
|
|
|
main_message = PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE.format(**kwargs)
|
|
|
|
|
2018-12-20 23:31:05 +01:00
|
|
|
if assignees:
|
|
|
|
assignees_string = ""
|
|
|
|
if len(assignees) == 1:
|
|
|
|
assignees_string = "{username}".format(**assignees[0])
|
|
|
|
else:
|
|
|
|
usernames = []
|
|
|
|
for a in assignees:
|
|
|
|
usernames.append(a['username'])
|
|
|
|
|
|
|
|
assignees_string = ", ".join(usernames[:-1]) + " and " + usernames[-1]
|
|
|
|
|
2019-04-19 22:02:41 +02:00
|
|
|
assignee_info = PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE.format(
|
|
|
|
assignee=assignees_string)
|
|
|
|
main_message = "{} {}".format(main_message, assignee_info)
|
2018-12-20 23:31:05 +01:00
|
|
|
|
|
|
|
elif assignee:
|
2019-04-19 22:02:41 +02:00
|
|
|
assignee_info = PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE.format(
|
|
|
|
assignee=assignee)
|
|
|
|
main_message = "{} {}".format(main_message, assignee_info)
|
2016-10-11 18:53:20 +02:00
|
|
|
|
|
|
|
if target_branch and base_branch:
|
2019-04-19 22:02:41 +02:00
|
|
|
branch_info = PULL_REQUEST_BRANCH_INFO_TEMPLATE.format(
|
2016-10-11 18:53:20 +02:00
|
|
|
target=target_branch,
|
|
|
|
base=base_branch
|
|
|
|
)
|
2019-04-19 22:02:41 +02:00
|
|
|
main_message = "{} {}".format(main_message, branch_info)
|
|
|
|
|
|
|
|
punctuation = ':' if message else '.'
|
|
|
|
if (assignees or assignee or (target_branch and base_branch) or (title is None)):
|
|
|
|
main_message = '{message}{punctuation}'.format(
|
|
|
|
message=main_message, punctuation=punctuation)
|
|
|
|
elif title is not None:
|
|
|
|
# Once we get here, we know that the message ends with a title
|
|
|
|
# which could already have punctuation at the end
|
|
|
|
if title[-1] not in string.punctuation:
|
|
|
|
main_message = '{message}{punctuation}'.format(
|
|
|
|
message=main_message, punctuation=punctuation)
|
|
|
|
|
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
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_setup_webhook_message(integration: str, user_name: Optional[str]=None) -> str:
|
2017-03-17 00:15:57 +01:00
|
|
|
content = SETUP_MESSAGE_TEMPLATE.format(integration=integration)
|
|
|
|
if user_name:
|
|
|
|
content += SETUP_MESSAGE_USER_PART.format(user_name=user_name)
|
2019-05-09 02:51:28 +02:00
|
|
|
content = "{}.".format(content)
|
2017-03-17 00:15:57 +01:00
|
|
|
return content
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_issue_event_message(user_name: str,
|
|
|
|
action: str,
|
|
|
|
url: str,
|
2017-11-05 11:15:10 +01:00
|
|
|
number: Optional[int]=None,
|
2018-05-10 19:13:36 +02:00
|
|
|
message: Optional[str]=None,
|
2018-07-13 21:28:16 +02:00
|
|
|
assignee: Optional[str]=None,
|
2018-12-20 23:31:05 +01:00
|
|
|
assignees: Optional[List[Dict[str, Any]]]=None,
|
2018-07-13 21:28:16 +02:00
|
|
|
title: Optional[str]=None) -> str:
|
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,
|
2018-12-20 23:31:05 +01:00
|
|
|
assignees=assignees,
|
2018-07-13 21:28:16 +02:00
|
|
|
type='Issue',
|
|
|
|
title=title,
|
2016-10-19 15:57:57 +02:00
|
|
|
)
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_push_tag_event_message(user_name: str,
|
|
|
|
tag_name: str,
|
|
|
|
tag_url: Optional[str]=None,
|
|
|
|
action: Optional[str]='pushed') -> str:
|
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)
|
2019-04-19 22:02:41 +02:00
|
|
|
|
|
|
|
message = PUSH_TAGS_MESSAGE_TEMPLATE.format(
|
2016-11-08 22:12:22 +01:00
|
|
|
user_name=user_name,
|
|
|
|
action=action,
|
|
|
|
tag=tag_part
|
|
|
|
)
|
|
|
|
|
2019-04-19 22:02:41 +02:00
|
|
|
if tag_name[-1] not in string.punctuation:
|
|
|
|
message = '{}.'.format(message)
|
|
|
|
|
|
|
|
return message
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_commits_comment_action_message(user_name: str,
|
|
|
|
action: str,
|
|
|
|
commit_url: str,
|
|
|
|
sha: str,
|
|
|
|
message: Optional[str]=None) -> str:
|
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
|
|
|
|
)
|
2019-04-19 22:02:41 +02:00
|
|
|
punctuation = ':' if message else '.'
|
|
|
|
content = '{}{}'.format(content, punctuation)
|
|
|
|
if message:
|
2016-10-27 21:43:15 +02:00
|
|
|
content += CONTENT_MESSAGE_TEMPLATE.format(
|
|
|
|
message=message
|
|
|
|
)
|
2019-04-19 22:02:41 +02:00
|
|
|
|
2016-10-27 21:43:15 +02:00
|
|
|
return content
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_commits_content(commits_data: List[Dict[str, Any]], is_truncated: Optional[bool]=False) -> str:
|
2017-11-03 03:12:25 +01:00
|
|
|
commits_content = ''
|
2016-10-12 23:38:49 +02:00
|
|
|
for commit in commits_data[:COMMITS_LIMIT]:
|
|
|
|
commits_content += COMMIT_ROW_TEMPLATE.format(
|
2017-05-24 21:28:26 +02:00
|
|
|
commit_short_sha=get_short_sha(commit['sha']),
|
2016-10-12 23:38:49 +02:00
|
|
|
commit_url=commit.get('url'),
|
2017-05-24 21:28:26 +02:00
|
|
|
commit_msg=commit['message'].partition('\n')[0]
|
2016-10-12 23:38:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_short_sha(sha: str) -> str:
|
2016-10-27 21:43:15 +02:00
|
|
|
return sha[:7]
|
2017-04-05 09:12:19 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_all_committers(commits_data: List[Dict[str, Any]]) -> List[Tuple[str, int]]:
|
2017-04-05 09:12:19 +02:00
|
|
|
committers = defaultdict(int) # type: Dict[str, int]
|
|
|
|
|
|
|
|
for commit in commits_data:
|
|
|
|
committers[commit['name']] += 1
|
|
|
|
|
2017-04-21 20:44:37 +02:00
|
|
|
# Sort by commit count, breaking ties alphabetically.
|
|
|
|
committers_items = sorted(list(committers.items()),
|
|
|
|
key=lambda item: (-item[1], item[0])) # type: List[Tuple[str, int]]
|
2017-04-05 09:12:19 +02:00
|
|
|
committers_values = [c_i[1] for c_i in committers_items] # type: List[int]
|
|
|
|
|
|
|
|
if len(committers) > PUSH_COMMITTERS_LIMIT_INFO:
|
|
|
|
others_number_of_commits = sum(committers_values[PUSH_COMMITTERS_LIMIT_INFO:])
|
|
|
|
committers_items = committers_items[:PUSH_COMMITTERS_LIMIT_INFO]
|
|
|
|
committers_items.append(('others', others_number_of_commits))
|
|
|
|
|
|
|
|
return committers_items
|