UNPKG

@gentrace/core

Version:
165 lines (161 loc) 6.64 kB
'use strict'; var init = require('./init.js'); var utils = require('./utils.js'); var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; const constructSubmissionPayloadSimple = (pipelineSlug, testRuns, context) => { const body = { pipelineSlug, testRuns, }; // Will be overwritten if GENTRACE_RESULT_NAME is specified if (init.GENTRACE_RUN_NAME) { body.name = init.GENTRACE_RUN_NAME; } if (init.GENTRACE_RESULT_NAME) { body.name = init.GENTRACE_RESULT_NAME; } if (context === null || context === void 0 ? void 0 : context.name) { body.name = context.name; } if (init.GENTRACE_BRANCH || utils.getProcessEnv("GENTRACE_BRANCH")) { body.branch = init.GENTRACE_BRANCH.length > 0 ? init.GENTRACE_BRANCH : utils.getProcessEnv("GENTRACE_BRANCH"); } if (init.GENTRACE_COMMIT || utils.getProcessEnv("GENTRACE_COMMIT")) { body.commit = init.GENTRACE_COMMIT.length > 0 ? init.GENTRACE_COMMIT : utils.getProcessEnv("GENTRACE_COMMIT"); } if (context === null || context === void 0 ? void 0 : context.metadata) { body.metadata = context.metadata; } return body; }; const constructSubmissionPayloadAdvanced = (pipelineIdentifier, testRuns, context, triggerRemoteEvals) => { const body = { testRuns, }; if (isUUID(pipelineIdentifier)) { body.pipelineId = pipelineIdentifier; } else { body.pipelineSlug = pipelineIdentifier; } // Will be overwritten if GENTRACE_RESULT_NAME is specified if (init.GENTRACE_RUN_NAME) { body.name = init.GENTRACE_RUN_NAME; } if (init.GENTRACE_RESULT_NAME) { body.name = init.GENTRACE_RESULT_NAME; } if (context === null || context === void 0 ? void 0 : context.name) { body.name = context.name; } if (context === null || context === void 0 ? void 0 : context.metadata) { body.metadata = context.metadata; } if (init.GENTRACE_BRANCH || utils.getProcessEnv("GENTRACE_BRANCH")) { body.branch = init.GENTRACE_BRANCH.length > 0 ? init.GENTRACE_BRANCH : utils.getProcessEnv("GENTRACE_BRANCH"); } if (init.GENTRACE_COMMIT || utils.getProcessEnv("GENTRACE_COMMIT")) { body.commit = init.GENTRACE_COMMIT.length > 0 ? init.GENTRACE_COMMIT : utils.getProcessEnv("GENTRACE_COMMIT"); } body.collectionMethod = "runner"; if (typeof triggerRemoteEvals !== "undefined") { body.triggerRemoteEvals = triggerRemoteEvals; } return body; }; function isUUID(str) { const uuidPattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/; return uuidPattern.test(str); } function submitTestResult(pipelineSlug, testCases, outputsList, context) { return __awaiter(this, void 0, void 0, function* () { if (!init.globalGentraceApi) { throw new Error("Gentrace API key not initialized. Call init() first."); } if (testCases.length !== outputsList.length) { throw new Error("The number of test cases must be equal to the number of outputs."); } const testRuns = testCases.map((testCase, index) => { const run = { caseId: testCase.id, inputs: testCase.inputs, outputs: outputsList[index], }; return run; }); const body = constructSubmissionPayloadSimple(pipelineSlug, testRuns, context); const response = yield init.globalGentraceApi.v1TestResultSimplePost(body); return response.data; }); } function updateTestResult(resultId, testCases, outputsList) { return __awaiter(this, void 0, void 0, function* () { if (testCases.length !== outputsList.length) { throw new Error("The number of test cases must be equal to the number of outputs."); } const testRuns = testCases.map((testCase, index) => { const run = { caseId: testCase.id, inputs: testCase.inputs, outputs: outputsList[index], }; return run; }); const response = yield init.globalGentraceApi.v1TestResultSimpleIdPost(resultId, { testRuns, }); return response.data; }); } const getTestResult = (resultId) => __awaiter(void 0, void 0, void 0, function* () { if (!init.globalGentraceApi) { throw new Error("Gentrace API key not initialized. Call init() first."); } const response = yield init.globalGentraceApi.v1TestResultIdGet(resultId); const testResult = response.data; return testResult; }); const getTestResultStatus = (resultId) => __awaiter(void 0, void 0, void 0, function* () { if (!init.globalGentraceApi) { throw new Error("Gentrace API key not initialized. Call init() first."); } const response = yield init.globalGentraceApi.v1TestResultStatusGet(resultId); const statusInfo = response.data; return statusInfo; }); const getTestResults = (pipelineSlug) => __awaiter(void 0, void 0, void 0, function* () { if (!init.globalGentraceApi) { throw new Error("Gentrace API key not initialized. Call init() first."); } const response = yield init.globalGentraceApi.v1TestResultGet(pipelineSlug); const testResults = response.data.testResults; return testResults; }); exports.constructSubmissionPayloadAdvanced = constructSubmissionPayloadAdvanced; exports.constructSubmissionPayloadSimple = constructSubmissionPayloadSimple; exports.getTestResult = getTestResult; exports.getTestResultStatus = getTestResultStatus; exports.getTestResults = getTestResults; exports.submitTestResult = submitTestResult; exports.updateTestResult = updateTestResult; //# sourceMappingURL=test-result.js.map