"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.readSdmVersion = exports.getGoalVersion = exports.goalInvocationVersion = exports.executeVersioner = void 0;
const HandlerResult_1 = require("@atomist/automation-client/lib/HandlerResult");
const GraphClient_1 = require("@atomist/automation-client/lib/spi/graph/GraphClient");
const slack_messages_1 = require("@atomist/slack-messages");
const _ = require("lodash");
/**
 * Version the project with a build specific version number
 * @param projectLoader used to load projects
 * @param projectVersioner decides on the version string
 */
function executeVersioner(projectVersioner) {
    return async (goalInvocation) => {
        const { configuration, goalEvent, credentials, id, context, progressLog } = goalInvocation;
        return configuration.sdm.projectLoader.doWithProject({ credentials, id, context, readOnly: false }, async (p) => {
            const version = await projectVersioner(goalEvent, p, progressLog);
            const sdmVersion = {
                sha: goalEvent.sha,
                branch: id.branch,
                version,
                repo: {
                    owner: goalEvent.repo.owner,
                    name: goalEvent.repo.name,
                    providerId: goalEvent.repo.providerId,
                },
            };
            await context.graphClient.mutate({
                name: "UpdateSdmVersion",
                variables: {
                    version: sdmVersion,
                },
                options: GraphClient_1.MutationNoCacheOptions,
            });
            return Object.assign(Object.assign({}, HandlerResult_1.Success), { description: `Versioned ${slack_messages_1.codeLine(version)}` });
        });
    };
}
exports.executeVersioner = executeVersioner;
/**
 * Get prerelease, i.e., timestamped, version associated with the goal
 * set for the provided goal invocation.  The Version goal must be
 * executed within the goal set prior to calling this function.
 *
 * @param gi Goal invocation
 * @return Prerelease semantic version string
 */
async function goalInvocationVersion(gi) {
    return getGoalVersion({
        branch: gi.id.branch,
        context: gi.context,
        owner: gi.goalEvent.repo.owner,
        providerId: gi.goalEvent.repo.providerId,
        repo: gi.goalEvent.repo.name,
        sha: gi.goalEvent.sha,
    });
}
exports.goalInvocationVersion = goalInvocationVersion;
/**
 * Read and return prerelease version for the goal set associated with
 * the provided commit.
 *
 * @param args Properties determining which version to retrieve
 * @return Prerelease semantic version string
 */
async function getGoalVersion(args) {
    const branch = args.branch || "master";
    const version = await args.context.graphClient.query({
        name: "SdmVersionForCommit",
        variables: {
            name: [args.repo],
            owner: [args.owner],
            providerId: [args.providerId],
            sha: [args.sha],
            branch: [branch],
        },
        options: GraphClient_1.QueryNoCacheOptions,
    });
    return _.get(version, "SdmVersion[0].version");
}
exports.getGoalVersion = getGoalVersion;
/** See getGoalVersion. */
async function readSdmVersion(owner, repo, providerId, sha, branch, context) {
    return getGoalVersion({ branch, context, owner, providerId, repo, sha });
}
exports.readSdmVersion = readSdmVersion;
//# sourceMappingURL=projectVersioner.js.map