"use strict";
/*
 * Copyright © 2019 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApproveGoalIfWarnComments = exports.FailGoalIfWarnComments = exports.ApproveGoalIfErrorComments = exports.FailGoalIfErrorComments = exports.severityReviewListener = void 0;
const PushImpactListenerRegistration_1 = require("../../../api/registration/PushImpactListenerRegistration");
/**
 * Return review listener function that returns `pir` if there are any
 * review comments with the provided severity.  The method will short
 * circuit once any comment with the provided severity is found.
 *
 * @param pir push impact response to return if there are comments
 *            with provided severities
 * @param severity severity that should trigger a return of `pir`,
 *                 "error" by default
 * @return ReviewListener that returns `pir` if any comments have
 *         a severity of `severity`, proceed otherwise
 */
function severityReviewListener(pir, severity = "error") {
    return async (rli) => {
        if (rli && rli.review && rli.review.comments && rli.review.comments.some(c => c.severity === severity)) {
            return pir;
        }
        return PushImpactListenerRegistration_1.PushImpactResponse.proceed;
    };
}
exports.severityReviewListener = severityReviewListener;
/**
 * Listener that fails the code inspection if the review has any
 * error comments.
 */
exports.FailGoalIfErrorComments = {
    name: "Fail goal if any code inspections result in comments with severity error",
    listener: severityReviewListener(PushImpactListenerRegistration_1.PushImpactResponse.failGoals),
};
/**
 * Listener that requires approval on the code inspection if the
 * review has any error comments.
 */
exports.ApproveGoalIfErrorComments = {
    name: "Require approval if any code inspections result in comments with severity error",
    listener: severityReviewListener(PushImpactListenerRegistration_1.PushImpactResponse.requireApprovalToProceed),
};
/**
 * Listener that fails the code inspection if the review has any
 * warn comments.
 */
exports.FailGoalIfWarnComments = {
    name: "Fail goal if any code inspections result in comments with severity warn",
    listener: severityReviewListener(PushImpactListenerRegistration_1.PushImpactResponse.failGoals, "warn"),
};
/**
 * Listener that requires approval on the code inspection if the
 * review has any warn comments.
 */
exports.ApproveGoalIfWarnComments = {
    name: "Require approval if any code inspections result in comments with severity warn",
    listener: severityReviewListener(PushImpactListenerRegistration_1.PushImpactResponse.requireApprovalToProceed, "warn"),
};
//# sourceMappingURL=goalReviewListener.js.map