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 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..7a59181 --- /dev/null +++ b/report.ts @@ -0,0 +1,82 @@ +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'); + +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]; + +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 { + checkGitInitialized(() => { + 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