Ejemplo n.º 1
0
def api_github_landing(
    request: HttpRequest,
    user_profile: UserProfile,
    event: str = REQ(),
    payload: Mapping[str, Any] = REQ(validator=check_dict([])),
    branches: str = REQ(default=''),
    stream: str = REQ(default=''),
    version: int = REQ(converter=to_non_negative_int, default=1),
    commit_stream: str = REQ(default=''),
    issue_stream: str = REQ(default=''),
    exclude_pull_requests: bool = REQ(converter=flexible_boolean,
                                      default=False),
    exclude_issues: bool = REQ(converter=flexible_boolean, default=False),
    exclude_commits: bool = REQ(converter=flexible_boolean, default=False),
    emphasize_branch_in_topic: bool = REQ(converter=flexible_boolean,
                                          default=False),
) -> HttpResponse:

    repository = payload['repository']

    # Special hook for capturing event data. If we see our special test repo, log the payload from github.
    try:
        if is_test_repository(repository) and settings.PRODUCTION:
            with open('/var/log/zulip/github-payloads', 'a') as f:
                f.write(
                    ujson.dumps({
                        'event':
                        event,
                        'payload':
                        payload,
                        'branches':
                        branches,
                        'stream':
                        stream,
                        'version':
                        version,
                        'commit_stream':
                        commit_stream,
                        'issue_stream':
                        issue_stream,
                        'exclude_pull_requests':
                        exclude_pull_requests,
                        'exclude_issues':
                        exclude_issues,
                        'exclude_commits':
                        exclude_commits,
                        'emphasize_branch_in_topic':
                        emphasize_branch_in_topic,
                    }))
                f.write('\n')
    except Exception:
        logging.exception('Error while capturing Github event')

    if not stream:
        stream = 'commits'

    short_ref = re.sub(r'^refs/heads/', '', payload.get('ref', ''))
    kwargs = dict()

    if emphasize_branch_in_topic and short_ref:
        kwargs['topic_focus'] = short_ref

    allowed_events = set()
    if not exclude_pull_requests:
        allowed_events.add('pull_request')

    if not exclude_issues:
        allowed_events.add('issues')
        allowed_events.add('issue_comment')

    if not exclude_commits:
        allowed_events.add('push')
        allowed_events.add('commit_comment')

    if event not in allowed_events:
        return json_success()

    # We filter issue_comment events for issue creation events
    if event == 'issue_comment' and payload['action'] != 'created':
        return json_success()

    if event == 'push':
        # If we are given a whitelist of branches, then we silently ignore
        # any push notification on a branch that is not in our whitelist.
        if branches and short_ref not in re.split(r'[\s,;|]+', branches):
            return json_success()

    # Map payload to the handler with the right version
    if version == 2:
        target_stream, subject, content = api_github_v2(
            user_profile, event, payload, branches, stream, commit_stream,
            issue_stream, **kwargs)
    else:
        target_stream, subject, content = api_github_v1(
            user_profile, event, payload, branches, stream, **kwargs)

    request.client = get_client('ZulipGitHubLegacyWebhook')
    return send_message_backend(request,
                                user_profile,
                                message_type_name='stream',
                                message_to=[target_stream],
                                forged=False,
                                topic_name=subject,
                                message_content=content)
