Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from articstudio/develop
Browse files Browse the repository at this point in the history
First stable version
  • Loading branch information
marcmascort authored Apr 30, 2017
2 parents 12d6e50 + 5f4af84 commit e8427b2
Show file tree
Hide file tree
Showing 50 changed files with 1,703 additions and 4 deletions.
26 changes: 22 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# COMPOSER
composer.lock
composer.phar
/vendor/
/vendor

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
# IDE
/nbproject

# Logs
/logs
!/logs/.gitkeep

# Repositories
/repositories
!/repositories/.gitkeep

# App
#/app
!/app/.gitkeep
!/app/ExampleCustomMiddleware.php
!/app/ExampleCustomProvider.php

# Config
/config/app.php
1 change: 1 addition & 0 deletions app/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

17 changes: 17 additions & 0 deletions app/ExampleCustomMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Articstudio\Bitbucket\Middleware\AbstractMiddleware;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class ExampleCustomMiddleware extends AbstractMiddleware {

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) {
$req = $request->withAddedHeader('ExampleMiddleware', 1);
$res = $response->withAddedHeader('ExampleMiddleware', 1);
return $next($req, $res);
}

}
16 changes: 16 additions & 0 deletions app/ExampleCustomProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

use Articstudio\Bitbucket\Provider\AbstractServiceProvider;
use Pimple\Container;

class ExampleCustomProvider extends AbstractServiceProvider {

public function register(Container $container) {
$container['example'] = function() use ($container) {
return 'example';
};
}

}
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "articstudio/bitbucket-deploy",
"description": "Simple bitbucket deploy using webhooks",
"homepage": "https://github.com/articstudio/bitbucket-deploy",
"type": "project",
"minimum-stability": "stable",
"authors": [
{
"name": "Marc",
"email": "marc@articstudio.com"
}
],
"autoload": {
"psr-4": {
"Articstudio\\Bitbucket\\": "src/",
"App\\": "app/"
}
},
"require": {
"monolog/monolog": "^1.22",
"pimple/pimple": "^3.0",
"guzzlehttp/psr7": "^1.4",
"psr/container": "^1.0"
}
}
28 changes: 28 additions & 0 deletions config/app.example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

return [
'settings' => [
'logs_name' => 'bitbucket',
'logs_file' => dirname(__DIR__) . '/logs/app.log',
'debug_to' => 'example@example.com',
'debug_subject' => '[DEPLOY] %datetime% - %message%',
'debug_from' => 'example@example.com',
'repositories_dir' => dirname(__DIR__) . '/repositories',
'git_bin' => 'git',
],
'providers' => [
\App\ExampleCustomProvider::class
],
'middlewares' => [
\App\ExampleCustomMiddleware::class
],
'repositories' => [
'REPOSITORY_NAME' => [
'BRANCH_NAME' => [
'dir' => '/path/to/code/',
'cmd' => [],
'debug' => 'repo_branch_admin@example.com'
]
]
]
];
20 changes: 20 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/* ----------------------------------------------------------------------------
* Autoload
*/

require_once '../vendor/autoload.php';

/* ----------------------------------------------------------------------------
* Config
*/

$config = require dirname(__DIR__) . '/config/app.php';

/* ----------------------------------------------------------------------------
* Application
*/

$app = new \Articstudio\Bitbucket\App($config);
$app->run();
18 changes: 18 additions & 0 deletions public/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

function __debug($title, $debug) {
echo '<h3>' . $title . '</h3>';
echo '<pre>' . print_r($debug, true) . '</pre>';
}

try {
$a = new FakeClass;
} catch (Exception $e) {
__debug('Exception', $e);
} catch (Throwable $e) {
__debug('Throwable', $e);
}




129 changes: 129 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Articstudio\Bitbucket;

use Articstudio\Bitbucket\ContainerAwareTrait;
use Articstudio\Bitbucket\Container;
use Psr\Container\ContainerInterface as ContainerContract;
use Articstudio\Bitbucket\Exception\App\InvalidArgumentException;
use Articstudio\Bitbucket\Exception\Middleware\NotFoundException as MiddlewareNotFound;
use Articstudio\Bitbucket\Provider\DefaultProviders;
use Articstudio\Bitbucket\Http\Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Exception;
use Throwable;

