diff --git a/.github/workflows/CLA.yml b/.github/workflows/CLA.yml index cd4e6bf..f5dd18e 100644 --- a/.github/workflows/CLA.yml +++ b/.github/workflows/CLA.yml @@ -1,29 +1,29 @@ -name: "CLA Assistant" +# name: "CLA Assistant" -on: - issue_comment: - types: - - "created" - pull_request_target: - types: - - "opened" - - "closed" - - "synchronize" +# on: +# issue_comment: +# types: +# - "created" +# pull_request_target: +# types: +# - "opened" +# - "closed" +# - "synchronize" -jobs: - cla-assistant: - runs-on: "ubuntu-latest" - steps: - - name: "CLA Assistant" - if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA') || github.event_name == 'pull_request_target') - uses: "cla-assistant/github-action@v2.1.3-beta" - env: - GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" - PERSONAL_ACCESS_TOKEN: "${{ secrets.PRO_ACCESS_TOKEN }}" - with: - remote-organization-name: "keploy" - remote-repository-name: "keploy" - path-to-signatures: "etc/cla-signatures/signatures.json" - path-to-document: "https://github.com/keploy/keploy/tree/main/.github/CLA.md" - branch: "cla-signatures" - allowlist: "keploy-bot,renovate" \ No newline at end of file +# jobs: +# cla-assistant: +# runs-on: "ubuntu-latest" +# steps: +# - name: "CLA Assistant" +# if: github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA') || github.event_name == 'pull_request_target' +# uses: "cla-assistant/github-action@v2.1.3-beta" +# env: +# GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" +# PERSONAL_ACCESS_TOKEN: "${{ secrets.PRO_ACCESS_TOKEN }}" +# with: +# remote-organization-name: "keploy" +# remote-repository-name: "keploy" +# path-to-signatures: "etc/cla-signatures/signatures.json" +# path-to-document: "https://github.com/keploy/keploy/tree/main/.github/CLA.md" +# branch: "cla-signatures" +# allowlist: "keploy-bot,renovate" \ No newline at end of file diff --git a/.github/workflows/keploy-lint.yml b/.github/workflows/keploy-lint.yml new file mode 100644 index 0000000..175e515 --- /dev/null +++ b/.github/workflows/keploy-lint.yml @@ -0,0 +1,140 @@ +name: Keploy Test & Lint + +on: + pull_request: + branches: [ main ] + +jobs: + keploy-test-and-lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Run Keploy TestGPT Action + id: keploy-test-report + uses: keploy/testGPT@main + with: + working-directory: ./ # Adjust if project structure differs + command: 'your-test-command-here' # Update this + keploy-path: ./ # Path where Keploy binary is installed + delay: 10 + + + + - name: Fetch PR Details + id: pr-details + uses: actions/github-script@v6 + with: + github-token: ${{ github.token }} + script: | + const pr = context.payload.pull_request; + if (!pr) { + console.log("Not a PR event."); + return; + } + const prDetails = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr.number, + }); + const changedFiles = await github.paginate(github.rest.pulls.listFiles, { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr.number, + per_page: 100, + }); + const filePaths = changedFiles.map(file => file.filename); + console.log("Changed Files:", filePaths); + const metadata = { + title: pr.title, + author: pr.user.login, + files: filePaths + }; + console.log("PR Metadata:", metadata); + + return metadata; + + + + - name: Detect Programming Languages + id: lang-detect + run: | + echo "Detecting languages..." + files=$(echo '${{ steps.pr-details.outputs.result }}' | jq -r '.files[]') + + touch languages.txt + for file in $files; do + ext="${file##*.}" + case "$ext" in + js|ts) echo "JavaScript" >> languages.txt ;; + go) echo "Go" >> languages.txt ;; + py) echo "Python" >> languages.txt ;; + *) echo "Unknown" >> languages.txt ;; + esac + done + + echo "Languages detected:" + cat languages.txt + shell: bash + + - name: Lint Changed Files + run: | + echo "Running linters..." + files=$(echo '${{ steps.pr-details.outputs.result }}' | jq -r '.files[]') + touch lint_report.txt + + for file in $files; do + ext="${file##*.}" + if [[ "$ext" == "js" || "$ext" == "ts" ]]; then + echo "Linting $file with ESLint..." >> lint_report.txt + eslint "$file" >> lint_report.txt 2>&1 || echo "Errors in $file" >> lint_report.txt + elif [[ "$ext" == "go" ]]; then + echo "Linting $file with Golint..." >> lint_report.txt + golint "$file" >> lint_report.txt 2>&1 + else + echo "No linter for $file" >> lint_report.txt + fi + done + + cat lint_report.txt + shell: bash + + - name: Comment Lint Results on PR + uses: actions/github-script@v6 + with: + github-token: ${{ github.token }} + script: | + const fs = require('fs'); + const lintReport = fs.readFileSync('lint_report.txt', 'utf8'); + const pr = context.payload.pull_request; + if (!pr) { + console.log("No PR context."); + return; + } + github.rest.issues.createComment({ + issue_number: pr.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `### ๐Ÿงน Lint Report:\n\`\`\`\n${lintReport}\n\`\`\`` + }); + + - name: Comment Keploy Test Report on PR + uses: actions/github-script@v6 + with: + github-token: ${{ github.token }} + script: | + const report = `${{ steps.keploy-test-report.outputs.KEPLOY_REPORT }}`; + if (!report) { + console.error('Error: KEPLOY_REPORT not found.'); + return; + } + github.rest.issues.createComment({ + issue_number: context.payload.pull_request.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: report + }); + - name: Debug Context + run: echo "${{ toJson(github) }}" diff --git a/.github/workflows/testGpt-workflow.yml b/.github/workflows/testGpt-workflow.yml new file mode 100644 index 0000000..4d2a608 --- /dev/null +++ b/.github/workflows/testGpt-workflow.yml @@ -0,0 +1,29 @@ +# name: Test TestGPT Lint + Static Analysis + +# on: +# pull_request: +# types: [opened, synchronize, reopened] + +# jobs: +# run-testgpt: +# runs-on: ubuntu-latest + +# steps: +# - name: Checkout Code +# uses: actions/checkout@v2 + +# # ๐Ÿ‘‡ Add this to test your custom TestGPT Action locally +# - name: Run Custom TestGPT Action +# uses: ./ # This refers to the action.yml in the root directory +# with: +# working-directory: ./ +# keploy-path: ./ +# command: "echo Hello from TestGPT!" + +# # Optional: Show files changed in PR (testing GitHub API call) +# - name: Show Changed Files +# run: | +# echo "Fetching changed files..." +# curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ +# https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files \ +# | jq '.[].filename' diff --git a/action.yml b/action.yml index 4262137..e69de29 100644 --- a/action.yml +++ b/action.yml @@ -1,89 +0,0 @@ -name: 'Keploy TestGPT' -description: "TestGPT is a GitHub Action designed to execute Keploy test cases and generate detailed test reports." -author: Sonichigo -branding: - icon: 'aperture' - color: 'orange' - -inputs: - working-directory: - description: Relative path under $GITHUB_WORKSPACE where the repository was checked out - required: true - command: - description: Command to run the application - required: true - keploy-path: - description: Path to keploy - required: true - default: ./ - delay: - description: Time to start application - required: true - default: 10 - container-name: - description: Name of the container in case of "docker compose" command - build-delay: - description: Time to wait for docker container build - default: 50s - -runs: - using: "composite" - steps: - - name: Setup GITHUB_PATH for script - run: | - echo "${{ github.action_path }}" >> $GITHUB_PATH - echo "${{ inputs.working-directory }}" - shell: bash - - name: Grant permissions - run: chmod +x ${GITHUB_ACTION_PATH}/install.sh - shell: bash - - id: keploy-test-report - name: Run Script - run: | - ${GITHUB_ACTION_PATH}/install.sh > ${GITHUB_WORKSPACE}/${WORKDIR}/report.txt - cat ${GITHUB_WORKSPACE}/${WORKDIR}/report.txt - - # Search for the complete testrun summary and extract total tests, total test passed, and total test failed data - grep -oE "COMPLETE TESTRUN SUMMARY\.\s+Total tests: [0-9]+" ${GITHUB_WORKSPACE}/${WORKDIR}/report.txt | sed -r "s/\x1B\[[0-9;]*[mGK]//g" > ${GITHUB_WORKSPACE}/${WORKDIR}/final_total_tests.out - grep -oE "COMPLETE TESTRUN SUMMARY\.\s+Total test passed: [0-9]+" ${GITHUB_WORKSPACE}/${WORKDIR}/report.txt | sed -r "s/\x1B\[[0-9;]*[mGK]//g" > ${GITHUB_WORKSPACE}/${WORKDIR}/final_total_passed.out - grep -oE "COMPLETE TESTRUN SUMMARY\.\s+Total test failed: [0-9]+" ${GITHUB_WORKSPACE}/${WORKDIR}/report.txt | sed -r "s/\x1B\[[0-9;]*[mGK]//g" > ${GITHUB_WORKSPACE}/${WORKDIR}/final_total_failed.out - - # Combine the results into a single file and prepare output - cat ${GITHUB_WORKSPACE}/${WORKDIR}/final_total_tests.out ${GITHUB_WORKSPACE}/${WORKDIR}/final_total_passed.out ${GITHUB_WORKSPACE}/${WORKDIR}/final_total_failed.out > ${GITHUB_WORKSPACE}/${WORKDIR}/final.out - echo 'KEPLOY_REPORT< $GITHUB_OUTPUT - cat ${GITHUB_WORKSPACE}/${WORKDIR}/final.out >> $GITHUB_OUTPUT - echo 'EOF' >> $GITHUB_OUTPUT - cat $GITHUB_OUTPUT - shell: bash - env: - WORKDIR: ${{ inputs.working-directory }} - DELAY: ${{ inputs.delay }} - COMMAND : ${{ inputs.command }} - KEPLOY_PATH: ${{inputs.keploy-path}} - - name: Check if report is generated - run: | - if [ -s ${GITHUB_WORKSPACE}/${WORKDIR}/final.out ]; then - echo "Report generated successfully." - else - echo "Error: Report not generated." >&2 - exit 1 - fi - shell: bash - - name: Comment on PR - if: success() - uses: actions/github-script@v6 - env: - KEPLOY_REPORT: ${{ steps.keploy-test-report.outputs.KEPLOY_REPORT }} - with: - github-token: ${{ github.token }} - script: | - if (!process.env.KEPLOY_REPORT) { - console.error('Error: KEPLOY_REPORT not found.'); - process.exit(1); - } - github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body: process.env.KEPLOY_REPORT - }) diff --git a/dummy.js b/dummy.js new file mode 100644 index 0000000..cadf7a3 --- /dev/null +++ b/dummy.js @@ -0,0 +1,10 @@ +const http = require('http'); + +const server = http.createServer((req, res) => { + res.write('Hello Keploy'); + res.end(); +}); + +server.listen(3000, () => { + console.log('Server running at http://localhost:3000/'); +}); diff --git a/install.sh b/install.sh index f998ddf..bb6595a 100644 --- a/install.sh +++ b/install.sh @@ -1,60 +1,97 @@ -# Install Keploy binary using curl command -curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp -echo "curl --silent --location 'https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz' | tar xz -C /tmp" +#!/bin/bash + +set -x +set -e +# Install Keploy +curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp sudo mv /tmp/keploy /usr/local/bin/keploy chmod +x /usr/local/bin/keploy - echo "Keploy installed successfully ๐ŸŽ‰" -cd ${GITHUB_WORKSPACE}/${WORKDIR} -echo "${GITHUB_WORKSPACE}/${WORKDIR}" -# Generate app binary -echo "ls" +cd "${GITHUB_WORKSPACE}/${WORKDIR}" +echo "Working Directory: ${GITHUB_WORKSPACE}/${WORKDIR}" ls +# Install dependencies and build app if [[ "$COMMAND" =~ .*"go".* ]]; then - echo "go is present." + echo "Go detected." go mod download go build -o application - echo 'Test Mode Starting ๐ŸŽ‰' - echo sudo -E keploy test -c "./application" --delay ${DELAY} --path "${KEPLOY_PATH}" - sudo -E keploy test -c "./application" --delay ${DELAY} --path "${KEPLOY_PATH}" + APP_COMMAND="./application" elif [[ "$COMMAND" =~ .*"node".* ]]; then - echo "Node is present." + echo "Node detected." npm install - echo 'Test Mode Starting ๐ŸŽ‰' - echo sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" - sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" + APP_COMMAND="${COMMAND}" -elif [[ "$COMMAND" =~ .*"java".* ]] || [[ "$COMMAND" =~ .*"mvn".* ]]; then - echo "Java is present." +elif [[ "$COMMAND" =~ .*"java".* ]] || [[ "$COMMAND" =~ .*"mvn".* ]]; then + echo "Java detected." mvn clean install - echo 'Test Mode Starting ๐ŸŽ‰' - echo sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" - sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" + APP_COMMAND="${COMMAND}" elif [[ "$COMMAND" =~ .*"python".* ]] || [[ "$COMMAND" =~ .*"python3".* ]]; then - echo "Python is present." + echo "Python detected." pip install -r requirements.txt - echo 'Test Mode Starting ๐ŸŽ‰' - echo sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" - sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" + APP_COMMAND="${COMMAND}" -elif [[ "$COMMAND" =~ .*"docker-compose".* ]] || [[ "$COMMAND" =~ .*"docker compose".* ]]; then - echo "Docker compose is present." +else + echo "Unsupported language or Docker required. Skipping record." echo 'Test Mode Starting ๐ŸŽ‰' - echo sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" --containerName "${CONTAINER_NAME}" --buildDelay ${BUILD_DELAY} - sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" --containerName "${CONTAINER_NAME}" --buildDelay ${BUILD_DELAY} + sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" \ + ${CONTAINER_NAME:+--containerName "${CONTAINER_NAME}"} \ + ${BUILD_DELAY:+--buildDelay ${BUILD_DELAY}} + exit 0 +fi -elif [[ "$COMMAND" =~ .*"docker".* ]]; then - echo "Docker is present." - echo 'Test Mode Starting ๐ŸŽ‰' - echo sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" --buildDelay ${BUILD_DELAY} - sudo -E keploy test -c "${COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" --buildDelay ${BUILD_DELAY} +# Start app in background +${APP_COMMAND} & +APP_PID=$! +trap "kill $APP_PID" EXIT +sleep 3 -else - echo "Language not found" - echo 'Test Mode Shutting ๐ŸŽ‰' -fi \ No newline at end of file +# Check if app is running +curl http://127.0.0.1:${APP_PORT:-3000} || { + echo "App not responding on port ${APP_PORT:-3000}." + kill $APP_PID + exit 1 +} + +# Record with Keploy +echo "Recording testcases with Keploy ๐ŸŽฅ" +sudo -E keploy record -c "${APP_COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" > keploy_record.log 2>&1 & +RECORD_PID=$! +sleep 5 + +# Send requests during recording +for i in {1..3}; do + curl http://127.0.0.1:${APP_PORT:-3000} || echo "Request $i failed" + sleep 1 +done + +wait $RECORD_PID + +# Retry recording if no test-sets are found +if [ ! -d "${KEPLOY_PATH}" ] || [ -z "$(ls -A "${KEPLOY_PATH}")" ]; then + echo "No test-sets found in ${KEPLOY_PATH}. Retrying recording..." + sudo -E keploy record -c "${APP_COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}" > keploy_record_retry.log 2>&1 & + RECORD_PID=$! + sleep 5 + + for i in {1..3}; do + curl http://127.0.0.1:${APP_PORT:-3000} || echo "Retry request $i failed" + sleep 1 + end + + wait $RECORD_PID +fi + +# Final check if test-sets are recorded +if [ ! -d "${KEPLOY_PATH}" ] || [ -z "$(ls -A "${KEPLOY_PATH}")" ]; then + echo "No test-sets found in ${KEPLOY_PATH} after retry. Recording failed." + exit 1 +fi + +# Test with Keploy +echo "Running tests with Keploy ๐ŸŽฏ" +sudo -E keploy test -c "${APP_COMMAND}" --delay ${DELAY} --path "${KEPLOY_PATH}"