def api_gogs_webhook(request: HttpRequest, user_profile: UserProfile, payload: Dict[str, Any]=REQ(argument_type='body'), branches: Optional[str]=REQ(default=None)) -> HttpResponse: repo = payload['repository']['name'] event = validate_extract_webhook_http_header(request, 'X_GOGS_EVENT', 'Gogs') 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: raise UnexpectedWebhookEventType('Gogs', event) check_send_webhook_message(request, user_profile, topic, body) return json_success()
def get_subject_based_on_type(payload, event): # type: (Dict[str, Any], Text) -> Text if 'pull_request' in event: return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format( repo=get_repository_name(payload), type='PR', id=payload['pull_request']['number'], title=payload['pull_request']['title']) elif event.startswith('issue'): return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format( repo=get_repository_name(payload), type='Issue', id=payload['issue']['number'], title=payload['issue']['title']) elif event.startswith('deployment'): return u"{} / Deployment on {}".format( get_repository_name(payload), payload['deployment']['environment']) elif event == 'membership': return u"{} organization".format(payload['organization']['login']) elif event == 'push_commits': return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload), branch=get_branch_name_from_ref(payload['ref'])) elif event == 'gollum': return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload), branch='Wiki Pages') elif event == 'ping': if payload.get('repository') is None: return get_organization_name(payload) return get_repository_name(payload)
def api_gogs_webhook(request, user_profile, client, payload=REQ(argument_type='body'), stream=REQ(default='commits')): # type: (HttpRequest, UserProfile, Client, Dict[str, Any], Text) -> HttpResponse repo = payload['repository']['name'] event = request.META['HTTP_X_GOGS_EVENT'] try: if event == 'push': body = format_push_event(payload) topic = SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=repo, branch=payload['ref'].replace('refs/heads/', '')) 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)) except KeyError as e: return json_error(_('Missing key {} in JSON').format(str(e))) check_send_message(user_profile, client, 'stream', [stream], topic, body) return json_success()
def api_gogs_webhook(request: HttpRequest, user_profile: UserProfile, payload: Dict[str, Any]=REQ(argument_type='body'), stream: Text=REQ(default='commits'), branches: Optional[Text]=REQ(default=None)) -> HttpResponse: repo = payload['repository']['name'] event = request.META['HTTP_X_GOGS_EVENT'] 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)) check_send_stream_message(user_profile, request.client, stream, topic, body) return json_success()
def get_subject_based_on_type(payload, event): # type: (Dict[str, Any], Text) -> Text if 'pull_request' in event: return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format( repo=get_repository_name(payload), type='PR', id=payload['pull_request']['number'], title=payload['pull_request']['title'] ) elif event.startswith('issue'): return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format( repo=get_repository_name(payload), type='Issue', id=payload['issue']['number'], title=payload['issue']['title'] ) elif event.startswith('deployment'): return u"{} / Deployment on {}".format( get_repository_name(payload), payload['deployment']['environment'] ) elif event == 'membership': return u"{} organization".format(payload['organization']['login']) elif event == 'push_commits': return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload), branch=get_branch_name_from_ref(payload['ref']) ) elif event == 'gollum': return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload), branch='Wiki Pages' ) return get_repository_name(payload)
def api_gogs_webhook(request, user_profile, payload=REQ(argument_type='body'), stream=REQ(default='commits'), branches=REQ(default=None)): # type: (HttpRequest, UserProfile, Dict[str, Any], Text, Optional[Text]) -> HttpResponse repo = payload['repository']['name'] event = request.META['HTTP_X_GOGS_EVENT'] 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)) check_send_stream_message(user_profile, request.client, stream, topic, body) return json_success()
def api_gogs_webhook( request: HttpRequest, user_profile: UserProfile, payload: Dict[str, Any] = REQ(argument_type='body'), branches: Optional[str] = REQ(default=None) ) -> HttpResponse: repo = payload['repository']['name'] event = validate_extract_webhook_http_header(request, 'X_GOGS_EVENT', 'Gogs') 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: raise UnexpectedWebhookEventType('Gogs', event) check_send_webhook_message(request, user_profile, topic, body) return json_success()
def api_bitbucket_webhook(request, user_profile, payload=REQ(validator=check_dict([])), stream=REQ(default='commits')): # type: (HttpRequest, UserProfile, Mapping[text_type, Any], text_type) -> HttpResponse repository = payload['repository'] commits = [{ 'sha': commit.get('raw_node'), 'message': commit.get('message'), 'url': u'{}{}commits/{}'.format(payload.get('canon_url'), repository.get('absolute_url'), commit.get('raw_node')) } for commit in payload.get('commits')] if len(commits) == 0: # Bitbucket doesn't give us enough information to really give # a useful message :/ subject = repository['name'] content = (u"%s [force pushed](%s)" % (payload['user'], payload['canon_url'] + repository['absolute_url'])) else: branch = payload['commits'][-1]['branch'] content = get_push_commits_event_message(payload.get('user'), None, branch, commits) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch) check_send_message(user_profile, get_client("ZulipBitBucketWebhook"), "stream", [stream], subject, content) return json_success()
def build_message_from_gitlog(user_profile, name, ref, commits, before, after, url, pusher, forced=None, created=None, deleted=False): # type: (UserProfile, Text, Text, List[Dict[str, str]], Text, Text, Text, Text, Optional[Text], Optional[Text], Optional[bool]) -> Tuple[Text, Text] short_ref = re.sub(r'^refs/heads/', '', ref) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref) if re.match(r'^0+$', after): content = get_remove_branch_event_message(pusher, short_ref) # 'created' and 'forced' are github flags; the second check is for beanstalk elif (forced and not created) or (forced is None and len(commits) == 0): content = get_force_push_commits_event_message(pusher, url, short_ref, after[:7]) else: commits = _transform_commits_list_to_common_format(commits) content = get_push_commits_event_message(pusher, url, short_ref, commits, deleted=deleted) return subject, content
def get_subject_for_branch_specified_events(payload: Dict[str, Any], branch_name: Optional[str] = None ) -> str: return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload['repository']), branch=get_branch_name_for_push_event(payload) if branch_name is None else branch_name)
def api_bitbucket_webhook(request: HttpRequest, user_profile: UserProfile, payload: Mapping[str, Any]=REQ(validator=check_dict([])), branches: Optional[str]=REQ(default=None)) -> HttpResponse: repository = payload['repository'] commits = [ { 'name': payload.get('user'), 'sha': commit.get('raw_node'), 'message': commit.get('message'), 'url': u'{}{}commits/{}'.format( payload.get('canon_url'), repository.get('absolute_url'), commit.get('raw_node')) } for commit in payload['commits'] ] if len(commits) == 0: # Bitbucket doesn't give us enough information to really give # a useful message :/ subject = repository['name'] content = (u"%s [force pushed](%s)" % (payload['user'], payload['canon_url'] + repository['absolute_url'])) else: branch = payload['commits'][-1]['branch'] if branches is not None and branches.find(branch) == -1: return json_success() content = get_push_commits_event_message(payload['user'], None, branch, commits) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch) check_send_webhook_message(request, user_profile, subject, content) return json_success()
def build_message_from_gitlog( user_profile: UserProfile, name: str, ref: str, commits: List[Dict[str, str]], before: str, after: str, url: str, pusher: str, forced: Optional[str] = None, created: Optional[str] = None, deleted: Optional[bool] = False) -> Tuple[str, str]: short_ref = re.sub(r'^refs/heads/', '', ref) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref) if re.match(r'^0+$', after): content = get_remove_branch_event_message(pusher, short_ref) # 'created' and 'forced' are github flags; the second check is for beanstalk elif (forced and not created) or (forced is None and len(commits) == 0): content = get_force_push_commits_event_message(pusher, url, short_ref, after[:7]) else: commits = _transform_commits_list_to_common_format(commits) try: content = get_push_commits_event_message(pusher, url, short_ref, commits, deleted=deleted) except TypeError: # nocoverage This error condition seems to # be caused by a change in GitHub's APIs. Since we've # deprecated this webhook, just suppress them with a 40x error. raise JsonableError("Malformed commit data") return subject, content
def api_bitbucket_webhook(request, user_profile, payload=REQ(validator=check_dict([])), stream=REQ(default='commits')): # type: (HttpRequest, UserProfile, Mapping[Text, Any], Text) -> HttpResponse repository = payload['repository'] commits = [ { 'sha': commit.get('raw_node'), 'message': commit.get('message'), 'url': u'{}{}commits/{}'.format( payload.get('canon_url'), repository.get('absolute_url'), commit.get('raw_node')) } for commit in payload.get('commits') ] if len(commits) == 0: # Bitbucket doesn't give us enough information to really give # a useful message :/ subject = repository['name'] content = (u"%s [force pushed](%s)" % (payload['user'], payload['canon_url'] + repository['absolute_url'])) else: branch = payload['commits'][-1]['branch'] content = get_push_commits_event_message(payload.get('user'), None, branch, commits) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch) check_send_message(user_profile, get_client("ZulipBitBucketWebhook"), "stream", [stream], subject, content) return json_success()
def api_gogs_webhook(request, user_profile, client, payload=REQ(argument_type='body'), stream=REQ(default='commits')): # type: (HttpRequest, UserProfile, Client, Dict[str, Any], Text) -> HttpResponse repo = payload['repository']['name'] event = request.META['HTTP_X_GOGS_EVENT'] try: if event == 'push': body = format_push_event(payload) topic = SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=repo, branch=payload['ref'].replace('refs/heads/', '') ) 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)) except KeyError as e: return json_error(_('Missing key {} in JSON').format(str(e))) check_send_message(user_profile, client, 'stream', [stream], topic, body) return json_success()
def build_message_from_gitlog(user_profile, name, ref, commits, before, after, url, pusher, forced=None, created=None): # type: (UserProfile, text_type, text_type, List[Dict[str, str]], text_type, text_type, text_type, text_type, Optional[text_type], Optional[text_type]) -> Tuple[text_type, text_type] short_ref = re.sub(r"^refs/heads/", "", ref) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref) if re.match(r"^0+$", after): content = get_remove_branch_event_message(pusher, short_ref) # 'created' and 'forced' are github flags; the second check is for beanstalk elif (forced and not created) or (forced is None and len(commits) == 0): content = get_force_push_commits_event_message(pusher, url, short_ref, after[:7]) else: commits = _transform_commits_list_to_common_format(commits) content = get_push_commits_event_message(pusher, url, short_ref, commits) return subject, content
def build_message_from_gitlog(user_profile: UserProfile, name: Text, ref: Text, commits: List[Dict[str, str]], before: Text, after: Text, url: Text, pusher: Text, forced: Optional[Text]=None, created: Optional[Text]=None, deleted: Optional[bool]=False ) -> Tuple[Text, Text]: short_ref = re.sub(r'^refs/heads/', '', ref) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref) if re.match(r'^0+$', after): content = get_remove_branch_event_message(pusher, short_ref) # 'created' and 'forced' are github flags; the second check is for beanstalk elif (forced and not created) or (forced is None and len(commits) == 0): content = get_force_push_commits_event_message(pusher, url, short_ref, after[:7]) else: commits = _transform_commits_list_to_common_format(commits) content = get_push_commits_event_message(pusher, url, short_ref, commits, deleted=deleted) return subject, content
def api_bitbucket_webhook( request: HttpRequest, user_profile: UserProfile, payload: Mapping[Text, Any] = REQ(validator=check_dict([])), stream: Text = REQ(default='commits'), branches: Optional[Text] = REQ(default=None) ) -> HttpResponse: repository = payload['repository'] commits = [{ 'name': payload.get('user'), 'sha': commit.get('raw_node'), 'message': commit.get('message'), 'url': u'{}{}commits/{}'.format(payload.get('canon_url'), repository.get('absolute_url'), commit.get('raw_node')) } for commit in payload['commits']] if len(commits) == 0: # Bitbucket doesn't give us enough information to really give # a useful message :/ subject = repository['name'] content = (u"%s [force pushed](%s)" % (payload['user'], payload['canon_url'] + repository['absolute_url'])) else: branch = payload['commits'][-1]['branch'] if branches is not None and branches.find(branch) == -1: return json_success() content = get_push_commits_event_message(payload['user'], None, branch, commits) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch) check_send_stream_message(user_profile, get_client("ZulipBitBucketWebhook"), stream, subject, content) return json_success()
def build_message_from_gitlog(user_profile: UserProfile, name: str, ref: str, commits: List[Dict[str, str]], before: str, after: str, url: str, pusher: str, forced: Optional[str]=None, created: Optional[str]=None, deleted: Optional[bool]=False ) -> Tuple[str, str]: short_ref = re.sub(r'^refs/heads/', '', ref) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref) if re.match(r'^0+$', after): content = get_remove_branch_event_message(pusher, short_ref) # 'created' and 'forced' are github flags; the second check is for beanstalk elif (forced and not created) or (forced is None and len(commits) == 0): content = get_force_push_commits_event_message(pusher, url, short_ref, after[:7]) else: commits = _transform_commits_list_to_common_format(commits) try: content = get_push_commits_event_message(pusher, url, short_ref, commits, deleted=deleted) except TypeError: # nocoverage This error condition seems to # be caused by a change in GitHub's APIs. Since we've # deprecated this webhook, just suppress them with a 40x error. raise JsonableError( "Malformed commit data") return subject, content
def get_subject_for_branch_specified_events(payload): # type: (Dict[str, Any]) -> text_type return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload['repository']), branch=get_branch_name_for_push_event(payload) )
def get_subject_for_branch_specified_events(payload, branch_name=None): # type: (Dict[str, Any], Optional[Text]) -> Text return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload['repository']), branch=get_branch_name_for_push_event(payload) if branch_name is None else branch_name )
def get_subject_for_branch_specified_events(payload: Dict[str, Any], branch_name: Optional[str]=None) -> str: return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload['repository']), branch=get_branch_name_for_push_event(payload) if branch_name is None else branch_name )
def get_subject_for_branch_specified_events(payload, branch_name=None): # type: (Dict[str, Any], Optional[text_type]) -> text_type return SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload['repository']), branch=get_branch_name_for_push_event(payload) if branch_name is None else branch_name)