class App {

use ContainerAwareTrait;

const VERSION = '1.0.0';

private $response;

public function __construct($container = []) {
if (is_array($container)) {
$container = new Container($container);
}
if (!$container instanceof ContainerContract) {
throw new InvalidArgumentException(sprintf('Expected a "%s"', ContainerContract::class));
}
$container['version'] = self::VERSION;
$this->setContainer($container);
}

public function run() {
$this->registerProviders()
->middlewares()
->process()
->respond();
}

private function registerProviders() {
$provider = new DefaultProviders(DefaultProviders::$defaults);
$provider->register($this->container);
return $this;
}

private function middlewares() {
$middlewares = $this->container->middlewares->getIterator();
foreach ($middlewares as $middleware) {
$this->addMiddleware($middleware);
}
$this->systemMiddlewares();
return $this;
}

private function addMiddleware($middleware) {
if (is_string($middleware)) {
try {
$middleware = new $middleware($this->container);
} catch (Exception $exception) {
throw new MiddlewareNotFound(sprintf('Middleware error while retrieving "%s"', $middleware), null, $exception);
} catch (Throwable $exception) {
throw new MiddlewareNotFound(sprintf('Middleware error while retrieving "%s"', $middleware), null, $exception);
}
}
$this->container->middle->add($middleware);
}

private function systemMiddlewares() {
$this->container->middle->add($this->container->middle_request_validator);
$this->container->middle->add($this->container->middle_request_parser);
}

private function process() {
$request = $this->container->request;
$response = $this->container->response;
try {
$this->response = $this->handleFound($request, $this->container->middle->call($request, $response));
} catch (Exception $exception) {
$this->response = $this->handleException($exception, $request, $response);
} catch (Throwable $exception) {
$this->response = $this->handleError($exception, $request, $response);
}
return $this;
}

protected function handleFound(ServerRequestInterface $request, ResponseInterface $response) {

//$response->getBody()->write('<h2>Found</h2>');
//$response->getBody()->write('<h4>Headers</h4><pre>' . print_r($this->container->headersInfo->all(), true) . '</pre>');
//$response->getBody()->write('<h4>Payload</h4><pre>' . print_r($this->container->payload, true) . '</pre>');
//$this->container->logger->info('Found', $this->container->payload);
return $response;
}

protected function handleException(Exception $exception, ServerRequestInterface $request, ResponseInterface $response) {
$this->container->logger->warning($exception->getMessage(), $exception->getTrace());

$response->getBody()->write('<h2>Exception</h2>');
$response->getBody()->write('<h4>' . $exception->getMessage() . '</h4><pre>' . $exception->getTraceAsString() . '</pre>');
$response->getBody()->write('<h4>Headers</h4><pre>' . print_r($this->container->headersInfo->all(), true) . '</pre>');
$response->getBody()->write('<h4>Payload</h4><pre>' . print_r($this->container->payload, true) . '</pre>');

return $response->withStatus(500, $exception->getMessage());
}

protected function handleError(Throwable $exception, ServerRequestInterface $request, ResponseInterface $response) {
$this->container->logger->error($exception->getMessage(), $exception->getTrace());

$response->getBody()->write('<h2>Error</h2>');
$response->getBody()->write('<h4>' . $exception->getMessage() . '</h4><pre>' . $exception->getTraceAsString() . '</pre>');
$response->getBody()->write('<h4>Headers</h4><pre>' . print_r($this->container->headersInfo->all(), true) . '</pre>');
$response->getBody()->write('<h4>Payload</h4><pre>' . print_r($this->container->payload, true) . '</pre>');

return $response->withStatus(500, $exception->getMessage());
}

private function respond() {
$response = new Response($this->response);
$response->respond();

/* echo '<pre>';
print_r($this->container);
echo '</pre>'; */
}

}
25 changes: 25 additions & 0 deletions src/Change.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Articstudio\Bitbucket;

use Articstudio\Bitbucket\Collection;
use DomainException;

class Change extends Collection {

public function __construct(array $items = []) {
parent::__construct($items);
$this->parse();
}

public function parse() {
$this->set('new', new Collection($this->get('new', [])));
}

public function validate() {
if ($this->get('new')->isEmpty() || !$this->get('new')->has('type') || !$this->get('new')->has('name')) {
throw new DomainException('Incomplete payload push change');
}
}

}
Loading

0 comments on commit e8427b2

Please sign in to comment.