23
loading...
This website collects cookies to deliver better user experience
phploc src | grep LLOC
). If you're not familiar with infection or mutation testing in general yet, I can recommend this blog post.--min-msi
& --min-covered-msi
flags and raise the bar from time to time, until you reach 100%.RUN wget https://github.com/infection/infection/releases/download/0.25.0/infection.phar \
&& wget https://github.com/infection/infection/releases/download/0.25.0/infection.phar.asc \
&& chmod +x infection.phar \
&& mv infection.phar /usr/local/bin/infection
infection.json
is pretty basic, but it's important to have the different outputs in the logs
section configured:{
"$schema": "https://raw.githubusercontent.com/infection/infection/0.25.0/resources/schema.json",
"source": {
"directories": [
"src/Pyz"
],
"excludes": [
"DependencyProvider.php",
"BusinessFactory.php"
]
},
"logs": {
"text": "infection.log",
"summary": "summary.log",
"perMutator": "per-mutator.md"
},
"mutators": {
"@default": true
}
}
src/
and you will need to adapt the excludes
as well.--git-diff-filter
option. We use that like so in our CI:- git fetch --depth=1 origin master
- infection --git-diff-filter=AM || true
https://<your-gitlab-domain>/<group>/<project>/-/settings/access_tokens
. Copy the token to a safe place and make sure that it's available as a environment variable during the CI job.composer require --dev "m4tthumphrey/php-gitlab-api:^11.4" "guzzlehttp/guzzle:^7.2" "http-interop/http-factory-guzzle:^1.0"
<?php
declare(strict_types = 1);
require_once 'vendor/autoload.php';
$projectId = (int)getenv('CI_PROJECT_ID');
if ($projectId === 0) {
die('CI_PROJECT_ID is missing!');
}
$mergeRequestId = (int)getenv('CI_MERGE_REQUEST_IID');
if ($mergeRequestId === 0) {
die('CI_MERGE_REQUEST_IID missing!');
}
$authToken = (string)getenv('GITLAB_AUTH_TOKEN');
if ($authToken === '') {
die('GITLAB_AUTH_TOKEN missing!');
}
$client = new Gitlab\Client();
$client->setUrl('<your-gitlab-domain>');
$client->authenticate($authToken, Gitlab\Client::AUTH_HTTP_TOKEN);
if (!function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle): bool
{
return strpos($haystack, $needle) === 0;
}
}
/* ---------------------------------------------- */
$data = json_decode(file_get_contents('infection-log.json'), true);
$identifierText = '<details><summary>infection results 📋</summary>';
$noteBody = $identifierText . PHP_EOL . PHP_EOL;
$noteBody .= '| metric | value |' . PHP_EOL;
$noteBody .= '| ------ | ----- |' . PHP_EOL;
$noteBody .= '| Mutation Score Indicator (MSI) | ' . $data['stats']['msi'] . '% |' . PHP_EOL;
$noteBody .= '| Mutation Code Coverage | ' . $data['stats']['mutationCodeCoverage'] . '% |' . PHP_EOL;
$noteBody .= '| Covered Code MSI | ' . $data['stats']['coveredCodeMsi'] . '% |' . PHP_EOL;
$noteBody .= '```
' . file_get_contents('summary.log') . '
```' . PHP_EOL;
$noteBody .= '```
' . file_get_contents('infection.log') . '
```' . PHP_EOL;
$noteBody .= file_get_contents('per-mutator.md') . PHP_EOL;
$noteBody .= PHP_EOL . '</details>';
/* ---------------------------------------------- */
$notes = $client->mergeRequests()->showNotes($projectId, $mergeRequestId);
foreach ($notes as $note) {
if (str_starts_with($note['body'], $identifierText)) {
$noteId = $note['id'];
}
}
if (isset($noteId)) {
$notes = $client->mergeRequests()->updateNote($projectId, $mergeRequestId, $noteId, $noteBody);
echo 'updated';
} else {
$notes = $client->mergeRequests()->addNote($projectId, $mergeRequestId, $noteBody);
echo 'added';
}
echo PHP_EOL;
CI_PROJECT_ID
and CI_MERGE_REQUEST_IID
are automatically available in every Gitlab CI job, you just need to make sure that the auth token you generated is available in GITLAB_AUTH_TOKEN
. Do not forget to replace <your-gitlab-domain>
with the real url.davidrjonas/composer-lock-diff
next.