Skip to content

Commit

Permalink
22706 configure gha and it for cli (#25674)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcolina authored Aug 18, 2023
1 parent ff9a106 commit d8325ad
Show file tree
Hide file tree
Showing 45 changed files with 923 additions and 190 deletions.
12 changes: 0 additions & 12 deletions .github/actions/github-status/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@ author: 'victoralfaro-dotcms'
inputs:
test_type:
description: 'Test Type'
type: choice
options:
- unit
- integration
- postman
required: true
db_type:
description: 'Database type'
type: choice
options:
- postgres
- mysql
required: false
test_results_status:
description: 'Status of run tests'
type: choice
Expand Down
35 changes: 6 additions & 29 deletions .github/actions/github-status/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ const node_fetch_1 = __importDefault(__nccwpck_require__(4429));
* Sends the tests results statsus to Github using its API.
*
* @param testType test type
* @param dbType database type
* @param testResultsStatus test results status (PASSED or FAILED)
*/
const send = (testType, dbType, testResultsStatus) => __awaiter(void 0, void 0, void 0, function* () {
const send = (testType, testResultsStatus) => __awaiter(void 0, void 0, void 0, function* () {
const pullRequest = core.getInput('pull_request');
if (!pullRequest) {
core.warning(`This was not triggered from a pull request, so skipping sending status `);
Expand All @@ -69,43 +68,23 @@ const send = (testType, dbType, testResultsStatus) => __awaiter(void 0, void 0,
}
const pr = (yield prResponse.json());
const testsReportUrl = core.getInput('tests_report_url');
const status = createStatus(testType, dbType, testResultsStatus, testsReportUrl);
const status = createStatus(testType, testResultsStatus, testsReportUrl);
const statusResponse = yield postStatus(pr._links.statuses.href, creds, status);
if (!statusResponse.ok) {
core.warning(`Could not send Github status for ${testType} tests`);
return;
}
});
exports.send = send;
/**
* Resolves what label to use based on the test type.
*
* @param testType test type
* @param dbType database type
* @returns status label
*/
const resolveStastusLabel = (testType, dbType) => {
switch (testType) {
case 'unit':
return '[Unit tests results]';
case 'integration':
return `[Integration tests results] - [${dbType}]`;
case 'postman':
return '[Postman tests results]';
default:
return '';
}
};
/**
* Creates a status object based on the provided params.
*
* @param testType test type
* @param dbType database type
* @param testResultsStatus test results status
* @param testsReportUrl report url where tests results are located
* @returns {@link GithubStatus} object to be used when reporting
*/
const createStatus = (testType, dbType, testResultsStatus, testsReportUrl) => {
const createStatus = (testType, testResultsStatus, testsReportUrl) => {
let statusLabel;
let description;
if (testResultsStatus === 'PASSED') {
Expand All @@ -120,7 +99,7 @@ const createStatus = (testType, dbType, testResultsStatus, testsReportUrl) => {
state: statusLabel,
description,
target_url: testsReportUrl,
context: `Github Actions - ${resolveStastusLabel(testType, dbType)}`
context: `Github Actions - [${testType.toUpperCase()} tests results]`
};
};
/**
Expand Down Expand Up @@ -209,11 +188,9 @@ const github = __importStar(__nccwpck_require__(6684));
*/
const run = () => {
const testType = core.getInput('test_type');
const dbType = core.getInput('db_type');
const testResultsStatus = core.getInput('test_results_status');
const dbLabel = !!dbType ? ` with ${dbType} database` : '';
core.info(`Submitting ${testResultsStatus} status to Github for ${testType} tests${dbLabel}`);
github.send(testType, dbType, testResultsStatus);
core.info(`Submitting ${testResultsStatus} status to Github for ${testType} tests`);
github.send(testType, testResultsStatus);
};
// Run main function
run();
Expand Down
35 changes: 4 additions & 31 deletions .github/actions/github-status/src/github-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ interface LinksSupport {
* Sends the tests results statsus to Github using its API.
*
* @param testType test type
* @param dbType database type
* @param testResultsStatus test results status (PASSED or FAILED)
*/
export const send = async (testType: string, dbType: string, testResultsStatus: string) => {
export const send = async (testType: string, testResultsStatus: string) => {
const pullRequest = core.getInput('pull_request')
if (!pullRequest) {
core.warning(`This was not triggered from a pull request, so skipping sending status `)
Expand All @@ -43,49 +42,23 @@ export const send = async (testType: string, dbType: string, testResultsStatus:

const pr = (await prResponse.json()) as LinksSupport
const testsReportUrl = core.getInput('tests_report_url')
const status = createStatus(testType, dbType, testResultsStatus, testsReportUrl)
const status = createStatus(testType, testResultsStatus, testsReportUrl)
const statusResponse = await postStatus(pr._links.statuses.href, creds, status)
if (!statusResponse.ok) {
core.warning(`Could not send Github status for ${testType} tests`)
return
}
}

/**
* Resolves what label to use based on the test type.
*
* @param testType test type
* @param dbType database type
* @returns status label
*/
const resolveStastusLabel = (testType: string, dbType: string): string => {
switch (testType) {
case 'unit':
return '[Unit tests results]'
case 'integration':
return `[Integration tests results] - [${dbType}]`
case 'postman':
return '[Postman tests results]'
default:
return ''
}
}

/**
* Creates a status object based on the provided params.
*
* @param testType test type
* @param dbType database type
* @param testResultsStatus test results status
* @param testsReportUrl report url where tests results are located
* @returns {@link GithubStatus} object to be used when reporting
*/
const createStatus = (
testType: string,
dbType: string,
testResultsStatus: string,
testsReportUrl: string
): GithubStatus => {
const createStatus = (testType: string, testResultsStatus: string, testsReportUrl: string): GithubStatus => {
let statusLabel
let description
if (testResultsStatus === 'PASSED') {
Expand All @@ -100,7 +73,7 @@ const createStatus = (
state: statusLabel,
description,
target_url: testsReportUrl,
context: `Github Actions - ${resolveStastusLabel(testType, dbType)}`
context: `Github Actions - [${testType.toUpperCase()} tests results]`
}
}

Expand Down
6 changes: 2 additions & 4 deletions .github/actions/github-status/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import * as github from './github-status'
*/
const run = () => {
const testType = core.getInput('test_type')
const dbType = core.getInput('db_type')
const testResultsStatus = core.getInput('test_results_status')

const dbLabel = !!dbType ? ` with ${dbType} database` : ''
core.info(`Submitting ${testResultsStatus} status to Github for ${testType} tests${dbLabel}`)
github.send(testType, dbType, testResultsStatus)
core.info(`Submitting ${testResultsStatus} status to Github for ${testType} tests`)
github.send(testType, testResultsStatus)
}

// Run main function
Expand Down
117 changes: 117 additions & 0 deletions .github/workflows/cli-cicd-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: CLI tests
on:
pull_request:
paths:
- 'tools/dotcms-cli/**'
push:
branches:
- master
- release-*
jobs:
cli_tests:
name: CLI tests
runs-on: ubuntu-latest
outputs:
tests_results_status: ${{ steps.run-cli-tests.outputs.status }}
tests_results_report_url: ${{ steps.publish-test-results.outputs.tests_report_url }}
tests_results_log_url: ${{ steps.publish-test-results.outputs.test_logs_url }}
steps:
- id: checkout-core
name: Checkout core
uses: actions/checkout@v3
- id: prepare-license
name: Prepare license
working-directory: ./tools/dotcms-cli/
env:
DOTCMS_LICENSE_KEY: ${{ secrets.DOTCMS_LICENSE }}
run: |
DOTCMS_LICENSE_PATH=${GITHUB_WORKSPACE}/tools/dotcms-cli/license
mkdir -p ${DOTCMS_LICENSE_PATH}
echo "${DOTCMS_LICENSE_KEY}" > ${DOTCMS_LICENSE_PATH}/license.dat
echo "DOTCMS_LICENSE_FILE=${DOTCMS_LICENSE_PATH}/license.dat" >> "$GITHUB_ENV"
- id: run-cli-tests
name: Run cli tests
working-directory: ./tools/dotcms-cli/
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
BUILD_ID="${{ github.head_ref }}"
else
BUILD_ID=$(basename "${{ github.ref }}")
fi
./mvnw clean verify
FAILURES=$(find target -name TEST-*.xml | xargs grep '<failure' | wc -l)
if [ $FAILURES -eq 0 ]; then
OUTPUT_STATUS="PASSED"
else
OUTPUT_STATUS="FAILED"
fi
echo "status=$OUTPUT_STATUS" >> $GITHUB_OUTPUT
./mvnw surefire-report:report-only surefire-report:failsafe-report-only
DOT_CLI_PATH=${{ github.workspace }}/tools/dotcms-cli
RESULTS_PATH=${DOT_CLI_PATH}/target/test-results
XML_REPORTS_PATH=${RESULTS_PATH}/xml-reports
HTML_REPORTS_PATH=${RESULTS_PATH}/html-reports
POM_FILE=${DOT_CLI_PATH}/pom.xml
CICD_RES_PATH=${{ github.workspace }}/cicd/resources
INDEX_FILE=${HTML_REPORTS_PATH}/index.html
mkdir -p ${HTML_REPORTS_PATH}
touch ${INDEX_FILE}
cat ${CICD_RES_PATH}/cli/cli-results-header.html >> ${INDEX_FILE}
MODULES=$(grep "<module>" $POM_FILE | sed 's/<module>//' | sed 's/<\/module>//')
for module in $MODULES
do
mkdir -p ${XML_REPORTS_PATH}/${module}
cp -r ${module}/target/surefire-reports ${XML_REPORTS_PATH}/${module}
cp -r ${module}/target/failsafe-reports ${XML_REPORTS_PATH}/${module}
mkdir -p ${HTML_REPORTS_PATH}/${module}
cp -r ${module}/target/site/* ${HTML_REPORTS_PATH}/${module}
for report in surefire failsafe
do
echo "<tr><td>${module}</td><td><a href=\"${module}/site/${report}-report.html\">${report} report</a></td></tr>" >> ${INDEX_FILE}
done
done
cat ${CICD_RES_PATH}/cli/cli-results-footer.html >> ${INDEX_FILE}
echo "BUILD_ID=${BUILD_ID}" >> "$GITHUB_ENV"
echo "results_location=${XML_REPORTS_PATH}" >> "$GITHUB_OUTPUT"
echo "results_report_location=${HTML_REPORTS_PATH}" >> "$GITHUB_OUTPUT"
tree ${RESULTS_PATH}
- id: publish-test-results
name: Github publish tests results
uses: ./.github/actions/publish-test-results
with:
build_id: ${{ env.BUILD_ID }}
build_hash: ${{ env.BUILD_HASH }}
test_type: cli
pull_request: ${{ github.event.number }} # optional in case the tests are triggered by a PR
tests_results_status: ${{ steps.run-cli-tests.outputs.status }} # tests results status, optional
tests_results_location: ${{ steps.run-cli-tests.outputs.results_location }} # tests results location, optional, most of the time this made up of a bunch of XML files, optional
tests_results_report_location: ${{ steps.run-cli-tests.outputs.results_report_location }} # actual reports made from XML files, probably in HTML format
tests_results_repo: tools-test-results
cicd_github_token: ${{ secrets.CICD_GITHUB_TOKEN }}
include: RESULTS

- id: github-status
name: Send Github Status
uses: ./.github/actions/github-status
with:
test_type: cli
test_results_status: ${{ steps.run-cli-tests.outputs.status }}
pull_request: ${{ github.event.number }}
github_user: ${{ env.GITHUB_USER }}
cicd_github_token: ${{ secrets.CICD_GITHUB_TOKEN }}
tests_report_url: ${{ steps.publish-test-results.outputs.tests_report_url }}
if: (success() || failure()) && steps.publish-test-results.outputs.tests_report_url != ''
5 changes: 5 additions & 0 deletions cicd/resources/cli/cli-results-footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
</tbody>
</table>
</div>
</body>
</html>
17 changes: 17 additions & 0 deletions cicd/resources/cli/cli-results-header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CLI Reports</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,700&display=swap" />
<link rel="stylesheet" href="https://dotcms.com/DOTSASS/application/themes/dotcms/css/main.css?v=05-21-2020" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
</head>
<body>
<div>
<h2>CLI Reports</h2>
<table style="width: 60%;" class="table table-bordered mb-5">
<tbody>
<tr><th>Module</th><th>Report</th></tr>
Loading

0 comments on commit d8325ad

Please sign in to comment.