Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andjsch committed Sep 15, 2020
0 parents commit a7daffb
Show file tree
Hide file tree
Showing 17 changed files with 416 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
/.editorconfig export-ignore
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
composer.lock
docs
vendor
coverage
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":425:{a:2:{s:7:"defects";a:1:{s:59:"SchantlDev\GitWebhook\Tests\WebhookTest::try_with_signature";i:3;}s:5:"times";a:4:{s:62:"SchantlDev\GitWebhook\Tests\WebhookTest::deploy_scripts_exists";d:0.51;s:53:"SchantlDev\GitWebhook\Tests\WebhookTest::route_exists";d:0.412;s:73:"SchantlDev\GitWebhook\Tests\WebhookTest::try_with_signature_no_secret_set";d:0.315;s:59:"SchantlDev\GitWebhook\Tests\WebhookTest::try_with_signature";d:0.305;}}}
19 changes: 19 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
filter:
excluded_paths: [tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

4 changes: 4 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
preset: laravel

disabled:
- single_class_element_per_statement
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: php

php:
- 7.1
- 7.2
- 7.3

env:
matrix:
- COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=""

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Schantl Web Development & Services

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Laravel - Git Webhook

[![Latest Version on Packagist](https://img.shields.io/packagist/v/schantldev/git-webhook.svg?style=flat-square)](https://packagist.org/packages/schantldev/git-webhook)
[![Build Status](https://img.shields.io/travis/schantldev/git-webhook/master.svg?style=flat-square)](https://travis-ci.org/schantldev/git-webhook)
[![Quality Score](https://img.shields.io/scrutinizer/g/schantldev/git-webhook.svg?style=flat-square)](https://scrutinizer-ci.com/g/schantldev/git-webhook)
[![Total Downloads](https://img.shields.io/packagist/dt/schantldev/git-webhook.svg?style=flat-square)](https://packagist.org/packages/schantldev/git-webhook)

An easy way to update your application using Github's webhooks. No manual deploy required.

This package sets up a route that you can use for your github webhooks. You can set your secret and define the URL of the route. The pacakge will publish a deploy script that you can customize to fit your needs. It will be executed to fit

## Installation

You can install the package via composer:

```bash
composer require schantldev/git-webhook
```

After that, publish the files using

```bash
php artisan vendor:publish
```

Ensure the deployment script is executable by running

```bash
chmod +x /storage/git-webhook/git_deploy.sh
```

The package will queue the command and run it in the background. Ensure you have a proper queue (eg. database driver) set up, otherwise the deployment script might not work.

## Usage

1. Define the webhook URL you want to use in the ``git-webhook.php`` config file
2. Define a secret if necessary
3. Activate the webhook inside your repository settings


## Contributing

Everybody is welcome to contribute towards this git webhook package.

### Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [Schantl Web Development & Services](https://github.com/schantldev)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
50 changes: 50 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "schantldev/git-webhook",
"description": "An easy way to update your application using Github's webhooks. No manual deploy required.",
"keywords": [
"schantldev",
"git-webhook"
],
"homepage": "https://github.com/schantldev/git-webhook",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Schantl Web Development & Services",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^7.1",
"illuminate/support": "^7.0"
},
"require-dev": {
"orchestra/testbench": "^5.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
"SchantlDev\\GitWebhook\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"SchantlDev\\GitWebhook\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"SchantlDev\\GitWebhook\\GitWebhookServiceProvider"
]
}
}
}
9 changes: 9 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/*
* You can place your custom package configuration in here.
*/
return [
'route' => '/webhooks/github',
'secret' => ''
];
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
19 changes: 19 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;

Route::post(config('git-webhook.route', '/webhooks/github'), function () {
$github_hash = request()->header('X-Hub-Signature');
$github_payload = request()->getContent();

$secret = config('git-webhook.secret');
$sent_hash = 'sha1=' . hash_hmac('sha1', $github_payload, $secret, false);

if ($github_hash === null || hash_equals($github_hash, $sent_hash)) {
Artisan::queue('github:deploy');
return response('', 200);
} else {
return response('', 403);
}
});
35 changes: 35 additions & 0 deletions src/Commands/DefaultGithubDeploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace SchantlDev\GitWebhook\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Process;

class DefaultGithubDeploy extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'github:deploy';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Pull changes from git and deploy.';


public function handle()
{
$process = new Process([storage_path('/git-webhook/git_deploy.sh')], base_path());
$process->run();

Log::info('====================== DEPLOY ======================');
Log::info($process->getOutput());
Log::info('===================== END DEPLOY =====================');
}
}
41 changes: 41 additions & 0 deletions src/GitWebhookServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace SchantlDev\GitWebhook;

use Illuminate\Support\ServiceProvider;
use SchantlDev\GitWebhook\Commands\DefaultGithubDeploy;
use Symfony\Component\Process\Process;

class GitWebhookServiceProvider extends ServiceProvider
{

/**
* Bootstrap the application services.
*/
public function boot()
{
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/config.php' => config_path('git-webhook.php'),
], 'config');

$this->publishes([
__DIR__ . '/../storage/deploy_stub.sh' => storage_path('git-webhook/git_deploy.sh'),
], 'scripts');
}

// Registering package commands.
$this->commands([DefaultGithubDeploy::class]);
}

/**
* Register the application services.
*/
public function register()
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'git-webhook');
}
}
21 changes: 21 additions & 0 deletions storage/deploy_stub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

# activate maintenance mode
php artisan down

# update source code
git pull

# update PHP dependencies
composer install --no-interaction --no-dev --prefer-dist
# --no-interaction Do not ask any interactive question
# --no-dev Disables installation of require-dev packages
# --prefer-dist Forces installation from package dist even for dev versions
# --with-all-dependencies Allow upgrades, downgrades and removals for currently locked to specific versions

# update database
php artisan migrate --force
# --force Required to run when in production.

# stop maintenance mode
php artisan up
Loading

0 comments on commit a7daffb

Please sign in to comment.