diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad5072f..7106f52 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,11 +12,17 @@ permissions: jobs: lint-and-test: + name: Linting and Positive Tests runs-on: ubuntu-latest + outputs: + test_result: ${{ steps.positive-test-outcome.outcome }} + lint_result: ${{ steps.lint-outcome.outcome }} steps: - - uses: actions/checkout@v4 + - name: Checkout Code + uses: actions/checkout@v4 - name: Validate action.yml Metadata + id: validate-action-yml run: | yq eval '.' action.yml > /dev/null @@ -27,7 +33,7 @@ jobs: label: "test-success-path-defaults" script: | console.log("✅ github-script-post-comment action executed in CI."); - return "Self-test successful."; + return "✅ Test Case 1 - Composite Action - Success Path with Defaults."; - name: Upload Embedded JavaScript uses: actions/upload-artifact@v4 @@ -37,47 +43,129 @@ jobs: path: 'final_script.js' - name: Lint Embedded JavaScript and project files + id: lint-check env: NODE_ENV: development run: | npm ci npx eslint . - - name: Test Case 2 - Composite Action - No Return + - name: Test Case 2 - Composite Action - Success Path with github-step-summary + uses: ./ + id: test-success-path-github-step-summary + with: + label: "test-success-path-github-step-summary" + post-target: github-step-summary + script: | + console.log("✅ github-script-post-comment action executed in CI."); + return "✅ Test Case 2 - Composite Action - Success Path with github-step-summary."; + + - name: Test Case 3 - Composite Action - Success Path with pull-request + uses: ./ + id: test-success-path-pull-request + with: + label: "test-success-path-pull-request" + post-target: pull-request + script: | + console.log("✅ github-script-post-comment action executed in CI."); + return "✅ Test Case 3 - Composite Action - Success Path with pull-request."; + - name: Lint Outcome + id: lint-outcome + if: always() + run: | + echo "steps.validate-action-yml.outcome = ${{ steps.validate-action-yml.outcome }}" + echo "steps.lint-check.outcome = ${{ steps.lint-check.outcome }}" + + if [[ "${{ steps.validate-action-yml.outcome }}" == "success" && "${{ steps.lint-check.outcome }}" == "success" ]]; then + echo "✅ Linting passed." + echo "✅ Linting passed." >> $GITHUB_STEP_SUMMARY + else + echo "❌ One or more lint rules failed." + echo "❌ One or more lint rules failed." >> $GITHUB_STEP_SUMMARY + exit 1 + fi + - name: Job Outcome + id: positive-test-outcome + if: always() + run: | + echo "steps.test-success-path-defaults.outcome = ${{ steps.test-success-path-defaults.outcome }}" + echo "steps.test-success-path-github-step-summary.outcome = ${{ steps.test-success-path-github-step-summary.outcome }}" + echo "steps.test-success-path-pull-request.outcome = ${{ steps.test-success-path-pull-request.outcome }}" + + if [[ "${{ steps.test-success-path-defaults.outcome }}" == "success" && "${{ steps.test-success-path-github-step-summary.outcome }}" == "success" && "${{ steps.test-success-path-pull-request.outcome }}" == "success" ]]; then + echo "✅ All positive tests passed." + echo "✅ All positive tests passed." >> $GITHUB_STEP_SUMMARY + else + echo "❌ One or more positive tests failed or did not run." + echo "❌ One or more positive tests failed or did not run." >> $GITHUB_STEP_SUMMARY + exit 1 + fi + + negative-tests: + name: Negative Tests - Expected to Fail + runs-on: ubuntu-latest + outputs: + test_result: ${{ steps.negative-tests-outcome.outcome }} + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Test Case 4 - Composite Action - No Return uses: ./ id: test-no-return continue-on-error: true with: label: "test-no-return" script: | + console.log("Test Case 2 - Composite Action - No Return"); console.log("Expect error: script does not return string."); - - name: Test Case 3 - Composite Action - Return Non-String + - name: Test Case 5 - Composite Action - Return Non-String uses: ./ id: test-return-non-string continue-on-error: true with: label: "test-return-non-string" script: | + console.log("Test Case 3 - Composite Action - Return Non-String"); console.log("Expect error: script returns non-string."); return {title: "Test Case 3", message: "test case return non-string", score: 50}; - - name: Test Case 4 - Composite Action - Success Path with github-step-summary - uses: ./ - id: test-success-path-github-step-summary - with: - label: "test-success-path-github-step-summary" - post-target: github-step-summary - script: | - console.log("✅ github-script-post-comment action executed in CI."); - return "Self-test successful."; + - name: Job Outcome + id: negative-tests-outcome + if: always() + run: | + echo "steps.test-no-return.outcome = ${{ steps.test-no-return.outcome }}" + echo "steps.test-return-non-string.outcome = ${{ steps.test-return-non-string.outcome }}" + + if [[ "${{ steps.test-no-return.outcome }}" == "failure" && "${{ steps.test-return-non-string.outcome }}" == "failure" ]]; then + echo "✅ All negative tests failed as expected." + echo "✅ All negative tests failed as expected." >> $GITHUB_STEP_SUMMARY + else + echo "❌ One or more negative tests did not fail as expected." + echo "❌ One or more negative tests did not fail as expected." >> $GITHUB_STEP_SUMMARY + exit 1 + fi - - name: Test Case 5 - Composite Action - Success Path with pull-request + test-summary: + name: Test Summary + runs-on: ubuntu-latest + needs: [lint-and-test, negative-tests] + if: always() + + steps: + - name: Checkout Code + uses: actions/checkout@v5 + - name: Add Test Summary uses: ./ - id: test-success-path-pull-request with: - label: "test-success-path-pull-request" - post-target: pull-request + label: "Test Summary" script: | - console.log("✅ github-script-post-comment action executed in CI."); - return "Self-test successful."; \ No newline at end of file + let body = "## 🧪 Test Summary\n\n" + + "| Check | Status |\n" + + "|-------|--------|\n" + + "| Lint Rules | ${{ needs.lint-and-test.outputs.lint_result == 'success' && '✅ Passed' || '❌ Failed' }} |\n" + + "| Positive Tests | ${{ needs.lint-and-test.outputs.test_result == 'success' && '✅ Passed' || '❌ Failed' }} |\n" + + "| Negative Tests | ${{ needs.negative-tests.outputs.test_result == 'success' && '✅ Passed' || '❌ Failed' }} |\n\n"; + + return body; \ No newline at end of file diff --git a/action.yml b/action.yml index 5e2971c..fa76ff3 100644 --- a/action.yml +++ b/action.yml @@ -113,6 +113,11 @@ runs: // Store the result as an output for downstream steps core.setOutput('result', JSON.stringify(body)); + // Validate that the returned body is a string + if (typeof body !== 'string') { + throw new TypeError(`Returned value from user script must be a string. Received type '${typeof body}'. `); + } + // Output to GITHUB_STEP_SUMMARY if (postTarget === 'both' || postTarget === 'github-step-summary') { const summaryPath = process.env.GITHUB_STEP_SUMMARY; @@ -143,7 +148,7 @@ runs: console.error(`❌ Error processing ${{ inputs.label }} results:`, error); // Detect when user forgot to return a value (causing body = undefined) - if (error.status === 'ERR_INVALID_ARG_TYPE' && /The "data" argument must be of type string/i.test(error.message)) { + if (error instanceof TypeError && /Returned value from user script must be a string/i.test(error.message)) { console.error(`💡 Hint: Your script returned a non-string value (e.g., object, array, or undefined) — or nothing at all.\n` + `Please return a string, or serialize your result using JSON.stringify.\n\n` + `For example:\n\n` diff --git a/package.json b/package.json index 85ab168..3e5eaa8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aguilita1/github-script-post-comment", - "version": "1.1.2", + "version": "1.1.3", "description": "GitHub Action to run custom JavaScript and post results to GitHub summary and Pull Request", "author": "Daniel I. Kelley", "license": "MIT",