diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 3524616..af6c690 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -1,4 +1,4 @@ -name: EXADS Pipeline +name: PHP Pipeline on: pull_request: @@ -21,38 +21,88 @@ jobs: with: path: repo + - name: Install dependencies run: | cd repo composer install - # - name: "Upload coverage data" - # uses: actions/upload-artifact@v3 - # with: - # name: covdata - # path: gh_output/clover.xml - - - - name: Run test suite + - name: Unit tests and code coverage report run: | cd repo composer run-script test:coverage - ls -la . + head -n9 coverage/cover.txt | tail -n3 > coverage/coverageSummary.txt - - name: Make code coverage badge - uses: timkrase/phpunit-coverage-badge@v1.2.1 + - name: Leave PR summary + uses: actions/github-script@v6 with: - report: ${{ GITHUB_WORKSPACE }}/coverage/clover.xml - coverage_badge_path: output/coverage.svg - push_badge: false + script: | + const CODE_COVERAGE_THRESHOLD = 0; + const reportFile = "repo/coverage/coverageSummary.txt"; + + require('fs').readFile(reportFile, (err, buffer)=>{ + if(err) throw new Error(err); + let summary = parseSummary(buffer.toString()); + if(CODE_COVERAGE_THRESHOLD > summary.Total.percent){ + throw new Error(thresholdErrorMessage(summary)); + } + writeCoverageReport(summary); + }) + + function thresholdErrorMessage(){ + msg = `** Code level coverage is too low (${summary.Total.percent}%, minimum required ${CODE_COVERAGE_THRESHOLD} %) **`; + let strLen = msg.length; + let border = "\n" +"".padStart(strLen, "*") + "\n"; + return border + msg + border; + } + + function parseSummary(summary){ + let result = {}; + const pattern = /(Classes|Methods|Lines):\s*(\d{1,3}\.\d{0,2})%\s*(\(.*\))/iug; + for (const matchLine of summary.matchAll(pattern)) { + if(matchLine[1] === "Lines") matchLine[1] = "Total"; + result[matchLine[1]] = { title: matchLine[1], percent: parseFloat(matchLine[2]), ratio: matchLine[3] } + } + return result; + } + + function writeCoverageReport(summary){ + let msgToPush = { + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: "" + }; + + console.log(summary); + + msgToPush.body = "### Pull request summary\n"; + msgToPush.body += "Code coverage:\n"; + msgToPush.body += `**${summary.Methods.title}**: ${summary.Methods.percent}% ${summary.Methods.ratio}\n`; + msgToPush.body += `**${summary.Total.title}**: ${summary.Total.percent}% ${summary.Total.ratio}\n`; + + + github.rest.issues.listComments({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + }).then((response)=>{ + let createNew = true; + for(const message of response.data){ + if(message.user.type == "Bot"){ + msgToPush.comment_id = message.id; + createNew = false; + break; + } + } + if(createNew){ + github.rest.issues.createComment(msgToPush); + }else{ + github.rest.issues.updateComment(msgToPush); + } + }, error => { + throw new Error(error); + }); + } - - - name: Git push to image-data branch - uses: peaceiris/actions-gh-pages@v3 - with: - publish_dir: ./output - publish_branch: image-data - github_token: ${{ secrets.GITHUB_TOKEN }} - user_name: 'github-actions[bot]' - user_email: 'alexey@exads.com' diff --git a/composer.json b/composer.json index 7d6bb2d..6a86826 100755 --- a/composer.json +++ b/composer.json @@ -19,6 +19,6 @@ "test": [ "vendor/bin/phpunit" ], - "test:coverage": "@php -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-clover ./coverage/clover.xml" + "test:coverage": "@php -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text=./coverage/cover.txt --coverage-filter=src" } }