| "use strict"; |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.ApiCheckout = void 0; |
| const context_1 = require("./context"); |
| class ApiCheckout { |
| octokit; |
| owner; |
| repo; |
| commitSha; |
| branchName; |
| cache = new Map(); |
| constructor(probotContext, options) { |
| |
| const ctx = probotContext || (0, context_1.getCurrentContext)(); |
| if (!ctx && !options) |
| throw new Error('No context available and no options provided'); |
| this.octokit = ctx?.probotContext?.octokit || ctx?.octokit; |
| this.owner = options?.owner || ctx?.repo?.owner; |
| this.repo = options?.repo || ctx?.repo?.repo; |
| |
| if (options?.ref) { |
| this.commitSha = options.ref; |
| } |
| else { |
| |
| const pullRequest = ctx?.probotContext?.payload?.pull_request || ctx?.payload?.pull_request; |
| if (pullRequest?.head?.sha) { |
| this.commitSha = pullRequest.head.sha; |
| } |
| else { |
| throw new Error('No commit SHA (ref) available for checkout. For issue_comment events, please provide ref in options.'); |
| } |
| } |
| this.branchName = |
| options?.branch || |
| ctx?.probotContext?.payload?.pull_request?.head?.ref || |
| ctx?.payload?.pull_request?.head?.ref; |
| } |
| |
| |
| |
| async getFile(path) { |
| |
| if (this.cache.has(path)) { |
| return this.cache.get(path); |
| } |
| try { |
| const response = await this.octokit.repos.getContent({ |
| owner: this.owner, |
| repo: this.repo, |
| path: path, |
| ref: this.commitSha |
| }); |
| |
| if (Array.isArray(response.data)) { |
| throw new Error(`Path ${path} is a directory, not a file`); |
| } |
| const content = Buffer.from(response.data.content, 'base64').toString('utf-8'); |
| const fileInfo = { |
| path, |
| content, |
| sha: response.data.sha |
| }; |
| |
| this.cache.set(path, fileInfo); |
| return fileInfo; |
| } |
| catch (error) { |
| throw new Error(`Failed to get file ${path}: ${error}`); |
| } |
| } |
| |
| |
| |
| async updateFile(path, newContent, message) { |
| const currentFile = await this.getFile(path); |
| try { |
| const response = await this.octokit.repos.createOrUpdateFileContents({ |
| owner: this.owner, |
| repo: this.repo, |
| path: path, |
| message: message, |
| content: Buffer.from(newContent).toString('base64'), |
| sha: currentFile.sha, |
| branch: this.branchName || this.commitSha |
| }); |
| |
| const updatedFile = { |
| path, |
| content: newContent, |
| sha: response.data.commit.sha |
| }; |
| this.cache.set(path, updatedFile); |
| return response.data.commit.sha; |
| } |
| catch (error) { |
| throw new Error(`Failed to update file ${path}: ${error}`); |
| } |
| } |
| |
| |
| |
| async createFile(path, content, message) { |
| try { |
| const response = await this.octokit.repos.createOrUpdateFileContents({ |
| owner: this.owner, |
| repo: this.repo, |
| path: path, |
| message: message, |
| content: Buffer.from(content).toString('base64'), |
| branch: this.branchName || this.commitSha |
| }); |
| |
| const newFile = { |
| path, |
| content, |
| sha: response.data.commit.sha |
| }; |
| this.cache.set(path, newFile); |
| return response.data.commit.sha; |
| } |
| catch (error) { |
| throw new Error(`Failed to create file ${path}: ${error}`); |
| } |
| } |
| |
| |
| |
| async getDirectory(path) { |
| try { |
| const response = await this.octokit.repos.getContent({ |
| owner: this.owner, |
| repo: this.repo, |
| path: path, |
| ref: this.commitSha |
| }); |
| if (!Array.isArray(response.data)) { |
| throw new Error(`Path ${path} is not a directory`); |
| } |
| return response.data |
| .filter((item) => item.type === 'file') |
| .map((item) => item.path); |
| } |
| catch (error) { |
| throw new Error(`Failed to get directory ${path}: ${error}`); |
| } |
| } |
| |
| |
| |
| async getTree() { |
| try { |
| const response = await this.octokit.git.getTree({ |
| owner: this.owner, |
| repo: this.repo, |
| tree_sha: this.commitSha, |
| recursive: true |
| }); |
| return response.data.tree |
| .filter((item) => item.type === 'blob') |
| .map((item) => item.path); |
| } |
| catch (error) { |
| throw new Error(`Failed to get repository tree: ${error}`); |
| } |
| } |
| } |
| exports.ApiCheckout = ApiCheckout; |
| |