"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubTagsDatasource = void 0;
const logger_1 = require("../../../logger");
const url_1 = require("../../../util/github/url");
const github_1 = require("../../../util/http/github");
const datasource_1 = require("../datasource");
class GithubTagsDatasource extends datasource_1.Datasource {
    constructor() {
        super(GithubTagsDatasource.id);
        this.defaultRegistryUrls = ['https://github.com'];
        this.http = new github_1.GithubHttp(GithubTagsDatasource.id);
    }
    async getTagCommit(registryUrl, githubRepo, tag) {
        const apiBaseUrl = (0, url_1.getApiBaseUrl)(registryUrl);
        let digest = null;
        try {
            const url = `${apiBaseUrl}repos/${githubRepo}/git/refs/tags/${tag}`;
            const res = (await this.http.getJson(url)).body.object;
            if (res.type === 'commit') {
                digest = res.sha;
            }
            else if (res.type === 'tag') {
                digest = (await this.http.getJson(res.url)).body.object
                    .sha;
            }
            else {
                logger_1.logger.warn({ res }, 'Unknown git tag refs type');
            }
        }
        catch (err) {
            logger_1.logger.debug({ githubRepo, err }, 'Error getting tag commit from GitHub repo');
        }
        return digest;
    }
    async getCommit(registryUrl, githubRepo) {
        const apiBaseUrl = (0, url_1.getApiBaseUrl)(registryUrl);
        let digest = null;
        try {
            const url = `${apiBaseUrl}repos/${githubRepo}/commits?per_page=1`;
            const res = await this.http.getJson(url);
            digest = res.body[0].sha;
        }
        catch (err) {
            logger_1.logger.debug({ githubRepo: githubRepo, err, registryUrl }, 'Error getting latest commit from GitHub repo');
        }
        return digest;
    }
    /**
     * github.getDigest
     *
     * The `newValue` supplied here should be a valid tag for the docker image.
     *
     * Returns the latest commit hash for the repository.
     */
    getDigest({ packageName: repo, registryUrl }, newValue) {
        return newValue
            ? this.getTagCommit(registryUrl, repo, newValue)
            : this.getCommit(registryUrl, repo);
    }
    async getReleases(config) {
        const { registryUrl, packageName: repo } = config;
        const apiBaseUrl = (0, url_1.getApiBaseUrl)(registryUrl);
        // tag
        const url = `${apiBaseUrl}repos/${repo}/tags?per_page=100`;
        const versions = (await this.http.getJson(url, {
            paginate: true,
        })).body.map((o) => o.name);
        const dependency = {
            sourceUrl: (0, url_1.getSourceUrl)(repo, registryUrl),
            releases: versions.map((version) => ({
                version,
                gitRef: version,
            })),
        };
        return dependency;
    }
}
exports.GithubTagsDatasource = GithubTagsDatasource;
GithubTagsDatasource.id = 'github-tags';
//# sourceMappingURL=index.js.map