"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "GithubService", {
    enumerable: true,
    get: function() {
        return GithubService;
    }
});
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
class GithubService {
    octokitService;
    logger;
    constructor({ loggerFactory, octokitService }){
        this.logger = loggerFactory(GithubService.name, false);
        this.octokitService = octokitService;
    }
    async wasAuthenticated() {
        const result = await this.octokitService.auth();
        return result?.type === "token";
    }
    async getRateLimit() {
        return this.octokitService.rest.rateLimit.get();
    }
    async getLatestRelease(owner, repo) {
        try {
            return await this.octokitService.rest.repos.getLatestRelease({
                owner,
                repo
            });
        } catch (e) {
            if (e?.name === "HttpError" && e.status == 404) {
                throw new _runtimeexceptions.NotFoundException(`Could not find latest release`);
            }
            throw e;
        }
    }
    async getReleases(owner, repo) {
        try {
            return await this.octokitService.rest.repos.listReleases({
                owner,
                repo
            });
        } catch (e) {
            if (e?.name === "HttpError" && e.status == 404) {
                throw new _runtimeexceptions.NotFoundException(`Could not find releases`);
            }
            throw e;
        }
    }
    async getReleaseByTag(owner, repo, tag) {
        return await this.octokitService.rest.repos.getReleaseByTag({
            owner,
            repo,
            tag
        }).catch((e)=>{
            if (e.name === "HttpError" && e.status == 404) {
                throw new _runtimeexceptions.NotFoundException(`Could not find release with tag ${tag}`);
            }
            throw e;
        });
    }
    async requestAsset(owner, repo, assetId) {
        return await this.octokitService.request("GET /repos/:owner/:repo/releases/assets/:asset_id", {
            headers: {
                Accept: "application/octet-stream"
            },
            owner,
            repo,
            asset_id: assetId
        }).catch((e)=>{
            if (e.name === "HttpError" && e.status == 404) {
                throw new _runtimeexceptions.NotFoundException(`Could not find asset with id ${assetId}`);
            }
            throw e;
        });
    }
}

//# sourceMappingURL=github.service.js.map