Skip to content

Commit

Permalink
HMS-5287: Add playwright and github action to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewgdewar committed Jan 30, 2025
1 parent 13ef705 commit a2c9128
Show file tree
Hide file tree
Showing 22 changed files with 1,294 additions and 32 deletions.
Binary file added .DS_Store
Binary file not shown.
75 changes: 75 additions & 0 deletions .github/workflowScripts/getFrontendRepo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# Check if the search string is provided as an argument
if [ -z "$1" ]; then
echo "Usage: $0 <search-string>"
exit 1
fi

# Variables
REPO_URL="https://api.github.com/repos/content-services/content-sources-frontend"
GIT_REPO_URL="https://github.com/content-services/content-sources-frontend.git"
CLONE_DIR=content-sources-frontend
TAG_NAME="#testwith"
SEARCH_STRING="$1"

# Check if the folder exists
if [ -d "$CLONE_DIR" ]; then
echo "Folder '$CLONE_DIR' exists. Removing it..."
rm -rf "$CLONE_DIR"

# Check if the removal was successful
if [ ! -d "$CLONE_DIR" ]; then
echo "Successfully removed the folder '$CLONE_DIR'."
else
echo "Failed to remove the folder '$CLONE_DIR'."
fi
fi

# Create clone directory if it doesn't exist
mkdir -p $CLONE_DIR

# Fetch the list of open pull requests
prs=$(curl -s "$REPO_URL/pulls")

# Iterate through the list of PRs and clone the first matching PR
found_pr=false

while read -r pr; do
# Extract PR title, body, and branch details
pr_title=$(echo "$pr" | jq -r '.title')
pr_body=$(echo "$pr" | jq -r '.body')
pr_number=$(echo "$pr" | jq -r '.number')
pr_branch=$(echo "$pr" | jq -r '.head.ref')
pr_repo=$(echo "$pr" | jq -r '.head.repo.clone_url')

# Check if PR title or body contains the search string
if [[ "$pr_body" == *"$TAG_NAME"* ]] && [[ "$pr_body" == *"$SEARCH_STRING"* ]]; then
echo "Cloning PR #$pr_number: $pr_title"
git clone --branch $pr_branch $pr_repo $CLONE_DIR

# Check if the clone was successful
if [ $? -eq 0 ]; then
found_pr=true
echo "Successfully cloned PR #$pr_number into $CLONE_DIR"
else
echo "Failed to clone PR #$pr_number"
fi

# Exit the loop after cloning the first matching PR
break
fi
done < <(echo "$prs" | jq -c '.[]')

# If no matching PR was found, clone the main branch
if [ "$found_pr" == false ]; then
echo "No PR title or description contains '$TAG_NAME $SEARCH_STRING'. Cloning the main branch."
git clone --branch main $GIT_REPO_URL $CLONE_DIR

# Check if the clone was successful
if [ $? -eq 0 ]; then
echo "Successfully cloned main branch into $CLONE_DIR"
else
echo "Failed to clone the main branch"
fi
fi
34 changes: 19 additions & 15 deletions .github/workflows/content-sources-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ on:
branches:
- main
paths-ignore:
- '**.md'
- "**.md"
pull_request:
paths-ignore:
- '**.md'
- "**.md"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
openapidiff:
name: Openapi diff
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.23"
- run: |
Expand All @@ -28,7 +33,7 @@ jobs:
name: openapi validate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: openapi-generators/openapitools-generator-action@v1
with:
generator: python
Expand All @@ -38,8 +43,8 @@ jobs:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.23"
- name: golangci-lint
Expand All @@ -48,12 +53,12 @@ jobs:
version: v1.61.0
skip-go-installation: true
args: --timeout=5m

