2016-06-17 19:53:21 +02:00
|
|
|
# Webhooks for external integrations.
|
2020-03-14 13:12:03 +01:00
|
|
|
from typing import Any, Dict, Tuple, Optional
|
|
|
|
|
|
|
|
from urllib.parse import urlparse
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2016-06-17 19:53:21 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
# Semaphore Classic Templates
|
|
|
|
|
2019-04-17 03:57:31 +02:00
|
|
|
BUILD_TEMPLATE = """
|
|
|
|
[Build {build_number}]({build_url}) {status}:
|
|
|
|
* **Commit**: [{commit_hash}: {commit_message}]({commit_url})
|
|
|
|
* **Author**: {email}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
DEPLOY_TEMPLATE = """
|
|
|
|
[Deploy {deploy_number}]({deploy_url}) of [build {build_number}]({build_url}) {status}:
|
|
|
|
* **Commit**: [{commit_hash}: {commit_message}]({commit_url})
|
|
|
|
* **Author**: {email}
|
|
|
|
* **Server**: {server_name}
|
|
|
|
""".strip()
|
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
# Semaphore 2.0 Templates
|
|
|
|
|
|
|
|
# Currently, Semaphore 2.0 only supports GitHub, while Semaphore Classic
|
|
|
|
# supports Bitbucket too. The payload does not have URLs for commits, tags,
|
|
|
|
# pull requests, etc. So, we use separate templates for GitHub and construct
|
|
|
|
# the URLs ourselves. For any other repository hosting services we use
|
|
|
|
# templates that don't have any links in them.
|
|
|
|
|
|
|
|
GH_PUSH_TEMPLATE = """
|
|
|
|
[{pipeline_name}]({workflow_url}) pipeline **{pipeline_result}**:
|
|
|
|
* **Commit**: [({commit_hash})]({commit_url}) {commit_message}
|
|
|
|
* **Branch**: {branch_name}
|
|
|
|
* **Author**: [{author_name}]({author_url})
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
PUSH_TEMPLATE = """
|
|
|
|
[{pipeline_name}]({workflow_url}) pipeline **{pipeline_result}**:
|
|
|
|
* **Commit**: ({commit_hash}) {commit_message}
|
|
|
|
* **Branch**: {branch_name}
|
|
|
|
* **Author**: {author_name}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
GH_PULL_REQUEST_TEMPLATE = """
|
|
|
|
[{pipeline_name}]({workflow_url}) pipeline **{pipeline_result}**:
|
|
|
|
* **Pull Request**: [{pull_request_title}]({pull_request_url})
|
|
|
|
* **Branch**: {branch_name}
|
|
|
|
* **Author**: [{author_name}]({author_url})
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
PULL_REQUEST_TEMPLATE = """
|
|
|
|
[{pipeline_name}]({workflow_url}) pipeline **{pipeline_result}**:
|
|
|
|
* **Pull Request**: {pull_request_title} (#{pull_request_number})
|
|
|
|
* **Branch**: {branch_name}
|
|
|
|
* **Author**: {author_name}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
GH_TAG_TEMPLATE = """
|
|
|
|
[{pipeline_name}]({workflow_url}) pipeline **{pipeline_result}**:
|
|
|
|
* **Tag**: [{tag_name}]({tag_url})
|
|
|
|
* **Author**: [{author_name}]({author_url})
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
TAG_TEMPLATE = """
|
|
|
|
[{pipeline_name}]({workflow_url}) pipeline **{pipeline_result}**:
|
|
|
|
* **Tag**: {tag_name}
|
|
|
|
* **Author**: {author_name}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
DEFAULT_TEMPLATE = """
|
|
|
|
[{pipeline_name}]({workflow_url}) pipeline **{pipeline_result}** for {event_name} event
|
|
|
|
""".strip()
|
|
|
|
|
2019-04-17 03:57:31 +02:00
|
|
|
TOPIC_TEMPLATE = "{project}/{branch}"
|
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
GITHUB_URL_TEMPLATES = {
|
|
|
|
'commit': '{repo_url}/commit/{commit_id}',
|
|
|
|
'pull_request': '{repo_url}/pull/{pr_number}',
|
|
|
|
'tag': '{repo_url}/tree/{tag_name}',
|
|
|
|
'user': 'https://github.com/{username}',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-17 19:53:21 +02:00
|
|
|
@api_key_only_webhook_view('Semaphore')
|
|
|
|
@has_request_variables
|
2017-12-03 20:51:06 +01:00
|
|
|
def api_semaphore_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-03-16 22:53:50 +01:00
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
content, project_name, branch_name = (
|
|
|
|
semaphore_classic(payload) if 'event' in payload else semaphore_2(payload)
|
|
|
|
)
|
|
|
|
subject = (
|
|
|
|
TOPIC_TEMPLATE.format(project=project_name, branch=branch_name) if branch_name else project_name
|
|
|
|
)
|
|
|
|
check_send_webhook_message(request, user_profile, subject, content)
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
def semaphore_classic(payload: Dict[str, Any]) -> Tuple[str, str, str]:
|
2016-06-17 19:53:21 +02:00
|
|
|
# semaphore only gives the last commit, even if there were multiple commits
|
|
|
|
# since the last build
|
2017-08-24 17:31:04 +02:00
|
|
|
branch_name = payload["branch_name"]
|
|
|
|
project_name = payload["project_name"]
|
|
|
|
result = payload["result"]
|
|
|
|
event = payload["event"]
|
|
|
|
commit_id = payload["commit"]["id"]
|
|
|
|
commit_url = payload["commit"]["url"]
|
|
|
|
author_email = payload["commit"]["author_email"]
|
2020-04-14 19:04:01 +02:00
|
|
|
message = summary_line(payload["commit"]["message"])
|
2016-06-17 19:53:21 +02:00
|
|
|
|
|
|
|
if event == "build":
|
2017-08-24 17:31:04 +02:00
|
|
|
build_url = payload["build_url"]
|
|
|
|
build_number = payload["build_number"]
|
2019-04-17 03:57:31 +02:00
|
|
|
content = BUILD_TEMPLATE.format(
|
|
|
|
build_number=build_number,
|
|
|
|
build_url=build_url,
|
|
|
|
status=result,
|
|
|
|
commit_hash=commit_id[:7],
|
|
|
|
commit_message=message,
|
|
|
|
commit_url=commit_url,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
email=author_email,
|
2019-04-17 03:57:31 +02:00
|
|
|
)
|
2016-06-17 19:53:21 +02:00
|
|
|
|
|
|
|
elif event == "deploy":
|
2017-08-24 17:31:04 +02:00
|
|
|
build_url = payload["build_html_url"]
|
|
|
|
build_number = payload["build_number"]
|
|
|
|
deploy_url = payload["html_url"]
|
|
|
|
deploy_number = payload["number"]
|
|
|
|
server_name = payload["server_name"]
|
2019-04-17 03:57:31 +02:00
|
|
|
content = DEPLOY_TEMPLATE.format(
|
|
|
|
deploy_number=deploy_number,
|
|
|
|
deploy_url=deploy_url,
|
|
|
|
build_number=build_number,
|
|
|
|
build_url=build_url,
|
|
|
|
status=result,
|
|
|
|
commit_hash=commit_id[:7],
|
|
|
|
commit_message=message,
|
|
|
|
commit_url=commit_url,
|
|
|
|
email=author_email,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
server_name=server_name,
|
2019-04-17 03:57:31 +02:00
|
|
|
)
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2017-05-07 21:37:02 +02:00
|
|
|
else: # should never get here
|
2020-06-10 06:40:53 +02:00
|
|
|
content = f"{event}: {result}"
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
return content, project_name, branch_name
|
|
|
|
|
|
|
|
def semaphore_2(payload: Dict[str, Any]) -> Tuple[str, str, Optional[str]]:
|
|
|
|
repo_url = payload["repository"]["url"]
|
|
|
|
project_name = payload["project"]["name"]
|
|
|
|
organization_name = payload["organization"]["name"]
|
|
|
|
author_name = payload["revision"]["sender"]["login"]
|
|
|
|
workflow_id = payload['workflow']['id']
|
|
|
|
context = dict(
|
|
|
|
author_name=author_name,
|
|
|
|
author_url=GITHUB_URL_TEMPLATES['user'].format(repo_url=repo_url, username=author_name),
|
|
|
|
pipeline_name=payload["pipeline"]["name"],
|
|
|
|
pipeline_result=payload["pipeline"]["result"],
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
workflow_url=f'https://{organization_name}.semaphoreci.com/workflows/{workflow_id}',
|
2019-04-17 03:57:31 +02:00
|
|
|
)
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
if payload["revision"]["reference_type"] == "branch": # push event
|
|
|
|
commit_id = payload["revision"]["commit_sha"]
|
|
|
|
branch_name = payload["revision"]["branch"]["name"]
|
|
|
|
context.update(
|
|
|
|
branch_name=branch_name,
|
|
|
|
commit_id=commit_id,
|
|
|
|
commit_hash=commit_id[:7],
|
2020-04-14 19:04:01 +02:00
|
|
|
commit_message=summary_line(payload["revision"]["commit_message"]),
|
2020-03-14 13:12:03 +01:00
|
|
|
commit_url=GITHUB_URL_TEMPLATES['commit'].format(repo_url=repo_url, commit_id=commit_id),
|
|
|
|
)
|
|
|
|
template = GH_PUSH_TEMPLATE if is_github_repo(repo_url) else PUSH_TEMPLATE
|
|
|
|
content = template.format(**context)
|
|
|
|
elif payload["revision"]["reference_type"] == "pull_request":
|
|
|
|
pull_request = payload["revision"]["pull_request"]
|
|
|
|
branch_name = pull_request["branch_name"]
|
|
|
|
pull_request_title = pull_request["name"]
|
|
|
|
pull_request_number = pull_request["number"]
|
|
|
|
pull_request_url = GITHUB_URL_TEMPLATES['pull_request'].format(
|
|
|
|
repo_url=repo_url, pr_number=pull_request_number)
|
|
|
|
context.update(
|
|
|
|
branch_name=branch_name,
|
|
|
|
pull_request_title=pull_request_title,
|
|
|
|
pull_request_url=pull_request_url,
|
|
|
|
pull_request_number=pull_request_number,
|
|
|
|
)
|
|
|
|
template = GH_PULL_REQUEST_TEMPLATE if is_github_repo(repo_url) else PULL_REQUEST_TEMPLATE
|
|
|
|
content = template.format(**context)
|
|
|
|
elif payload["revision"]["reference_type"] == "tag":
|
|
|
|
branch_name = ''
|
|
|
|
tag_name = payload["revision"]["tag"]["name"]
|
|
|
|
tag_url = GITHUB_URL_TEMPLATES['tag'].format(repo_url=repo_url, tag_name=tag_name)
|
|
|
|
context.update(
|
|
|
|
tag_name=tag_name,
|
|
|
|
tag_url=tag_url,
|
|
|
|
)
|
|
|
|
template = GH_TAG_TEMPLATE if is_github_repo(repo_url) else TAG_TEMPLATE
|
|
|
|
content = template.format(**context)
|
|
|
|
else: # should never get here: unknown event
|
|
|
|
branch_name = ''
|
|
|
|
context.update(event_name=payload["revision"]["reference_type"])
|
|
|
|
content = DEFAULT_TEMPLATE.format(**context)
|
|
|
|
return content, project_name, branch_name
|
|
|
|
|
|
|
|
def is_github_repo(repo_url: str) -> bool:
|
|
|
|
return urlparse(repo_url).hostname == 'github.com'
|
2020-04-14 19:04:01 +02:00
|
|
|
|
|
|
|
def summary_line(message: str) -> str:
|
|
|
|
return message.splitlines()[0]
|