Page MenuHomeDevCentral

D626.id1552.diff
No OneTemporary

D626.id1552.diff

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
@@ -158,7 +158,7 @@
// symbols, we need to sort it to the right group.
$repository = $this->getRepository();
foreach ($this->configuration->repositoryMapping as $mapping) {
- if ($mapping->doesRepositoryBelong($repository)) {
+ if ($mapping->doesItemBelong($repository)) {
return $mapping->group;
}
}
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
@@ -36,7 +36,7 @@
/**
* An array of RepositoryGroupMapping objects to match repositories & groups
*
- * @var RepositoryGroupMapping[]
+ * @var \Nasqueron\Notifications\Analyzers\ItemGroupMapping[]
*/
public $repositoryMapping;
diff --git a/app/Analyzers/GitHub/RepositoryGroupMapping.php b/app/Analyzers/GitHub/RepositoryGroupMapping.php
deleted file mode 100644
--- a/app/Analyzers/GitHub/RepositoryGroupMapping.php
+++ /dev/null
@@ -1,53 +0,0 @@
-<?php
-
-namespace Nasqueron\Notifications\Analyzers\GitHub;
-
-class RepositoryGroupMapping {
- ///
- /// Properties
- ///
-
- /**
- * The group the mapped repositories belong to
- *
- * @var string
- */
- public $group;
-
- /**
- * An array of the repositories, each item a string with the name of the
- * repository. The wildcard '*' is allowed to specify several repositories.
- *
- * @var array
- */
- public $repositories;
-
- ///
- /// Helper methods
- ///
-
- /**
- * Determines if the specified repository matches a pattern
- *
- * @param string $pattern The pattern, with * allowed as wildcard character
- * @param string $repository The repository name to compare with the pattern
- * @return bool
- */
- public static function doesRepositoryMatch ($pattern, $repository) {
- return str_is($pattern, $repository);
- }
-
- /**
- * Determines if the specified repository belong to this mapping
- *
- * @return bool
- */
- public function doesRepositoryBelong ($actualRepository) {
- foreach ($this->repositories as $candidateRepository) {
- if (static::doesRepositoryMatch($candidateRepository, $actualRepository)) {
- return true;
- }
- }
- return false;
- }
-}
diff --git a/app/Analyzers/ItemGroupMapping.php b/app/Analyzers/ItemGroupMapping.php
new file mode 100644
--- /dev/null
+++ b/app/Analyzers/ItemGroupMapping.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Nasqueron\Notifications\Analyzers;
+
+/**
+ * Map items (repositories, projects, items, etc.) names to groups
+ */
+class ItemGroupMapping {
+
+ ///
+ /// Properties
+ ///
+
+ /**
+ * The group the mapped items belong to
+ *
+ * @var string
+ */
+ public $group;
+
+ /**
+ * An array of the items to map, each item a string with the name of the
+ * repository, project or item used for mapping.
+ * The wildcard '*' is allowed to specify several items.
+ *
+ * @var array
+ */
+ public $items;
+
+ ///
+ /// Helper methods
+ ///
+
+ /**
+ * Determines if the specified item matches a pattern.
+ *
+ * @param string $pattern The pattern, with * allowed as wildcard character
+ * @param string $item The item name to compare with the pattern
+ * @return bool
+ */
+ public static function doesItemMatch ($pattern, $item) {
+ return str_is($pattern, $item);
+ }
+
+ /**
+ * Determines if the specified item belong to this mapping
+ *
+ * @return bool
+ */
+ public function doesItemBelong ($actualItem) {
+ foreach ($this->items as $candidateItem) {
+ if (static::doesItemMatch($candidateItem, $actualItem)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+}
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 \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;
+ }
+
+ ///
+ /// 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/Phabricator/PhabricatorGroupMapping.php b/app/Analyzers/Phabricator/PhabricatorGroupMapping.php
--- a/app/Analyzers/Phabricator/PhabricatorGroupMapping.php
+++ b/app/Analyzers/Phabricator/PhabricatorGroupMapping.php
@@ -2,29 +2,16 @@
namespace Nasqueron\Notifications\Analyzers\Phabricator;
+use Nasqueron\Notifications\Analyzers\ItemGroupMapping;
use Nasqueron\Notifications\Phabricator\PhabricatorStory;
-class PhabricatorGroupMapping {
+class PhabricatorGroupMapping extends ItemGroupMapping {
+
///
- /// Properties
+ /// Extra properties
///
/**
- * The group the mapped projects belong to
- *
- * @var string
- */
- public $group;
-
- /**
- * An array of the projects, each item a string with the name of the
- * project. The wildcard '*' is allowed to specify several projects.
- *
- * @var array
- */
- public $projects;
-
- /**
* An array of words, each item a string with a word to find in the story.
*
* @var array
@@ -32,35 +19,10 @@
public $words = [];
///
- /// Helper methods
+ /// Helper methods to process words
///
/**
- * Determines if the specified project matches a pattern
- *
- * @param string $pattern The pattern, with * allowed as wildcard character
- * @param string $project The project name to compare with the pattern
- * @return bool
- */
- public static function doesProjectMatch ($pattern, $project) {
- return str_is($pattern, $project);
- }
-
- /**
- * Determines if the specified project belong to this mapping
- *
- * @return bool
- */
- public function doesProjectBelong ($actualProject) {
- foreach ($this->projects as $candidateProject) {
- if (static::doesProjectMatch($candidateProject, $actualProject)) {
- return true;
- }
- }
- return false;
- }
-
- /**
* Determines if the specified story belong to this mapping
*
* @return bool
@@ -73,4 +35,5 @@
}
return false;
}
+
}
diff --git a/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php b/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php
--- a/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php
+++ b/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php
@@ -8,7 +8,7 @@
use Storage;
class PhabricatorPayloadAnalyzer {
-
+
///
/// Private members
///
@@ -115,7 +115,7 @@
// symbols, we need to sort it to the right group.
foreach ($this->configuration->groupsMapping as $mapping) {
foreach ($this->story->getProjects() as $project) {
- if ($mapping->doesProjectBelong($project)) {
+ if ($mapping->doesItemBelong($project)) {
return $mapping->group;
}
}
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/GitHub/GitHubPayloadAnalyzerConfigurationTest.php b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
--- a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
+++ b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
@@ -5,6 +5,7 @@
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzerConfiguration;
+use Nasqueron\Notifications\Analyzers\ItemGroupMapping;
use Nasqueron\Notifications\Tests\TestCase;
class GitHubPayloadAnalyzerConfigurationTest extends TestCase {
@@ -38,10 +39,7 @@
$this->assertSame("nasqueron", $this->configuration->defaultGroup);
foreach ($this->configuration->repositoryMapping as $item) {
- $this->assertInstanceOf(
- 'Nasqueron\Notifications\Analyzers\GitHub\RepositoryGroupMapping',
- $item
- );
+ $this->assertInstanceOf(ItemGroupMapping::class, $item);
}
}
diff --git a/tests/Analyzers/GitHub/RepositoryGroupMappingTest.php b/tests/Analyzers/ItemGroupMappingTest.php
rename from tests/Analyzers/GitHub/RepositoryGroupMappingTest.php
rename to tests/Analyzers/ItemGroupMappingTest.php
--- a/tests/Analyzers/GitHub/RepositoryGroupMappingTest.php
+++ b/tests/Analyzers/ItemGroupMappingTest.php
@@ -4,42 +4,42 @@
use Illuminate\Foundation\Testing\WithoutMiddleware;
-use Nasqueron\Notifications\Analyzers\GitHub\RepositoryGroupMapping;
+use Nasqueron\Notifications\Analyzers\ItemGroupMapping;
use Nasqueron\Notifications\Tests\TestCase;
-class RepositoryGroupMappingTest extends TestCase {
+class ItemGroupMappingTest extends TestCase {
- public function testDoesRepositoryMatch () {
+ public function testDoesItemMatch () {
$this->assertTrue(
- RepositoryGroupMapping::doesRepositoryMatch(
+ ItemGroupMapping::doesItemMatch(
'quux*',
'quuxians'
)
);
$this->assertTrue(
- RepositoryGroupMapping::doesRepositoryMatch(
+ ItemGroupMapping::doesItemMatch(
'quux*',
'quux'
)
);
$this->assertFalse(
- RepositoryGroupMapping::doesRepositoryMatch(
+ ItemGroupMapping::doesItemMatch(
'foobar',
'quux'
)
);
$this->assertFalse(
- RepositoryGroupMapping::doesRepositoryMatch(
+ ItemGroupMapping::doesItemMatch(
'',
'quuxians'
)
);
$this->assertFalse(
- RepositoryGroupMapping::doesRepositoryMatch(
+ ItemGroupMapping::doesItemMatch(
'quux*',
''
)
diff --git a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
copy from tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
copy to tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
--- a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerConfigurationTest.php
+++ b/tests/Analyzers/Jenkins/JenkinsPayloadAnalyzerConfigurationTest.php
@@ -4,15 +4,16 @@
use Illuminate\Foundation\Testing\WithoutMiddleware;
-use Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzerConfiguration;
+use Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration;
+use Nasqueron\Notifications\Analyzers\ItemGroupMapping;
use Nasqueron\Notifications\Tests\TestCase;
-class GitHubPayloadAnalyzerConfigurationTest extends TestCase {
+class JenkinsPayloadAnalyzerConfigurationTest extends TestCase {
/**
* Configuration
*
- * @var Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzerConfiguration
+ * @var Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzerConfiguration
*/
protected $configuration;
@@ -20,11 +21,11 @@
* Prepares the test
*/
public function setUp () {
- $filename = __DIR__ . '/../../data/GitHubPayloadAnalyzer/Nasqueron.json';
+ $filename = __DIR__ . '/../../data/JenkinsPayloadAnalyzer/Nasqueron.json';
$mapper = new \JsonMapper();
$this->configuration = $mapper->map(
json_decode(file_get_contents($filename)),
- new GitHubPayloadAnalyzerConfiguration('Nasqueron')
+ new JenkinsPayloadAnalyzerConfiguration('Nasqueron')
);
parent::setUp();
@@ -34,14 +35,10 @@
* Determines the JSON object is well parsed
*/
public function testProperties () {
- $this->assertSame("orgz", $this->configuration->administrativeGroup);
- $this->assertSame("nasqueron", $this->configuration->defaultGroup);
+ $this->assertSame("ci", $this->configuration->defaultGroup);
- foreach ($this->configuration->repositoryMapping as $item) {
- $this->assertInstanceOf(
- 'Nasqueron\Notifications\Analyzers\GitHub\RepositoryGroupMapping',
- $item
- );
+ foreach ($this->configuration->groupsMapping as $item) {
+ $this->assertInstanceOf(ItemGroupMapping::class, $item);
}
}
diff --git a/tests/Analyzers/Phabricator/PhabricatorGroupMappingTest.php b/tests/Analyzers/Phabricator/PhabricatorGroupMappingTest.php
--- a/tests/Analyzers/Phabricator/PhabricatorGroupMappingTest.php
+++ b/tests/Analyzers/Phabricator/PhabricatorGroupMappingTest.php
@@ -39,63 +39,26 @@
/// Tests
///
- public function testDoesProjectMatch () {
- $this->assertTrue(
- PhabricatorGroupMapping::doesProjectMatch(
- 'quux*',
- 'quuxians'
- )
- );
-
- $this->assertTrue(
- PhabricatorGroupMapping::doesProjectMatch(
- 'quux*',
- 'quux'
- )
- );
-
- $this->assertFalse(
- PhabricatorGroupMapping::doesProjectMatch(
- 'foobar',
- 'quux'
- )
- );
-
- $this->assertFalse(
- PhabricatorGroupMapping::doesProjectMatch(
- '',
- 'quuxians'
- )
- );
-
- $this->assertFalse(
- PhabricatorGroupMapping::doesProjectMatch(
- 'quux*',
- ''
- )
- );
- }
-
public function testDoesProjectBelong () {
$mapping = $this->mappings['projects'];
$this->assertFalse(
- $mapping->doesProjectBelong("")
+ $mapping->doesItemBelong("")
);
$this->assertFalse(
- $mapping->doesProjectBelong("Tasacora")
+ $mapping->doesItemBelong("Tasacora")
);
$this->assertTrue(
- $mapping->doesProjectBelong("Docker images")
+ $mapping->doesItemBelong("Docker images")
);
$this->assertFalse(
- $mapping->doesProjectBelong("Docker")
+ $mapping->doesItemBelong("Docker")
);
$this->assertFalse(
- $mapping->doesProjectBelong("Docker images quux")
+ $mapping->doesItemBelong("Docker images quux")
);
}
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
@@ -4,19 +4,19 @@
"repositoryMapping": [
{
"group": "docker",
- "repositories": [
+ "items": [
"docker-*"
]
},
{
"group": "tasacora",
- "repositories": [
+ "items": [
"tasacora-*"
]
},
{
"group": "ops",
- "repositories": [
+ "items": [
"decommission",
"discourse-config",
"ftp",
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": []
+}
diff --git a/tests/data/PhabricatorPayloadAnalyzer/Nasqueron.json b/tests/data/PhabricatorPayloadAnalyzer/Nasqueron.json
--- a/tests/data/PhabricatorPayloadAnalyzer/Nasqueron.json
+++ b/tests/data/PhabricatorPayloadAnalyzer/Nasqueron.json
@@ -4,14 +4,14 @@
"groupsMapping": [
{
"group": "docker",
- "projects": [
+ "items": [
"Docker images",
"Nasqueron Docker deployment squad"
]
},
{
"group": "tasacora",
- "projects":[
+ "items":[
"Tasacora"
],
"words": [
@@ -21,7 +21,7 @@
},
{
"group": "ops",
- "projects": [
+ "items": [
"Continous integration and delivery",
"IPv6",
"Mail",

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 26, 16:12 (9 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2264806
Default Alt Text
D626.id1552.diff (22 KB)

Event Timeline