Page MenuHomeDevCentral

D628.diff
No OneTemporary

D628.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],
+ ];
+ }
+
+}
diff --git a/tests/Notifications/JenkinsNotificationTest.php b/tests/Notifications/JenkinsNotificationTest.php
--- a/tests/Notifications/JenkinsNotificationTest.php
+++ b/tests/Notifications/JenkinsNotificationTest.php
@@ -16,12 +16,12 @@
*/
private $payload;
- public function prepareNotification ($payloadFile) {
+ public function prepareNotification ($payloadFile, $project = "Acme") {
$path = __DIR__ . '/../data/payloads/' . $payloadFile;
$this->payload = json_decode(file_get_contents($path));
$this->notification = new JenkinsNotification(
- "Acme",
+ $project,
$this->payload
);
}
@@ -53,4 +53,14 @@
$this->notification->text
);
}
+
+ public function testShouldNotifyOnDefaultConfiguration () {
+ $this->prepareNotification('JenkinsPayload.json');
+ $this->assertTrue($this->notification->shouldNotify());
+ }
+
+ public function testShouldNotifyWhenConfiguredNotTo () {
+ $this->prepareNotification('JenkinsToIgnorePayload.json', 'Nasqueron');
+ $this->assertFalse($this->notification->shouldNotify());
+ }
}
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
Wed, Nov 6, 16:42 (19 h, 34 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2232115
Default Alt Text
D628.diff (7 KB)

Event Timeline