checkmigrations:
name: Check db migrations
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: tj-actions/changed-files@v41
id: files
- run: |
Expand All @@ -62,12 +67,12 @@ jobs:
migrations_latest=$(cat db/migrations.latest)
num_files_added=${{ steps.files.outputs.added_files_count }}
if [ "$num_files_added" -gt 0 ]; then
if [ "$num_files_added" -gt 0 ]; then
new_migrations_added=$(echo "${{ steps.files.outputs.added_files }}" | grep 'db/migrations' || true)
if [ -n "$new_migrations_added" ]; then
if echo "$new_migrations_added" | grep -q "$migrations_latest"; then
echo "OK: Latest migration content (db/migrations.latest) reflects the timestamp of the latest migrations added."
else
else
echo "Error: Latest migration content (db/migrations.latest) does NOT reflect the timestamp of the latest migrations added."
echo "New migration files added: $new_migrations_added"
echo "In db/migration.latest: $migrations_latest"
Expand Down Expand Up @@ -96,8 +101,8 @@ jobs:
ports:
- 5433:5432
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.23"
- name: unit tests
Expand All @@ -118,8 +123,7 @@ jobs:
CLIENTS_CANDLEPIN_DEVEL_ORG: true
- name: Add hosts to /etc/hosts
run: |
# allows pulp container to talk to the host at this hostname
sudo echo "172.17.0.1 pulp.content" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 pulp.content" | sudo tee -a /etc/hosts
- name: start pulp
uses: isbang/[email protected]
with:
Expand Down
221 changes: 221 additions & 0 deletions .github/workflows/playwright-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
name: build-playwright-test
on:
push:
branches:
- main
paths-ignore:
- "**.md"
pull_request:
paths-ignore:
- "**.md"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
playwright-tests:
runs-on:
- codebuild-content-services-backend-repo-${{ github.run_id }}-${{ github.run_attempt }}
- instance-size:medium
- buildspec-override:true

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Get current PR URL
id: get-pr-url
run: |
# Extract the pull request URL from the event payload
pr_url=$(jq -r '.pull_request.html_url' < "$GITHUB_EVENT_PATH")
echo "Pull Request URL: $pr_url"
# Set the PR URL as an output using the environment file
echo "pr_url=$pr_url" >> $GITHUB_ENV
- name: Get appropriate front-end branch
run: |
chmod +x .github/workflowScripts/getFrontendRepo.sh
.github/workflowScripts/getFrontendRepo.sh "${{ env.pr_url }}"
- name: Compare .nvmrc files
id: compare-nvmrc
run: |
# Get both .nvmrc files
backendNvmrc="_playwright-tests/.nvmrc"
frontendNvmrc="content-sources-frontend/.nvmrc"
# Check if both files exist
if [ ! -f "$backendNvmrc" ]; then
echo "Error: $backendNvmrc does not exist."
exit 1
fi
if [ ! -f "$frontendNvmrc" ]; then
echo "Error: $frontendNvmrc does not exist."
exit 1
fi
# Compare the contents of the two files
if ! cmp -s "$backendNvmrc" "$frontendNvmrc"; then
echo "Error: The .nvmrc files are not the same."
exit 1
fi
echo "The .nvmrc versions are the same!"
- name: Cache - API-test node_modules
if: always()
uses: actions/cache@v4
with:
path: "_playwright-tests/node_modules"
key: ${{ runner.os }}-api-node-modules-${{ hashFiles('_playwright-tests/yarn.lock') }}
restore-keys: |
${{ runner.os }}-api-node-modules-
- name: Cache - Front-end-test node_modules
if: always()
uses: actions/cache@v4
with:
path: "content-sources-frontend/node_modules"
key: ${{ runner.os }}-frontend-node-modules-${{ hashFiles('content-sources-frontend/yarn.lock') }}
restore-keys: |
${{ runner.os }}-frontend-node-modules-
- name: Cache - Compose files
if: always()
uses: actions/cache@v4
with:
path: |
release
key: ${{ runner.os }}-compose-${{ hashFiles('compose_files/pulp/docker-compose.yml') }}-${{ hashFiles('compose_files/candlepin/Dockerfile') }}
restore-keys: |
${{ runner.os }}-compose-
- name: Cache Playwright browsers
if: always()
id: cache-playwright-browsers
uses: actions/cache@v3
with:
path: ~/.cache/ms-playwright
key: ${{ runner.os }}-playwright-${{ hashFiles('_playwright-tests/playwright.config.ts') }}
restore-keys: |
${{ runner.os }}-playwright-
- name: Run get_go_version.sh script
id: get-go-version
run: |
chmod +x ./scripts/get_go_version.sh
GO_VERSION=$(./scripts/get_go_version.sh ./go.mod)
echo "go-version=$GO_VERSION" >> $GITHUB_OUTPUT
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "${{ steps.get-go-version.outputs.go-version }}"

- name: Set up Make
run: |
sudo apt-get install make
- name: Copy the example config
run: |
cp ./configs/config.yaml.example ./configs/config.yaml
- name: Create backend .env file
working-directory: _playwright-tests
run: |
echo "BASE_URL=http://127.0.0.1:8000" >> .env
echo "TOKEN=apple" >> .env
echo "CI=true" >> .env
- name: Backend compose-up
run: make compose-down compose-clean compose-up

- name: Backend run
run: make run &

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version-file: "_playwright-tests/.nvmrc"

- name: Install API playwright and dependencies
working-directory: _playwright-tests
run: yarn install

- name: Install Playwright Browsers
working-directory: _playwright-tests
run: yarn playwright install --with-deps

- name: Run API Playwright tests
working-directory: _playwright-tests
run: yarn playwright test
# continue-on-error: true

- name: Publish API Test Report
uses: ctrf-io/github-test-reporter@v1
with:
report-path: "./_playwright-tests/playwright-ctrf/playwright-ctrf.json"
if: always()

- name: Store API Test report
uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-ctrf-backend
path: ./_playwright-tests/playwright-ctrf
retention-days: 10

- name: Create front-end .env file
working-directory: content-sources-frontend
run: |
echo "USER1USERNAME=${{ secrets.USER1USERNAME }}" >> .env
echo "USER1PASSWORD=${{ secrets.USER1PASSWORD }}" >> .env
echo "BASE_URL=https://stage.foo.redhat.com:1337" >> .env
- name: Install front-end dependencies
working-directory: content-sources-frontend
run: yarn install

- name: Install front-end Playwright Browsers
working-directory: content-sources-frontend
run: yarn playwright install --with-deps
- name: Increase file watchers limit
run: |
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
- name: Update /etc/hosts
working-directory: content-sources-frontend
run: |
sudo npm run patch:hosts
- name: Build front-end FEC container
working-directory: content-sources-frontend
run: |
yarn build
- name: Start front-end server
working-directory: content-sources-frontend
run: |
BACKEND_PORT=8000 yarn start:stage &
npx wait-on https://localhost:1337
- name: Run front-end Playwright tests
working-directory: content-sources-frontend
run: yarn playwright test
# continue-on-error: true

- name: Publish front-end Test Report
uses: ctrf-io/github-test-reporter@v1
with:
report-path: "./content-sources-frontend/playwright-ctrf/playwright-ctrf.json"
if: always()

- name: Store front-end Test report
uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-ctrf-frontend
path: ./content-sources-frontend/playwright-ctrf
retention-days: 10
Loading

0 comments on commit a2c9128

Please sign in to comment.