UNPKG

js-tests-results-collector

Version:

Universal test results collector for Jest, Jasmine, Mocha, Cypress, and Playwright that sends results to Buddy Works API

127 lines (104 loc) 3.93 kB
const { execSync } = require('child_process'); class Config { constructor() { // Get the token first to determine the correct API URL this.utToken = process.env.BUDDY_UT_TOKEN; // Intelligent API URL detection based on token prefix when BUDDY_API_URL is not provided if (process.env.BUDDY_API_URL) { this.apiBaseUrl = process.env.BUDDY_API_URL; } else if (this.utToken && this.utToken.startsWith('bud_ut_eu')) { this.apiBaseUrl = 'https://api.eu.buddy.works'; } else { this.apiBaseUrl = 'https://api.buddy.works'; } this.sessionId = process.env.BUDDY_SESSION_ID; this.runHash = process.env.BUDDY_RUN_HASH; this.runRefName = process.env.BUDDY_RUN_REF_NAME; this.runRefType = process.env.BUDDY_RUN_REF_TYPE || 'BRANCH'; this.runCommit = process.env.BUDDY_RUN_COMMIT || this.getCurrentCommit(); this.runPreCommit = process.env.BUDDY_RUN_PRE_COMMIT || this.getCurrentCommit(); this.runBranch = process.env.BUDDY_RUN_BRANCH || this.getCurrentBranch(); this.buildUrl = process.env.BUDDY_RUN_URL; this.debugEnabled = process.env.BUDDY_LOGGER_DEBUG === '1'; // Log all configuration values for debugging (mask sensitive data) const Logger = require('../utils/logger'); this.logger = new Logger('Config'); this.logger.debug('Config loaded from environment:', { apiBaseUrl: this.apiBaseUrl, utToken: this.utToken ? `${this.utToken.substring(0, 8)}...` : 'NOT SET', sessionId: this.sessionId || 'NOT SET', runHash: this.runHash || 'NOT SET', runRefName: this.runRefName || 'NOT SET', runRefType: this.runRefType, runCommit: this.runCommit ? `${this.runCommit.substring(0, 8)}...` : 'NOT SET', runPreCommit: this.runPreCommit ? `${this.runPreCommit.substring(0, 8)}...` : 'NOT SET', runBranch: this.runBranch || 'NOT SET', buildUrl: this.buildUrl || 'NOT SET', debugEnabled: this.debugEnabled }); this.validate(); } validate() { const required = ['utToken']; const missing = required.filter(field => !this[field]); if (missing.length > 0) { const errorMsg = `Missing required configuration: ${missing.join(', ')}. ` + `Please set environment variables: ${missing.map(f => f === 'utToken' ? 'BUDDY_UT_TOKEN' : `BUDDY_${f.toUpperCase().replace(/([A-Z])/g, '_$1')}`).join(', ')}.`; this.logger.error(errorMsg); throw new Error(errorMsg); } this.logger.debug('Configuration validation passed'); } getCurrentBranch() { try { return execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8' }).trim(); } catch (error) { return 'main'; } } getCurrentCommit() { try { return execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim(); } catch (error) { return ''; } } getBaseUrl() { return this.apiBaseUrl; } getHeaders() { const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.utToken}` }; // Log headers but mask the token this.logger.debug('Generated headers:', { 'Content-Type': headers['Content-Type'], 'Authorization': `Bearer ${this.utToken ? this.utToken.substring(0, 8) + '...' : 'NOT SET'}` }); return headers; } getSessionPayload() { const payload = { ref_type: this.runRefType, ref_name: this.runRefName || this.runBranch, from_revision: this.runPreCommit, to_revision: this.runCommit }; if (this.runHash) { payload.build_id = this.runHash; } if (this.buildUrl) { payload.build_url = this.buildUrl; } this.logger.debug('Generated session payload:', payload); return payload; } hasExistingSession() { return !!this.sessionId; } getExistingSessionId() { return this.sessionId; } } module.exports = Config;