From 45534932104b7aaa985d290522f5dd0d18ae0bcb Mon Sep 17 00:00:00 2001 From: Karolina Rakoczy Date: Mon, 27 May 2024 10:31:15 +0200 Subject: [PATCH] Run cypress manual with testmo and slack (#4803) (#4860) * Run cypress manual with testmo and slack * Run cypress manual with testmo and slack * Run cypress manual with testmo and slack * Run cypress manual with testmo and slack * post tests results even if some tests fail * Add cypress tests manual * Add changeset * Remove on push trigger * Fix workflow name * Remove info about number of failed/passed tests --- .changeset/gorgeous-crews-attend.md | 5 + .github/actions/e2e/action.yml | 4 + .github/actions/testmo/testmo-init/action.yml | 6 +- .github/workflows/postTestsResults.js | 89 +++++++++++ .github/workflows/run-test-manual.yml | 145 ++++++++++++++++++ 5 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 .changeset/gorgeous-crews-attend.md create mode 100644 .github/workflows/postTestsResults.js create mode 100644 .github/workflows/run-test-manual.yml diff --git a/.changeset/gorgeous-crews-attend.md b/.changeset/gorgeous-crews-attend.md new file mode 100644 index 00000000000..3fc9952a2ea --- /dev/null +++ b/.changeset/gorgeous-crews-attend.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Add manual workflow for cypress tests diff --git a/.github/actions/e2e/action.yml b/.github/actions/e2e/action.yml index d2c1cf931d5..0bb2a4363f0 100644 --- a/.github/actions/e2e/action.yml +++ b/.github/actions/e2e/action.yml @@ -52,6 +52,10 @@ inputs: runs: using: "composite" steps: + - name: Install dependencies + shell: bash + run: npm ci + - name: e2e with reports uses: cypress-io/github-action@v6 with: diff --git a/.github/actions/testmo/testmo-init/action.yml b/.github/actions/testmo/testmo-init/action.yml index 287cd36ba8e..ed3de6d47e1 100644 --- a/.github/actions/testmo/testmo-init/action.yml +++ b/.github/actions/testmo/testmo-init/action.yml @@ -7,6 +7,9 @@ inputs: testmoToken: description: "Testmo token" required: true + testmoRunName: + description: "Displayed name in testmo" + required: true outputs: testmo-run-id: description: "Testmo run id" @@ -29,10 +32,11 @@ runs: ID=$(npx testmo automation:run:create \ --instance "$TESTMO_URL" \ --project-id 1 \ - --name "Deployment tests" \ + --name "$RUN_NAME" \ --source frontend-e2e-tests) echo "TESTMO_RUN_ID=$ID" >> $GITHUB_OUTPUT env: TESTMO_URL: ${{ inputs.testmoUrl }} TESTMO_TOKEN: ${{ inputs.testmoToken }} + RUN_NAME: ${{inputs.testmoRunName}} id: run-tests diff --git a/.github/workflows/postTestsResults.js b/.github/workflows/postTestsResults.js new file mode 100644 index 00000000000..c97dd80f137 --- /dev/null +++ b/.github/workflows/postTestsResults.js @@ -0,0 +1,89 @@ +const { Command } = require("commander"); +const program = new Command(); + +program + .name("Send tests results") + .description( + "Get tests results from testmo and post message to slack or on release PR", + ) + .option("--run_id ", "Testmo run id") + .option( + "--testmo_token ", + "Bearer token for authorization in testmo", + ) + .option( + "--slack_webhook_url ", + "Should send notification on slack", + ) + .option("--environment ", "Environment") + .option("--url_to_action ", "Url to enter github action") + .option("--ref_name ", "Ref to point where tests where run") + .action(async options => { + const runId = options.run_id; + const testmoAuthToken = options.testmo_token; + const testsResults = await getTestsStatus(runId, testmoAuthToken); + const testsStatus = convertResults( + testsResults, + options.environment, + options.ref_name, + ); + + await sendMessageOnSlack( + testsStatus, + options.slack_webhook_url, + options.url_to_action, + ); + }) + .parse(); + +async function getTestsStatus(runId, testmoToken) { + const runResult = await fetch( + `https://saleor.testmo.net/api/v1/automation/runs/${runId}`, + { + headers: { + Authorization: `Bearer ${testmoToken}`, + }, + }, + ); + return await runResult.json(); +} + +function convertResults(results, environment, refName) { + let status = results?.result?.status === 2 ? "SUCCESS" : "FAILURE"; + let message = `Tests run on environment: \n${environment}\n`; + const linkToResults = `https:\/\/saleor.testmo.net\/automation\/runs\/view\/${results.result.id}`; + + message += `See results at ${linkToResults}`; + + return { + status, + message, + title: `Tests run on ${refName}`, + linkToResults, + }; +} + +async function sendMessageOnSlack(testsStatus, webhookUrl, urlToAction) { + const JOB_STATUS_COLOR_MAP = { + SUCCESS: "#5DC292", + FAILURE: "#FE6E76", + }; + + const messageData = { + attachments: [ + { + fallback: testsStatus.message, + pretext: testsStatus.status, + title: testsStatus.title, + title_link: urlToAction, + text: testsStatus.message, + color: JOB_STATUS_COLOR_MAP[testsStatus.status], + }, + ], + }; + await fetch(webhookUrl, { + body: JSON.stringify(messageData), + method: "post", + headers: { "content-type": "application/json" }, + }); +} diff --git a/.github/workflows/run-test-manual.yml b/.github/workflows/run-test-manual.yml new file mode 100644 index 00000000000..067378ecdc7 --- /dev/null +++ b/.github/workflows/run-test-manual.yml @@ -0,0 +1,145 @@ +name: Run test manually +run-name: Run cypress tests + +on: + workflow_dispatch: + inputs: + tags: + required: true + description: "Select tests to run" + default: "@allEnv" + type: choice + options: + - "@allEnv" + - "@critical" + - "@stable" + - "@oldRelease" + browser: + required: true + description: "Browser" + default: "electron" + type: choice + options: + - electron + - firefox + - all +jobs: + + testmo-report-preparation: + runs-on: ubuntu-22.04 + outputs: + TESTMO_RUN_ID: ${{ steps.init-testmo.outputs.testmo-run-id }} + BASE_URL: ${{ steps.set_variables.outputs.BASE_URL }} + API_URL: ${{ steps.set_variables.outputs.API_URL }} + steps: + - uses: actions/checkout@v4 + + - name: Set variables + id: set_variables + run: | + CURRENT_BRANCH=$(echo "${GITHUB_REF}" | sed "s/refs\/heads\///") + VERSION_SLUG=$(echo "${CURRENT_BRANCH}" | sed "s/\.//") + echo "BASE_URL=https://v${VERSION_SLUG}.staging.saleor.cloud/dashboard/" >> $GITHUB_OUTPUT + echo "API_URL=https://v${VERSION_SLUG}.staging.saleor.cloud/graphql/" >> $GITHUB_OUTPUT + + + - uses: ./.github/actions/testmo/testmo-init + with: + testmoUrl: ${{ secrets.TESTMO_URL }} + testmoToken: ${{ secrets.TESTMO_TOKEN }} + testmoRunName: "Cypress run ${{ github.ref_name }}" + id: init-testmo + + run-tests-in-parallel: + needs: testmo-report-preparation + runs-on: ubuntu-22.04 + container: + image: cypress/browsers:node18.12.0-chrome106-ff106 + options: --user 1001 + env: + GREP_TAGS: ${{ github.event.inputs.tags || '@critical'}} + BROWSER: ${{ inputs.browser || 'electron' }} + strategy: + fail-fast: false + matrix: + containers: [1, 2, 3, 4, 5, 6] + steps: + - uses: actions/checkout@v4 + + + - name: Cypress run + id: cypress-run + uses: ./.github/actions/e2e + continue-on-error: true + with: + apiUrl: ${{ needs.testmo-report-preparation.outputs.API_URL}} + appMountUri: ${{ secrets.APP_MOUNT_URI }} + baseUrl: ${{ needs.testmo-report-preparation.outputs.BASE_URL}} + userName: ${{ secrets.CYPRESS_USER_NAME }} + secondUserName: ${{ secrets.CYPRESS_SECOND_USER_NAME }} + userPassword: ${{ secrets.CYPRESS_USER_PASSWORD }} + permissionsUserPassword: ${{ secrets.CYPRESS_PERMISSIONS_USERS_PASSWORD }} + mailpitUrl: ${{ secrets.CYPRESS_MAILPITURL }} + stripeSecretKey: ${{ secrets.STRIPE_SECRET_KEY }} + stripePublicKey: ${{ secrets.STRIPE_PUBLIC_KEY }} + cypressGrepTags: ${{ env.GREP_TAGS }} + split: ${{ strategy.job-total }} + splitIndex: ${{ strategy.job-index }} + commitInfoMessage: All tests triggered via ${{ github.event_name}} on ${{ steps.get-env-uri.outputs.ENV_URI }} + install: false + browser: ${{ env.BROWSER }} + + - name: Testmo threads submit + uses: ./.github/actions/testmo/testmo-threads-submit + with: + testmoUrl: ${{ secrets.TESTMO_URL }} + testmoToken: ${{ secrets.TESTMO_TOKEN }} + testmoRunId: ${{ needs.testmo-report-preparation.outputs.TESTMO_RUN_ID }} + + + post-tests-results: + defaults: + run: + shell: bash + working-directory: .github/workflows + runs-on: ubuntu-22.04 + needs: [run-tests-in-parallel, testmo-report-preparation] + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + + - name: Install dependencies + run: npm ci + + - uses: ./.github/actions/combineReportsFromE2E + + - name: complete testmo report + uses: ./.github/actions/testmo/testmo-finish + with: + testmoUrl: ${{ secrets.TESTMO_URL }} + testmoToken: ${{ secrets.TESTMO_TOKEN }} + testmoRunId: ${{ needs.testmo-report-preparation.outputs.TESTMO_RUN_ID }} + + - name: send message on slack + working-directory: ".github" + env: + run_id: ${{ needs.testmo-report-preparation.outputs.TESTMO_RUN_ID }} + url_to_action: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + testmo_token: ${{ secrets.TESTMO_TOKEN }} + environment: ${{ needs.testmo-report-preparation.outputs.BASE_URL }} + slack_webhook_url: ${{ secrets.SLACK_QA_STATUSES_WEBHOOK_URL }} + ref_name: ${{github.ref_name}} + + run: | + node workflows/postTestsResults.js \ + --run_id "$run_id" \ + --testmo_token "$testmo_token" \ + --slack_webhook_url "$slack_webhook_url" \ + --environment "$environment" \ + --url_to_action "$url_to_action" \ + --ref_name "$ref_name"