From f5316078cd3ad39277f9a7a346c9159fca49cd7e Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Thu, 20 Feb 2025 12:22:35 +0530 Subject: [PATCH 1/9] feat: add code analysis and reporting for multiple languages Signed-off-by: Sky Singh --- action.yml | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 4262137..6bc89a9 100644 --- a/action.yml +++ b/action.yml @@ -60,6 +60,85 @@ runs: DELAY: ${{ inputs.delay }} COMMAND : ${{ inputs.command }} KEPLOY_PATH: ${{inputs.keploy-path}} + + - name: Fetch PR changes + id: pr-files + uses: actions/github-script@v6 + with: + github-token: ${{ inputs.github-token }} + script: | + try { + const response = await github.rest.pulls.listFiles({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number, + }); + return response.data.map(file => file.filename); + } catch (error) { + core.setFailed(`Failed to fetch PR files: ${error.message}`); + } + + - name: Detect languages + id: language-detection + shell: bash + run: | + declare -A extensions=( + ["go"]="go" + ["js"]="javascript" + ["ts"]="javascript" + ["py"]="python" + ["java"]="java" + ) + + LANGUAGES="" + for file in ${{ steps.pr-files.outputs.result }}; do + ext="${file##*.}" + if [[ -n "${extensions[$ext]}" ]]; then + LANGUAGES+="${extensions[$ext]} " + fi + done + + UNIQUE_LANGS=$(echo "$LANGUAGES" | tr ' ' '\n' | sort -u | tr '\n' ' ') + echo "languages=$UNIQUE_LANGS" >> $GITHUB_OUTPUT + + - name: Install analysis tools + shell: bash + run: | + for lang in ${{ steps.language-detection.outputs.languages }}; do + case "$lang" in + go) + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s + go install github.com/securego/gosec/v2/cmd/gosec@latest + ;; + javascript) + npm install -g eslint + ;; + python) + pip install flake8 bandit + ;; + esac + done + - name: Run code analysis + shell: bash + run: | + cd ${{ inputs.working-directory }} + mkdir -p analysis-reports + + for lang in ${{ steps.language-detection.outputs.languages }}; do + case "$lang" in + go) + golangci-lint run --out-format=github-actions ./... > analysis-reports/go-lint.txt + gosec ./... > analysis-reports/go-security.txt + ;; + javascript) + eslint . --format junit -o analysis-reports/js-lint.xml + ;; + python) + flake8 . --output-file=analysis-reports/py-lint.txt + bandit -r . -f txt -o analysis-reports/py-security.txt + ;; + esac + done - name: Check if report is generated run: | if [ -s ${GITHUB_WORKSPACE}/${WORKDIR}/final.out ]; then @@ -81,9 +160,32 @@ runs: console.error('Error: KEPLOY_REPORT not found.'); process.exit(1); } + const fs = require('fs'); + const path = require('path'); + + function readReport(filepath) { + try { + return fs.readFileSync(filepath, 'utf8'); + } catch (error) { + return null; + } + } + + const reportsDir = path.join('${{ inputs.working-directory }}', 'analysis-reports'); + const reports = fs.readdirSync(reportsDir) + .filter(file => file.endsWith('.txt') || file.endsWith('.xml')) + .map(file => readReport(path.join(reportsDir, file))) + .filter(Boolean); + + const summary = `## 📊 TestGPT Analysis Report + + ${process.env.KEPLOY_REPORT || 'No test results available'} + + ${reports.join('\n\n')}`; + github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - body: process.env.KEPLOY_REPORT + body: summary }) From d3fb640e87b9c14fa7c784c9bfe1df3af03e3592 Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Wed, 5 Mar 2025 12:27:19 +0530 Subject: [PATCH 2/9] feat: enhance action.yml with GitHub token input and report generation Signed-off-by: Sky Singh --- action.yml | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/action.yml b/action.yml index 6bc89a9..e170af4 100644 --- a/action.yml +++ b/action.yml @@ -25,6 +25,10 @@ inputs: build-delay: description: Time to wait for docker container build default: 50s + github-token: + description: GitHub token for PR interactions + required: false + default: ${{ github.token }} runs: using: "composite" @@ -91,7 +95,7 @@ runs: ) LANGUAGES="" - for file in ${{ steps.pr-files.outputs.result }}; do + for file in $(find . -type f); do ext="${file##*.}" if [[ -n "${extensions[$ext]}" ]]; then LANGUAGES+="${extensions[$ext]} " @@ -107,8 +111,7 @@ runs: for lang in ${{ steps.language-detection.outputs.languages }}; do case "$lang" in go) - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s - go install github.com/securego/gosec/v2/cmd/gosec@latest + go install golang.org/x/lint/golint@latest ;; javascript) npm install -g eslint @@ -127,18 +130,28 @@ runs: for lang in ${{ steps.language-detection.outputs.languages }}; do case "$lang" in go) - golangci-lint run --out-format=github-actions ./... > analysis-reports/go-lint.txt - gosec ./... > analysis-reports/go-security.txt + golangci-lint run --out-format=github-actions ./... > analysis-reports/go-lint.txt || true ;; javascript) - eslint . --format junit -o analysis-reports/js-lint.xml + eslint . --format junit -o analysis-reports/js-lint.xml || true ;; python) - flake8 . --output-file=analysis-reports/py-lint.txt - bandit -r . -f txt -o analysis-reports/py-security.txt + flake8 . --output-file=analysis-reports/py-lint.txt || true ;; esac done + + # Combine reports + temp_file=$(mktemp) + for report in analysis-reports/*; do + if [ -f "$report" ]; then + echo "### $(basename "$report")" >> "$temp_file" + cat "$report" >> "$temp_file" + echo -e "\n---\n" >> "$temp_file" + fi + done + mv "$temp_file" analysis-reports/final-report.txt + - name: Check if report is generated run: | if [ -s ${GITHUB_WORKSPACE}/${WORKDIR}/final.out ]; then @@ -154,7 +167,7 @@ runs: env: KEPLOY_REPORT: ${{ steps.keploy-test-report.outputs.KEPLOY_REPORT }} with: - github-token: ${{ github.token }} + github-token: ${{ inputs.github-token }} script: | if (!process.env.KEPLOY_REPORT) { console.error('Error: KEPLOY_REPORT not found.'); From fca9d5e0c6fe9fd49d8f3e38be28f8a767becd00 Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Thu, 6 Mar 2025 15:26:15 +0530 Subject: [PATCH 3/9] feat: add language detection and code analysis scripts with result parsing Signed-off-by: Sky Singh --- action.yml | 114 +++++++++++--------------------------------- analyze-code.sh | 48 +++++++++++++++++++ detect-languages.sh | 20 ++++++++ parse-results.sh | 11 +++++ 4 files changed, 108 insertions(+), 85 deletions(-) create mode 100644 analyze-code.sh create mode 100644 detect-languages.sh create mode 100644 parse-results.sh diff --git a/action.yml b/action.yml index e170af4..30ceb3c 100644 --- a/action.yml +++ b/action.yml @@ -38,33 +38,31 @@ runs: echo "${{ github.action_path }}" >> $GITHUB_PATH echo "${{ inputs.working-directory }}" shell: bash - - name: Grant permissions - run: chmod +x ${GITHUB_ACTION_PATH}/install.sh + + - name: Grant permissions to scripts + run: | + chmod +x ${GITHUB_ACTION_PATH}/install.sh + chmod +x ${GITHUB_ACTION_PATH}/detect-languages.sh + chmod +x ${GITHUB_ACTION_PATH}/analyze-code.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 + name: Run Keploy Tests + run: ${GITHUB_ACTION_PATH}/install.sh > ${GITHUB_WORKSPACE}/${WORKDIR}/report.txt shell: bash env: WORKDIR: ${{ inputs.working-directory }} DELAY: ${{ inputs.delay }} - COMMAND : ${{ inputs.command }} + COMMAND: ${{ inputs.command }} KEPLOY_PATH: ${{inputs.keploy-path}} + - name: Parse Test Results + id: parse-results + run: ${GITHUB_ACTION_PATH}/parse-results.sh + shell: bash + env: + WORKDIR: ${{ inputs.working-directory }} + - name: Fetch PR changes id: pr-files uses: actions/github-script@v6 @@ -84,73 +82,15 @@ runs: - name: Detect languages id: language-detection + run: ${GITHUB_ACTION_PATH}/detect-languages.sh shell: bash - run: | - declare -A extensions=( - ["go"]="go" - ["js"]="javascript" - ["ts"]="javascript" - ["py"]="python" - ["java"]="java" - ) - - LANGUAGES="" - for file in $(find . -type f); do - ext="${file##*.}" - if [[ -n "${extensions[$ext]}" ]]; then - LANGUAGES+="${extensions[$ext]} " - fi - done - - UNIQUE_LANGS=$(echo "$LANGUAGES" | tr ' ' '\n' | sort -u | tr '\n' ' ') - echo "languages=$UNIQUE_LANGS" >> $GITHUB_OUTPUT - - - name: Install analysis tools - shell: bash - run: | - for lang in ${{ steps.language-detection.outputs.languages }}; do - case "$lang" in - go) - go install golang.org/x/lint/golint@latest - ;; - javascript) - npm install -g eslint - ;; - python) - pip install flake8 bandit - ;; - esac - done + - name: Run code analysis + run: ${GITHUB_ACTION_PATH}/analyze-code.sh shell: bash - run: | - cd ${{ inputs.working-directory }} - mkdir -p analysis-reports - - for lang in ${{ steps.language-detection.outputs.languages }}; do - case "$lang" in - go) - golangci-lint run --out-format=github-actions ./... > analysis-reports/go-lint.txt || true - ;; - javascript) - eslint . --format junit -o analysis-reports/js-lint.xml || true - ;; - python) - flake8 . --output-file=analysis-reports/py-lint.txt || true - ;; - esac - done - - # Combine reports - temp_file=$(mktemp) - for report in analysis-reports/*; do - if [ -f "$report" ]; then - echo "### $(basename "$report")" >> "$temp_file" - cat "$report" >> "$temp_file" - echo -e "\n---\n" >> "$temp_file" - fi - done - mv "$temp_file" analysis-reports/final-report.txt + env: + WORKDIR: ${{ inputs.working-directory }} + DETECTED_LANGUAGES: ${{ steps.language-detection.outputs.languages }} - name: Check if report is generated run: | @@ -161,11 +101,14 @@ runs: exit 1 fi shell: bash + env: + WORKDIR: ${{ inputs.working-directory }} + - name: Comment on PR if: success() uses: actions/github-script@v6 env: - KEPLOY_REPORT: ${{ steps.keploy-test-report.outputs.KEPLOY_REPORT }} + KEPLOY_REPORT: ${{ steps.parse-results.outputs.KEPLOY_REPORT }} with: github-token: ${{ inputs.github-token }} script: | @@ -173,6 +116,7 @@ runs: console.error('Error: KEPLOY_REPORT not found.'); process.exit(1); } + const fs = require('fs'); const path = require('path'); @@ -201,4 +145,4 @@ runs: owner: context.repo.owner, repo: context.repo.repo, body: summary - }) + }) \ No newline at end of file diff --git a/analyze-code.sh b/analyze-code.sh new file mode 100644 index 0000000..3e40c58 --- /dev/null +++ b/analyze-code.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +cd $GITHUB_WORKSPACE/$WORKDIR +mkdir -p analysis-reports + +for lang in $DETECTED_LANGUAGES; do + case "$lang" in + go) + command -v golangci-lint >/dev/null || go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + ;; + javascript) + command -v eslint >/dev/null || npm install -g eslint + ;; + python) + command -v flake8 >/dev/null || pip install flake8 bandit + ;; + esac +done + +for lang in $DETECTED_LANGUAGES; do + case "$lang" in + go) + golangci-lint run --out-format=github-actions ./... > analysis-reports/go-lint.txt 2>&1 || true + ;; + javascript) + if [ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ] || [ -f ".eslintrc.yml" ]; then + eslint . --format junit -o analysis-reports/js-lint.xml 2>&1 || true + else + echo "No ESLint config found, skipping JavaScript linting" > analysis-reports/js-lint.txt + fi + ;; + python) + flake8 . --output-file=analysis-reports/py-lint.txt 2>&1 || true + bandit -r . -f txt -o analysis-reports/py-security.txt 2>&1 || true + ;; + esac +done + +# Combine reports into a single file +temp_file=$(mktemp) +for report in analysis-reports/*; do + if [ -f "$report" ]; then + echo "### $(basename "$report")" >> "$temp_file" + cat "$report" >> "$temp_file" + echo -e "\n---\n" >> "$temp_file" + fi +done +mv "$temp_file" analysis-reports/final-report.txt \ No newline at end of file diff --git a/detect-languages.sh b/detect-languages.sh new file mode 100644 index 0000000..b8c31fb --- /dev/null +++ b/detect-languages.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +declare -A extensions=( + ["go"]="go" + ["js"]="javascript" + ["ts"]="javascript" + ["py"]="python" + ["java"]="java" +) + +LANGUAGES="" +for file in $(find . -type f); do + ext="${file##*.}" + if [[ -n "${extensions[$ext]}" ]]; then + LANGUAGES+="${extensions[$ext]} " + fi +done + +UNIQUE_LANGS=$(echo "$LANGUAGES" | tr ' ' '\n' | sort -u | tr '\n' ' ') +echo "languages=$UNIQUE_LANGS" >> $GITHUB_OUTPUT \ No newline at end of file diff --git a/parse-results.sh b/parse-results.sh new file mode 100644 index 0000000..5b90274 --- /dev/null +++ b/parse-results.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +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 + +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 \ No newline at end of file From 9fa757a9158fd777411e9580e9cba07ec6956ef4 Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Thu, 6 Mar 2025 15:44:40 +0530 Subject: [PATCH 4/9] feat: enhance code analysis scripts to handle changed files and add result parsing Signed-off-by: Sky Singh --- action.yml | 6 +++- analyze-code.sh | 86 +++++++++++++++++++++++++++++++++++---------- detect-languages.sh | 4 ++- 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/action.yml b/action.yml index 30ceb3c..cd413a1 100644 --- a/action.yml +++ b/action.yml @@ -44,6 +44,7 @@ runs: chmod +x ${GITHUB_ACTION_PATH}/install.sh chmod +x ${GITHUB_ACTION_PATH}/detect-languages.sh chmod +x ${GITHUB_ACTION_PATH}/analyze-code.sh + chmod +x ${GITHUB_ACTION_PATH}/parse-results.sh shell: bash - id: keploy-test-report @@ -75,7 +76,9 @@ runs: repo: context.repo.repo, pull_number: context.issue.number, }); - return response.data.map(file => file.filename); + const files = response.data.map(file => file.filename); + core.setOutput('files', files.join(',')); + return files; } catch (error) { core.setFailed(`Failed to fetch PR files: ${error.message}`); } @@ -91,6 +94,7 @@ runs: env: WORKDIR: ${{ inputs.working-directory }} DETECTED_LANGUAGES: ${{ steps.language-detection.outputs.languages }} + PR_FILES: ${{ steps.pr-files.outputs.files }} - name: Check if report is generated run: | diff --git a/analyze-code.sh b/analyze-code.sh index 3e40c58..c8f8720 100644 --- a/analyze-code.sh +++ b/analyze-code.sh @@ -17,26 +17,74 @@ for lang in $DETECTED_LANGUAGES; do esac done -for lang in $DETECTED_LANGUAGES; do - case "$lang" in - go) - golangci-lint run --out-format=github-actions ./... > analysis-reports/go-lint.txt 2>&1 || true - ;; - javascript) - if [ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ] || [ -f ".eslintrc.yml" ]; then - eslint . --format junit -o analysis-reports/js-lint.xml 2>&1 || true - else - echo "No ESLint config found, skipping JavaScript linting" > analysis-reports/js-lint.txt - fi - ;; - python) - flake8 . --output-file=analysis-reports/py-lint.txt 2>&1 || true - bandit -r . -f txt -o analysis-reports/py-security.txt 2>&1 || true - ;; - esac -done +if [ -n "$PR_FILES" ]; then + IFS=',' read -ra FILES <<< "$PR_FILES" + + for lang in $DETECTED_LANGUAGES; do + case "$lang" in + go) + GO_FILES=() + for file in "${FILES[@]}"; do + if [[ $file == *.go ]]; then + GO_FILES+=("$file") + fi + done + if [ ${#GO_FILES[@]} -gt 0 ]; then + golangci-lint run --out-format=github-actions "${GO_FILES[@]}" > analysis-reports/go-lint.txt 2>&1 || true + else + echo "No Go files changed in PR" > analysis-reports/go-lint.txt + fi + ;; + javascript) + JS_FILES=() + for file in "${FILES[@]}"; do + if [[ $file == *.js || $file == *.ts || $file == *.jsx || $file == *.tsx ]]; then + JS_FILES+=("$file") + fi + done + if [ ${#JS_FILES[@]} -gt 0 ] && ([ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ] || [ -f ".eslintrc.yml" ]); then + eslint "${JS_FILES[@]}" --format junit -o analysis-reports/js-lint.xml 2>&1 || true + else + echo "No JavaScript/TypeScript files changed in PR or no ESLint config found" > analysis-reports/js-lint.txt + fi + ;; + python) + PY_FILES=() + for file in "${FILES[@]}"; do + if [[ $file == *.py ]]; then + PY_FILES+=("$file") + fi + done + if [ ${#PY_FILES[@]} -gt 0 ]; then + flake8 "${PY_FILES[@]}" --output-file=analysis-reports/py-lint.txt 2>&1 || true + bandit -r "${PY_FILES[@]}" -f txt -o analysis-reports/py-security.txt 2>&1 || true + else + echo "No Python files changed in PR" > analysis-reports/py-lint.txt + fi + ;; + esac + done +else + for lang in $DETECTED_LANGUAGES; do + case "$lang" in + go) + golangci-lint run --out-format=github-actions ./... > analysis-reports/go-lint.txt 2>&1 || true + ;; + javascript) + if [ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ] || [ -f ".eslintrc.yml" ]; then + eslint . --format junit -o analysis-reports/js-lint.xml 2>&1 || true + else + echo "No ESLint config found, skipping JavaScript linting" > analysis-reports/js-lint.txt + fi + ;; + python) + flake8 . --output-file=analysis-reports/py-lint.txt 2>&1 || true + bandit -r . -f txt -o analysis-reports/py-security.txt 2>&1 || true + ;; + esac + done +fi -# Combine reports into a single file temp_file=$(mktemp) for report in analysis-reports/*; do if [ -f "$report" ]; then diff --git a/detect-languages.sh b/detect-languages.sh index b8c31fb..21a4dde 100644 --- a/detect-languages.sh +++ b/detect-languages.sh @@ -3,7 +3,9 @@ declare -A extensions=( ["go"]="go" ["js"]="javascript" - ["ts"]="javascript" + ["ts"]="javascript" + ["jsx"]="javascript" + ["tsx"]="javascript" ["py"]="python" ["java"]="java" ) From 5d06e9a9ce6be0175f0bdebccf56c7419768c053 Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Thu, 6 Mar 2025 17:01:29 +0530 Subject: [PATCH 5/9] feat: improve linter installation checks in analyze-code.sh Signed-off-by: Sky Singh --- analyze-code.sh | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/analyze-code.sh b/analyze-code.sh index c8f8720..4df7632 100644 --- a/analyze-code.sh +++ b/analyze-code.sh @@ -3,16 +3,48 @@ cd $GITHUB_WORKSPACE/$WORKDIR mkdir -p analysis-reports +# Function to find existing linter installations +find_linter() { + local linter_name=$1 + local system_path=$(which $linter_name 2>/dev/null) + echo $system_path +} + for lang in $DETECTED_LANGUAGES; do case "$lang" in go) - command -v golangci-lint >/dev/null || go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + GO_LINTER=$(find_linter golangci-lint) + if [ -z "$GO_LINTER" ]; then + echo "Installing golangci-lint..." + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + else + echo "Using system golangci-lint: $GO_LINTER" + fi ;; javascript) - command -v eslint >/dev/null || npm install -g eslint + JS_LINTER=$(find_linter eslint) + if [ -z "$JS_LINTER" ]; then + echo "Installing eslint..." + npm install -g eslint + else + echo "Using system eslint: $JS_LINTER" + fi ;; python) - command -v flake8 >/dev/null || pip install flake8 bandit + PY_LINTER=$(find_linter flake8) + PY_SECURITY=$(find_linter bandit) + if [ -z "$PY_LINTER" ]; then + echo "Installing flake8..." + pip install flake8 + else + echo "Using system flake8: $PY_LINTER" + fi + if [ -z "$PY_SECURITY" ]; then + echo "Installing bandit..." + pip install bandit + else + echo "Using system bandit: $PY_SECURITY" + fi ;; esac done From cab32c08afb7005fa7659659e2253e11a2dcbfa8 Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Sun, 16 Mar 2025 00:19:31 +0530 Subject: [PATCH 6/9] refactor: streamline script permission grants and enhance result parsing logic - Combined multiple chmod commands into a single line for efficiency in action.yml. - Added a check in parse-results.sh to handle cases where no tests were executed, ensuring proper output generation. Signed-off-by: Sky Singh --- action.yml | 5 +---- parse-results.sh | 7 ++++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index cd413a1..9025bb6 100644 --- a/action.yml +++ b/action.yml @@ -41,10 +41,7 @@ runs: - name: Grant permissions to scripts run: | - chmod +x ${GITHUB_ACTION_PATH}/install.sh - chmod +x ${GITHUB_ACTION_PATH}/detect-languages.sh - chmod +x ${GITHUB_ACTION_PATH}/analyze-code.sh - chmod +x ${GITHUB_ACTION_PATH}/parse-results.sh + chmod +x ${GITHUB_ACTION_PATH}/install.sh ${GITHUB_ACTION_PATH}/detect-languages.sh ${GITHUB_ACTION_PATH}/analyze-code.sh ${GITHUB_ACTION_PATH}/parse-results.sh shell: bash - id: keploy-test-report diff --git a/parse-results.sh b/parse-results.sh index 5b90274..a2d8b1f 100644 --- a/parse-results.sh +++ b/parse-results.sh @@ -4,7 +4,12 @@ grep -oE "COMPLETE TESTRUN SUMMARY\.\s+Total tests: [0-9]+" ${GITHUB_WORKSPACE}/ 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 -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 +# Check if any test results were found +if [ ! -s "${GITHUB_WORKSPACE}/${WORKDIR}/final_total_tests.out" ]; then + echo "No tests were executed" > ${GITHUB_WORKSPACE}/${WORKDIR}/final.out +else + 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 +fi echo 'KEPLOY_REPORT<> $GITHUB_OUTPUT cat ${GITHUB_WORKSPACE}/${WORKDIR}/final.out >> $GITHUB_OUTPUT From 2b4dbb14c04c755c15e3629e91a76b8365e72a46 Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Sun, 16 Mar 2025 00:29:29 +0530 Subject: [PATCH 7/9] feat: enhance code analysis and result parsing with logging and directory checks - Added logging functionality to provide better insights during linter installations and code analysis completion in analyze-code.sh. - Ensured the existence of the analysis-reports directory before generating reports. - Improved error handling in parse-results.sh to check for the presence of the report file and handle cases where no tests were executed. Signed-off-by: Sky Singh --- analyze-code.sh | 30 +++++++++++++++++++++--------- parse-results.sh | 23 +++++++++++++++-------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/analyze-code.sh b/analyze-code.sh index 4df7632..7c981f1 100644 --- a/analyze-code.sh +++ b/analyze-code.sh @@ -1,6 +1,9 @@ #!/bin/bash +# Change to the workspace directory cd $GITHUB_WORKSPACE/$WORKDIR + +# Ensure analysis-reports directory exists mkdir -p analysis-reports # Function to find existing linter installations @@ -10,40 +13,47 @@ find_linter() { echo $system_path } +# Log message function +log_message() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" +} + +log_message "Starting code analysis" + for lang in $DETECTED_LANGUAGES; do case "$lang" in go) GO_LINTER=$(find_linter golangci-lint) if [ -z "$GO_LINTER" ]; then - echo "Installing golangci-lint..." + log_message "Installing golangci-lint..." go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest else - echo "Using system golangci-lint: $GO_LINTER" + log_message "Using system golangci-lint: $GO_LINTER" fi ;; javascript) JS_LINTER=$(find_linter eslint) if [ -z "$JS_LINTER" ]; then - echo "Installing eslint..." + log_message "Installing eslint..." npm install -g eslint else - echo "Using system eslint: $JS_LINTER" + log_message "Using system eslint: $JS_LINTER" fi ;; python) PY_LINTER=$(find_linter flake8) PY_SECURITY=$(find_linter bandit) if [ -z "$PY_LINTER" ]; then - echo "Installing flake8..." + log_message "Installing flake8..." pip install flake8 else - echo "Using system flake8: $PY_LINTER" + log_message "Using system flake8: $PY_LINTER" fi if [ -z "$PY_SECURITY" ]; then - echo "Installing bandit..." + log_message "Installing bandit..." pip install bandit else - echo "Using system bandit: $PY_SECURITY" + log_message "Using system bandit: $PY_SECURITY" fi ;; esac @@ -125,4 +135,6 @@ for report in analysis-reports/*; do echo -e "\n---\n" >> "$temp_file" fi done -mv "$temp_file" analysis-reports/final-report.txt \ No newline at end of file +mv "$temp_file" analysis-reports/final-report.txt + +log_message "Code analysis completed" \ No newline at end of file diff --git a/parse-results.sh b/parse-results.sh index a2d8b1f..42e3d6f 100644 --- a/parse-results.sh +++ b/parse-results.sh @@ -1,16 +1,23 @@ #!/bin/bash -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 +mkdir -p "${GITHUB_WORKSPACE}/${WORKDIR}" -# Check if any test results were found -if [ ! -s "${GITHUB_WORKSPACE}/${WORKDIR}/final_total_tests.out" ]; then - echo "No tests were executed" > ${GITHUB_WORKSPACE}/${WORKDIR}/final.out +ANSI_FILTER="s/\x1B\[([0-9]{1,3}(;[0-9]{1,3})*)?[mGK]//g" + +if [ ! -f "${GITHUB_WORKSPACE}/${WORKDIR}/report.txt" ]; then + echo "No test report file found" > "${GITHUB_WORKSPACE}/${WORKDIR}/final.out" else - 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 + grep -oE "COMPLETE TESTRUN SUMMARY\.\s+Total tests: [0-9]+" "${GITHUB_WORKSPACE}/${WORKDIR}/report.txt" | sed -r "${ANSI_FILTER}" > "${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 "${ANSI_FILTER}" > "${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 "${ANSI_FILTER}" > "${GITHUB_WORKSPACE}/${WORKDIR}/final_total_failed.out" + + if [ ! -s "${GITHUB_WORKSPACE}/${WORKDIR}/final_total_tests.out" ]; then + echo "No tests were executed" > "${GITHUB_WORKSPACE}/${WORKDIR}/final.out" + else + 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" + fi fi echo 'KEPLOY_REPORT<> $GITHUB_OUTPUT -cat ${GITHUB_WORKSPACE}/${WORKDIR}/final.out >> $GITHUB_OUTPUT +cat "${GITHUB_WORKSPACE}/${WORKDIR}/final.out" >> $GITHUB_OUTPUT echo 'EOF' >> $GITHUB_OUTPUT \ No newline at end of file From d12f0ef5724cf61911c4ee8727d1990c600bf5ec Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Fri, 21 Mar 2025 20:05:58 +0530 Subject: [PATCH 8/9] fix: improve error messages in parse-results.sh for better clarity - Updated error messages to provide clearer guidance when the test report file is not found and when no valid test summary is present. - Enhanced user feedback to ensure proper understanding of issues related to test execution. Signed-off-by: Sky Singh --- parse-results.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parse-results.sh b/parse-results.sh index 42e3d6f..2072365 100644 --- a/parse-results.sh +++ b/parse-results.sh @@ -5,14 +5,14 @@ mkdir -p "${GITHUB_WORKSPACE}/${WORKDIR}" ANSI_FILTER="s/\x1B\[([0-9]{1,3}(;[0-9]{1,3})*)?[mGK]//g" if [ ! -f "${GITHUB_WORKSPACE}/${WORKDIR}/report.txt" ]; then - echo "No test report file found" > "${GITHUB_WORKSPACE}/${WORKDIR}/final.out" + echo "ERROR: Test report file not found. Please ensure tests were executed correctly." > "${GITHUB_WORKSPACE}/${WORKDIR}/final.out" else grep -oE "COMPLETE TESTRUN SUMMARY\.\s+Total tests: [0-9]+" "${GITHUB_WORKSPACE}/${WORKDIR}/report.txt" | sed -r "${ANSI_FILTER}" > "${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 "${ANSI_FILTER}" > "${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 "${ANSI_FILTER}" > "${GITHUB_WORKSPACE}/${WORKDIR}/final_total_failed.out" if [ ! -s "${GITHUB_WORKSPACE}/${WORKDIR}/final_total_tests.out" ]; then - echo "No tests were executed" > "${GITHUB_WORKSPACE}/${WORKDIR}/final.out" + echo "ERROR: No valid test summary found in report file. Please check test execution output format." > "${GITHUB_WORKSPACE}/${WORKDIR}/final.out" else 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" fi From 864896b0ac609de3c9f10e89e3af6937cd0f971d Mon Sep 17 00:00:00 2001 From: Sky Singh Date: Fri, 21 Mar 2025 20:08:20 +0530 Subject: [PATCH 9/9] feat: exclude common non-source files in language detection script Signed-off-by: Sky Singh --- detect-languages.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/detect-languages.sh b/detect-languages.sh index 21a4dde..f2d2750 100644 --- a/detect-languages.sh +++ b/detect-languages.sh @@ -11,12 +11,22 @@ declare -A extensions=( ) LANGUAGES="" -for file in $(find . -type f); do + +# Exclude common non-source files +while IFS= read -r file; do ext="${file##*.}" if [[ -n "${extensions[$ext]}" ]]; then LANGUAGES+="${extensions[$ext]} " fi -done +done < <(find . -type f \ + -not -path "*/\.*/*" \ + -not -path "*/node_modules/*" \ + -not -path "*/dist/*" \ + -not -path "*/build/*" \ + -not -path "*/target/*" \ + -not -path "*/vendor/*" \ + -not -path "*/__pycache__/*" \ + -size -1M) UNIQUE_LANGS=$(echo "$LANGUAGES" | tr ' ' '\n' | sort -u | tr '\n' ' ') echo "languages=$UNIQUE_LANGS" >> $GITHUB_OUTPUT \ No newline at end of file