Skip to content

Commit

Permalink
Add option to initialised for empty directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Nov 25, 2024
1 parent 5c2e68c commit 14dff1b
Showing 1 changed file with 57 additions and 15 deletions.
72 changes: 57 additions & 15 deletions report.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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<void>{
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);
}
});
});
});
})
}
});

0 comments on commit 14dff1b

Please sign in to comment.