Page MenuHomeDevCentral

No OneTemporary

diff --git a/app/Console/Commands/PhabricatorGetProjectsMap.php b/app/Console/Commands/PhabricatorGetProjectsMap.php
index 9822eea..122c765 100644
--- a/app/Console/Commands/PhabricatorGetProjectsMap.php
+++ b/app/Console/Commands/PhabricatorGetProjectsMap.php
@@ -1,63 +1,51 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
use Illuminate\Console\Command;
use Storage;
use Nasqueron\Notifications\Phabricator\ProjectsMap;
+use Nasqueron\Notifications\Services;
class PhabricatorGetProjectsMap extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'phabricator:projectsmap';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Regerate the projects map for each Phabricator instances';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
- foreach ($this->getServicesCredentials() as $service) {
- if ($service->gate == "Phabricator") {
- $this->info("Querying projects map for " . $service->instance);
- $map = ProjectsMap::fetch($service->instance);
- $map->saveToCache();
- $this->table(
- ['PHID', 'Project name'],
- $map->toArray()
- );
- }
+ foreach (Services::getForGate('Phabricator') as $service) {
+ $this->info("Querying projects map for " . $service->instance);
+ $map = ProjectsMap::fetch($service->instance);
+ $map->saveToCache();
+ $this->table(
+ ['PHID', 'Project name'],
+ $map->toArray()
+ );
}
}
-
- /**
- * Gets service credentials
- *
- * @return stdClass the services credentials
- */
- protected function getServicesCredentials () {
- $path = config('services.gate.credentials');
- $data = json_decode(Storage::get($path));
- return $data->services;
- }
}
diff --git a/app/Http/Controllers/Gate/GateController.php b/app/Http/Controllers/Gate/GateController.php
index c045c21..4a929a1 100644
--- a/app/Http/Controllers/Gate/GateController.php
+++ b/app/Http/Controllers/Gate/GateController.php
@@ -1,129 +1,102 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
use Nasqueron\Notifications\Features;
+use Nasqueron\Notifications\Services;
use Nasqueron\Notifications\Http\Controllers\Controller;
use Report;
use Storage;
/**
* Represents a controller handling an entry-point for API payloads
*/
class GateController extends Controller {
///
/// Private members
///
/**
* @var string
*/
protected $door;
///
/// Requests
///
/**
* Handles GET requests
*/
public function onGet () {
// Virtually all the push APIs will send they payloads
// using a POST request, so we can provide a sensible
// default GET error message.
return view('gate/ispostonly');
}
/**
* Logs the request
*/
protected function logRequest () {
Log::info('[Gate] New payload.', [
'service' => static::SERVICE_NAME,
'door' => $this->door,
]);
}
///
/// Reports
///
/**
* Initializes the report and registers it
*/
protected function initializeReport () {
if (Features::isEnabled('ActionsReport')) {
Report::attachToGate(static::SERVICE_NAME, $this->door);
}
}
/**
* Renders the report
*
* @return Illuminate\Http\Response|null
*/
protected function renderReport () {
if (Features::isEnabled('ActionsReport')) {
return Report::render();
}
}
///
/// Credentials
///
- /**
- * Gets services credentials
- *
- * @return stdClass the services credentials
- */
- protected function getServicesCredentials () {
- $path = config('services.gate.credentials');
- $data = json_decode(Storage::get($path));
- return $data->services;
- }
-
- /**
- * Determines if a service definition matches this current gate and door.
- *
- * @param stdClass $service the service to check
- * @return true if the service matches our gate and door; otherwise, false.
- */
- protected function doesServiceMatch ($service) {
- return $service->gate == static::SERVICE_NAME
- && $service->door == $this->door;
- }
-
/**
* Gets service credentials for this gate and door
*
* @return stdClass the service credentials
*/
public function getService () {
- foreach ($this->getServicesCredentials() as $service) {
- if ($this->doesServiceMatch($service)) {
- return $service;
- }
- }
-
- return null;
+ return Services::findServiceByDoor(static::SERVICE_NAME, $this->door);
}
/**
* Gets secret for this service and door.
*
* @return string the secret, or if unknown, an empty string
*/
protected function getSecret () {
$service= $this->getService();
if ($service !== null) {
return $service->secret;
}
return "";
}
}
diff --git a/app/Phabricator/PhabricatorAPI.php b/app/Phabricator/PhabricatorAPI.php
index b505cfb..8a8659f 100644
--- a/app/Phabricator/PhabricatorAPI.php
+++ b/app/Phabricator/PhabricatorAPI.php
@@ -1,171 +1,139 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
-use Storage;
+use Nasqueron\Notifications\Services;
class PhabricatorAPI {
///
/// Private members
///
/**
* The Phabricator main URL
*
* @var string
*/
private $instance;
/**
* The token generated at /settings/panel/apitokens/ to query the API
*
* @var string
*/
private $apiToken;
///
/// Constructors
///
/**
* Initializes a new instance of the Phabricator API class
*
* @param string $instance The Phabricator main URL, without trailing slash
* @param string $apiToken The token generated at /settings/panel/apitokens/
*/
public function __construct ($instance, $apiToken) {
$this->instance = $instance;
$this->apiToken = $apiToken;
}
/**
* Gets an API instance for the specific instance
*
* @param string $instance The name of the instance (this matches that parameter in credentials.json)
* @return PhabricatorAPI|null A PhabricatorAPI instance for the project if found; otherwise, null.
*/
public static function forInstance ($instance) {
- $service = self::getServiceForInstance($instance);
+ $service = Services::findServiceByProperty('Phabricator', 'instance', $instance);
if ($service === null) {
throw new \RuntimeException("No credentials for Phabricator instance $instance.");
}
-
return new self($service->instance, $service->secret);
}
/**
* Gets an API instance for the specific project
*
* @param string $project The name of the project (this matches the door parameter in credentials.json)
* @return PhabricatorAPI|null A PhabricatorAPI instance for the project if found; otherwise, null.
*/
public static function forProject ($project) {
- $service = self::getServiceForProject($project);
+ $service = Services::findServiceByDoor('Phabricator', $project);
if ($service === null) {
return null;
}
return new self($service->instance, $service->secret);
}
-
- ///
- /// Helper methods for static constructors
- ///
-
- private static function getServices () {
- $path = config('services.gate.credentials');
- $data = json_decode(Storage::get($path));
- return $data->services;
- }
-
- private static function getServiceForInstance ($instance) {
- foreach (self::getServices() as $service) {
- if ($service->gate === "Phabricator" && $service->instance === $instance) {
- return $service;
- }
- }
-
- return null;
- }
-
- private static function getServiceForProject ($project) {
- foreach (self::getServices() as $service) {
- if ($service->gate === "Phabricator" && $service->door === $project) {
- return $service;
- }
- }
-
- return null;
- }
-
///
/// Public methods
///
/**
* Calls a Conduit API method
*
* @param $method The method to call (e.g. repository.create)
* @param $arguments The arguments to use
*/
public function call ($method, $arguments = []) {
$url = $this->instance . '/api/' . $method;
$arguments['api.token'] = $this->apiToken;
$reply = json_decode(static::post($url, $arguments));
if ($reply->error_code !== null) {
throw new PhabricatorAPIException(
$reply->error_code,
$reply->error_info
);
}
return $reply->result;
}
/**
* Gets the first result of an API reply
*
* @param Traversable|array $reply
* @return mixed
*/
public static function getFirstResult ($reply) {
if (is_object($reply) && property_exists($reply, 'data')) {
$reply = $reply->data;
}
foreach ($reply as $value) {
return $value;
}
}
///
/// CURL session
///
protected static function getPostFields ($arguments) {
$items = [];
foreach ($arguments as $key => $value) {
$items[] = urlencode($key) . '=' . urlencode($value);
}
return implode('&', $items);
}
protected static function post ($url, $arguments) {
$options = [
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_POSTFIELDS => static::getPostFields($arguments),
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
diff --git a/app/Services.php b/app/Services.php
new file mode 100644
index 0000000..26ed891
--- /dev/null
+++ b/app/Services.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Nasqueron\Notifications;
+
+use Storage;
+
+class Services {
+
+ /**
+ * Gets the services found in credentials.json
+ *
+ * @return array
+ */
+ public static function get () {
+ $path = config('services.gate.credentials');
+ $data = json_decode(Storage::get($path));
+ return $data->services;
+ }
+
+ /**
+ * Gets all the services for a specific gate
+ *
+ * @param string $gate The gate (e.g. GitHub)
+ * @return array
+ */
+ public static function getForGate ($gate) {
+ return $services = [];
+
+ foreach (self::get() as $service) {
+ if ($service->gate === $gate) {
+ $services[] = $service;
+ }
+ }
+
+ return $services;
+ }
+
+
+ /**
+ * Gets the service for a specific gate and door
+ *
+ * @param string $gate The gate (e.g. GitHub)
+ * @param string $door The door (e.g. Nasqueron)
+ * @return stdClass|null The service information is found; otherwise, null.
+ */
+ public static function findServiceByDoor ($gate, $door) {
+ foreach (self::get() as $service) {
+ if ($service->gate === $gate && $service->door === $door) {
+ return $service;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Finds a service for a specific gate, property and value
+ *
+ * @param string $gate The gate (e.g. GitHub)
+ * @param string $property The property to check (e.g. instance)
+ * @param mixed $value The property value to find (e.g. 'http://devcentral.nasqueron.org')
+ * @return stdClass|null The service information is found; otherwise, null.
+ */
+ public static function findServiceByProperty ($gate, $property, $value) {
+ foreach (Services::get() as $service) {
+ if ($service->gate === $gate && $service->$property === $value) {
+ return $service;
+ }
+ }
+
+ return null;
+ }
+}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Oct 12, 10:05 (8 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3065720
Default Alt Text
(12 KB)

Event Timeline