From 30db5377ebd24842bfdf138e971f4c2080dfe25b Mon Sep 17 00:00:00 2001 From: James Date: Tue, 11 Feb 2020 20:12:34 +0000 Subject: [PATCH] add closing of failed tests --- README.md | 1 + src/PostFailedTests.php | 43 +++++++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 91f6ec1..32b63ce 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Enable in phpunit.xml with + diff --git a/src/PostFailedTests.php b/src/PostFailedTests.php index 59daa42..c155bb4 100644 --- a/src/PostFailedTests.php +++ b/src/PostFailedTests.php @@ -3,6 +3,11 @@ namespace JHodges\GiteaBotPHPUnit; use \JHodges\GiteaBot\Client; +/* creates a new issue for each failed test +* if it already exists then report is added as a comment +* if it passes then the issue is closed +*/ + final class PostFailedTests{ private $fails=[]; @@ -26,23 +31,15 @@ final class PostFailedTests{ $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"; + private function fail($name,$att,$error,$message){ + //composer the issue message + $issue_title="$this->title $name ($error)"; if(trim($message)) $message="```plain\n$message\n```\n"; foreach($att as $k=>$v){ $message.=" * $k = $v\n"; } - + // upload any screenshots and add to message if(file_exists("/tmp/$name.png")){ $data=$this->repo->addReleaseAttachment(1,"/tmp/$name.png"); $url=$data->browser_download_url; @@ -50,8 +47,8 @@ final class PostFailedTests{ unlink("/tmp/$name.png"); } - $issue=$this->findIssue($issue_title); - + // does the issue exist + $issue=$this->repo->getIssues(['q' => $issue_title])[0]??null; if($issue){ $issue->addComment($message); }else{ @@ -62,13 +59,29 @@ final class PostFailedTests{ } } + private function success($name,$att){ + $issue_title="$this->title $name"; + + $issue=$this->repo->getIssues(['q' => $issue_title])[0]??null; + + if($issue){ + $issue->state='closed'; + $issue->save(); + } + } + 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]; + $fail=false; foreach($testcase as $k=>$v){ - $this->fail($name,$k,$v,$att); + $this->fail($name,$att,$k,$v); + $fail=true; + } + if(!$fail){ + $this->success($name,$att); } } }