Skip to content

Commit

Permalink
Added COVERALLS_PARALLEL support and Configured CI_BUILD_NUMBER for T…
Browse files Browse the repository at this point in the history
…ravis CI (#279)

* updated to detect COVERALLS_PARALLEL env var

* added CI_BUILD_NUMBER into Configuration

* updated README

* fixed tests

* Update RequirementsNotSatisfiedException.php

* fix CS

* fix checking of required fields + tests

* add missing class property

Co-authored-by: Dariusz Rumiński <dariusz.ruminski@gmail.com>
  • Loading branch information
yihyang and keradus committed Sep 24, 2020
1 parent 7d872a9 commit 32ba402
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -257,6 +257,19 @@ php-coveralls set the following properties to `json_file` which is sent to Cover
- service_name: php-coveralls
- service_event_type: manual

## Parallel Builds

Coveralls provides the ability to combine coverage result from multiple parallel builds into one. To enable the feature you can set the following in your environment variable.

```sh
COVERALLS_PARALLEL=true
```

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.


## CLI options

You can get help information for `coveralls` with the `--help (-h)` option.
Expand Down
23 changes: 20 additions & 3 deletions src/Bundle/CoverallsBundle/Collector/CiEnvVarsCollector.php
Expand Up @@ -64,7 +64,8 @@ public function collect(array $env)
->fillAppVeyor()
->fillJenkins()
->fillLocal()
->fillRepoToken();
->fillRepoToken()
->fillParallel();

return $this->env;
}
Expand All @@ -86,14 +87,15 @@ public function getReadEnv()
/**
* Fill Travis CI environment variables.
*
* "TRAVIS", "TRAVIS_JOB_ID" must be set.
* "TRAVIS", "TRAVIS_BUILD_NUMBER", TRAVIS_JOB_ID" must be set.
*
* @return $this
*/
protected function fillTravisCi()
{
if (isset($this->env['TRAVIS']) && $this->env['TRAVIS'] && isset($this->env['TRAVIS_JOB_ID'])) {
if (isset($this->env['TRAVIS']) && $this->env['TRAVIS'] && isset($this->env['TRAVIS_JOB_ID']) && isset($this->env['TRAVIS_BUILD_NUMBER'])) {
$this->env['CI_JOB_ID'] = $this->env['TRAVIS_JOB_ID'];
$this->env['CI_BUILD_NUMBER'] = $this->env['TRAVIS_BUILD_NUMBER'];

if ($this->config->hasServiceName()) {
$this->env['CI_NAME'] = $this->config->getServiceName();
Expand All @@ -105,6 +107,7 @@ protected function fillTravisCi()
$this->readEnv['TRAVIS'] = $this->env['TRAVIS'];
$this->readEnv['TRAVIS_JOB_ID'] = $this->env['TRAVIS_JOB_ID'];
$this->readEnv['CI_NAME'] = $this->env['CI_NAME'];
$this->readEnv['CI_BUILD_NUMBER'] = $this->env['CI_BUILD_NUMBER'];
}

return $this;
Expand Down Expand Up @@ -226,4 +229,18 @@ protected function fillRepoToken()

return $this;
}

/**
* Fill parallel for parallel jobs.
*
* @return $this
*/
protected function fillParallel()
{
if (isset($this->env['COVERALLS_PARALLEL'])) {
$this->readEnv['COVERALLS_PARALLEL'] = $this->env['COVERALLS_PARALLEL'];
}

return $this;
}
}
Expand Up @@ -53,6 +53,7 @@ public function getHelpMessage()
For Travis users:
- TRAVIS
- TRAVIS_BUILD_NUMBER
- TRAVIS_JOB_ID
For CircleCI users:
Expand Down
55 changes: 50 additions & 5 deletions src/Bundle/CoverallsBundle/Entity/JsonFile.php
Expand Up @@ -99,6 +99,14 @@ class JsonFile extends Coveralls
*/
protected $metrics;

/**
* If this is set, the build will not be considered done until a webhook has
* been sent to https://coveralls.io/webhook?repo_token=….
*
* @var bool
*/
protected $parallel;

// API

/**
Expand All @@ -120,6 +128,7 @@ public function toArray()
'service_pull_request' => 'servicePullRequest',
'service_event_type' => 'serviceEventType',
'repo_token' => 'repoToken',
'parallel' => 'parallel',
'git' => 'git',
'run_at' => 'runAt',
'source_files' => 'sourceFiles',
Expand Down Expand Up @@ -298,6 +307,30 @@ public function getRepoToken()
return $this->repoToken;
}

/**
* Set parallel.
*
* @param string $parallel parallel
*
* @return $this
*/
public function setParallel($parallel)
{
$this->parallel = $parallel;

return $this;
}

/**
* Return parallel.
*
* @return null|bool
*/
public function getParallel()
{
return $this->parallel;
}

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

foreach ($map as $propName => $envName) {
Expand Down Expand Up @@ -561,7 +595,10 @@ protected function ensureJobs()
*/
protected function requireServiceJobId()
{
return $this->serviceName !== null && $this->serviceJobId !== null && $this->repoToken === null;
return $this->serviceName !== null
&& $this->serviceNumber !== null
&& $this->serviceJobId !== null
&& $this->repoToken === null;
}

/**
Expand All @@ -571,7 +608,9 @@ protected function requireServiceJobId()
*/
protected function requireServiceNumber()
{
return $this->serviceName !== null && $this->serviceNumber !== null && $this->repoToken !== null;
return $this->serviceName !== null
&& $this->serviceNumber !== null
&& $this->repoToken !== null;
}

/**
Expand All @@ -581,7 +620,9 @@ protected function requireServiceNumber()
*/
protected function requireServiceEventType()
{
return $this->serviceName !== null && $this->serviceEventType !== null && $this->repoToken !== null;
return $this->serviceName !== null
&& $this->serviceEventType !== null
&& $this->repoToken !== null;
}

/**
Expand All @@ -591,7 +632,8 @@ protected function requireServiceEventType()
*/
protected function requireRepoToken()
{
return $this->serviceName === 'travis-pro' && $this->repoToken !== null;
return $this->serviceName === 'travis-pro'
&& $this->repoToken !== null;
}

/**
Expand All @@ -601,6 +643,9 @@ protected function requireRepoToken()
*/
protected function isUnsupportedServiceJob()
{
return $this->serviceJobId === null && $this->serviceNumber === null && $this->serviceEventType === null && $this->repoToken !== null;
return $this->serviceJobId === null
&& $this->serviceNumber === null
&& $this->serviceEventType === null
&& $this->repoToken !== null;
}
}
7 changes: 5 additions & 2 deletions tests/Bundle/CoverallsBundle/Api/JobsTest.php
Expand Up @@ -275,6 +275,7 @@ public function shouldSendTravisCiJob()

$server = [];
$server['TRAVIS'] = true;
$server['TRAVIS_BUILD_NUMBER'] = '654321';
$server['TRAVIS_JOB_ID'] = $serviceJobId;

$object = $this->createJobsWith();
Expand All @@ -300,6 +301,7 @@ public function shouldSendTravisProJob()

$server = [];
$server['TRAVIS'] = true;
$server['TRAVIS_BUILD_NUMBER'] = '8888';
$server['TRAVIS_JOB_ID'] = $serviceJobId;
$server['COVERALLS_REPO_TOKEN'] = $repoToken;

Expand Down Expand Up @@ -439,6 +441,7 @@ public function shouldNotSendJobIfTestEnv()

$server = [];
$server['TRAVIS'] = true;
$server['TRAVIS_BUILD_NUMBER'] = '198765';
$server['TRAVIS_JOB_ID'] = '1.1';

$object = $this->createJobsNeverSendOnDryRun();
Expand Down Expand Up @@ -478,12 +481,12 @@ public function throwRuntimeExceptionIfNoSourceFiles()
{
$server = [];
$server['TRAVIS'] = true;
$server['TRAVIS_BUILD_NUMBER'] = '12345';
$server['TRAVIS_JOB_ID'] = '1.1';
$server['COVERALLS_REPO_TOKEN'] = 'token';
$server['GIT_COMMIT'] = 'abc123';

$object = $this->createJobsNeverSend();
$jsonFile = $this->collectJsonFile();
$jsonFile = $this->collectJsonFileWithoutSourceFiles();

$object
->setJsonFile($jsonFile)
Expand Down
36 changes: 34 additions & 2 deletions tests/Bundle/CoverallsBundle/Collector/CiEnvVarsCollectorTest.php
Expand Up @@ -27,10 +27,12 @@ public function shouldCollectTravisCiEnvVars()
{
$serviceName = 'travis-ci';
$serviceJobId = '1.1';
$serviceBuildNumber = '123456';

$env = [];
$env['TRAVIS'] = true;
$env['TRAVIS_JOB_ID'] = $serviceJobId;
$env['TRAVIS_BUILD_NUMBER'] = $serviceBuildNumber;

$object = $this->createCiEnvVarsCollector();

Expand All @@ -42,6 +44,9 @@ public function shouldCollectTravisCiEnvVars()
$this->assertArrayHasKey('CI_JOB_ID', $actual);
$this->assertSame($serviceJobId, $actual['CI_JOB_ID']);

$this->assertArrayHasKey('CI_BUILD_NUMBER', $actual);
$this->assertSame($serviceBuildNumber, $actual['CI_BUILD_NUMBER']);

return $object;
}

Expand All @@ -52,11 +57,13 @@ public function shouldCollectTravisProEnvVars()
{
$serviceName = 'travis-pro';
$serviceJobId = '1.2';
$serviceBuildNumber = '12345';
$repoToken = 'your_token';

$env = [];
$env['TRAVIS'] = true;
$env['TRAVIS_JOB_ID'] = $serviceJobId;
$env['TRAVIS_BUILD_NUMBER'] = $serviceBuildNumber;
$env['COVERALLS_REPO_TOKEN'] = $repoToken;

$config = $this->createConfiguration();
Expand All @@ -72,6 +79,9 @@ public function shouldCollectTravisProEnvVars()
$this->assertArrayHasKey('CI_JOB_ID', $actual);
$this->assertSame($serviceJobId, $actual['CI_JOB_ID']);

$this->assertArrayHasKey('CI_BUILD_NUMBER', $actual);
$this->assertSame($serviceBuildNumber, $actual['CI_BUILD_NUMBER']);

$this->assertArrayHasKey('COVERALLS_REPO_TOKEN', $actual);
$this->assertSame($repoToken, $actual['COVERALLS_REPO_TOKEN']);

Expand Down Expand Up @@ -204,6 +214,26 @@ public function shouldCollectUnsupportedEnvVars()
return $object;
}

/**
* @test
*/
public function shouldCollectParallel()
{
$parallel = true;

$env = [];
$env['COVERALLS_PARALLEL'] = $parallel;

$object = $this->createCiEnvVarsCollector();

$actual = $object->collect($env);

$this->assertArrayHasKey('COVERALLS_PARALLEL', $actual);
$this->assertSame($parallel, $actual['COVERALLS_PARALLEL']);

return $object;
}

// getReadEnv()

/**
Expand All @@ -226,10 +256,11 @@ public function shouldHaveReadEnvAfterCollectTravisCiEnvVars(CiEnvVarsCollector
{
$readEnv = $object->getReadEnv();

$this->assertCount(3, $readEnv);
$this->assertCount(4, $readEnv);
$this->assertArrayHasKey('TRAVIS', $readEnv);
$this->assertArrayHasKey('TRAVIS_JOB_ID', $readEnv);
$this->assertArrayHasKey('CI_NAME', $readEnv);
$this->assertArrayHasKey('CI_BUILD_NUMBER', $readEnv);
}

/**
Expand All @@ -242,10 +273,11 @@ public function shouldHaveReadEnvAfterCollectTravisProEnvVars(CiEnvVarsCollector
{
$readEnv = $object->getReadEnv();

$this->assertCount(4, $readEnv);
$this->assertCount(5, $readEnv);
$this->assertArrayHasKey('TRAVIS', $readEnv);
$this->assertArrayHasKey('TRAVIS_JOB_ID', $readEnv);
$this->assertArrayHasKey('CI_NAME', $readEnv);
$this->assertArrayHasKey('CI_BUILD_NUMBER', $readEnv);
$this->assertArrayHasKey('COVERALLS_REPO_TOKEN', $readEnv);
}

Expand Down
Expand Up @@ -45,6 +45,7 @@ public function shouldExecuteCoverallsJobsCommand()
$commandTester = new CommandTester($command);

$_SERVER['TRAVIS'] = true;
$_SERVER['TRAVIS_BUILD_NUMBER'] = '12345';
$_SERVER['TRAVIS_JOB_ID'] = 'command_test';

$actual = $commandTester->execute(
Expand Down
1 change: 1 addition & 0 deletions tests/Bundle/CoverallsBundle/Console/ApplicationTest.php
Expand Up @@ -39,6 +39,7 @@ public function shouldExecuteCoverallsJobsCommand()

// run
$_SERVER['TRAVIS'] = true;
$_SERVER['TRAVIS_BUILD_NUMBER'] = 'application_build';
$_SERVER['TRAVIS_JOB_ID'] = 'application_test';

$tester = new ApplicationTester($app);
Expand Down

0 comments on commit 32ba402

Please sign in to comment.