"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 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 getCopilotSessionsForPRWithToken(prId, token) {
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/sessions/resource/pull/${prId}`, 'GET', token);
    }
    async getSessionLogsWithToken(sessionId, token) {
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/sessions/${sessionId}/logs`, 'GET', token, undefined, undefined, 'text');
    }
    async getSessionInfoWithToken(sessionId, token) {
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/sessions/${sessionId}`, 'GET', token, undefined, undefined, 'text');
    }
    async postCopilotAgentJobWithToken(owner, name, apiVersion, userAgent, payload, token) {
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/${apiVersion}/jobs/${owner}/${name}`, 'POST', token, payload, undefined, undefined, userAgent);
    }
    async getJobByJobIdWithToken(owner, repo, jobId, userAgent, token) {
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/v1/jobs/${owner}/${repo}/${jobId}`, 'GET', token, undefined, undefined, undefined, userAgent);
    }
    async getJobBySessionIdWithToken(owner, repo, sessionId, userAgent, token) {
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/v1/jobs/${owner}/${repo}/session/${sessionId}`, 'GET', token, undefined, undefined, undefined, userAgent);
    }
    async addPullRequestCommentWithToken(pullRequestId, commentBody, token) {
        return (0, githubAPI_1.addPullRequestCommentGraphQLRequest)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, pullRequestId, commentBody);
    }
    async getAllOpenSessionsWithToken(nwo, token) {
        return (0, githubAPI_1.makeGitHubAPIRequestWithPagination)(this._fetcherService, this._logService, `https://api.githubcopilot.com`, 'agents/sessions', nwo, token);
    }
    async getPullRequestFromSessionWithToken(globalId, token) {
        return (0, githubAPI_1.getPullRequestFromGlobalId)(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, globalId);
    }
    async getCustomAgentsWithToken(owner, repo, token) {
        const queryParams = '?exclude_invalid_config=true';
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/custom-agents/${owner}/${repo}${queryParams}`, 'GET', token, undefined, undefined, 'json', 'vscode-copilot-chat');
    }
    async getCustomAgentDetailsWithToken(owner, repo, agentName, token, version) {
        const queryParams = version ? `?version=${encodeURIComponent(version)}` : '';
        return (0, githubAPI_1.makeGitHubAPIRequest)(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/custom-agents/${owner}/${repo}/${agentName}${queryParams}`, 'GET', token, undefined, undefined, 'json', 'vscode-copilot-chat');
    }
    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);
    }
}
exports.BaseOctoKitService = BaseOctoKitService;
//# sourceMappingURL=githubService.js.map