2017-01-28 18:42:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
2017-09-30 04:18:16 +02:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
2017-01-28 18:42:59 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-01-28 18:42:59 +01:00
|
|
|
from zerver.lib.webhooks.git import get_push_commits_event_message, \
|
|
|
|
get_pull_request_event_message, get_create_branch_event_message, \
|
|
|
|
SUBJECT_WITH_BRANCH_TEMPLATE, SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.models import UserProfile
|
2017-01-28 18:42:59 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from typing import Dict, Any, Iterable, Optional, Text
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def format_push_event(payload: Dict[str, Any]) -> Text:
|
2017-01-28 18:42:59 +01:00
|
|
|
|
|
|
|
for commit in payload['commits']:
|
|
|
|
commit['sha'] = commit['id']
|
2017-04-26 02:57:47 +02:00
|
|
|
commit['name'] = (commit['author']['username'] or
|
|
|
|
commit['author']['name'].split()[0])
|
2017-01-28 18:42:59 +01:00
|
|
|
|
|
|
|
data = {
|
|
|
|
'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)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def format_new_branch_event(payload: Dict[str, Any]) -> Text:
|
2017-01-28 18:42:59 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return get_create_branch_event_message(**data)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def format_pull_request_event(payload: Dict[str, Any]) -> Text:
|
2017-01-28 18:42:59 +01:00
|
|
|
|
|
|
|
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'],
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload['pull_request']['merged']:
|
|
|
|
data['user_name'] = payload['pull_request']['merged_by']['username']
|
|
|
|
data['action'] = 'merged'
|
|
|
|
|
|
|
|
return get_pull_request_event_message(**data)
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Gogs')
|
|
|
|
@has_request_variables
|
2017-05-02 01:00:50 +02:00
|
|
|
def api_gogs_webhook(request, user_profile,
|
2017-01-28 18:42:59 +01:00
|
|
|
payload=REQ(argument_type='body'),
|
2017-04-06 23:26:29 +02:00
|
|
|
stream=REQ(default='commits'),
|
|
|
|
branches=REQ(default=None)):
|
2017-05-02 01:00:50 +02:00
|
|
|
# type: (HttpRequest, UserProfile, Dict[str, Any], Text, Optional[Text]) -> HttpResponse
|
2017-01-28 18:42:59 +01:00
|
|
|
|
|
|
|
repo = payload['repository']['name']
|
|
|
|
event = request.META['HTTP_X_GOGS_EVENT']
|
2017-08-24 17:31:04 +02:00
|
|
|
if event == 'push':
|
|
|
|
branch = payload['ref'].replace('refs/heads/', '')
|
|
|
|
if branches is not None and branches.find(branch) == -1:
|
|
|
|
return json_success()
|
|
|
|
body = format_push_event(payload)
|
|
|
|
topic = SUBJECT_WITH_BRANCH_TEMPLATE.format(
|
|
|
|
repo=repo,
|
|
|
|
branch=branch
|
|
|
|
)
|
|
|
|
elif event == 'create':
|
|
|
|
body = format_new_branch_event(payload)
|
|
|
|
topic = SUBJECT_WITH_BRANCH_TEMPLATE.format(
|
|
|
|
repo=repo,
|
|
|
|
branch=payload['ref']
|
|
|
|
)
|
|
|
|
elif event == 'pull_request':
|
|
|
|
body = format_pull_request_event(payload)
|
|
|
|
topic = SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
|
|
|
repo=repo,
|
|
|
|
type='PR',
|
|
|
|
id=payload['pull_request']['id'],
|
|
|
|
title=payload['pull_request']['title']
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return json_error(_('Invalid event "{}" in request headers').format(event))
|
2017-01-28 18:42:59 +01:00
|
|
|
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, request.client, stream, topic, body)
|
2017-01-28 18:42:59 +01:00
|
|
|
return json_success()
|