From 14dff1b7f0889d33a621b0dd2aad1aa3b7930a7e Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 25 Nov 2024 13:10:51 +0100 Subject: [PATCH] Add option to initialised for empty directory --- report.ts | 72 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/report.ts b/report.ts index bda5d28..e51170b 100644 --- a/report.ts +++ b/report.ts @@ -1,7 +1,15 @@ import fs from 'fs'; import path from "path"; import {exec} from "child_process"; +import { GITHUB_PAT} from "config/wp.config"; +const GIT_PAT = GITHUB_PAT; +if (!GIT_PAT) { + console.error('Github PAT is not defined in the environment variables.'); + process.exit(1); +} + +const remoteUrl = `https://${GIT_PAT}@github.com/wp-media/e2e_release_reports.git`; const testResults = path.join(__dirname, 'test-results'); @@ -11,29 +19,63 @@ if(args.length < 1) { } const newTestDir = args[0]; -const gitCommands = `git add . && git commit -m "Add test report" && git push origin test`; +const gitCommands = ` + git checkout -b ${newTestDir}; + git add . && git commit -m "Test report"; + git push origin test +`; + + +//Check if .git directory exists +export async function checkGitInitialized(callback) : Promise{ + const gitDir = path.join(newTestDir, '.git'); + if (!fs.existsSync(gitDir)) { + console.log('Git is not initialized. Setting it up...'); + exec(`git init && git remote add origin ${remoteUrl}`, {cwd: newTestDir}, (initErr) => { + if (initErr) { + console.error('Error initializing Git:', initErr); + process.exit(1); + } + console.log('Git initialized and remote added.'); + callback(); + }); + + //Execute command to have username, default it to e2e environment + exec('git config --global user.name "E2E Environment" && git config --global user.email "e2e.report@wp-media.me"', (err) => { + if (err) { + console.error('Error setting global Git config:', err); + } else { + console.log('Global Git config set.'); + } + }); + } else { + callback(); + } +} // Rename test folder and push fs.rename(testResults, newTestDir, (err) => { if (err) { console.error('Error renaming/moving file:', err); } else { - exec(gitCommands, { cwd: newTestDir }, (err, stdout, stderr) => { - if (err) { - console.error('Error executing git commands:', err); - } - - if (stderr) { - console.error('Git stderr output:', stderr); - return; - } - - //Revert the renaming of test folder to avoid tracking it in e2e - fs.rename(newTestDir, testResults, (err) => { + checkGitInitialized(() => { + exec(gitCommands, { cwd: newTestDir }, (err, stdout, stderr) => { if (err) { - console.error('Error reverting the directory name:', err); + console.error('Error executing git commands:', err); + } + + if (stderr) { + console.error('Git stderr output:', stderr); + return; } + + //Revert the renaming of test folder to avoid tracking it in e2e + fs.rename(newTestDir, testResults, (err) => { + if (err) { + console.error('Error reverting the directory name:', err); + } + }); }); - }); + }) } }); \ No newline at end of file