import { GitHubProvider } from '../providers'; import { ReservedFileName, ReservedYamlFileName } from '../configurations'; import YAML from 'yaml'; import { YAMLExtension } from '../extensions'; import { container, injectable, singleton } from 'tsyringe'; interface OfficialTemplateGitHubClientConstructor { accessToken: string; } interface GetOutputAndVariablesFilePerSystemRequest { systemName: string; version: string; } interface GetOutputAndVariablesFilePerSystemResponse { outputContent: object; variablesContent: object; } @singleton() export class OfficialTemplateGitHubClient extends GitHubProvider { _yamlExtension: YAMLExtension; constructor(args?: OfficialTemplateGitHubClientConstructor) { super({ ...args, repository: 'official-templates', }); this._yamlExtension = container.resolve(YAMLExtension); } async GetOutputAndVariablesFilePerSystem({ systemName, version, }: GetOutputAndVariablesFilePerSystemRequest) { var outputContent = {}; var variablesContent = {}; const tree = await this.rest.GetTreeByWorkingRef({ workingRef: version }); const systemNode = tree.tree.find((node) => node.path === systemName); if (!systemNode) { throw new Error(` OfficialTemplateGitHubClient.ts > GetOutputAndVariablesFilePerSystem() error: systemName not founded - systemName: ${systemName}, - version: ${version} `); } const systemTree = await this.rest.GetTreeByFullUrl({ url: systemNode.url, }); const outputNode = systemTree.tree.find( (node) => node.path === `${ReservedFileName.OUTPUT}.json`, ); // Prefer load JSON to not do transform YAML to JSON if (outputNode) { const outputBlob = await this.rest.GetBlobByFullUrl({ url: outputNode.url, }); const outputContentJSONStr = this.rest.DecodeBlobContent(outputBlob); outputContent = JSON.parse(outputContentJSONStr); } else { throw new Error(` OfficialTemplateGitHubClient.ts > GetOutputAndVariablesFilePerSystem() error: output.json not founded - systemName: ${systemName}, - version: ${version} `); } const variablesNode = systemTree.tree.find( (node) => node.path === `${ReservedYamlFileName.VARIABLES}.yaml`, ); if (variablesNode) { const variablesBlob = await this.rest.GetBlobByFullUrl({ url: variablesNode.url, }); const variablesContentYAML = this.rest.DecodeBlobContent(variablesBlob); variablesContent = this._yamlExtension?.parse(variablesContentYAML); } return { outputContent, variablesContent, }; } }