Giter Site home page Giter Site logo

mr-feek / laravelgithooks Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 6.0 137 KB

:boom: A package for running artisan commands as git hooks in your laravel projects. Ships with phpunit, phpcs, and eslint :fishing_pole_and_fish:

License: MIT License

PHP 98.06% Shell 1.94%
php laravel laravel-5-package git hooks hook-manager githook githooks-plugin

laravelgithooks's Introduction

Laravel Git Hooks

Laravel Git Hooks

This is a community project and not an "official" Laravel one

Latest Stable Version Total Downloads License Build Status

This package provides a way to add custom git hooks to your laravel project. Easily configure any command to be fired throughout the git-hook process. Want to ensure that all tests pass before a bad commit is pushed? Now's your chance!

Currently, the following git hooks are supported:

  • pre-commit
  • prepare-commit-msg
  • pre-push
  • post-checkout

Install

Via Composer

$ composer require mr-feek/laravel-git-hooks --dev

Edit your laravel project's composer.json so that these hooks are installed for every developer after they use composer.

"post-autoload-dump": [
    ...
    "@php artisan hooks:install"
]

Configuration

  • Publish this package's configuration file: php artisan vendor:publish --provider="Feek\LaravelGitHooks\LaravelGitHooksServiceProvider"
  • Register specific artisan commands to be run in the configuration array. For example, all commands nested within the pre-commit array key will be run prior to a git commit. All commands nested within the pre-push array key will be run prior to a git push. If any of these registered commands fail, then the git action will be prevented.
<?php
return [
    'commit-msg' => [
        //
    ],
    'pre-commit' => [
        'php artisan hooks:phpcs --diff --proxiedArguments="-p -n --standard=PSR2"',
        'php artisan hooks:eslint --diff --proxiedArguments="--fix --quiet"',
    ],
    'pre-push' => [
        './vendor/bin/phpunit'
    ],
    'post-checkout' => [
        'php artisan hooks:install-deps'
    ],
    'prepare-commit-msg' => [
        'php artisan hooks:semantic-commits',
    ],
];

Commands

This package ships with several handy artisan commands which work nicely as git hooks. The following commands come included:

  • phpcs
  • phpcbf
  • phpstan
  • install dependencies (composer, yarn, npm)
  • eslint
  • semantic commit messages

Sniffer Commands

The PHPCS, PHPCBF, PHPSTAN, and ESLINT commands all allow you to pass arguments to the underlying process being executed. You can utilize this via the --proxiedArguments flag. In the code examples above, the following phpcs command will be executed: phpcs -p -n --standard=PSR2

Supported Versions Of Laravel

Laravel ^5.5 is actively supported. Need support for earlier versions of Laravel? Feel free to open a PR

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Wondering how to go about working on a laravel package? See http://laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/ and https://laravel.com/docs/5.5/packages

Credits

License

The MIT License (MIT). Please see License File for more information

laravelgithooks's People

Contributors

achmadfatoni avatar mr-feek avatar nidble avatar rossdeane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravelgithooks's Issues

Allow skipped or incomplete tests

Hey @mr-feek , thanks for the nice tool ๐Ÿ‘

Description

In my understanding the current code does not consider valid a phpunitresult with warnings (e.g. OK, but incomplete, skipped, or risky tests!).
I do set some tests as skipped or incomplete during development as kind of reminders or TODOs, and I'd like to be able to push, for example, just as long as no test actually fails.

Possible implementation

Not an expert on this, but I believe you could handle the result like:

switch ($result) {
    case TestRunner::SUCCESS_EXIT:
        $this->info($this->commandOutputFormatter->success('Running PHPUnit'));
        return 0;
    case TestRunner::EXCEPTION_EXIT:
        $this->error($this->commandOutputFormatter->warn('Running PHPUnit'));
        return 0;
    case TestRunner::FAILURE_EXIT:
        $this->error($this->commandOutputFormatter->error('Running PHPUnit'));
        return 1;
}

Not sure if there are actually additional results to keep in account.

Maybe you could add an option to allow the user to define the behaviour in case of warnings. Here though I can't really help with the code... ๐Ÿ˜…

Your environment

MAC, PHP 7, Laravel 5.5, PHPUnit 6.4.3

Note: my phpunit.xml file contains:

     ...
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"

and I also tried to add

     failOnWarning="false"
     failOnRisky="false"

but the output of my tests (with a skipped or incomplete test) is always 2, so the hook fails.

Ensure phpcs / phpcbf are included upon install

Detailed description

Provide a detailed description of the change or addition you are proposing.

Make it clear if the issue is a bug, an enhancement or just a question.

Context

Why is this change important to you? How would you use it?

How can it benefit other users?

Possible implementation

Not obligatory, but suggest an idea for implementing addition or change.

Your environment

Include as many relevant details about the environment you experienced the bug in and how to reproduce it.

  • Version used (e.g. PHP 5.6, HHVM 3):
  • Operating system and version (e.g. Ubuntu 16.04, Windows 7):
  • Link to your project:
  • ...
  • ...

