Page MenuHomeDevCentral

D626.diff
No OneTemporary

D626.diff

diff --git a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
new file mode 100644
--- /dev/null
+++ b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace Nasqueron\Notifications\Analyzers\Jenkins;
+
+use Config;
+use Storage;
+
+use InvalidArgumentException;
+
+class JenkinsPayloadAnalyzer {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * The project name, used to load specific configuration and offer defaults
+ * @var string
+ */
+ private $project;
+
+ /**
+ * The request content, as a structured data
+ * @var stdClass
+ */
+ private $payload;
+
+ /**
+ * The configuration for the payload analyzer
+ * @var Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration;
+ */
+ private $configuration;
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Creates a new JenkinsPayloadAnalyzer instance.
+ *
+ * @param string $project
+ * @param stdClass $payload
+ */
+ public function __construct($project, $payload) {
+ if (!is_object($payload)) {
+ throw new InvalidArgumentException("Payload must be an object.");
+ }
+
+ $this->project = $project;
+ $this->payload = $payload;
+
+ $this->loadConfiguration($project);
+ }
+
+ ///
+ /// Configuration
+ ///
+
+ /**
+ * The default name of the configuration file
+ */
+ const CONFIG_DEFAULT_FILE = 'default.json';
+
+ /**
+ * Gets the full path to the configuration file.
+ *
+ * @return string
+ */
+ public function getConfigurationFileName () {
+ $dir = Config::get('services.jenkins.analyzer.configDir');
+ $filename = $dir . '/' . $this->project . '.json';
+
+ if (!Storage::has($filename)) {
+ return $dir . '/' . static::CONFIG_DEFAULT_FILE;
+ }
+
+ return $filename;
+ }
+
+ public function loadConfiguration () {
+ $fileName = $this->getConfigurationFileName();
+ $mapper = new \JsonMapper();
+ $this->configuration = $mapper->map(
+ json_decode(Storage::get($fileName)),
+ new JenkinsPayloadAnalyzerConfiguration($this->project)
+ );
+ }
+
+ ///
+ /// Properties
+ ///
+
+ /**
+ * Gets the name of the repository.
+ *
+ * @var string
+ */
+ public function getJobName () {
+ return $this->payload->name;
+ }
+
+ /**
+ * Gets the group for a specific payload.
+ *
+ * @return string The group, central part of the routing key
+ */
+ public function getGroup () {
+ // If the payload is about some repository matching a table of
+ // symbols, we need to sort it to the right group.
+ $item = $this->getJobName();
+ foreach ($this->configuration->groupsMapping as $mapping) {
+ if ($mapping->doesItemBelong($item)) {
+ return $mapping->group;
+ }
+ }
+
+ return $this->configuration->getDefaultGroup();
+ }
+
+}
diff --git a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfiguration.php b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfiguration.php
new file mode 100644
--- /dev/null
+++ b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfiguration.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Nasqueron\Notifications\Analyzers\Jenkins;
+
+class JenkinsPayloadAnalyzerConfiguration {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * The project this configuration is for
+ *
+ * @var string
+ */
+ private $project;
+
+ ///
+ /// Public properties
+ ///
+
+ /**
+ * The default group to fallback for any event not mapped in another group
+ *
+ * @var string
+ */
+ public $defaultGroup;
+
+ /**
+ * An array of RepositoryGroupMapping objects to match jobs & groups
+ *
+ * @var \Nasqueron\Notifications\Analyzers\ItemGroupMapping[]
+ */
+ public $groupsMapping;
+
+ /**
+ * @var string[]
+ */
+ public $notifyOnlyOnFailureJobs;
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Initializes a new instance of the GitHubPayloadAnalyzerConfiguration class
+ *
+ * @param string $project The project name this configuration is related to
+ */
+ public function __construct ($project) {
+ $this->project = $project;
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ /**
+ * Gets the default group
+ *
+ * @return string the default group, as set in the configuration,
+ * or if omitted, the project name as fallback.
+ */
+ public function getDefaultGroup () {
+ if (empty($this->defaultGroup)) {
+ return strtolower($this->project);
+ }
+
+ return $this->defaultGroup;
+ }
+
+}
diff --git a/app/Notifications/JenkinsNotification.php b/app/Notifications/JenkinsNotification.php
--- a/app/Notifications/JenkinsNotification.php
+++ b/app/Notifications/JenkinsNotification.php
@@ -2,6 +2,7 @@
namespace Nasqueron\Notifications\Notifications;
+use Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer;
use Nasqueron\Notifications\Notification;
/**
@@ -13,6 +14,11 @@
class JenkinsNotification extends Notification {
/**
+ * @var \Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer
+ */
+ private $analyzer = null;
+
+ /**
* Initializes a new instance of the JenkinsNotification class.
*
* @param string $project The project this message is for
@@ -71,12 +77,27 @@
}
/**
+ * Gets analyzer
+ *
+ * @return \Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer
+ */
+ private function getAnalyzer () {
+ if ($this->analyzer === null) {
+ $this->analyzer = new JenkinsPayloadAnalyzer(
+ $this->project,
+ $this->rawContent
+ );
+ }
+ return $this->analyzer;
+ }
+
+ /**
* Gets the notification group.
*
* @return string
*/
public function getGroup () {
- return "ci"; // This is a temporary group, intended before mapping.
+ return $this->getAnalyzer()->getGroup();
}
}
diff --git a/config/services.php b/config/services.php
--- a/config/services.php
+++ b/config/services.php
@@ -45,6 +45,12 @@
]
],
+ 'jenkins' => [
+ 'analyzer' => [
+ 'configDir' => env('JENKINS_ANALYZER_CONFIG_DIR', 'JenkinsPayloadAnalyzer')
+ ]
+ ],
+
'phabricator' => [
'analyzer' => [
'configDir' => env('PHABRICATOR_ANALYZER_CONFIG_DIR', 'PhabricatorPayloadAnalyzer')
diff --git a/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace Nasqueron\Notifications\Tests\Analyzers;
+
+use Illuminate\Foundation\Testing\WithoutMiddleware;
+
+use Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration;
+use Nasqueron\Notifications\Analyzers\ItemGroupMapping;
+use Nasqueron\Notifications\Tests\TestCase;
+
+class JenkinsPayloadAnalyzerConfigurationTest extends TestCase {
+
+ /**
+ * Configuration
+ *
+ * @var Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration
+ */
+ protected $configuration;
+
+ /**
+ * Prepares the test
+ */
+ public function setUp () {
+ $filename = __DIR__ . '/../../data/JenkinsPayloadAnalyzer/Nasqueron.json';
+ $mapper = new \JsonMapper();
+ $this->configuration = $mapper->map(
+ json_decode(file_get_contents($filename)),
+ new JenkinsPayloadAnalyzerConfiguration('Nasqueron')
+ );
+
+ parent::setUp();
+ }
+
+ /**
+ * Determines the JSON object is well parsed
+ */
+ public function testProperties () {
+ $this->assertSame("ci", $this->configuration->defaultGroup);
+
+ foreach ($this->configuration->groupsMapping as $item) {
+ $this->assertInstanceOf(ItemGroupMapping::class, $item);
+ }
+ }
+
+ ///
+ /// Tests for getDefaultGroup
+ ///
+
+ public function testGetDefaultGroup () {
+ $this->configuration->defaultGroup = "quux";
+ $this->assertSame("quux", $this->configuration->getDefaultGroup());
+ }
+
+ public function testGetDefaultGroupWhenNotInConfig () {
+ $this->configuration->defaultGroup = "";
+ $this->assertSame("nasqueron", $this->configuration->getDefaultGroup());
+
+ $this->configuration->defaultGroup = null;
+ $this->assertSame("nasqueron", $this->configuration->getDefaultGroup());
+ }
+
+}
diff --git a/tests/data/JenkinsPayloadAnalyzer/Nasqueron.json b/tests/data/JenkinsPayloadAnalyzer/Nasqueron.json
new file mode 100644
--- /dev/null
+++ b/tests/data/JenkinsPayloadAnalyzer/Nasqueron.json
@@ -0,0 +1,27 @@
+{
+ "defaultGroup": "ci",
+ "groupsMapping": [
+ {
+ "group": "wikidata",
+ "items": [
+ "deploy-irc-daeghrefn-wikidata"
+ ]
+ },
+ {
+ "group": "ops",
+ "items": [
+ "deploy-website-*",
+ "test-prod-env"
+ ]
+ },
+ {
+ "group": "devtools",
+ "items": [
+ "test-notifications-*"
+ ]
+ }
+ ],
+ "notifyOnlyOnFailure": [
+ "test-prod-env"
+ ]
+}
diff --git a/tests/data/JenkinsPayloadAnalyzer/default.json b/tests/data/JenkinsPayloadAnalyzer/default.json
new file mode 100644
--- /dev/null
+++ b/tests/data/JenkinsPayloadAnalyzer/default.json
@@ -0,0 +1,5 @@
+{
+ "defaultGroup": "ci",
+ "groupsMapping": [],
+ "notifyOnlyOnFailure": []
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 02:45 (8 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2256922
Default Alt Text
D626.diff (9 KB)

Event Timeline