UNPKG

@openinc/parse-server-opendash

Version:
134 lines (133 loc) 5.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentationImporter = void 0; const services_1 = require("../services"); const types_1 = require("../../../types"); const Organizer_1 = require("./Organizer"); const Cleanup_1 = require("./Cleanup"); /** * Main Documentation Importer class */ class DocumentationImporter { constructor(token) { this.githubClient = new services_1.GitHubClient(token); } /** * Import documentation from a GitHub repository */ async importFromRepository(options, user, tenant) { // Validate token const gitUser = await this.githubClient.validateToken(); // Get repository info const repo = await this.githubClient.getRepository(); // Determine branch to use const branch = options.branch || repo.default_branch; // Validate branch exists await this.validateBranch(options.organization, options.repository, branch); // Get branch info (commit SHA) const branchInfo = await this.githubClient.getBranch(); const latestCommitSha = branchInfo.commit.sha; // Attempt to load previously imported commit SHA from Parse let previousCommitSha; let stateObj; try { stateObj = await new Parse.Query(types_1.Documentation_Config) .equalTo("tenant", tenant) .equalTo("user", user) .first({ useMasterKey: true, }); previousCommitSha = stateObj?.get("lastCommitSha"); } catch (err) { console.log(`Could not load previous commit SHA: ${err.message}`); } // Early exit if unchanged if (previousCommitSha && previousCommitSha === latestCommitSha) { return { metadata: { totalFiles: 0, fileTypes: [], // importedAt still reflects the check time importedAt: new Date(), repository: `${options.organization}/${options.repository}`, branch, commitSha: latestCommitSha, previousCommitSha, skipped: true, skipReason: "unchanged", }, }; } const treeData = await this.githubClient.getTree(latestCommitSha); // Organize structure with filtering and root path const structure = await Organizer_1.DocumentationOrganizer.organizeRepositoryStructure(treeData, options.fileFilter, options.rootPath, this.githubClient, options.organization, options.repository, branch, options.defaultFolderConfig); // Clean up old documentation that is no longer in the repository // This only runs when commit SHA has changed (not on first import when previousCommitSha is undefined) let cleanupResult = { deletedDocuments: 0, deletedCategories: 0 }; if (previousCommitSha) { console.log(`[DocumentationImporter] Repository content changed, cleaning up old documentation...`); try { cleanupResult = await Cleanup_1.DocumentationCleanup.cleanupOldDocumentation(structure, user, tenant); console.log(`[DocumentationImporter] Cleanup completed: ${cleanupResult.deletedDocuments} documents and ${cleanupResult.deletedCategories} categories removed`); } catch (error) { console.error(`[DocumentationImporter] Cleanup failed:`, error); // Continue with import even if cleanup fails } } else { console.log(`[DocumentationImporter] First import detected, skipping cleanup`); } // Persist latest commit SHA for future runs try { if (!stateObj) { stateObj = new types_1.Documentation_Config(); } stateObj.set("lastCommitSha", latestCommitSha); if (tenant) stateObj.set("tenant", tenant); if (user) stateObj.set("user", user); await stateObj.save(null, { useMasterKey: true }); } catch (err) { console.log(`Could not persist latest commit SHA: ${err.message}`); } return { structure, metadata: { totalFiles: structure.allFiles.length, fileTypes: Array.from(structure.filesByExtension.keys()), importedAt: new Date(), repository: `${options.organization}/${options.repository}`, branch, commitSha: latestCommitSha, previousCommitSha, cleanup: cleanupResult, }, }; } /** * Validate that a branch exists */ async validateBranch(owner, repo, branch) { try { const branches = await this.githubClient.getBranches(); const branchNames = branches.map((b) => b.name); if (!branchNames.includes(branch)) { throw new Error(`Branch '${branch}' not found. Available branches: ${branchNames.join(", ")}`); } } catch (error) { console.log(`Could not validate branches: ${error}`); } } /** * Get file content from GitHub */ async getFileContent(path) { return this.githubClient.getFileContent(path); } } exports.DocumentationImporter = DocumentationImporter;