Ejemplo n.º 1
0
def parseReviewFilters(db, data):
    reviewfilters = []

    for filter_data in data:
        filter_username = filter_data["username"]
        filter_type = filter_data["type"]
        filter_path = filter_data["path"]

        filter_user = dbutils.User.fromName(db, filter_username)
        if not filter_user:
            raise OperationError("no such user: '******'" % filter_username)

        filter_directory_id = 0
        filter_file_id = 0

        if filter_path != "/":
            if filter_path[-1] == "/" or dbutils.is_directory(filter_path):
                filter_directory_id = dbutils.find_directory(db,
                                                             path=filter_path)
            else:
                filter_file_id = dbutils.find_file(db, path=filter_path)

        reviewfilters.append((filter_directory_id, filter_file_id, filter_type,
                              None, filter_user.id))

    return reviewfilters
Ejemplo n.º 2
0
    def process(self, db, creator, review_id, filters):
        review = dbutils.Review.fromId(db, review_id)
        by_user = {}

        for filter in filters:
            if "user_ids" in filter:
                user_ids = set(filter["user_ids"])
            else:
                user_ids = set([])

            if "user_names" in filter:
                for user_name in filter["user_names"]:
                    user_ids.add(dbutils.User.fromName(db, user_name).id)

            if "directory_ids" in filter:
                directory_ids = set(filter["directory_ids"])
            else:
                directory_ids = set([])

            if "file_ids" in filter:
                file_ids = set(filter["file_ids"])
            else:
                file_ids = set([])

            if "paths" in filter:
                for path in filter["paths"]:
                    if not path or path == "/":
                        directory_ids.add(0)
                    elif path.endswith("/") or dbutils.is_directory(
                            db, path) or ("." not in path.split("/")[-1]
                                          and not dbutils.is_file(db, path)):
                        directory_ids.add(dbutils.find_directory(db, path))
                    else:
                        file_ids.add(dbutils.find_file(db, path))

            for user_id in user_ids:
                reviewer_directory_ids, reviewer_file_ids, watcher_directory_ids, watcher_file_ids = by_user.setdefault(
                    user_id, (set(), set(), set(), set()))

                if filter["type"] == "reviewer":
                    reviewer_directory_ids |= directory_ids
                    reviewer_file_ids |= file_ids
                else:
                    watcher_directory_ids |= directory_ids
                    watcher_file_ids |= file_ids

        pending_mails = []

        for user_id, args in by_user.items():
            user = dbutils.User.fromId(db, user_id)
            pending_mails.extend(
                review_utils.addReviewFilters(db, creator, user, review,
                                              *args))

        db.commit()

        mailutils.sendPendingMails(pending_mails)

        return OperationResult()
Ejemplo n.º 3
0
def showfilters(req, db, user):
    user = dbutils.User.fromName(db, req.getParameter("user", req.user))
    path = req.getParameter("path")
    repository = gitutils.Repository.fromParameter(db, req.getParameter("repository", user.getPreference(db, "defaultRepository")))

    path = path.rstrip("/")

    if dbutils.is_directory(db, path):
        directory_id = dbutils.find_directory(db, path=path)
        show_path = path + "/"

        cursor = db.cursor()
        cursor.execute("SELECT name FROM files WHERE directory=%s ORDER BY id ASC LIMIT 1", (directory_id,))

        row = cursor.fetchone()
        if row: path += "/" + row[0]
        else: path += "/dummy.txt"
    else:
        show_path = path

    file_id = dbutils.find_file(db, path=path)

    filters = review_filters.Filters()
    filters.load(db, repository=repository, recursive=True)

    reviewers = []
    watchers = []

    for user_id, (filter_type, _delegate) in filters.listUsers(db, file_id).items():
        if filter_type == 'reviewer': reviewers.append(user_id)
        else: watchers.append(user_id)

    result = "Path: %s\n" % show_path

    reviewers_found = False
    watchers_found = False

    for reviewer_id in sorted(reviewers):
        if not reviewers_found:
            result += "\nReviewers:\n"
            reviewers_found = True

        reviewer = dbutils.User.fromId(db, reviewer_id)
        result += "  %s <%s>\n" % (reviewer.fullname, reviewer.email)

    for watcher_id in sorted(watchers):
        if not watchers_found:
            result += "\nWatchers:\n"
            watchers_found = True

        watcher = dbutils.User.fromId(db, watcher_id)
        result += "  %s <%s>\n" % (watcher.fullname, watcher.email)

    if not reviewers_found and not watchers_found:
        result += "\nNo matching filters found.\n"

    return result
