Page MenuHomeDevCentral

D628.id1558.diff
No OneTemporary

D628.id1558.diff

diff --git a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
--- a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
+++ b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
@@ -117,4 +117,57 @@
return $this->configuration->getDefaultGroup();
}
+ ///
+ /// Notify only on failure helper methods
+ ///
+
+ /**
+ * Tries to get build status.
+ *
+ * @param out string $status
+ * @return bool indicates if the build status is defined in the payload
+ */
+ private function tryGetBuildStatus (&$status) {
+ if (!isset($this->payload->build->status)) {
+ return false;
+ }
+
+ $status = $this->payload->build->status;
+ return true;
+ }
+
+ /**
+ * @return bool
+ */
+ public function shouldNotifyOnlyOnFailure () {
+ return in_array(
+ $this->getJobName(),
+ $this->configuration->notifyOnlyOnFailure
+ );
+ }
+
+ /**
+ * Determines if the build status is a failure
+ *
+ * @return bool
+ */
+ public function isFailure () {
+ if (!$this->tryGetBuildStatus($status)) {
+ return false;
+ }
+
+ return $status === "FAILURE"
+ || $status === "ABORTED"
+ || $status === "UNSTABLE";
+ }
+
+ /**
+ * Indicates if we should handle this payload to trigger a notification.
+ *
+ * @return bool if false, this payload is to be ignored for notifications
+ */
+ public function shouldNotify () {
+ return $this->isFailure() || !$this->shouldNotifyOnlyOnFailure();
+ }
+
}
diff --git a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfiguration.php b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfiguration.php
--- a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfiguration.php
+++ b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfiguration.php
@@ -34,9 +34,9 @@
public $groupsMapping;
/**
- * @var string[]
+ * @var array
*/
- public $notifyOnlyOnFailureJobs;
+ public $notifyOnlyOnFailure;
///
/// Constructor
diff --git a/app/Jobs/FireJenkinsNotification.php b/app/Jobs/FireJenkinsNotification.php
--- a/app/Jobs/FireJenkinsNotification.php
+++ b/app/Jobs/FireJenkinsNotification.php
@@ -36,7 +36,9 @@
*/
public function handle() {
$notification = $this->createNotification();
- Event::fire(new NotificationEvent($notification));
+ if ($notification->shouldNotify()) {
+ Event::fire(new NotificationEvent($notification));
+ }
}
/**
diff --git a/app/Notifications/JenkinsNotification.php b/app/Notifications/JenkinsNotification.php
--- a/app/Notifications/JenkinsNotification.php
+++ b/app/Notifications/JenkinsNotification.php
@@ -100,4 +100,13 @@
return $this->getAnalyzer()->getGroup();
}
+ /**
+ * Indicates if we should handle this payload to trigger a notification.
+ *
+ * @return bool if false, this payload is to be ignored for notifications
+ */
+ public function shouldNotify () {
+ return $this->getAnalyzer()->shouldNotify();
+ }
+
}
diff --git a/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerTest.php b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerTest.php
@@ -0,0 +1,78 @@
+<?php
+
+namespace Nasqueron\Notifications\Tests\Analyzers;
+
+use Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer;
+use Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration;
+use Nasqueron\Notifications\Tests\TestCase;
+
+class JenkinsPayloadAnalyzerTest extends TestCase {
+
+ /**
+ * Jenkins analyzer to a successful build
+ *
+ * @var Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer
+ */
+ protected $analyzer;
+
+ /**
+ * @var stdClass
+ */
+ protected $payload;
+
+ /**
+ * Prepares the test
+ */
+ public function setUp () {
+ parent::setUp();
+
+ $filename = __DIR__ . '/../../data/payloads/JenkinsToIgnorePayload.json';
+ $this->payload = json_decode(file_get_contents($filename));
+ $this->analyzer = new JenkinsPayloadAnalyzer("Nasqueron", $this->payload);
+ }
+
+ public function testGetJobName () {
+ $this->assertSame("test-prod-env", $this->analyzer->getJobName());
+ }
+
+ public function testGetGroup () {
+ $this->assertSame("ops", $this->analyzer->getGroup());
+ }
+
+ public function testGetGroupWhenWeNeedDefaultFallback () {
+ $this->payload->name = "quux";
+ $this->assertSame("ci", $this->analyzer->getGroup());
+ }
+
+ public function testShouldNotifyWhenStatusIsUndefined () {
+ unset($this->payload->build->status);
+ $this->assertFalse($this->analyzer->shouldNotify());
+ }
+
+ /**
+ * @dataProvider payloadStatusProvider
+ */
+ public function testShouldNotifyByStatus ($status, $shouldNotify) {
+ $this->payload->build->status = $status;
+ $this->assertSame($shouldNotify, $this->analyzer->shouldNotify());
+ }
+
+ /**
+ * Provides data for testShouldNotifyByStatus
+ *
+ * @return array
+ */
+ public function payloadStatusProvider () {
+ return [
+ // Build status to notify
+ ["FAILURE", true],
+ ["ABORTED", true],
+ ["UNSTABLE", true],
+
+ // Build status to ignore
+ ["SUCCESS", false],
+ ["NOT_BUILT", false],
+ ];
+ }
+
+}
\ No newline at end of file
diff --git a/tests/data/payloads/JenkinsToIgnorePayload.json b/tests/data/payloads/JenkinsToIgnorePayload.json
new file mode 100644
--- /dev/null
+++ b/tests/data/payloads/JenkinsToIgnorePayload.json
@@ -0,0 +1,9 @@
+{
+ "name": "test-prod-env",
+ "url": "job/test-prod-env/",
+ "build": {
+ "full_url": "https://ci.nasqueron.org/job/test-prod-env/5944/",
+ "phase": "COMPLETED",
+ "status": "SUCCESS"
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 19:26 (18 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258754
Default Alt Text
D628.id1558.diff (5 KB)

Event Timeline