Install Dependencies Command Has Incorrect Logic

Description

The logic in https://github.com/mr-feek/LaravelGitHooks/blob/master/src/Commands/InstallDependencies.php#L139 InstallDependencies::isFileChanged incorrectly defers to the git diff to check if dependencies should be installed. This is incorrect, as what we actually want to check is if any of the lock files changed upon switching branches.

Possible implementation

The arguments passed to the post-checkout command are "...The hook is given three parameters: the ref of the previous HEAD, the ref of the new HEAD (which may or may not have changed), and a flag indicating whether the checkout was a branch checkout"

We might be able to somehow check for changes in the commits based on the refs given to us.

respect verbosity flags

Description

--quiet, -v, -vvv

Possible implementation

Not obligatory, but suggest an idea for implementing addition or change.

Your environment

Cannot install the package

Guys, i have issue installing the package, i am installing screenshot. It obviously has some issue with versions of the dependencies but i have not idea how to fix it

screen shot 2018-09-05 at 12 14 43 am

Thanks for the great package!

Create An Artisan Command For PHPStan

Detailed description

Create an artisan command for phpstan, which can be invoked from any git hook. By default, lets have it run pre-push.

Possible implementation

PHPStan has some gotchas with laravel, and the user will likely want to have laravel ide helper installed. Perhaps we should hint of this if the command is invoked. Then by default we can autoload the ide_helper.php file.

SemanticCommitMessages command should allow choosing a commit type

Description

The SemanticCommitMessages command fails a commit if the message does not contain a whitelisted commit type. Instead, the command should prompt the user and ask them what type of commit they are applying, and should then apply the correct type to their pre-existing commit message.

Possible implementation

Use artisan's choice method.

Pull handle logic out of commands and into a service.

Description

Avoid inheritance, move command handling logic into a service. Have the commit hook commands implement an interface

Possible implementation

Not obligatory, but suggest an idea for implementing addition or change.

Your environment

Make Hooks Executable When Copying Over

Detailed description

Provide a detailed description of the change or addition you are proposing.

Make it clear if the issue is a bug, an enhancement or just a question.

Context

Why is this change important to you? How would you use it?

How can it benefit other users?

Possible implementation

Not obligatory, but suggest an idea for implementing addition or change.

Your environment

Include as many relevant details about the environment you experienced the bug in and how to reproduce it.

  • Version used (e.g. PHP 5.6, HHVM 3):
  • Operating system and version (e.g. Ubuntu 16.04, Windows 7):
  • Link to your project:
  • ...
  • ...

reinstalling hooks does not overwrite existing contents

Description

php artisan hooks:install should overwrite any existing content previously created by this package.

Currently, no updates to the hook files themselves in this package will be updated upon installation

Possible implementation

Your environment

Create prepare-commit-msg git hook

Detailed description

Currently I only support pre-push and pre-commit. Next plan is to allow running artisan commands during the prepare-commit-msg git hook.

Context

Possible implementation

  • create bash file in src/hooks named prepare-commit-msg.sh, and can use the others as a template.
  • edit config/hooks.php and add a new array key config for the pre-commit-msg hook.
  • create a new command in src/Commands/CommitHooks named PrepareCommitMessage, which extends CommitHookCommand. The getConfigKey function should be implemented to point to the configuration key created in the previous step.
  • ensure that this new command is made available
  • add the new prepare-commit-msg.sh file to the list of files to be copied over.

Create a command to remove all laravel git hooks

Description

Create an artisan command that can be run to remove all laravel git hooks.

php artisan hooks:uninstall

Possible implementation

  • remove all files in .git/hooks folder
  • could be done with #7 to keep previous hooks in tact

Do Not Overwrite Existing Hook File Contents

Detailed description

Currently when php artisan hooks:install is run, the entire file contents of .git/hooks/{file} are replaced. This means that developers can not run their own custom hooks alongside the hooks provided by this package.

Instead, php artisan hooks:install should append its hooks to the bottom of the .git/hooks/{file}

Possible implementation

  • create a delimiter so that we know which contents of the git hook file have been scaffolded.

IE

# LARAVEL GIT HOOKS BEGIN #

[insert file contents]

# LARAVEL GIT HOOKS END #

- only replace the contents between the delimiters, leaving the rest of the file intact.

Please write tests.

Set up Travis CI

Description

I would like to ensure that on every PR submission, the code is sent to travis CI and unit tests are run.

Create Post Checkout Git Hook

use #8 as a guideline!

Detailed description

I would like to allow developers to specify commands to be run during the post-checkout git hook.

Possible implementation

  • create bash file in src/hooks named post-checkout.sh, and can use the others as a template.
  • edit config/hooks.php and add a new array key config for the post-checkout hook.
  • create a new command in src/Commands/CommitHooks named PostCheckout, which extends CommitHookCommand. The getConfigKey function should be implemented to point to the configuration key created in the previous step.
  • ensure that this new command is made available
  • add the new post-checkout.sh file to the list of files to be copied over.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.