-
Notifications
You must be signed in to change notification settings - Fork 35
/
global.setup.js
151 lines (127 loc) · 5.44 KB
/
global.setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { exit } from 'process';
const { execSync } = require('child_process');
const { isBranchURLValid } = require('./libs/baseurl.js');
const axios = require('axios');
const MAIN_BRANCH_LIVE_URL = 'https://main--milo--adobecom.hlx.live';
const STAGE_BRANCH_URL = 'https://milo.stage.adobe.com';
const LOCALHOST_URL = 'http://localhost:3000';
async function getGitHubPRBranchLiveUrl() {
// get the pr number
const prReference = process.env.GITHUB_REF;
const prNumber = prReference.split('/')[2];
// get the pr branch name
const branch = process.env.GITHUB_HEAD_REF;
const prBranch = branch.replace(/\//g, '-');
// get the org and repo
const repository = process.env.GITHUB_REPOSITORY;
const repoParts = repository.split('/');
const toRepoOrg = repoParts[0];
const toRepoName = repoParts[1];
// Get the org and repo from the environment variables
const prFromOrg = process.env.prOrg;
const prFromRepoName = process.env.prRepo;
let prBranchLiveUrl;
if (toRepoName === 'nala' || toRepoName === 'janus') {
prBranchLiveUrl = MAIN_BRANCH_LIVE_URL;
} else {
prBranchLiveUrl = `https://${prBranch}--${prFromRepoName}--${prFromOrg}.hlx.live`;
}
try {
if (await isBranchURLValid(prBranchLiveUrl)) {
process.env.PR_BRANCH_LIVE_URL = prBranchLiveUrl;
}
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 From Branch live url : ', prBranchLiveUrl);
} 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.`);
process.exit(1);
}
}
async function getGitHubMiloLibsBranchLiveUrl() {
const repository = process.env.GITHUB_REPOSITORY;
let prBranchLiveUrl;
let miloLibs;
prBranchLiveUrl = process.env.PR_BRANCH_MILOLIBS_LIVE_URL;
miloLibs = process.env.MILO_LIBS;
try {
if (await isBranchURLValid(prBranchLiveUrl)) {
process.env.PR_BRANCH_LIVE_URL = prBranchLiveUrl;
}
console.info('PR Repository : ', repository);
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.`);
process.exit(1);
}
}
async function getCircleCIBranchLiveUrl() {
const stageBranchLiveUrl = STAGE_BRANCH_URL;
try {
if (await isBranchURLValid(stageBranchLiveUrl)) {
process.env.PR_BRANCH_LIVE_URL = stageBranchLiveUrl;
}
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.');
process.exit(1);
}
}
async function getLocalBranchLiveUrl() {
try {
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 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();
let localTestLiveUrl;
if (localRepo === 'nala' || localRepo === 'janus') {
localTestLiveUrl = process.env.FEATURE_BRANCH_LIVE_URL || MAIN_BRANCH_LIVE_URL;
} else {
localTestLiveUrl = LOCALHOST_URL;
}
if (await isBranchURLValid(localTestLiveUrl)) {
process.env.LOCAL_TEST_LIVE_URL = localTestLiveUrl;
}
console.info('Git ORG : ', localOrg);
console.info('Git REPO : ', localRepo);
console.info('Local Branch : ', localBranch);
console.info('Local Test Live URL : ', process.env.LOCAL_TEST_LIVE_URL);
}
}
} catch (error) {
console.error(`Error => Error in setting local test URL : ${localTestLiveUrl}`);
console.info(`Note: Local or branch test url is not valid, Exiting test execution.`);
process.exit(1);
}
}
async function globalSetup() {
console.info('----Executing Global setup---------');
if (process.env.GITHUB_ACTIONS === 'true') {
console.info('---- Running Tests in the GitHub environment ---------');
if (process.env.MILO_LIBS_RUN === 'true') {
await getGitHubMiloLibsBranchLiveUrl();
} else {
await getGitHubPRBranchLiveUrl();
}
} else if (process.env.CIRCLECI) {
console.info('---- Running Tests in the CircleCI environment ---------');
await getCircleCIBranchLiveUrl();
} else {
console.info('---- Running Tests in the Local environment ---------');
await getLocalBranchLiveUrl();
}
}
export default globalSetup;