Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F7137539
D626.id1550.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
10 KB
Referenced Files
None
Subscribers
None
D626.id1550.diff
View Options
diff --git a/app/Analyzers/Jenkins/JenkinsPayloadAnalayzer.php b/app/Analyzers/Jenkins/JenkinsPayloadAnalayzer.php
new file mode 100644
--- /dev/null
+++ b/app/Analyzers/Jenkins/JenkinsPayloadAnalayzer.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace Nasqueron\Notifications\Analyzers\Jenkins;
+
+use InvalidArgumentException;
+
+class JenkinsPayloadAnalayzer {
+
+ ///
+ /// 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 JenkinsPayloadAnalayzer instance.
+ *
+ * @param string $project
+ * @param string $event
+ * @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)
+ );
+ }
+
+
+}
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,132 @@
+<?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 JobGroupMapping[]
+ */
+ 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;
+ }
+
+ ///
+ /// Properties
+ ///
+
+ /**
+ * Gets the name of the job.
+ *
+ * @var string
+ */
+ public function getJob () {
+ return $this->payload->name;
+ }
+
+ ///
+ /// Notify only on failure helper methods
+ ///
+
+ /**
+ * Gets build status
+ * @param out string $status
+ * @return bool
+ */
+ 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->getJob(), $this->notifyOnlyOnFailureJobs);
+ }
+
+ /**
+ * Determines if the builld status is a failure
+ *
+ * @return bool
+ */
+ public function isFailure () {
+ if (!$this->tryGetBuildStatus($status)) {
+ return false;
+ }
+
+ return $status === "FAILURE"
+ || $status === "ABORTED"
+ || $status === "UNSTABLE";
+ }
+
+ /**
+ * @return bool
+ */
+ public function shouldNotify () {
+ return $this->isFailure() || !$this->shouldNotifyOnlyOnFailure();
+ }
+
+}
diff --git a/app/Analyzers/Jenkins/JobGroupMapping.php b/app/Analyzers/Jenkins/JobGroupMapping.php
new file mode 100644
--- /dev/null
+++ b/app/Analyzers/Jenkins/JobGroupMapping.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Nasqueron\Notifications\Analyzers\Jenkins;
+
+class JobGroupMapping {
+
+ ///
+ /// Properties
+ ///
+
+ /**
+ * The group the mapped jobs belong to
+ *
+ * @var string
+ */
+ public $group;
+
+ /**
+ * An array of the jobs, each item a string with the name of the job.
+ * The wildcard '*' is allowed to specify several jobs.
+ *
+ * @var array
+ */
+ public $jobs;
+
+ ///
+ /// Helper methods
+ ///
+
+ /**
+ * Determines if the specified job matches a pattern
+ *
+ * @param string $pattern The pattern, with * allowed as wildcard character
+ * @param string $job The job name to compare with the pattern
+ * @return bool
+ */
+ public static function doesJobMatch ($pattern, $job) {
+ return str_is($pattern, $job);
+ }
+
+ /**
+ * Determines if the specified job belong to this mapping
+ *
+ * @return bool
+ */
+ public function doesJobBelong ($actualJob) {
+ foreach ($this->jobs as $candidateJob) {
+ if (static::doesJobMatch($candidateJob, $actualJob)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
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,64 @@
+<?php
+
+namespace Nasqueron\Notifications\Tests\Analyzers;
+
+use Illuminate\Foundation\Testing\WithoutMiddleware;
+
+use Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration;
+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(
+ 'Nasqueron\Notifications\Analyzers\Jenkins\JobGroupMapping',
+ $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",
+ "jobs": [
+ "deploy-irc-daeghrefn-wikidata"
+ ]
+ },
+ {
+ "group": "ops",
+ "jobs": [
+ "deploy-website-*",
+ "test-prod-env"
+ ]
+ },
+ {
+ "group": "devtools",
+ "jobs": [
+ "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
Details
Attached
Mime Type
text/plain
Expires
Tue, Apr 22, 07:31 (11 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2594745
Default Alt Text
D626.id1550.diff (10 KB)
Attached To
Mode
D626: Analyze Jenkins payloads
Attached
Detach File
Event Timeline
Log In to Comment