only post failed tests. comment if already open, else create new issue

This commit is contained in:
James 2020-02-11 15:51:41 +00:00
parent afc4c618f4
commit 811f953e6e
3 changed files with 83 additions and 1 deletions

82
src/PosterFailedTests.php Normal file
View File

@ -0,0 +1,82 @@
<?php
namespace JHodges\GiteaBotPHPUnit;
use \JHodges\GiteaBot\Client;
final class Poster2{
private $fails=[];
private $client=null;
private $repo=null;
private $title='';
function __construct($xml_path){
$this->xml = simplexml_load_file($xml_path);
}
public function post($url, $user, $pass, $repoUser, $repo, $title='Test Results'){
if(!$url) return;
$this->title=$title;
// open connection and repo
$this->client=new Client($url, $user, $pass, true);
$this->repo=$this->client->getRepo($repoUser, $repo);
$this->process($this->xml);
}
private function findIssue($title){
foreach($this->repo->getIssues(['q' => $title]) as $issue) {
// loop through the current issues comments
if($issue->title===$title){
return $issue;
}
}
return false;
}
private function fail($name,$error,$message,$att){
$issue_title="$this->title $error $name";
if(trim($message)) $message="```plain\n$message\n```\n";
foreach($att as $k=>$v){
$message.=" * $k = $v\n";
}
if(file_exists("/tmp/$name.png")){
$data=$this->repo->addReleaseAttachment(1,"/tmp/$name.png");
$url=$data->browser_download_url;
$message="\n![]($url)\n".$message;
unlink("/tmp/$name.png");
}
$issue=$this->findIssue($issue_title);
if($issue){
$issue->addComment($message);
}else{
$issue=$this->repo->createIssue([
'title'=>$issue_title,
'body'=>$message
]);
}
}
private function process($item){
if(isset($item->testcase)){
foreach($item->testcase as $testcase) {
$att=$testcase->attributes();
$name=$testcase->attributes()->class[0].'::'.$testcase->attributes()->name[0];
foreach($testcase as $k=>$v){
$this->fail($name,$k,$v,$att);
}
}
}
if(isset($item->testsuite)){
foreach($item->testsuite as $testsuite) {
$this->process($testsuite);
}
}
}
}

View File

@ -9,7 +9,7 @@ class ResultPrinter extends \PHPUnit\TextUI\ResultPrinter{
public function printResult(\PHPUnit\Framework\TestResult $result){
parent::printResult($result);
$poster=new \JHodges\GiteaBotPHPUnit\Poster('/tmp/logfile.xml');
$poster=new \JHodges\GiteaBotPHPUnit\PostFailedTests('/tmp/logfile.xml');
$poster->post(
getenv('GiteaUrl'),
getenv('GiteaUser'),