diff --git a/nala/utils/global.setup.js b/nala/utils/global.setup.js index e453973589..93a0839ae6 100644 --- a/nala/utils/global.setup.js +++ b/nala/utils/global.setup.js @@ -7,13 +7,17 @@ const MAIN_BRANCH_LIVE_URL = 'https://main--milo--adobecom.hlx.live'; const STAGE_BRANCH_URL = 'https://milo.stage.adobe.com'; async function getGitHubPRBranchLiveUrl() { - // get the pr number + // get the pr number and branch name const prReference = process.env.GITHUB_REF; - const prNumber = prReference.split('/')[2]; + const prHeadReference = process.env.GITHUB_HEAD_REF; - // get the pr branch name - const branch = process.env.GITHUB_HEAD_REF; - const prBranch = branch.replace(/\//g, '-'); + const prNumber = prReference.startsWith('refs/pull/') + ? prReference.split('/')[2] + : null; + + const prBranch = prHeadReference + ? prHeadReference.replace(/\//g, '-') + : prReference.split('/')[2].replace(/\//g, '-'); // get the org and repo const repository = process.env.GITHUB_REPOSITORY; @@ -22,8 +26,8 @@ async function getGitHubPRBranchLiveUrl() { const toRepoName = repoParts[1]; // Get the org and repo from the environment variables - const prFromOrg = process.env.prOrg; - const prFromRepoName = process.env.prRepo; + const prFromOrg = process.env.prOrg || toRepoOrg; + const prFromRepoName = process.env.prRepo || toRepoName; const prBranchLiveUrl = `https://${prBranch}--${prFromRepoName}--${prFromOrg}.hlx.live`; @@ -31,14 +35,15 @@ async function getGitHubPRBranchLiveUrl() { if (await isBranchURLValid(prBranchLiveUrl)) { process.env.PR_BRANCH_LIVE_URL = prBranchLiveUrl; } + console.info('GH Ref : ', prReference); + console.info('GH Head Ref : ', prHeadReference); console.info('PR Repository : ', repository); console.info('PR TO ORG : ', toRepoOrg); console.info('PR TO REPO : ', toRepoName); console.info('PR From ORG : ', prFromOrg); console.info('PR From REPO : ', prFromRepoName); - console.info('PR Branch : ', branch); console.info('PR Branch(U) : ', prBranch); - console.info('PR Number : ', prNumber); + console.info('PR Number : ', prNumber || 'Auto-PR'); console.info('PR From Branch live url : ', prBranchLiveUrl); } catch (err) { console.error(`Error => Error in setting PR Branch test URL : ${prBranchLiveUrl}`); @@ -60,8 +65,12 @@ async function getGitHubMiloLibsBranchLiveUrl() { console.info('PR Branch live url : ', prBranchLiveUrl); console.info('Milo Libs : ', miloLibs); } catch (err) { - console.error(`Error => Error in setting PR Branch test URL : ${prBranchLiveUrl}`); - console.info(`Note: PR branch test url ${prBranchLiveUrl} is not valid, Exiting test execution.`); + console.error( + `Error => Error in setting PR Branch test URL : ${prBranchLiveUrl}`, + ); + console.info( + `Note: PR branch test url ${prBranchLiveUrl} is not valid, Exiting test execution.`, + ); process.exit(1); } } @@ -75,8 +84,13 @@ async function getCircleCIBranchLiveUrl() { } console.info('Stage Branch Live URL : ', stageBranchLiveUrl); } catch (err) { - console.error('Error => Error in setting Stage Branch test URL : ', stageBranchLiveUrl); - console.info('Note: Stage branch test url is not valid, Exiting test execution.'); + console.error( + 'Error => Error in setting Stage Branch test URL : ', + stageBranchLiveUrl, + ); + console.info( + 'Note: Stage branch test url is not valid, Exiting test execution.', + ); process.exit(1); } } @@ -87,12 +101,18 @@ async function getLocalBranchLiveUrl() { const localGitRootDir = execSync('git rev-parse --show-toplevel', { encoding: 'utf-8' }).trim(); if (localGitRootDir) { - const gitRemoteOriginUrl = execSync('git config --get remote.origin.url', { cwd: localGitRootDir, encoding: 'utf-8' }).trim(); + const gitRemoteOriginUrl = execSync( + 'git config --get remote.origin.url', + { cwd: localGitRootDir, encoding: 'utf-8' }, + ).trim(); const match = gitRemoteOriginUrl.match(/github\.com\/(.*?)\/(.*?)\.git/); if (match) { const [localOrg, localRepo] = match.slice(1, 3); - const localBranch = execSync('git rev-parse --abbrev-ref HEAD', { cwd: localGitRootDir, encoding: 'utf-8' }).trim(); + const localBranch = execSync('git rev-parse --abbrev-ref HEAD', { + cwd: localGitRootDir, + encoding: 'utf-8', + }).trim(); localTestLiveUrl = process.env.LOCAL_TEST_LIVE_URL || MAIN_BRANCH_LIVE_URL; if (await isBranchURLValid(localTestLiveUrl)) { console.info('Git ORG : ', localOrg); @@ -103,8 +123,12 @@ async function getLocalBranchLiveUrl() { } } } catch (error) { - console.error(`Error => Error in setting local test URL : ${localTestLiveUrl}\n`); - console.info('Note: Local or branch test url is not valid, Exiting test execution.\n'); + console.error( + `Error => Error in setting local test URL : ${localTestLiveUrl}\n`, + ); + console.info( + 'Note: Local or branch test url is not valid, Exiting test execution.\n', + ); process.exit(1); } } diff --git a/nala/utils/pr.run.sh b/nala/utils/pr.run.sh index 0d72a8d66a..a9d70c091b 100755 --- a/nala/utils/pr.run.sh +++ b/nala/utils/pr.run.sh @@ -4,22 +4,43 @@ TAGS="" REPORTER="" EXCLUDE_TAGS="--grep-invert nopr" EXIT_STATUS=0 -PR_NUMBER=$(echo "$GITHUB_REF" | awk -F'/' '{print $3}') -echo "PR Number: $PR_NUMBER" -# Extract feature branch name from GITHUB_HEAD_REF -FEATURE_BRANCH="$GITHUB_HEAD_REF" +echo "GITHUB_REF: $GITHUB_REF" +echo "GITHUB_HEAD_REF: $GITHUB_HEAD_REF" + +if [[ "$GITHUB_REF" == refs/pull/* ]]; then + # extract PR number and branch name + PR_NUMBER=$(echo "$GITHUB_REF" | awk -F'/' '{print $3}') + FEATURE_BRANCH="$GITHUB_HEAD_REF" +elif [[ "$GITHUB_REF" == refs/heads/* ]]; then + # extract branch name from GITHUB_REF + FEATURE_BRANCH=$(echo "$GITHUB_REF" | awk -F'/' '{print $3}') +else + echo "Unknown reference format" +fi + # Replace "/" characters in the feature branch name with "-" FEATURE_BRANCH=$(echo "$FEATURE_BRANCH" | sed 's/\//-/g') + +echo "PR Number: ${PR_NUMBER:-"N/A"}" echo "Feature Branch Name: $FEATURE_BRANCH" +repository=${GITHUB_REPOSITORY} +repoParts=(${repository//\// }) +toRepoOrg=${repoParts[0]} +toRepoName=${repoParts[1]} + +prRepo=${prRepo:-$toRepoName} +prOrg=${prOrg:-$toRepoOrg} + PR_BRANCH_LIVE_URL_GH="https://$FEATURE_BRANCH--$prRepo--$prOrg.hlx.live" + # set pr branch url as env export PR_BRANCH_LIVE_URL_GH export PR_NUMBER echo "PR Branch live URL: $PR_BRANCH_LIVE_URL_GH" -echo "*******************************" + # Convert GitHub Tag(@) labels that can be grepped for label in ${labels}; do @@ -37,9 +58,11 @@ done REPORTER=$reporter [[ ! -z "$REPORTER" ]] && REPORTER="--reporter $REPORTER" -echo "*** Running Nala on $FEATURE_BRANCH ***" -echo "Tags : $TAGS" -echo "npx playwright test ${TAGS} ${EXCLUDE_TAGS} ${REPORTER}" +echo "Running Nala on branch: $FEATURE_BRANCH " +echo "Tags : ${TAGS:-"No @tags or annotations on this PR"}" +echo "Run Command : npx playwright test ${TAGS} ${EXCLUDE_TAGS} ${REPORTER}" +echo -e "\n" +echo "*******************************" # Navigate to the GitHub Action path and install dependencies cd "$GITHUB_ACTION_PATH" || exit