only post failed tests. comment if already open, else create new issue
This commit is contained in:
parent
afc4c618f4
commit
811f953e6e
82
src/PosterFailedTests.php
Normal file
82
src/PosterFailedTests.php
Normal 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\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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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'),
|
||||
|
Loading…
Reference in New Issue
Block a user