Skip to content

Commit

Permalink
Add Github Actions and COVERALLS_FLAG_NAME support (#296)
Browse files Browse the repository at this point in the history
Co-authored-by: Smolevich <smolevich90@gmail.com>
Co-authored-by: Simon Podlipsky <simon@podlipsky.net>
  • Loading branch information
3 people committed Oct 5, 2020
1 parent c106696 commit f266bd4
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 2 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/blank.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
branches:
tags:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
php: [7.1, 7.2, 7.3, 7.4]
steps:
- uses: actions/checkout@v1
- name: Debug if needed
run: |
export DEBUG=${DEBUG:-false}
if [[ "$DEBUG" == "true" ]]; then
env
fi
env:
DEBUG: ${{secrets.DEBUG}}
- name: Setup php
uses: shivammathur/setup-php@1.7.0
with:
php-version: ${{ matrix.php }}
extensions: dom, mbstring
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer install
- name: Run composer script
run: |
composer install-dev-tools;
composer sca
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ In the "Configure your environment variables" section:
COVERALLS_REPO_TOKEN=your_token
```

## GitHub Actions

Add a new step after phpunit generate coverage report.

```yaml
- name: Upload coverage results to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=build/logs/clover.xml -v
```

## From local environment

If you would like to call Coveralls API from your local environment, you can set `COVERALLS_RUN_LOCALLY` environment variable. This configuration requires `repo_token` to specify which project on Coveralls your project maps to. This can be done by configuring `.coveralls.yml` or `COVERALLS_REPO_TOKEN` environment variable.
Expand Down Expand Up @@ -265,6 +278,12 @@ Coveralls provides the ability to combine coverage result from multiple parallel
COVERALLS_PARALLEL=true
```

To distinguish your job name, please set the `COVERALLS_FLAG_NAME` environment variable.

```sh
COVERALLS_FLAG_NAME=$YOUR_PHP_VERSION
```

Bear in mind that you will need to configure your build to send a webhook after all the parallel builds are done in order for Coveralls to merge the results.

Refer to [Parallel Builds Webhook](https://docs.coveralls.io/parallel-build-webhook) for more information for setup on your environment.
Expand Down
45 changes: 45 additions & 0 deletions src/Bundle/CoverallsBundle/Collector/CiEnvVarsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function collect(array $env)
->fillCircleCi()
->fillAppVeyor()
->fillJenkins()
->fillGithubActions()
->fillLocal()
->fillRepoToken()
->fillParallel();
Expand Down Expand Up @@ -113,6 +114,46 @@ protected function fillTravisCi()
return $this;
}

/**
* Fill Github Actions environment variables.
*
* @return $this
*/
protected function fillGithubActions()
{
if (!isset($this->env['GITHUB_ACTIONS'])) {
return $this;
}
$this->env['CI_NAME'] = 'github';

$githubEventName = $this->env['GITHUB_EVENT_NAME'];
$githubRef = $this->env['GITHUB_REF'];

if (strpos($githubRef, 'refs/heads/') !== false) {
$githubRef = str_replace('refs/heads/', '', $githubRef);
} elseif ($githubEventName === 'pull_request') {
$refParts = explode('/', $githubRef);
$prNumber = $refParts[2];
$this->env['CI_PULL_REQUEST'] = $prNumber;
$this->readEnv['CI_PULL_REQUEST'] = $this->env['CI_PULL_REQUEST'];
} elseif (strpos($githubRef, 'refs/tags/') !== false) {
$githubRef = str_replace('refs/tags/', '', $githubRef);
}

// Same as coverallsapp/github-action
// @link https://github.com/coverallsapp/github-action/blob/5984097c6e76d873ef1d8e8e1836b0914d307c3c/src/run.ts#L47
$this->env['CI_JOB_ID'] = $this->env['GITHUB_RUN_ID'];
$this->env['CI_BRANCH'] = $githubRef;

$this->readEnv['GITHUB_ACTIONS'] = $this->env['GITHUB_ACTIONS'];
$this->readEnv['GITHUB_REF'] = $this->env['GITHUB_REF'];
$this->readEnv['CI_NAME'] = $this->env['CI_NAME'];
$this->readEnv['CI_JOB_ID'] = $this->env['CI_JOB_ID'];
$this->readEnv['CI_BRANCH'] = $this->env['CI_BRANCH'];

return $this;
}

/**
* Fill CircleCI environment variables.
*
Expand Down Expand Up @@ -241,6 +282,10 @@ protected function fillParallel()
$this->readEnv['COVERALLS_PARALLEL'] = $this->env['COVERALLS_PARALLEL'];
}

if (isset($this->env['COVERALLS_FLAG_NAME'])) {
$this->readEnv['COVERALLS_FLAG_NAME'] = $this->env['COVERALLS_FLAG_NAME'];
}

return $this;
}
}
4 changes: 2 additions & 2 deletions src/Bundle/CoverallsBundle/Collector/GitInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ protected function collectBranch()

foreach ($branchesResult as $result) {
if (strpos($result, '* ') === 0) {
$exploded = explode('* ', $result, 2);
preg_match('~^\* (?:\(HEAD detached at )?([\w/]+)\)?~', $result, $matches);

return $exploded[1];
return $matches[1];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public function getHelpMessage()
- APPVEYOR
- APPVEYOR_BUILD_NUMBER
For Githib Actions users:
- GITHUB_REF
- GITHUB_ACTIONS
- GITHUB_RUN_ID
- GITHUB_EVENT_NAME
From local environment:
- COVERALLS_RUN_LOCALLY
Expand Down
49 changes: 49 additions & 0 deletions src/Bundle/CoverallsBundle/Entity/JsonFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Data represents "json_file" of Coveralls API.
*
* @author Kitamura Satoshi <with.no.parachute@gmail.com>
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class JsonFile extends Coveralls
{
Expand Down Expand Up @@ -107,6 +108,14 @@ class JsonFile extends Coveralls
*/
protected $parallel;

/**
* If this is set, the job being reported will be named in the view and have
* it’s own independent status reported to your VCS provider.
*
* @var string
*/
protected $flagName;

// API

/**
Expand All @@ -129,6 +138,7 @@ public function toArray()
'service_event_type' => 'serviceEventType',
'repo_token' => 'repoToken',
'parallel' => 'parallel',
'flag_name' => 'flagName',
'git' => 'git',
'run_at' => 'runAt',
'source_files' => 'sourceFiles',
Expand Down Expand Up @@ -331,6 +341,30 @@ public function getParallel()
return $this->parallel;
}

/**
* Set flag name.
*
* @param string $flagName flag name
*
* @return $this
*/
public function setFlagName($flagName)
{
$this->flagName = $flagName;

return $this;
}

/**
* Return flag name.
*
* @return null|string
*/
public function getFlagName()
{
return $this->flagName;
}

/**
* Set service job id.
*
Expand Down Expand Up @@ -541,6 +575,7 @@ protected function fillStandardizedEnvVars(array $env)
'serviceEventType' => 'COVERALLS_EVENT_TYPE',
'repoToken' => 'COVERALLS_REPO_TOKEN',
'parallel' => 'COVERALLS_PARALLEL',
'flagName' => 'COVERALLS_FLAG_NAME',
];

foreach ($map as $propName => $envName) {
Expand Down Expand Up @@ -581,6 +616,10 @@ protected function ensureJobs()
return $this;
}

if ($this->requireGithubActions()) {
return $this;
}

if ($this->isUnsupportedServiceJob()) {
return $this;
}
Expand Down Expand Up @@ -636,6 +675,16 @@ protected function requireRepoToken()
&& $this->repoToken !== null;
}

/**
* Return whether the job requires "service_number", "service_job_id" and "repo_token" (for GithubActions).
*
* @return bool
*/
protected function requireGithubActions()
{
return $this->serviceName === 'github' && $this->serviceJobId !== null && $this->repoToken !== null;
}

/**
* Return whether the job is running on unsupported service.
*
Expand Down

0 comments on commit f266bd4

Please sign in to comment.