From ce911d6fd5acaa58b671e94b827b178da27b8ee0 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Thu, 21 Nov 2024 11:10:48 +0100 Subject: [PATCH 1/4] :closes: #143 add process to rename and push test files to git --- package.json | 1 + report.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 report.ts diff --git a/package.json b/package.json index 26841f3..26d3f2c 100755 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "test:lrc": "$npm_package_config_testCommand --tags @lrc", "test:performancehints": "$npm_package_config_testCommand --tags @performancehints", "healthcheck": "ts-node healthcheck.ts", + "push-report": "ts-node report.ts", "wp-env": "wp-env" }, "repository": { diff --git a/report.ts b/report.ts new file mode 100644 index 0000000..bda5d28 --- /dev/null +++ b/report.ts @@ -0,0 +1,39 @@ +import fs from 'fs'; +import path from "path"; +import {exec} from "child_process"; + + +const testResults = path.join(__dirname, 'test-results'); + +const args = process.argv.slice(2); +if(args.length < 1) { + console.error('Please provide the new naming for test folder') +} +const newTestDir = args[0]; + +const gitCommands = `git add . && git commit -m "Add test report" && git push origin test`; + +// 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) => { + if (err) { + console.error('Error reverting the directory name:', err); + } + }); + }); + } +}); \ No newline at end of file From 5c2e68c93c14081512ede501173009bd9e1e04c0 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 25 Nov 2024 13:10:30 +0100 Subject: [PATCH 2/4] Add PAT variable to config --- config/wp.config.sample.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/config/wp.config.sample.ts b/config/wp.config.sample.ts index 6eb476b..f78fec9 100755 --- a/config/wp.config.sample.ts +++ b/config/wp.config.sample.ts @@ -44,7 +44,8 @@ const { WP_SSH_USERNAME = '', WP_SSH_ADDRESS = '', WP_SSH_KEY = '', - WP_SSH_ROOT_DIR = '' + WP_SSH_ROOT_DIR = '', + GITHUB_PAT = '' } = process.env; /** @@ -98,5 +99,6 @@ export { WP_SSH_ADDRESS, WP_SSH_KEY, WP_SSH_ROOT_DIR, - SCENARIO_URLS + SCENARIO_URLS, + GITHUB_PAT }; \ No newline at end of file From 14dff1b7f0889d33a621b0dd2aad1aa3b7930a7e Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 25 Nov 2024 13:10:51 +0100 Subject: [PATCH 3/4] 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 From 60c347980f8b5af475adc36624f49398c66bb89a Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 25 Nov 2024 13:22:05 +0100 Subject: [PATCH 4/4] Fix import error --- report.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/report.ts b/report.ts index e51170b..7a59181 100644 --- a/report.ts +++ b/report.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import path from "path"; import {exec} from "child_process"; -import { GITHUB_PAT} from "config/wp.config"; +import { GITHUB_PAT} from "./config/wp.config"; const GIT_PAT = GITHUB_PAT; if (!GIT_PAT) { @@ -15,6 +15,7 @@ const testResults = path.join(__dirname, 'test-results'); const args = process.argv.slice(2); if(args.length < 1) { + console.log(GIT_PAT); console.error('Please provide the new naming for test folder') } const newTestDir = args[0];