Download lib/src/api-checkout.js from Rachit-Tw/PRININIT: direct link, hf CLI and curl.
- Browser
- Download file 5.7 kB
-
https://huggingface.co/spaces/Rachit-Tw/PRININIT/resolve/main/lib/src/api-checkout.js
- Command line
-
hf download hf://spaces/Rachit-Tw/PRININIT/lib/src/api-checkout.js
-
curl -L -o api-checkout.js https://huggingface.co/spaces/Rachit-Tw/PRININIT/resolve/main/lib/src/api-checkout.js
5.7 kB
| ; | |
| 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) { | |
| // Use passed probotContext or get from async context | |
| 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; | |
| // Primary source: options | |
| if (options?.ref) { | |
| this.commitSha = options.ref; | |
| } | |
| else { | |
| // Fallback: pull_request payload | |
| 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; | |
| } | |
| /** | |
| * Get file content via GitHub API | |
| */ | |
| async getFile(path) { | |
| // Check cache first | |
| 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 | |
| }); | |
| // Handle both file and directory responses | |
| 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 | |
| }; | |
| // Cache the result | |
| this.cache.set(path, fileInfo); | |
| return fileInfo; | |
| } | |
| catch (error) { | |
| throw new Error(`Failed to get file ${path}: ${error}`); | |
| } | |
| } | |
| /** | |
| * Update file content via GitHub API | |
| */ | |
| 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 | |
| }); | |
| // Update cache | |
| 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}`); | |
| } | |
| } | |
| /** | |
| * Create a new file via GitHub API | |
| */ | |
| 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 | |
| }); | |
| // Update cache | |
| 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}`); | |
| } | |
| } | |
| /** | |
| * Get directory listing | |
| */ | |
| 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}`); | |
| } | |
| } | |
| /** | |
| * Get repository tree structure | |
| */ | |
| 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; | |
| //# sourceMappingURL=api-checkout.js.map |