diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 06dde78..4c775e4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -48,3 +48,30 @@ jobs: phpunit-command-options: '--filter testTripalAdminPages' build-image: true dockerfile: "Dockerfile" + test_codeclimate: + name: "Run providing inputs needed for codeclimate coverage reporting" + runs-on: ubuntu-latest + steps: + # To use this repository's private action, + # you must check out the repository + - name: Checkout + uses: actions/checkout@v4 + - name: Clone Tripal for testing purposes + run: | + git clone https://github.com/tripal/tripal.git tmp + cd tmp + git checkout 4.x + mv * ../ + ls ../ + - name: Run the action step + uses: ./ # Uses an action in the root directory + with: + directory-name: tripal + modules: tripal + php-version: "8.3" + pgsql-version: "16" + drupal-version: "10.4.x-dev" + phpunit-command-options: '--filter testTripalAdminPages' + build-image: true + dockerfile: "Dockerfile" + codeclimate-reporter-id: ${{ secrets.CODECLIMATE_TEST_REPORTER_ID }} diff --git a/README.md b/README.md index 550a320..fb57823 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,9 @@ This repo provides a Github Action to make automated testing workflows for Tripa ## Example usage -The following example for a module named `my_tripal_extension` uses this action in a matrix to automate testing across multiple php and drupal versions. +The following examples for a module named `my_tripal_extension` uses this action in a matrix to automate testing across multiple php and drupal versions. + +### Basic Automated Testing ```yml name: PHPUnit @@ -40,6 +42,38 @@ jobs: drupal-version: ${{ matrix.drupal-version }} ``` +### CodeClimate Test Coverage + +The following example assumes you have setup PHPUnit to support test coverage reporting and registered your repo with CodeClimate Quality. + +Once that is complete, you can find the CodeClimate "Test Reporter ID" by going to Repo Settings > Test Coverage and copying the "Test Reporter ID" on the CodeClimate Quality page for your repo (i.e. https://codeclimate.com/github/[organization]/[repo]). + +This CodeClimate "Test Reporter ID" should then be saved as a secret in your repository on Github by going to Settings > Secrets and Variables > Actions on your repos github page (i.e. https://github.com/[organization]/[repo]/settings/secrets/actions) and adding a repository secret with a name of `CODECLIMATE_TEST_REPORTER_ID` and a value matching the CodeClimate "Test Reporter ID". + +Once you've completed those steps you can now create a workflow like the following which runs this action on a specific Drupal-PHP-PostgreSQL version (this should be the best supported version). This workflow uses the github secret as an arguement to this Github Action so that the generated clover.xml can be pushed to CodeClimate. + +You can confirm the workflow has worked by going to the CodeClimate Quality page for your repo (i.e. https://codeclimate.com/github/[organization]/[repo]). On the Repo Settings > Test Coverage page near the bottom there is a Recent Reports section and you should see a report appearing here when the workflow completes successfully. + +```yml +name: Test Code Coverage (CodeClimate) +on: [push] +jobs: + run-tests: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + - name: Run Automated testing + report coverage + uses: tripal/test-tripal-action@v1.6 + with: + directory-name: my_tripal_extension + modules: my_tripal_extension + php-version: 8.3 + pgsql-version: 16 + drupal-version: 10.4.x-dev + codeclimate-reporter-id: ${{ secrets.CODECLIMATE_TEST_REPORTER_ID }} +``` + ## Inputs ### `directory-name` @@ -75,10 +109,13 @@ A string to be appended to the end of the PHPUnit command. See the following exa - `--testsuite MyCustomTestSuite`: only run tests in a specific Test suite as configured in your phpunit.xml. - `--group MyGroupName`: runs tests with the "@group MyGroupName" added to their docblock headers. - `--filter testMySpecificMethod`: runs the testMySpecificMethod method specifically. This option can also be used to more generally filter your tests using regular expressions. -- `--coverage-clover /var/www/drupal/web/modules/mydir/clover.xml`: to run code coverage with the output format matching that for CodeClimate and place the file in a specific directory. For a full listing of options for the PHPUnit [see the docs](https://docs.phpunit.de/en/9.6/textui.html). +### `codeclimate-reporter-id` + +Provides the codeclimate reporter ID to report any code coverage to. You can find this ID by registering your repo for code climate and then going to Repo Settings > Test Coverage and copying the "Test Reporter ID". This ID should then be saved as a secret in your repository on Github by going to Settings > Secrets and Variables > Actions on your repos github page and adding a repository secret (e.g. `CODECLIMATE_TEST_REPORTER_ID`) where the value is the CodeClimate "Test Reporter ID". See the Test coverage example for more details on how to use this in your workflow. + ### `build-image` Indicates whether you would like to build the docker based on your code (true) or pull an existing image (false). diff --git a/action.yml b/action.yml index 2a56828..7a99cb8 100644 --- a/action.yml +++ b/action.yml @@ -24,6 +24,9 @@ inputs: description: "A string providing any options you would like added to the phpunit command." required: false default: ' ' + codeclimate-reporter-id: + description: the reporter ID for codeclimate to publish results to. This should be saved as a secret in your reporsitory. + required: false build-image: description: "Indicates whether you would like to build the docker based on your code (true) or pull an existing image (false)." required: true @@ -67,8 +70,20 @@ runs: shell: bash run: | docker exec tripaldocker drush en ${{ inputs.modules }} --yes - # Runs the PHPUnit tests. + # If we have codeclimate enabled, then prepare for it here. + - name: Prepare for Codeclimate coverage reporting + if: "${{ inputs.codeclimate-reporter-id != '' }}" + shell: bash + env: + ABSOLUTE_MODULE_PATH: "/var/www/drupal/web/modules/contrib/${{ inputs.directory-name }}" + run: | + curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter + docker cp cc-test-reporter tripaldocker:$ABSOLUTE_MODULE_PATH + docker exec tripaldocker chmod a+x $ABSOLUTE_MODULE_PATH/cc-test-reporter + docker exec --workdir=$ABSOLUTE_MODULE_PATH tripaldocker ./cc-test-reporter before-build --debug + # Runs the PHPUnit tests without coverage. - name: Run PHPUnit Tests + if: "${{ inputs.codeclimate-reporter-id == '' }}" shell: bash env: SIMPLETEST_BASE_URL: "http://localhost" @@ -80,3 +95,34 @@ runs: -e BROWSER_OUTPUT_DIRECTORY=$BROWSER_OUTPUT_DIRECTORY \ --workdir=/var/www/drupal/web/modules/contrib/${{ inputs.directory-name }} \ tripaldocker phpunit ${{ inputs.phpunit-command-options }} + # Runs the PHPUnit tests WITH coverage. + - name: Run PHPUnit Tests with coverage for CodeClimate + if: "${{ inputs.codeclimate-reporter-id != '' }}" + shell: bash + env: + SIMPLETEST_BASE_URL: "http://localhost" + SIMPLETEST_DB: "pgsql://drupaladmin:drupaldevelopmentonlylocal@localhost/sitedb" + BROWSER_OUTPUT_DIRECTORY: "/var/www/drupal/web/sites/default/files/simpletest" + CODECLIMATE_REPORT: "/var/www/drupal/web/modules/contrib/${{ inputs.directory-name }}/clover.xml" + run: | + docker exec -e SIMPLETEST_BASE_URL=$SIMPLETEST_BASE_URL \ + -e SIMPLETEST_DB=$SIMPLETEST_DB \ + -e BROWSER_OUTPUT_DIRECTORY=$BROWSER_OUTPUT_DIRECTORY \ + --workdir=/var/www/drupal/web/modules/contrib/${{ inputs.directory-name }} \ + tripaldocker phpunit ${{ inputs.phpunit-command-options }} \ + --coverage-text --coverage-clover $CODECLIMATE_REPORT + docker exec tripaldocker ls /var/www/drupal/web/modules/contrib/${{ inputs.directory-name }} + # Publish to codeclimate + - name: Publish code coverage to Code Climate + if: "${{ inputs.codeclimate-reporter-id != '' }}" + shell: bash + env: + CODECLIMATE_REPORT: "/var/www/drupal/web/modules/contrib/${{ inputs.directory-name }}/clover.xml" + ABSOLUTE_MODULE_PATH: "/var/www/drupal/web/modules/contrib/${{ inputs.directory-name }}" + run: | + docker exec --workdir=$ABSOLUTE_MODULE_PATH tripaldocker \ + git config --global --add safe.directory $ABSOLUTE_MODULE_PATH + docker exec --workdir=$ABSOLUTE_MODULE_PATH \ + tripaldocker ./cc-test-reporter after-build clover.xml \ + --id ${{ inputs.codeclimate-reporter-id }} \ + --debug -t clover -p $ABSOLUTE_MODULE_PATH