diff --git a/x.js b/x.js index 7800adf..0f88ae7 100644 --- a/x.js +++ b/x.js @@ -635,6 +635,126 @@ app.post('/releasehook_kjfhdkf987987', async (req, res) => { }); return; // Exit early to avoid double response } + + if(payload && payload.repository && payload.repository.name == 'dashApp'){ + // Check if push was to 'prod' branch + const targetBranch = 'prod'; + const ref = payload.ref || ''; + const branchName = ref.replace('refs/heads/', ''); + + if (branchName !== targetBranch) { + logMessage(`dashApp push to branch '${branchName}' - skipping version update (only updates on '${targetBranch}' branch)`); + if (!responseSent) { + responseSent = true; + res.status(200).json({ success: true, repository: 'dashApp', message: 'No action taken - not prod branch' }); + } + return; + } + + logMessage(`dashApp push to ${targetBranch} branch detected - updating version.json`); + + const repoPath = '/home/seb/src/dashApp'; + const versionFilePath = path.join(repoPath, 'version.json'); + + // Function to update version.json and commit changes + const updateVersionFile = async () => { + try { + // Read current version.json + let currentVersion = { v: '0.0.0', d: '' }; + if (fs.existsSync(versionFilePath)) { + const versionContent = fs.readFileSync(versionFilePath, 'utf8'); + try { + currentVersion = JSON.parse(versionContent); + } catch (parseError) { + logMessage(`Error parsing version.json, using defaults: ${parseError.message}`, 'warn'); + } + } else { + logMessage('version.json not found, creating new file with initial version'); + } + + // Increment version number + const versionParts = currentVersion.v.split('.'); + if (versionParts.length >= 3) { + const patch = parseInt(versionParts[2]) || 0; + versionParts[2] = (patch + 1).toString(); + currentVersion.v = versionParts.join('.'); + } else { + // Fallback if version format is unexpected + const lastNum = parseInt(currentVersion.v.replace(/\D/g, '')) || 0; + currentVersion.v = `0.0.${lastNum + 1}`; + } + + // Update date in format YYYYMMDDHHMM + const now = new Date(); + const year = now.getFullYear(); + const month = String(now.getMonth() + 1).padStart(2, '0'); + const day = String(now.getDate()).padStart(2, '0'); + const hour = String(now.getHours()).padStart(2, '0'); + const minute = String(now.getMinutes()).padStart(2, '0'); + currentVersion.d = `${year}${month}${day}${hour}${minute}`; + + // Write updated version.json + const updatedContent = JSON.stringify(currentVersion); + fs.writeFileSync(versionFilePath, updatedContent, 'utf8'); + logMessage(`Updated version.json: ${updatedContent}`); + + // Git commands to commit and push + const gitCommands = [ + `cd ${repoPath}`, + 'git add version.json', + `git commit -m "Auto-update version to ${currentVersion.v} [${currentVersion.d}]"`, + `git push origin ${targetBranch}` + ].join(' && '); + + logMessage(`Executing git commands for dashApp: ${gitCommands}`); + + // Execute git commands + const { stdout: gitOutput, stderr: gitError } = await execCmd(gitCommands, { + cwd: repoPath, + timeoutMs: 30000 + }); + + if (gitOutput) { + logMessage(`Git output for dashApp: ${gitOutput.trim()}`); + } + + if (gitError) { + logMessage(`Git stderr for dashApp: ${gitError.trim()}`, 'warn'); + } + + logMessage(`Successfully updated and pushed version.json for dashApp: v${currentVersion.v}`); + + if (!responseSent) { + responseSent = true; + res.status(200).json({ + success: true, + repository: 'dashApp', + version: currentVersion.v, + date: currentVersion.d, + message: 'Version updated and committed' + }); + } + + } catch (error) { + logMessage(`Error updating version.json for dashApp: ${error.message}`, 'error'); + if (error.stack) { + logMessage(`Stack trace: ${error.stack}`, 'error'); + } + + if (!responseSent) { + responseSent = true; + return res.status(500).json({ + error: 'Version update failed', + details: error.message + }); + } + } + }; + + // Execute the version update + updateVersionFile(); + return; // Exit early to avoid double response + } // If we get here, no repository matched logMessage(`No handler found for repository: ${payload?.repository?.name || 'unknown'}`, 'warn');