'Sync the version data in the repo (on the build server) with what we have in the database.
Returns the identifiers for the versions that have been deleted.'

@decorators.detail_route(permission_classes=[permissions.IsAdminUser], methods=['post'])
def sync_versions(self, request, **kwargs):

    project = get_object_or_404(Project.objects.api(request.user), pk=kwargs['pk'])
    old_highest_version = determine_stable_version(project.versions.all())
    if (old_highest_version is not None):
        activate_new_stable = old_highest_version.active
    else:
        activate_new_stable = False
    try:
        data = request.data
        added_versions = set()
        if ('tags' in data):
            ret_set = api_utils.sync_versions(project=project, versions=data['tags'], type=TAG)
            added_versions.update(ret_set)
        if ('branches' in data):
            ret_set = api_utils.sync_versions(project=project, versions=data['branches'], type=BRANCH)
            added_versions.update(ret_set)
        deleted_versions = api_utils.delete_versions(project, data)
    except Exception as e:
        log.exception('Sync    Versions    Error:    %s', e.message)
        return Response({'error': e.message}, status=status.HTTP_400_BAD_REQUEST)
    promoted_version = project.update_stable_version()
    if promoted_version:
        new_stable = project.get_stable_version()
        log.info('Triggering    new    stable    build:    {project}:{version}'.format(project=project.slug, version=new_stable.identifier))
        trigger_build(project=project, version=new_stable)
        if (activate_new_stable and (promoted_version.slug in added_versions)):
            promoted_version.active = True
            promoted_version.save()
            trigger_build(project=project, version=promoted_version)
    return Response({'added_versions': added_versions, 'deleted_versions': deleted_versions})
