Skip to content

feat: add dev and pr name check workflows #3

feat: add dev and pr name check workflows

feat: add dev and pr name check workflows #3

name: Lint Unit Tests and Sample Feature Run
on:
pull_request:
branches:
- dev
- main
jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn install
- name: Check for linting errors
run: yarn run lint > lintOutput.txt || true
# WIP - Unit tests need to be fixed before this can be enabled
# - name: Run tests
# id: test
# run: yarn run test || echo "TESTS_FAILED=true" >> $GITHUB_ENV
# NOTE - Make sure to Remove TESTS_FAILED=false from next step once unit tests are fixed
- name: Determine comment based on linting and test results
id: comment
if: ${{ github.event_name == 'pull_request' }}
run: |
TESTS_FAILED=false
LINT_ERRORS=""
if [ -f lintOutput.txt ]; then
LINT_ERRORS=$(grep -E '[1-9][0-9]*\serror' lintOutput.txt || true)
fi
if [ "$TESTS_FAILED" = "true" ] || [ -n "$LINT_ERRORS" ]; then
{
echo 'PR_COMMENT<<EOF'
echo "Issue with linting or unit tests detected."
if [ -n "$LINT_ERRORS" ]; then
echo "Linting failed with the following errors:"
echo "\`\`\`"
cat lintOutput.txt
echo "\`\`\`"
echo ""
echo "For more information on our linting policies, please see our [Linting-Guide](../tree/dev/Linting-Guide.md)."
echo ""
fi
if [ "$TESTS_FAILED" == "true" ]; then
echo "Unit tests failed."
fi
echo EOF
} >> "$GITHUB_OUTPUT"
else
echo "PR_COMMENT=Linting and unit tests succeeded." >> "$GITHUB_OUTPUT"
fi
- name: Fail if linting errors or unit tests failed
if: steps.comment.outputs.PR_COMMENT != 'Linting and unit tests succeeded.'
run: exit 1
- name: Comment on PR
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_COMMENT: ${{ steps.comment.outputs.PR_COMMENT }}
run: |
if [[ "${{ github.event_name }}" == 'pull_request' && "$PR_COMMENT" != 'Linting and unit tests succeeded.' ]]; then
JSON_PAYLOAD=$(jq -n --arg body "$PR_COMMENT" '{"body": $body}')
echo "Generated JSON payload:"
echo "$JSON_PAYLOAD"
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
fi
run_testcase:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn install
- name: Run Hello feature
run: yarn run cy:run -- --spec "cypress/TestCases/Sample/Hello.feature" --env testSuite="sample",generateLocalReport="false" > samplRunOutput.txt || true
# To remove ANSI escape characters
- name: Remove ANSI escape codes from samplRunOutput.txt
run: sed -i "s/\x1B\[[0-9;]*[JKmsu]//g" samplRunOutput.txt
- name: Determine comment based on test results
id: comment
if: ${{ github.event_name == 'pull_request' }}
run: |
if grep -q "0 passing" samplRunOutput.txt; then
FAILURE_REASON=$(sed -n '/1 failing/,/\[mochawesome\]/ {
/1 failing/b
/\[mochawesome\]/b
p
}' samplRunOutput.txt > failure_reason.txt)
echo "PR_COMMENT=Hello feature Run failed" >> "$GITHUB_OUTPUT"
else
echo "PR_COMMENT=Hello feature Run Passed." >> "$GITHUB_OUTPUT"
fi
- name: Fail if run testcase failed
if: steps.comment.outputs.PR_COMMENT != 'Hello feature Run Passed.'
run: exit 1
- name: Comment on PR
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_COMMENT: ${{ steps.comment.outputs.PR_COMMENT }}
run: |
if [[ "${{ github.event_name }}" == 'pull_request' && "$PR_COMMENT" != 'Hello feature Run Passed.' ]]; then
# Read the content of failure_reason.txt into a variable
FAILURE_REASON_CONTENT=$(cat failure_reason.txt)
JSON_PAYLOAD=$(jq -n --arg body "$PR_COMMENT :: $FAILURE_REASON_CONTENT" '{"body": $body}')
echo "Generated JSON payload:"
echo "$JSON_PAYLOAD"
curl -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
fi