Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3764676
D629.id1565.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
18 KB
Referenced Files
None
Subscribers
None
D629.id1565.diff
View Options
diff --git a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php b/app/Analyzers/BasePayloadAnalyzer.php
copy from app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
copy to app/Analyzers/BasePayloadAnalyzer.php
--- a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
+++ b/app/Analyzers/BasePayloadAnalyzer.php
@@ -1,13 +1,13 @@
<?php
-namespace Nasqueron\Notifications\Analyzers\Jenkins;
+namespace Nasqueron\Notifications\Analyzers;
use Config;
use Storage;
use InvalidArgumentException;
-class JenkinsPayloadAnalyzer {
+abstract class BasePayloadAnalyzer {
///
/// Private members
@@ -17,19 +17,19 @@
* The project name, used to load specific configuration and offer defaults
* @var string
*/
- private $project;
+ protected $project;
/**
* The request content, as a structured data
* @var stdClass
*/
- private $payload;
+ protected $payload;
/**
* The configuration for the payload analyzer
- * @var Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration;
+ * @var Nasqueron\Notifications\Analyzers\BasePayloadAnalyzerConfiguration;
*/
- private $configuration;
+ protected $configuration;
///
/// Constructor
@@ -67,7 +67,8 @@
* @return string
*/
public function getConfigurationFileName () {
- $dir = Config::get('services.jenkins.analyzer.configDir');
+ $dir = Config::get('services.' . strtolower(static::SERVICE_NAME) . '.analyzer.configDir');
+
$filename = $dir . '/' . $this->project . '.json';
if (!Storage::has($filename)) {
@@ -77,12 +78,28 @@
return $filename;
}
+ /**
+ * Return full qualified class name for configuration.
+ *
+ * @return string
+ */
+ private function getConfigurationClassName() {
+ $namespace = 'Nasqueron\Notifications\Analyzers\\' . static::SERVICE_NAME;
+ $class = static::SERVICE_NAME . 'PayloadAnalyzerConfiguration';
+ return $namespace . "\\" . $class;
+ }
+
+ /**
+ * Loads configuration for the analyzer
+ */
public function loadConfiguration () {
$fileName = $this->getConfigurationFileName();
+ $class = $this->getConfigurationClassName();
+
$mapper = new \JsonMapper();
$this->configuration = $mapper->map(
json_decode(Storage::get($fileName)),
- new JenkinsPayloadAnalyzerConfiguration($this->project)
+ new $class($this->project)
);
}
@@ -91,13 +108,11 @@
///
/**
- * Gets the name of the repository.
+ * Gets the name of the item.
*
* @var string
*/
- public function getJobName () {
- return $this->payload->name;
- }
+ abstract public function getItemName ();
/**
* Gets the group for a specific payload.
@@ -107,8 +122,8 @@
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) {
+ $item = $this->getItemName();
+ foreach ($this->configuration->map as $mapping) {
if ($mapping->doesItemBelong($item)) {
return $mapping->group;
}
@@ -117,57 +132,4 @@
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/BasePayloadAnalyzerConfiguration.php b/app/Analyzers/BasePayloadAnalyzerConfiguration.php
new file mode 100644
--- /dev/null
+++ b/app/Analyzers/BasePayloadAnalyzerConfiguration.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace Nasqueron\Notifications\Analyzers;
+
+
+abstract class BasePayloadAnalyzerConfiguration {
+}
diff --git a/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php b/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
--- a/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
+++ b/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
@@ -2,43 +2,28 @@
namespace Nasqueron\Notifications\Analyzers\GitHub;
+use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzer;
use Nasqueron\Notifications\Analyzers\GitHub\Events\Event;
use Nasqueron\Notifications\Analyzers\GitHub\Events\UnknownEvent;
-use Config;
-use Storage;
+class GitHubPayloadAnalyzer extends BasePayloadAnalyzer {
-class GitHubPayloadAnalyzer {
+ /**
+ * The name of the service, used to get specific classes and config
+ */
+ const SERVICE_NAME = "GitHub";
///
/// Private members
///
/**
- * The project name, used to load specific configuration and offer defaults
- * @var string
- */
- private $project;
-
- /**
* The GitHub event triggering this request
* @var string
*/
private $event;
/**
- * The request content, as a structured data
- * @var stdClass
- */
- private $payload;
-
- /**
- * The configuration for the payload analyzer
- * @var Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzerConfiguration;
- */
- private $configuration;
-
- /**
* The payload analyzer event
*
* @var Nasqueron\Notifications\Analyzers\GitHub\Events\Event;
@@ -57,15 +42,9 @@
* @param stdClass $payload
*/
public function __construct($project, $event, $payload) {
- if (!is_object($payload)) {
- throw new \InvalidArgumentException("Payload must be an object.");
- }
+ parent::__construct($project, $payload);
- $this->project = $project;
$this->event = $event;
- $this->payload = $payload;
-
- $this->loadConfiguration($project);
try {
$this->analyzerEvent = Event::forPayload($event, $payload);
@@ -75,49 +54,15 @@
}
///
- /// 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.github.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 GitHubPayloadAnalyzerConfiguration($this->project)
- );
- }
-
- ///
/// Properties
///
/**
- * Gets the name of the repository.
+ * Gets the name of the item, ie here of the name of the repository.
*
* @var string
*/
- public function getRepository () {
+ public function getItemName () {
if ($this->isAdministrativeEvent()) {
return '';
}
@@ -154,16 +99,7 @@
return $this->configuration->administrativeGroup;
}
- // If the payload is about some repository matching a table of
- // symbols, we need to sort it to the right group.
- $repository = $this->getRepository();
- foreach ($this->configuration->repositoryMapping as $mapping) {
- if ($mapping->doesItemBelong($repository)) {
- return $mapping->group;
- }
- }
-
- return $this->configuration->getDefaultGroup();
+ return parent::getGroup();
}
///
diff --git a/app/Analyzers/GitHub/GitHubPayloadAnalyzerConfiguration.php b/app/Analyzers/GitHub/GitHubPayloadAnalyzerConfiguration.php
--- a/app/Analyzers/GitHub/GitHubPayloadAnalyzerConfiguration.php
+++ b/app/Analyzers/GitHub/GitHubPayloadAnalyzerConfiguration.php
@@ -2,7 +2,9 @@
namespace Nasqueron\Notifications\Analyzers\GitHub;
-class GitHubPayloadAnalyzerConfiguration {
+use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzerConfiguration;
+
+class GitHubPayloadAnalyzerConfiguration extends BasePayloadAnalyzerConfiguration {
///
/// Private members
@@ -38,7 +40,7 @@
*
* @var \Nasqueron\Notifications\Analyzers\ItemGroupMapping[]
*/
- public $repositoryMapping;
+ public $map;
///
/// Constructor
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
@@ -2,121 +2,33 @@
namespace Nasqueron\Notifications\Analyzers\Jenkins;
+use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzer;
+
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';
+class JenkinsPayloadAnalyzer extends BasePayloadAnalyzer {
/**
- * Gets the full path to the configuration file.
- *
- * @return string
+ * The name of the service, used to get specific classes and config
*/
- 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)
- );
- }
+ const SERVICE_NAME = "Jenkins";
///
- /// Properties
+ /// Payload custom properties
///
/**
- * Gets the name of the repository.
+ * Gets the name of the item, ie here of the job.
*
* @var string
*/
- public function getJobName () {
+ public function getItemName () {
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();
- }
-
///
/// Notify only on failure helper methods
///
@@ -141,7 +53,7 @@
*/
public function shouldNotifyOnlyOnFailure () {
return in_array(
- $this->getJobName(),
+ $this->getItemName(),
$this->configuration->notifyOnlyOnFailure
);
}
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
@@ -2,7 +2,9 @@
namespace Nasqueron\Notifications\Analyzers\Jenkins;
-class JenkinsPayloadAnalyzerConfiguration {
+use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzerConfiguration;
+
+class JenkinsPayloadAnalyzerConfiguration extends BasePayloadAnalyzerConfiguration {
///
/// Private members
@@ -31,7 +33,7 @@
*
* @var \Nasqueron\Notifications\Analyzers\ItemGroupMapping[]
*/
- public $groupsMapping;
+ public $map;
/**
* @var array
diff --git a/storage/app/GitHubPayloadAnalyzer/default.json b/storage/app/GitHubPayloadAnalyzer/default.json
--- a/storage/app/GitHubPayloadAnalyzer/default.json
+++ b/storage/app/GitHubPayloadAnalyzer/default.json
@@ -1,5 +1,5 @@
{
"administrativeGroup": "orgz",
"defaultGroup": "",
- "repositoryMapping": []
+ "map": []
}
diff --git a/storage/app/JenkinsPayloadAnalyzer/default.json b/storage/app/JenkinsPayloadAnalyzer/default.json
--- a/storage/app/JenkinsPayloadAnalyzer/default.json
+++ b/storage/app/JenkinsPayloadAnalyzer/default.json
@@ -1,5 +1,5 @@
{
"defaultGroup": "ci",
- "groupsMapping": [],
+ "map": [],
"notifyOnlyOnFailure": []
}
diff --git a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
--- a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
+++ b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
@@ -38,7 +38,7 @@
$this->assertSame("orgz", $this->configuration->administrativeGroup);
$this->assertSame("nasqueron", $this->configuration->defaultGroup);
- foreach ($this->configuration->repositoryMapping as $item) {
+ foreach ($this->configuration->map as $item) {
$this->assertInstanceOf(ItemGroupMapping::class, $item);
}
}
diff --git a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php
--- a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php
+++ b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php
@@ -98,15 +98,15 @@
}
///
- /// Test getRepository
+ /// Test getItemName
///
- public function testGetRepositoryWhenEventIsAdministrative () {
- $this->assertEmpty($this->pingAnalyzer->getRepository());
+ public function testGetItemNameWhenEventIsAdministrative () {
+ $this->assertEmpty($this->pingAnalyzer->getItemName());
}
- public function testGetRepositoryWhenEventIsRepositoryRelative () {
- $this->assertSame("public-repo", $this->pushAnalyzer->getRepository());
+ public function testGetItemNameWhenEventIsRepositoryRelative () {
+ $this->assertSame("public-repo", $this->pushAnalyzer->getItemName());
}
///
diff --git a/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
--- a/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
+++ b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
@@ -37,7 +37,7 @@
public function testProperties () {
$this->assertSame("ci", $this->configuration->defaultGroup);
- foreach ($this->configuration->groupsMapping as $item) {
+ foreach ($this->configuration->map as $item) {
$this->assertInstanceOf(ItemGroupMapping::class, $item);
}
}
diff --git a/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerTest.php b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerTest.php
--- a/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerTest.php
+++ b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerTest.php
@@ -31,8 +31,8 @@
$this->analyzer = new JenkinsPayloadAnalyzer("Nasqueron", $this->payload);
}
- public function testGetJobName () {
- $this->assertSame("test-prod-env", $this->analyzer->getJobName());
+ public function testGetItemName () {
+ $this->assertSame("test-prod-env", $this->analyzer->getItemName());
}
public function testGetGroup () {
diff --git a/tests/data/GitHubPayloadAnalyzer/Nasqueron.json b/tests/data/GitHubPayloadAnalyzer/Nasqueron.json
--- a/tests/data/GitHubPayloadAnalyzer/Nasqueron.json
+++ b/tests/data/GitHubPayloadAnalyzer/Nasqueron.json
@@ -1,7 +1,7 @@
{
"administrativeGroup": "orgz",
"defaultGroup": "nasqueron",
- "repositoryMapping": [
+ "map": [
{
"group": "docker",
"items": [
diff --git a/tests/data/GitHubPayloadAnalyzer/default.json b/tests/data/GitHubPayloadAnalyzer/default.json
--- a/tests/data/GitHubPayloadAnalyzer/default.json
+++ b/tests/data/GitHubPayloadAnalyzer/default.json
@@ -1,5 +1,5 @@
{
"administrativeGroup": "orgz",
"defaultGroup": "",
- "repositoryMapping": []
+ "map": []
}
diff --git a/tests/data/JenkinsPayloadAnalyzer/Nasqueron.json b/tests/data/JenkinsPayloadAnalyzer/Nasqueron.json
--- a/tests/data/JenkinsPayloadAnalyzer/Nasqueron.json
+++ b/tests/data/JenkinsPayloadAnalyzer/Nasqueron.json
@@ -1,6 +1,6 @@
{
"defaultGroup": "ci",
- "groupsMapping": [
+ "map": [
{
"group": "wikidata",
"items": [
diff --git a/tests/data/JenkinsPayloadAnalyzer/default.json b/tests/data/JenkinsPayloadAnalyzer/default.json
--- a/tests/data/JenkinsPayloadAnalyzer/default.json
+++ b/tests/data/JenkinsPayloadAnalyzer/default.json
@@ -1,5 +1,5 @@
{
"defaultGroup": "ci",
- "groupsMapping": [],
+ "map": [],
"notifyOnlyOnFailure": []
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 23, 07:24 (16 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2257499
Default Alt Text
D629.id1565.diff (18 KB)
Attached To
Mode
D629: Refactor analyzer classes to offer better modularity
Attached
Detach File
Event Timeline
Log In to Comment