Exemplo n.º 1
0
def merge_pull(ticket, pullreq, user, message, request):
    from dispatches import dispatch
    from queues_handler import sphinx_builds_add
    from vilya.libs.signals import pullrequest_signal
    project = pullreq.to_proj
    # before
    # check user permission
    if not project.has_push_perm(user.name):
        return 'You do not have merge permission'
    # check if pull merged
    if pullreq.merged:
        return 'This pull was already merged'

    with MLock.merge_pull(proj_id=pullreq.to_proj.id) as err:
        if err:
            return "Merging by someone else ..."

        # if up-to-date, just close it
        if pullreq.is_up_to_date():
            content = u"Closed due to up-to-date merge"
            comment = ticket.add_comment(content, user.name)
            close_pull(ticket, pullreq, user, content, comment, request)
            return "Closed due to up-to-date merge"

        # do
        merge_commit_sha = pullreq.merge(user, message)
        if merge_commit_sha is None:
            return "Merge failed"
        # after
        ticket_id = ticket.ticket_id
        # close ticket
        ticket.close(user.name)
        # build docs
        sphinx_builds_add(project.name)
        # delete tmp branch
        pullreq.remove_branch_if_temp()

    # TODO: 重构Feed后取消这个信号的发送
    pullrequest_signal.send(user.username,
                            comment='',
                            pullreq=pullreq,
                            ticket_id=ticket_id,
                            ticket=ticket,
                            status="merged",
                            new_version=True)

    dispatch('pullreq',
             data=dict(sender=user.username,
                       comment=None,
                       ticket=ticket,
                       status='merged',
                       new_version=True))

    # remove argu: request
    dispatch('pr_actions',
             data=dict(type='pr_merge',
                       hooks=project.hooks,
                       request=request,
                       commit_message=message,
                       author=user,
                       ticket=ticket,
                       pullreq=pullreq))
    return None