From 493015840b7b8c25d5376866cb68e6a15b884ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 6 Oct 2025 12:37:13 +0200 Subject: [PATCH 1/3] Github Action for Metrics --- .github/workflows/code-metrics.yaml | 85 +++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 .github/workflows/code-metrics.yaml diff --git a/.github/workflows/code-metrics.yaml b/.github/workflows/code-metrics.yaml new file mode 100644 index 0000000..64cd354 --- /dev/null +++ b/.github/workflows/code-metrics.yaml @@ -0,0 +1,85 @@ +name: Code Metrics + +on: + pull_request: + paths: + - '**/*.php' + branches: + - '*' + +permissions: + pull-requests: write + contents: read + +jobs: + code-metrics: + name: Cognitive Code Analysis + runs-on: ubuntu-24.04 + if: github.event_name == 'pull_request' + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Fetch base branch + run: | + git fetch origin ${{ github.base_ref }}:${{ github.base_ref }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.4' + extensions: json, fileinfo + tools: composer + + - name: Install dependencies + run: composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts + + - name: Analyze changed PHP files + id: analyze + run: | + BASE_SHA="${{ github.event.pull_request.base.sha }}" + HEAD_SHA="${{ github.sha }}" + + CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR $BASE_SHA...$HEAD_SHA | grep '\.php$' | tr '\n' ' ' || echo "") + + if [ -n "$CHANGED_FILES" ]; then + echo "Analyzing files: $CHANGED_FILES" + bin/phpcca analyse $CHANGED_FILES --report-type=markdown --report-file=cca-report.md --config=phpcca.yml || true + + if [ -f "cca-report.md" ] && [ -s "cca-report.md" ]; then + echo "has_report=true" >> $GITHUB_OUTPUT + echo "Report generated successfully" + else + echo "has_report=false" >> $GITHUB_OUTPUT + echo "No report generated" + fi + else + echo "has_report=false" >> $GITHUB_OUTPUT + echo "No PHP files changed, skipping analysis" + fi + + - name: Post comment to PR + if: steps.analyze.outputs.has_report == 'true' + uses: actions/github-script@v7 + with: + script: | + const fs = require('fs'); + const report = fs.readFileSync('cca-report.md', 'utf8'); + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body: report + }); + + - name: Upload report artifact + if: always() + uses: actions/upload-artifact@v4 + with: + name: cca-report + path: cca-report.md + if-no-files-found: ignore From df57df490b3d207a6a7c7f9fafe212af30a03031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 6 Oct 2025 12:38:12 +0200 Subject: [PATCH 2/3] Test file to trigger the action --- src/Test.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/Test.php diff --git a/src/Test.php b/src/Test.php new file mode 100644 index 0000000..d567e8f --- /dev/null +++ b/src/Test.php @@ -0,0 +1,26 @@ + Date: Mon, 6 Oct 2025 12:40:09 +0200 Subject: [PATCH 3/3] Test file to trigger the action --- .github/workflows/code-metrics.yaml | 2 +- src/Test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/code-metrics.yaml b/.github/workflows/code-metrics.yaml index 64cd354..ba3c0b3 100644 --- a/.github/workflows/code-metrics.yaml +++ b/.github/workflows/code-metrics.yaml @@ -47,7 +47,7 @@ jobs: if [ -n "$CHANGED_FILES" ]; then echo "Analyzing files: $CHANGED_FILES" - bin/phpcca analyse $CHANGED_FILES --report-type=markdown --report-file=cca-report.md --config=phpcca.yml || true + bin/phpcca analyse $CHANGED_FILES --report-type=markdown --report-file=cca-report.md --config=config.yml || true if [ -f "cca-report.md" ] && [ -s "cca-report.md" ]; then echo "has_report=true" >> $GITHUB_OUTPUT diff --git a/src/Test.php b/src/Test.php index d567e8f..68c0d25 100644 --- a/src/Test.php +++ b/src/Test.php @@ -18,7 +18,7 @@ public function run(): void if (false === false) { echo "Nested condition met."; if (1 + 1 === 2) { - echo "Math still works."; + echo "Math still workss."; } } }