├── .gitignore ├── README.md ├── composer.json ├── php-cs-fixer-commit └── src └── Command.php /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.lock 2 | /phpunit.xml 3 | /vendor 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-cs-fixer-commit 2 | 3 | ## Installation 4 | 5 | ``` 6 | $ composer require --dev enomotodev/php-cs-fixer-commit 7 | ``` 8 | 9 | ## Usage (GitHub + GitHub Actions) 10 | 11 | ### Create workflow file 12 | 13 | In `.github/workflows` , add a .yml or .yaml file for your workflow. 14 | 15 | For example, `.github/workflows/php-cs-fixer-commit.yml` . 16 | 17 | ```yaml 18 | name: php-cs-fixer-commit 19 | 20 | on: 21 | push: 22 | 23 | jobs: 24 | php-cs-fixer-commit: 25 | runs-on: ubuntu-latest 26 | steps: 27 | - uses: actions/checkout@v2 28 | - name: Install Dependencies 29 | run: composer install 30 | - name: php-cs-fixer-commit 31 | env: 32 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 33 | run: ./vendor/bin/php-cs-fixer-commit 34 | ``` 35 | 36 | NOTE: Please make sure you replace `` and `` with yours. 37 | 38 | ## Usage (GitHub + CircleCI) 39 | 40 | ### Setting GitHub personal access token to CircleCI 41 | 42 | GitHub personal access token is required for sending pull requests to your repository. 43 | 44 | 1. Go to [your account's settings page](https://github.com/settings/tokens) and generate a personal access token with "repo" scope 45 | 1. On CircleCI dashboard, go to your application's "Project Settings" -> "Environment Variables" 46 | 1. Add an environment variable `GITHUB_ACCESS_TOKEN` with your GitHub personal access token 47 | 48 | ### Configure circle.yml 49 | 50 | Configure your `circle.yml` or `.circleci/config.yml` to run `php-cs-fixer-commit`, for example: 51 | 52 | ```yaml 53 | version: 2 54 | 55 | jobs: 56 | build: 57 | # ... 58 | fixer: 59 | steps: 60 | # ... 61 | - run: 62 | name: php-cs-fixer-commit 63 | command: ./vendor/bin/php-cs-fixer-commit 64 | ``` 65 | 66 | NOTE: Please make sure you replace `` and `` with yours. 67 | 68 | ## Usage (GitLab + GitLabCI) 69 | 70 | ### Setting GitLab personal access token to GitLabCI 71 | 72 | GitLab personal access token is required for sending merge requests to your repository. 73 | 74 | 1. Go to [your account's settings page](https://gitlab.com/profile/personal_access_tokens) and generate a personal access token with "api" scope 75 | 1. On GitLab dashboard, go to your application's "Settings" -> "CI /CD" -> "Environment variables" 76 | 1. Add an environment variable `GITLAB_API_PRIVATE_TOKEN` with your GitLab personal access token 77 | 78 | ### Configure .gitlab-ci.yml 79 | 80 | Configure your `.gitlab-ci.yml` to run `php-cs-fixer-commit`, for example: 81 | 82 | ```yaml 83 | stages: 84 | # ... 85 | - fixer 86 | 87 | # ... 88 | 89 | fixer-commit: 90 | image: composer:latest 91 | stage: fixer 92 | script: 93 | - "composer install" 94 | - "$COMPOSER_HOME/vendor/bin/php-cs-fixer-commit " 95 | ``` 96 | 97 | NOTE: Please make sure you replace `` and `` with yours. 98 | 99 | ## License 100 | 101 | php-cs-fixer-commit is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) 102 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "enomotodev/php-cs-fixer-commit", 3 | "description": "Create commit of php-cs-fixer", 4 | "keywords": ["circleci", "github", "gitlabci", "gitlab"], 5 | "license": "MIT", 6 | "type": "library", 7 | "homepage": "https://enomotodev.hatenablog.com/", 8 | "support": { 9 | "issues": "https://github.com/enomotodev/php-cs-fixer-commit/issues", 10 | "source": "https://github.com/enomotodev/php-cs-fixer-commit" 11 | }, 12 | "authors": [ 13 | { 14 | "name": "enomotodev", 15 | "email": "enomoto.dev@gmail.com" 16 | } 17 | ], 18 | "require": { 19 | "friendsofphp/php-cs-fixer": "^2.4.0 || ^3.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Enomotodev\\PhpCsFixerCommit\\": "src/" 24 | } 25 | }, 26 | "bin": [ 27 | "php-cs-fixer-commit" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /php-cs-fixer-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | run($_SERVER['argv']); 15 | } 16 | 17 | /** 18 | * @param array $argv 19 | * @return void 20 | */ 21 | public function run(array $argv) 22 | { 23 | $argvCount = count($argv); 24 | if ($argvCount !== 3 && $argvCount !== 4) { 25 | fwrite(STDERR, 'Invalid arguments.' . PHP_EOL); 26 | exit(1); 27 | } 28 | 29 | list(, $name, $email) = $argv; 30 | $path = isset($argv[3]) ? $argv[3] : ''; 31 | 32 | if (file_exists('./vendor/bin/php-cs-fixer')) { 33 | $commandPath = './vendor/bin'; 34 | } elseif ((bool) getenv('COMPOSER_HOME')) { 35 | $commandPath = getenv('COMPOSER_HOME') . '/vendor/bin'; 36 | } else { 37 | $commandPath = '~/.composer/vendor/bin'; 38 | } 39 | system("{$commandPath}/php-cs-fixer fix {$path}"); 40 | system('git clean -df'); 41 | 42 | if (strpos(system('git status -sb'), '.php') === false) { 43 | fwrite(STDOUT, 'No changes.' . PHP_EOL); 44 | exit(0); 45 | } 46 | 47 | $this->setupGitConfig($name, $email); 48 | $this->createCommit(); 49 | } 50 | 51 | /** 52 | * @param string $name 53 | * @param string $email 54 | * @return void 55 | */ 56 | private function setupGitConfig($name, $email) 57 | { 58 | system("git config user.name {$name}"); 59 | system("git config user.email {$email}"); 60 | } 61 | 62 | /** 63 | * @return void 64 | */ 65 | private function createCommit() 66 | { 67 | if ((bool) getenv('GITHUB_ACTIONS')) { 68 | $branch = substr(system('git branch'), 2); 69 | $accessToken = getenv('GITHUB_TOKEN'); 70 | $repositoryName = getenv('GITHUB_REPOSITORY'); 71 | 72 | system("git remote set-url origin https://{$accessToken}@github.com/{$repositoryName}/"); 73 | system('git add -u'); 74 | system('git commit -m "php-cs-fixer"'); 75 | system("git push -q origin {$branch}"); 76 | } elseif ((bool) getenv('CIRCLECI')) { 77 | $branch = getenv('CIRCLE_BRANCH'); 78 | $accessToken = getenv('GITHUB_ACCESS_TOKEN'); 79 | $repositoryName = getenv('CIRCLE_PROJECT_REPONAME'); 80 | $repositoryUserName = getenv('CIRCLE_PROJECT_USERNAME'); 81 | 82 | system("git remote set-url origin https://{$accessToken}@github.com/{$repositoryUserName}/{$repositoryName}/"); 83 | system('git add -u'); 84 | system('git commit -m "php-cs-fixer"'); 85 | system("git push -q origin {$branch}"); 86 | } elseif ((bool) getenv('GITLAB_CI')) { 87 | $branch = getenv('CI_COMMIT_REF_NAME'); 88 | $token = getenv('GITLAB_API_PRIVATE_TOKEN'); 89 | $repositoryUrl = getenv('CI_REPOSITORY_URL'); 90 | preg_match('/https:\/\/gitlab-ci-token:(.*)@(.*)/', $repositoryUrl, $matches); 91 | 92 | system("git remote set-url origin https://gitlab-ci-token:{$token}@{$matches[2]}"); 93 | system("git checkout {$branch}"); 94 | system('git add -u'); 95 | system('git commit -m "php-cs-fixer"'); 96 | system("git push -q origin {$branch}"); 97 | } 98 | } 99 | } 100 | --------------------------------------------------------------------------------