"use strict";
/*
 * Copyright © 2020 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.transformToProjectListener = void 0;
const HandlerResult_1 = require("@atomist/automation-client/lib/HandlerResult");
const GoalInvocation_1 = require("../goal/GoalInvocation");
const commonPushTests_1 = require("../mapping/support/commonPushTests");
function isTransformResult(tr) {
    const maybe = tr;
    return maybe && maybe.success !== undefined;
}
/**
 * Convert a CodeTransform to a GoalProjectListener
 * @param transform
 * @param name
 * @param pushTest
 */
function transformToProjectListener(transform, name, pushTest = commonPushTests_1.AnyPush) {
    return {
        name,
        pushTest,
        events: [GoalInvocation_1.GoalProjectListenerEvent.before],
        listener: async (p, gi) => {
            try {
                const result = await transform(p, Object.assign({}, gi), {});
                if (isTransformResult(result)) {
                    return {
                        code: result.success === true ? 0 : 1,
                    };
                }
            }
            catch (e) {
                return {
                    code: 1,
                    message: e.message,
                };
            }
            return HandlerResult_1.Success;
        },
    };
}
exports.transformToProjectListener = transformToProjectListener;
//# sourceMappingURL=transformUtils.js.map