function closestMatchingQueries()

in prow/cmd/deck/static/pr/pr.ts [1040:1138]


function closestMatchingQueries(pr: PullRequest, queries: TideQuery[]): ProcessedQuery[] {
    const prLabelsSet = new Set();
    if (pr.Labels && pr.Labels.Nodes) {
        pr.Labels.Nodes.forEach((label) => {
            prLabelsSet.add(label.Label.Name);
        });
    }
    const processedQueries: ProcessedQuery[] = [];
    queries.forEach((query) => {
        let score = 0.0;
        const labels: ProcessedLabel[] = [];
        const missingLabels: ProcessedLabel[] = [];
        (query.labels || []).sort((a, b) => {
            if (a.length === b.length) {
                return 0;
            }
            return a.length < b.length ? -1 : 1;
        });
        (query.missingLabels || []).sort((a, b) => {
            if (a.length === b.length) {
                return 0;
            }
            return a.length < b.length ? -1 : 1;
        });
        (query.labels || []).forEach((label) => {
            labels.push({name: label, own: prLabelsSet.has(label)});
            score += labels[labels.length - 1].own ? 1 : 0;
        });
        (query.missingLabels || []).forEach((label) => {
            missingLabels.push({name: label, own: prLabelsSet.has(label)});
            score += missingLabels[missingLabels.length - 1].own ? 0 : 1;
        });
        score = (labels.length + missingLabels.length > 0) ? score
            / (labels.length + missingLabels.length) : 1.0;
        processedQueries.push({
            author: query.author,
            excludedBranches: query.excludedBranches,
            includedBranches: query.includedBranches,
            labels,
            milestone: query.milestone,
            missingLabels,
            score,
        });
    });
    // Sort queries by descending score order.
    processedQueries.sort((q1, q2) => {
        if (pr.BaseRef && pr.BaseRef.Name) {
            let q1Excluded = 0;
            let q2Excluded = 0;
            if (q1.excludedBranches && q1.excludedBranches.indexOf(pr.BaseRef.Name) !== -1) {
                q1Excluded = 1;
            }
            if (q2.excludedBranches && q2.excludedBranches.indexOf(pr.BaseRef.Name) !== -1) {
                q2Excluded = -1;
            }
            if (q1Excluded + q2Excluded !== 0) {
                return q1Excluded + q2Excluded;
            }

            let q1Included = 0;
            let q2Included = 0;
            if (q1.includedBranches && q1.includedBranches.indexOf(pr.BaseRef.Name) !== -1) {
                q1Included = -1;
            }
            if (q2.includedBranches && q2.includedBranches.indexOf(pr.BaseRef.Name) !== -1) {
                q2Included = 1;
            }
            if (q1Included + q2Included !== 0) {
                return q1Included + q2Included;
            }
        }

        const prAuthor = pr.Author && normLogin(pr.Author.Login);
        const q1Author = normLogin(q1.author);
        const q2Author = normLogin(q2.author);

        if (prAuthor && q1Author !== q2Author) {
            if (q1.author && prAuthor === q1Author) {
                return -1;
            }
            if (q2.author && prAuthor === q2Author) {
                return 1;
            }
        }
        if (pr.Milestone && pr.Milestone.Title && q1.milestone !== q2.milestone) {
            if (q1.milestone && pr.Milestone.Title === q1.milestone) {
                return -1;
            }
            if (q2.milestone && pr.Milestone.Title === q2.milestone) {
                return 1;
            }
        }
        if (Math.abs(q1.score - q2.score) < Number.EPSILON) {
            return 0;
        }
        return q1.score > q2.score ? -1 : 1;
    });
    return processedQueries;
}