Ejemplo n.º 2
0
def api_github_landing(
    request,
    user_profile,
    event=REQ(),
    payload=REQ(validator=check_dict([])),
    branches=REQ(default=""),
    stream=REQ(default=""),
    version=REQ(converter=to_non_negative_int, default=1),
    commit_stream=REQ(default=""),
    issue_stream=REQ(default=""),
    exclude_pull_requests=REQ(converter=flexible_boolean, default=False),
    exclude_issues=REQ(converter=flexible_boolean, default=False),
    exclude_commits=REQ(converter=flexible_boolean, default=False),
    emphasize_branch_in_topic=REQ(converter=flexible_boolean, default=False),
):
    # type: (HttpRequest, UserProfile, text_type, Mapping[text_type, Any], text_type, text_type, int, text_type, text_type, bool, bool, bool, bool) -> HttpResponse

    repository = payload["repository"]

    # Special hook for capturing event data. If we see our special test repo, log the payload from github.
    try:
        if is_test_repository(repository) and settings.PRODUCTION:
            with open("/var/log/zulip/github-payloads", "a") as f:
                f.write(
                    ujson.dumps(
                        {
                            "event": event,
                            "payload": payload,
                            "branches": branches,
                            "stream": stream,
                            "version": version,
                            "commit_stream": commit_stream,
                            "issue_stream": issue_stream,
                            "exclude_pull_requests": exclude_pull_requests,
                            "exclude_issues": exclude_issues,
                            "exclude_commits": exclude_commits,
                            "emphasize_branch_in_topic": emphasize_branch_in_topic,
                        }
                    )
                )
                f.write("\n")
    except Exception:
        logging.exception("Error while capturing Github event")

    if not stream:
        stream = "commits"

    short_ref = re.sub(r"^refs/heads/", "", payload.get("ref", ""))
    kwargs = dict()

    if emphasize_branch_in_topic and short_ref:
        kwargs["topic_focus"] = short_ref

    allowed_events = set()
    if not exclude_pull_requests:
        allowed_events.add("pull_request")

    if not exclude_issues:
        allowed_events.add("issues")
        allowed_events.add("issue_comment")

    if not exclude_commits:
        allowed_events.add("push")
        allowed_events.add("commit_comment")

    if event not in allowed_events:
        return json_success()

    # We filter issue_comment events for issue creation events
    if event == "issue_comment" and payload["action"] != "created":
        return json_success()

    if event == "push":
        # If we are given a whitelist of branches, then we silently ignore
        # any push notification on a branch that is not in our whitelist.
        if branches and short_ref not in re.split("[\s,;|]+", branches):
            return json_success()

    # Map payload to the handler with the right version
    if version == 2:
        target_stream, subject, content = api_github_v2(
            user_profile, event, payload, branches, stream, commit_stream, issue_stream, **kwargs
        )
    else:
        target_stream, subject, content = api_github_v1(user_profile, event, payload, branches, stream, **kwargs)

    request.client = get_client("ZulipGitHubWebhook")
    return send_message_backend(
        request,
        user_profile,
        message_type_name="stream",
        message_to=[target_stream],
        forged=False,
        subject_name=subject,
        message_content=content,
    )
Ejemplo n.º 3
0
def api_github_landing(request, user_profile, event=REQ,
                       payload=REQ(validator=check_dict([])),
                       branches=REQ(default=''),
                       stream=REQ(default=''),
                       version=REQ(converter=to_non_negative_int, default=1),
                       commit_stream=REQ(default=''),
                       issue_stream=REQ(default=''),
                       exclude_pull_requests=REQ(converter=flexible_boolean, default=False),
                       exclude_issues=REQ(converter=flexible_boolean, default=False),
                       exclude_commits=REQ(converter=flexible_boolean, default=False),
                       emphasize_branch_in_topic=REQ(converter=flexible_boolean, default=False),
                       ):

    repository = payload['repository']

    # Special hook for capturing event data. If we see our special test repo, log the payload from github.
    try:
        if repository['name'] == 'zulip-test' and repository['id'] == 6893087 and settings.PRODUCTION:
            with open('/var/log/zulip/github-payloads', 'a') as f:
                f.write(ujson.dumps({'event': event,
                                     'payload': payload,
                                     'branches': branches,
                                     'stream': stream,
                                     'version': version,
                                     'commit_stream': commit_stream,
                                     'issue_stream': issue_stream,
                                     'exclude_pull_requests': exclude_pull_requests,
                                     'exclude_issues': exclude_issues,
                                     'exclude_commits': exclude_commits,
                                     'emphasize_branch_in_topic': emphasize_branch_in_topic,
                                     }))
                f.write("\n")
    except Exception:
        logging.exception("Error while capturing Github event")

    if not stream:
        stream = 'commits'

    short_ref = re.sub(r'^refs/heads/', '', payload.get('ref', ""))
    kwargs = dict()

    if emphasize_branch_in_topic and short_ref:
        kwargs['topic_focus'] = short_ref

    allowed_events = set()
    if not exclude_pull_requests:
        allowed_events.add('pull_request')

    if not exclude_issues:
        allowed_events.add("issues")
        allowed_events.add("issue_comment")

    if not exclude_commits:
        allowed_events.add("push")
        allowed_events.add("commit_comment")

    if event not in allowed_events:
        return json_success()

    # We filter issue_comment events for issue creation events
    if event == 'issue_comment' and payload['action'] != 'created':
        return json_success()

    if event == 'push':
        # If we are given a whitelist of branches, then we silently ignore
        # any push notification on a branch that is not in our whitelist.
        if branches and short_ref not in re.split('[\s,;|]+', branches):
            return json_success()

    # Map payload to the handler with the right version
    if version == 2:
        target_stream, subject, content = api_github_v2(user_profile, event, payload, branches, stream, commit_stream, issue_stream, **kwargs)
    else:
        target_stream, subject, content = api_github_v1(user_profile, event, payload, branches, stream, **kwargs)

    request.client = get_client("ZulipGitHubWebhook")
    return send_message_backend(request, user_profile,
                                message_type_name="stream",
                                message_to=[target_stream],
                                forged=False, subject_name=subject,
                                message_content=content)