"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.GitHubReleaseRegistration = exports.GitHubPushTest = exports.GitHubReleaseCreator = void 0;
const GitHubRepoRef_1 = require("@atomist/automation-client/lib/operations/common/GitHubRepoRef");
const ProjectOperationCredentials_1 = require("@atomist/automation-client/lib/operations/common/ProjectOperationCredentials");
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const logInterpreters_1 = require("../../api-helper/log/logInterpreters");
const GoalNameGenerator_1 = require("../../api/goal/GoalNameGenerator");
const octokit_1 = require("./octokit");
/**
 * Create a GitHub release for GitHub.com or GHE projects.  If the
 * project is not a GitHub project or the project credentials do not
 * have a token, it issues are warning and returns success.
 */
const GitHubReleaseCreator = async (args) => {
    const slug = `${args.id.owner}/${args.id.repo}`;
    if (!GitHubRepoRef_1.isGitHubRepoRef(args.id)) {
        const message = `Project ${slug} is neither a GitHub.com nor GHE remote repository`;
        logger_1.logger.warn(message);
        args.log.write(message);
        return { code: 0, message };
    }
    if (!ProjectOperationCredentials_1.isTokenCredentials(args.credentials)) {
        const message = `Project ${slug} credentials are not TokenCredentials`;
        logger_1.logger.warn(message);
        args.log.write(message);
        return { code: 0, message };
    }
    try {
        let changelog;
        for (const ch of ["CHANGELOG.md", "CHANGELOG", "ChangeLog", "Changelog", "changelog"]) {
            if (await args.project.hasFile(ch)) {
                changelog = ch;
                break;
            }
        }
        await octokit_1.createRelease({
            auth: args.credentials.token,
            baseUrl: args.id.scheme + args.id.apiBase,
            owner: args.id.owner,
            repo: args.id.repo,
            version: args.releaseVersion,
            sha: args.goalEvent.sha,
            changelog,
        });
        const message = `Created GitHub release ${args.releaseVersion} for ${slug}`;
        args.log.write(message);
        const externalUrls = [{ label: args.releaseVersion, url: releaseUrl(args.id, args.releaseVersion) }];
        return { code: 0, message, externalUrls };
    }
    catch (e) {
        const message = `Failed to create GitHub release ${args.releaseVersion} for project ${slug}: ${e.message}`;
        logger_1.logger.warn(message);
        args.log.write(message);
        return { code: 1, message };
    }
};
exports.GitHubReleaseCreator = GitHubReleaseCreator;
/**
 * Push test that returns true if the push was to a GitHub.com or GHE
 * remote repository.
 */
exports.GitHubPushTest = {
    name: "GitHubPushTest",
    mapping: async (pli) => GitHubRepoRef_1.isGitHubRepoRef(pli.id),
};
/**
 * Release registration for GitHub release creator implementation.
 */
exports.GitHubReleaseRegistration = {
    logInterpreter: logInterpreters_1.LogSuppressor,
    name: GoalNameGenerator_1.DefaultGoalNameGenerator.generateName("github-release"),
    pushTest: exports.GitHubPushTest,
    releaseCreator: exports.GitHubReleaseCreator,
};
function releaseUrl(id, version) {
    return `${id.scheme}${id.remoteBase}/${id.owner}/${id.repo}/releases/tag/${version}`;
}
//# sourceMappingURL=github.js.map