Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWPW-158872 : NALA] Update Nala GitHub Action runs to correctly handle bot-generated PR details. #2930

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions nala/utils/global.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,23 +26,24 @@ 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`;

try {
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}`);
Expand All @@ -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}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the additional , here? Same for some of the other console messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@narcis-radu : The additional comma is required to adhere Eslint comma-dangle, rule

);
console.info(
`Note: PR branch test url ${prBranchLiveUrl} is not valid, Exiting test execution.`,
);
process.exit(1);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
Expand All @@ -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);
}
}
Expand Down
39 changes: 31 additions & 8 deletions nala/utils/pr.run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading