2016-03-13 13:43:34 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from __future__ import absolute_import
|
2016-05-25 15:02:02 +02:00
|
|
|
|
2016-06-06 00:00:13 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-05-25 15:02:02 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2016-03-13 13:43:34 +01:00
|
|
|
from zerver.models import get_client
|
|
|
|
from zerver.lib.actions import check_send_message
|
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.decorator import REQ, has_request_variables, authenticated_rest_api_view
|
2016-06-06 00:00:13 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-03-13 13:43:34 +01:00
|
|
|
import ujson
|
|
|
|
|
2016-12-04 18:45:46 +01:00
|
|
|
from typing import Any, Dict, Text
|
2016-03-13 13:43:34 +01:00
|
|
|
|
|
|
|
|
2016-05-18 20:35:35 +02:00
|
|
|
@authenticated_rest_api_view(is_webhook=True)
|
2016-03-13 13:43:34 +01:00
|
|
|
@has_request_variables
|
2016-05-07 20:07:48 +02:00
|
|
|
def api_stash_webhook(request, user_profile, payload=REQ(argument_type='body'),
|
|
|
|
stream=REQ(default='commits')):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Dict[str, Any], Text) -> HttpResponse
|
2016-03-13 13:43:34 +01:00
|
|
|
# We don't get who did the push, or we'd try to report that.
|
|
|
|
try:
|
|
|
|
repo_name = payload["repository"]["name"]
|
|
|
|
project_name = payload["repository"]["project"]["name"]
|
|
|
|
branch_name = payload["refChanges"][0]["refId"].split("/")[-1]
|
|
|
|
commit_entries = payload["changesets"]["values"]
|
|
|
|
commits = [(entry["toCommit"]["displayId"],
|
2016-12-03 18:07:49 +01:00
|
|
|
entry["toCommit"]["message"].split("\n")[0]) for
|
2016-11-30 14:17:35 +01:00
|
|
|
entry in commit_entries]
|
2016-03-13 13:43:34 +01:00
|
|
|
head_ref = commit_entries[-1]["toCommit"]["displayId"]
|
|
|
|
except KeyError as e:
|
2016-07-13 17:09:49 +02:00
|
|
|
return json_error(_("Missing key %s in JSON") % (str(e),))
|
2016-03-13 13:43:34 +01:00
|
|
|
|
|
|
|
subject = "%s/%s: %s" % (project_name, repo_name, branch_name)
|
|
|
|
|
|
|
|
content = "`%s` was pushed to **%s** in **%s/%s** with:\n\n" % (
|
|
|
|
head_ref, branch_name, project_name, repo_name)
|
|
|
|
content += "\n".join("* `%s`: %s" % (
|
2017-01-24 07:06:13 +01:00
|
|
|
commit[0], commit[1]) for commit in commits)
|
2016-03-13 13:43:34 +01:00
|
|
|
|
|
|
|
check_send_message(user_profile, get_client("ZulipStashWebhook"), "stream",
|
|
|
|
[stream], subject, content)
|
|
|
|
return json_success()
|