mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 20:44:04 +00:00
python: Normalize quotes with Black.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
11741543da
commit
6e4c3e41dc
@@ -5,9 +5,9 @@ from zerver.lib.webhooks.git import COMMITS_LIMIT
|
||||
|
||||
|
||||
class GogsHookTests(WebhookTestCase):
|
||||
STREAM_NAME = 'commits'
|
||||
STREAM_NAME = "commits"
|
||||
URL_TEMPLATE = "/api/v1/external/gogs?&api_key={api_key}&stream={stream}"
|
||||
FIXTURE_DIR_NAME = 'gogs'
|
||||
FIXTURE_DIR_NAME = "gogs"
|
||||
|
||||
def test_push(self) -> None:
|
||||
expected_topic = "try-git / master"
|
||||
@@ -17,20 +17,20 @@ class GogsHookTests(WebhookTestCase):
|
||||
self.check_webhook("push", expected_topic, expected_message)
|
||||
|
||||
def test_push_multiple_committers(self) -> None:
|
||||
commit_info = '* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))\n'
|
||||
commit_info = "* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))\n"
|
||||
expected_topic = "try-git / master"
|
||||
expected_message = f"""john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 2 commits to branch master. Commits by Benjamin (1) and John (1).\n\n{commit_info}* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))"""
|
||||
self.check_webhook("push__commits_multiple_committers", expected_topic, expected_message)
|
||||
|
||||
def test_push_multiple_committers_filtered_by_branches(self) -> None:
|
||||
self.url = self.build_webhook_url(branches='master,development')
|
||||
commit_info = '* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))\n'
|
||||
self.url = self.build_webhook_url(branches="master,development")
|
||||
commit_info = "* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))\n"
|
||||
expected_topic = "try-git / master"
|
||||
expected_message = f"""john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 2 commits to branch master. Commits by Benjamin (1) and John (1).\n\n{commit_info}* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))"""
|
||||
self.check_webhook("push__commits_multiple_committers", expected_topic, expected_message)
|
||||
|
||||
def test_push_filtered_by_branches(self) -> None:
|
||||
self.url = self.build_webhook_url(branches='master,development')
|
||||
self.url = self.build_webhook_url(branches="master,development")
|
||||
expected_topic = "try-git / master"
|
||||
expected_message = """john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 1 commit to branch master. Commits by John (1).
|
||||
|
||||
@@ -44,7 +44,7 @@ class GogsHookTests(WebhookTestCase):
|
||||
self.check_webhook("push__commits_more_than_limits", expected_topic, expected_message)
|
||||
|
||||
def test_push_commits_more_than_limits_filtered_by_branches(self) -> None:
|
||||
self.url = self.build_webhook_url(branches='master,development')
|
||||
self.url = self.build_webhook_url(branches="master,development")
|
||||
expected_topic = "try-git / master"
|
||||
commits_info = "* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))\n"
|
||||
expected_message = f"john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 30 commits to branch master. Commits by John (30).\n\n{commits_info * COMMITS_LIMIT}[and {30 - COMMITS_LIMIT} more commit(s)]"
|
||||
@@ -63,7 +63,7 @@ class GogsHookTests(WebhookTestCase):
|
||||
self.check_webhook("pull_request__opened", expected_topic, expected_message)
|
||||
|
||||
def test_pull_request_opened_with_custom_topic_in_url(self) -> None:
|
||||
self.url = self.build_webhook_url(topic='notifications')
|
||||
self.url = self.build_webhook_url(topic="notifications")
|
||||
expected_topic = "notifications"
|
||||
expected_message = """john opened [PR #1 Title Text for Pull Request](http://localhost:3000/john/try-git/pulls/1) from `feature` to `master`."""
|
||||
self.check_webhook("pull_request__opened", expected_topic, expected_message)
|
||||
@@ -138,38 +138,38 @@ class GogsHookTests(WebhookTestCase):
|
||||
expected_message = """cestrell published release [Title](https://try.gogs.io/cestrell/zulip_test) for tag v1.4."""
|
||||
self.check_webhook("release__published", expected_topic, expected_message)
|
||||
|
||||
@patch('zerver.webhooks.gogs.view.check_send_webhook_message')
|
||||
@patch("zerver.webhooks.gogs.view.check_send_webhook_message")
|
||||
def test_push_filtered_by_branches_ignore(
|
||||
self, check_send_webhook_message_mock: MagicMock
|
||||
) -> None:
|
||||
self.url = self.build_webhook_url(branches='changes,development')
|
||||
payload = self.get_body('push')
|
||||
self.url = self.build_webhook_url(branches="changes,development")
|
||||
payload = self.get_body("push")
|
||||
result = self.client_post(
|
||||
self.url, payload, HTTP_X_GOGS_EVENT='push', content_type="application/json"
|
||||
self.url, payload, HTTP_X_GOGS_EVENT="push", content_type="application/json"
|
||||
)
|
||||
self.assertFalse(check_send_webhook_message_mock.called)
|
||||
self.assert_json_success(result)
|
||||
|
||||
@patch('zerver.webhooks.gogs.view.check_send_webhook_message')
|
||||
@patch("zerver.webhooks.gogs.view.check_send_webhook_message")
|
||||
def test_push_commits_more_than_limits_filtered_by_branches_ignore(
|
||||
self, check_send_webhook_message_mock: MagicMock
|
||||
) -> None:
|
||||
self.url = self.build_webhook_url(branches='changes,development')
|
||||
payload = self.get_body('push__commits_more_than_limits')
|
||||
self.url = self.build_webhook_url(branches="changes,development")
|
||||
payload = self.get_body("push__commits_more_than_limits")
|
||||
result = self.client_post(
|
||||
self.url, payload, HTTP_X_GOGS_EVENT='push', content_type="application/json"
|
||||
self.url, payload, HTTP_X_GOGS_EVENT="push", content_type="application/json"
|
||||
)
|
||||
self.assertFalse(check_send_webhook_message_mock.called)
|
||||
self.assert_json_success(result)
|
||||
|
||||
@patch('zerver.webhooks.gogs.view.check_send_webhook_message')
|
||||
@patch("zerver.webhooks.gogs.view.check_send_webhook_message")
|
||||
def test_push_multiple_committers_filtered_by_branches_ignore(
|
||||
self, check_send_webhook_message_mock: MagicMock
|
||||
) -> None:
|
||||
self.url = self.build_webhook_url(branches='changes,development')
|
||||
payload = self.get_body('push__commits_multiple_committers')
|
||||
self.url = self.build_webhook_url(branches="changes,development")
|
||||
payload = self.get_body("push__commits_multiple_committers")
|
||||
result = self.client_post(
|
||||
self.url, payload, HTTP_X_GOGS_EVENT='push', content_type="application/json"
|
||||
self.url, payload, HTTP_X_GOGS_EVENT="push", content_type="application/json"
|
||||
)
|
||||
self.assertFalse(check_send_webhook_message_mock.called)
|
||||
self.assert_json_success(result)
|
||||
|
||||
@@ -33,15 +33,15 @@ def get_issue_url(repo_url: str, issue_nr: int) -> str:
|
||||
|
||||
def format_push_event(payload: Dict[str, Any]) -> str:
|
||||
|
||||
for commit in payload['commits']:
|
||||
commit['sha'] = commit['id']
|
||||
commit['name'] = commit['author']['username'] or commit['author']['name'].split()[0]
|
||||
for commit in payload["commits"]:
|
||||
commit["sha"] = commit["id"]
|
||||
commit["name"] = commit["author"]["username"] or commit["author"]["name"].split()[0]
|
||||
|
||||
data = {
|
||||
'user_name': payload['sender']['username'],
|
||||
'compare_url': payload['compare_url'],
|
||||
'branch_name': payload['ref'].replace('refs/heads/', ''),
|
||||
'commits_data': payload['commits'],
|
||||
"user_name": payload["sender"]["username"],
|
||||
"compare_url": payload["compare_url"],
|
||||
"branch_name": payload["ref"].replace("refs/heads/", ""),
|
||||
"commits_data": payload["commits"],
|
||||
}
|
||||
|
||||
return get_push_commits_event_message(**data)
|
||||
@@ -49,13 +49,13 @@ def format_push_event(payload: Dict[str, Any]) -> str:
|
||||
|
||||
def format_new_branch_event(payload: Dict[str, Any]) -> str:
|
||||
|
||||
branch_name = payload['ref']
|
||||
url = '{}/src/{}'.format(payload['repository']['html_url'], branch_name)
|
||||
branch_name = payload["ref"]
|
||||
url = "{}/src/{}".format(payload["repository"]["html_url"], branch_name)
|
||||
|
||||
data = {
|
||||
'user_name': payload['sender']['username'],
|
||||
'url': url,
|
||||
'branch_name': branch_name,
|
||||
"user_name": payload["sender"]["username"],
|
||||
"url": url,
|
||||
"branch_name": branch_name,
|
||||
}
|
||||
return get_create_branch_event_message(**data)
|
||||
|
||||
@@ -63,75 +63,75 @@ def format_new_branch_event(payload: Dict[str, Any]) -> str:
|
||||
def format_pull_request_event(payload: Dict[str, Any], include_title: bool = False) -> str:
|
||||
|
||||
data = {
|
||||
'user_name': payload['pull_request']['user']['username'],
|
||||
'action': payload['action'],
|
||||
'url': payload['pull_request']['html_url'],
|
||||
'number': payload['pull_request']['number'],
|
||||
'target_branch': payload['pull_request']['head_branch'],
|
||||
'base_branch': payload['pull_request']['base_branch'],
|
||||
'title': payload['pull_request']['title'] if include_title else None,
|
||||
"user_name": payload["pull_request"]["user"]["username"],
|
||||
"action": payload["action"],
|
||||
"url": payload["pull_request"]["html_url"],
|
||||
"number": payload["pull_request"]["number"],
|
||||
"target_branch": payload["pull_request"]["head_branch"],
|
||||
"base_branch": payload["pull_request"]["base_branch"],
|
||||
"title": payload["pull_request"]["title"] if include_title else None,
|
||||
}
|
||||
|
||||
if payload['pull_request']['merged']:
|
||||
data['user_name'] = payload['pull_request']['merged_by']['username']
|
||||
data['action'] = 'merged'
|
||||
if payload["pull_request"]["merged"]:
|
||||
data["user_name"] = payload["pull_request"]["merged_by"]["username"]
|
||||
data["action"] = "merged"
|
||||
|
||||
return get_pull_request_event_message(**data)
|
||||
|
||||
|
||||
def format_issues_event(payload: Dict[str, Any], include_title: bool = False) -> str:
|
||||
issue_nr = payload['issue']['number']
|
||||
assignee = payload['issue']['assignee']
|
||||
issue_nr = payload["issue"]["number"]
|
||||
assignee = payload["issue"]["assignee"]
|
||||
return get_issue_event_message(
|
||||
payload['sender']['login'],
|
||||
payload['action'],
|
||||
get_issue_url(payload['repository']['html_url'], issue_nr),
|
||||
payload["sender"]["login"],
|
||||
payload["action"],
|
||||
get_issue_url(payload["repository"]["html_url"], issue_nr),
|
||||
issue_nr,
|
||||
payload['issue']['body'],
|
||||
assignee=assignee['login'] if assignee else None,
|
||||
title=payload['issue']['title'] if include_title else None,
|
||||
payload["issue"]["body"],
|
||||
assignee=assignee["login"] if assignee else None,
|
||||
title=payload["issue"]["title"] if include_title else None,
|
||||
)
|
||||
|
||||
|
||||
def format_issue_comment_event(payload: Dict[str, Any], include_title: bool = False) -> str:
|
||||
action = payload['action']
|
||||
comment = payload['comment']
|
||||
issue = payload['issue']
|
||||
action = payload["action"]
|
||||
comment = payload["comment"]
|
||||
issue = payload["issue"]
|
||||
|
||||
if action == 'created':
|
||||
action = '[commented]'
|
||||
if action == "created":
|
||||
action = "[commented]"
|
||||
else:
|
||||
action = f'{action} a [comment]'
|
||||
action += '({}) on'.format(comment['html_url'])
|
||||
action = f"{action} a [comment]"
|
||||
action += "({}) on".format(comment["html_url"])
|
||||
|
||||
return get_issue_event_message(
|
||||
payload['sender']['login'],
|
||||
payload["sender"]["login"],
|
||||
action,
|
||||
get_issue_url(payload['repository']['html_url'], issue['number']),
|
||||
issue['number'],
|
||||
comment['body'],
|
||||
title=issue['title'] if include_title else None,
|
||||
get_issue_url(payload["repository"]["html_url"], issue["number"]),
|
||||
issue["number"],
|
||||
comment["body"],
|
||||
title=issue["title"] if include_title else None,
|
||||
)
|
||||
|
||||
|
||||
def format_release_event(payload: Dict[str, Any], include_title: bool = False) -> str:
|
||||
data = {
|
||||
'user_name': payload['release']['author']['username'],
|
||||
'action': payload['action'],
|
||||
'tagname': payload['release']['tag_name'],
|
||||
'release_name': payload['release']['name'],
|
||||
'url': payload['repository']['html_url'],
|
||||
"user_name": payload["release"]["author"]["username"],
|
||||
"action": payload["action"],
|
||||
"tagname": payload["release"]["tag_name"],
|
||||
"release_name": payload["release"]["name"],
|
||||
"url": payload["repository"]["html_url"],
|
||||
}
|
||||
|
||||
return get_release_event_message(**data)
|
||||
|
||||
|
||||
@webhook_view('Gogs')
|
||||
@webhook_view("Gogs")
|
||||
@has_request_variables
|
||||
def api_gogs_webhook(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
payload: Dict[str, Any] = REQ(argument_type='body'),
|
||||
payload: Dict[str, Any] = REQ(argument_type="body"),
|
||||
branches: Optional[str] = REQ(default=None),
|
||||
user_specified_topic: Optional[str] = REQ("topic", default=None),
|
||||
) -> HttpResponse:
|
||||
@@ -157,65 +157,65 @@ def gogs_webhook_main(
|
||||
branches: Optional[str],
|
||||
user_specified_topic: Optional[str],
|
||||
) -> HttpResponse:
|
||||
repo = payload['repository']['name']
|
||||
repo = payload["repository"]["name"]
|
||||
event = validate_extract_webhook_http_header(request, http_header_name, integration_name)
|
||||
if event == 'push':
|
||||
branch = payload['ref'].replace('refs/heads/', '')
|
||||
if branches is not None and branch not in branches.split(','):
|
||||
if event == "push":
|
||||
branch = payload["ref"].replace("refs/heads/", "")
|
||||
if branches is not None and branch not in branches.split(","):
|
||||
return json_success()
|
||||
body = format_push_event(payload)
|
||||
topic = TOPIC_WITH_BRANCH_TEMPLATE.format(
|
||||
repo=repo,
|
||||
branch=branch,
|
||||
)
|
||||
elif event == 'create':
|
||||
elif event == "create":
|
||||
body = format_new_branch_event(payload)
|
||||
topic = TOPIC_WITH_BRANCH_TEMPLATE.format(
|
||||
repo=repo,
|
||||
branch=payload['ref'],
|
||||
branch=payload["ref"],
|
||||
)
|
||||
elif event == 'pull_request':
|
||||
elif event == "pull_request":
|
||||
body = format_pull_request_event(
|
||||
payload,
|
||||
include_title=user_specified_topic is not None,
|
||||
)
|
||||
topic = TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
||||
repo=repo,
|
||||
type='PR',
|
||||
id=payload['pull_request']['id'],
|
||||
title=payload['pull_request']['title'],
|
||||
type="PR",
|
||||
id=payload["pull_request"]["id"],
|
||||
title=payload["pull_request"]["title"],
|
||||
)
|
||||
elif event == 'issues':
|
||||
elif event == "issues":
|
||||
body = format_issues_event(
|
||||
payload,
|
||||
include_title=user_specified_topic is not None,
|
||||
)
|
||||
topic = TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
||||
repo=repo,
|
||||
type='Issue',
|
||||
id=payload['issue']['number'],
|
||||
title=payload['issue']['title'],
|
||||
type="Issue",
|
||||
id=payload["issue"]["number"],
|
||||
title=payload["issue"]["title"],
|
||||
)
|
||||
elif event == 'issue_comment':
|
||||
elif event == "issue_comment":
|
||||
body = format_issue_comment_event(
|
||||
payload,
|
||||
include_title=user_specified_topic is not None,
|
||||
)
|
||||
topic = TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
||||
repo=repo,
|
||||
type='Issue',
|
||||
id=payload['issue']['number'],
|
||||
title=payload['issue']['title'],
|
||||
type="Issue",
|
||||
id=payload["issue"]["number"],
|
||||
title=payload["issue"]["title"],
|
||||
)
|
||||
elif event == 'release':
|
||||
elif event == "release":
|
||||
body = format_release_event(
|
||||
payload,
|
||||
include_title=user_specified_topic is not None,
|
||||
)
|
||||
topic = TOPIC_WITH_RELEASE_TEMPLATE.format(
|
||||
repo=repo,
|
||||
tag=payload['release']['tag_name'],
|
||||
title=payload['release']['name'],
|
||||
tag=payload["release"]["tag_name"],
|
||||
title=payload["release"]["name"],
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user