"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseOctoKitService = exports.VSCodeTeamId = exports.IOctoKitService = exports.IGithubRepositoryService = void 0;
const services_1 = require("../../../util/common/services");
const buffer_1 = require("../../../util/vs/base/common/buffer");
const githubAPI_1 = require("./githubAPI");
exports.IGithubRepositoryService = (0, services_1.createServiceIdentifier)('IGithubRepositoryService');
exports.IOctoKitService = (0, services_1.createServiceIdentifier)('IOctoKitService');
exports.VSCodeTeamId = 1682102;
/**
 * The same as {@link OctoKitService} but doesn't require the AuthService.
 * This is because we want to call certain Octokit method inside the Authservice and must
 * avoid a circular dependency.
 * Note: Only OctoKitService is exposed on the accessor to avoid confusion.
 */
class BaseOctoKitService {
    constructor(_capiClientService, _fetcherService, _logService, _telemetryService) {
        this._capiClientService = _capiClientService;
        this._fetcherService = _fetcherService;
        this._logService = _logService;
        this._telemetryService = _telemetryService;
    }
    async getCurrentAuthedUserWithToken(token) {
        return this._makeGHAPIRequest('user', 'GET', token);
    }
    async getTeamMembershipWithToken(teamId, token, username) {
        return this._makeGHAPIRequest(`teams/${teamId}/memberships/${username}`, 'GET', token);
    }
    async _makeGHAPIRequest(routeSlug, method, token, body) {
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, routeSlug, method, token, body, '2022-11-28');
    }
    async getCopilotPullRequestForUserWithToken(owner, repo, user, token) {
        const query = `repo:${owner}/${repo} is:open author:copilot-swe-agent[bot] involves:${user}`;
        return (0, githubAPI_1.makeSearchGraphQLRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, query);
    }
    async addPullRequestCommentWithToken(pullRequestId, commentBody, token) {
        return (0, githubAPI_1.addPullRequestCommentGraphQLRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, pullRequestId, commentBody);
    }
    async getPullRequestFromSessionWithToken(globalId, token) {
        return (0, githubAPI_1.getPullRequestFromGlobalId)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, globalId);
    }
    async getPullRequestFilesWithToken(owner, repo, pullNumber, token) {
        const result = await (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, `repos/${owner}/${repo}/pulls/${pullNumber}/files`, 'GET', token, undefined, '2022-11-28');
        return result || [];
    }
    async closePullRequestWithToken(owner, repo, pullNumber, token) {
        return (0, githubAPI_1.closePullRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, owner, repo, pullNumber);
    }
    async getFileContentWithToken(owner, repo, ref, path, token) {
        const route = `repos/${owner}/${repo}/contents/${path}?ref=${encodeURIComponent(ref)}`;
        const response = await (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, route, 'GET', token, undefined);
        if (!response || Array.isArray(response)) {
            throw new Error('Unable to fetch file content');
        }
        const typedResponse = response;
        if (typedResponse.content && typedResponse.encoding === 'base64') {
            return (0, buffer_1.decodeBase64)(typedResponse.content.replace(/\n/g, '')).toString();
        }
        if (typedResponse.sha) {
            const blob = await this.getBlobContentWithToken(owner, repo, typedResponse.sha, token);
            if (blob) {
                return blob;
            }
        }
        this._logService.error(`Failed to get file content for ${owner}/${repo}/${path} at ref ${ref}`);
        return '';
    }
    async getBlobContentWithToken(owner, repo, sha, token) {
        const blobRoute = `repos/${owner}/${repo}/git/blobs/${sha}`;
        const blobResponse = await (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, blobRoute, 'GET', token, undefined, '2022-11-28');
        if (!blobResponse || Array.isArray(blobResponse)) {
            return undefined;
        }
        const typedBlob = blobResponse;
        if (typedBlob.content && typedBlob.encoding === 'base64') {
            return (0, buffer_1.decodeBase64)(typedBlob.content.replace(/\n/g, '')).toString();
        }
        return undefined;
    }
}
exports.BaseOctoKitService = BaseOctoKitService;
//# sourceMappingURL=githubService.js.map