Ejemplo n.º 4
0
    def process(self, db, creator, review_id, filters):
        review = dbutils.Review.fromId(db, review_id)
        by_user = {}

        for filter in filters:
            if "user_ids" in filter:
                user_ids = set(filter["user_ids"])
            else:
                user_ids = set([])

            if "user_names" in filter:
                for user_name in filter["user_names"]:
                    user_ids.add(dbutils.User.fromName(db, user_name).id)

            if "directory_ids" in filter:
                directory_ids = set(filter["directory_ids"])
            else:
                directory_ids = set([])

            if "file_ids" in filter:
                file_ids = set(filter["file_ids"])
            else:
                file_ids = set([])

            if "paths" in filter:
                for path in filter["paths"]:
                    if not path or path == "/":
                        directory_ids.add(0)
                    elif path.endswith("/") or dbutils.is_directory(db, path) or ("." not in path.split("/")[-1] and not dbutils.is_file(db, path)):
                        directory_ids.add(dbutils.find_directory(db, path))
                    else:
                        file_ids.add(dbutils.find_file(db, path))

            for user_id in user_ids:
                reviewer_directory_ids, reviewer_file_ids, watcher_directory_ids, watcher_file_ids = by_user.setdefault(user_id, (set(), set(), set(), set()))

                if filter["type"] == "reviewer":
                    reviewer_directory_ids |= directory_ids
                    reviewer_file_ids |= file_ids
                else:
                    watcher_directory_ids |= directory_ids
                    watcher_file_ids |= file_ids

        pending_mails = []

        for user_id, args in by_user.items():
            user = dbutils.User.fromId(db, user_id)
            pending_mails.extend(review_utils.addReviewFilters(db, creator, user, review, *args))

        db.commit()

        mailutils.sendPendingMails(pending_mails)

        return OperationResult()
Ejemplo n.º 5
0
def addfilter(req, db, user):
    if user.isAnonymous(): return OperationFailureMustLogin()

    cursor = db.cursor()

    repository_id = req.getParameter("repository", filter=int)
    filter_type = req.getParameter("type")
    filter_path = req.getParameter("path")
    filter_delegate = req.getParameter("delegate", "")
    force = req.getParameter("force", "no") == "yes"

    repository = gitutils.Repository.fromId(db, repository_id)

    if filter_delegate:
        invalid_users = []
        for delegate_name in map(str.strip, filter_delegate.split(',')):
            if dbutils.User.fromName(db, delegate_name) is None:
                invalid_users.append(delegate_name)
        if invalid_users: return "error:invalid-users:%s" % ','.join(invalid_users)

    if filter_path == '/':
        directory_id, file_id = 0, 0
    elif filter_path[-1] == '/':
        directory_id, file_id = dbutils.find_directory(db, path=filter_path[:-1]), 0
    else:
        if not force and dbutils.is_directory(db, filter_path): return "error:directory"
        else: directory_id, file_id = dbutils.find_directory_file(db, filter_path)

    if directory_id:
        specificity = len(dbutils.explode_path(db, directory_id=directory_id))
        if file_id: specificity += 1
    else:
        specificity = 0

    cursor.execute("INSERT INTO filters (uid, repository, directory, file, specificity, type, delegate) VALUES (%s, %s, %s, %s, %s, %s, %s)", [user.id, repository.id, directory_id, file_id, specificity, filter_type, ','.join(map(str.strip, filter_delegate.split(',')))])
    user.setPreference(db, "email.activated", True)

    db.commit()

    return "ok:directory=%d,file=%d" % (directory_id, file_id)
Ejemplo n.º 6
0
def parseReviewFilters(db, data):
    reviewfilters = []

    for filter_data in data:
        filter_username = filter_data["username"]
        filter_type = filter_data["type"]
        filter_path = filter_data["path"]

        filter_user = dbutils.User.fromName(db, filter_username)
        if not filter_user:
            raise OperationError("no such user: '******'" % filter_username)

        filter_directory_id = 0
        filter_file_id = 0

        if filter_path != "/":
            if filter_path[-1] == "/" or dbutils.is_directory(filter_path):
                filter_directory_id = dbutils.find_directory(db, path=filter_path)
            else:
                filter_file_id = dbutils.find_file(db, path=filter_path)

        reviewfilters.append((filter_directory_id, filter_file_id, filter_type, None, filter_user.id))

    return reviewfilters