{"id":18023630,"url":"https://github.com/jeffotoni/gitwebhooks","last_synced_at":"2026-04-16T11:02:23.329Z","repository":{"id":134047114,"uuid":"89519780","full_name":"jeffotoni/gitwebhooks","owner":"jeffotoni","description":"Application responsible for receiving messages from github webhooks, and automating our production environment by deploying.","archived":false,"fork":false,"pushed_at":"2018-09-11T04:26:27.000Z","size":2426,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-05T09:54:21.546Z","etag":null,"topics":["git","git-hooks","githook","githook-webservice","github-webhooks","php","php7","receive-requests","ssh-key","webhook","webhooks"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeffotoni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-26T19:39:38.000Z","updated_at":"2024-07-24T09:59:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"d7c29258-bc89-47ca-8567-4a889cb877de","html_url":"https://github.com/jeffotoni/gitwebhooks","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/jeffotoni/gitwebhooks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeffotoni%2Fgitwebhooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeffotoni%2Fgitwebhooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeffotoni%2Fgitwebhooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeffotoni%2Fgitwebhooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeffotoni","download_url":"https://codeload.github.com/jeffotoni/gitwebhooks/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeffotoni%2Fgitwebhooks/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31882886,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T09:23:21.276Z","status":"ssl_error","status_checked_at":"2026-04-16T09:23:15.028Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git","git-hooks","githook","githook-webservice","github-webhooks","php","php7","receive-requests","ssh-key","webhook","webhooks"],"created_at":"2024-10-30T07:10:04.406Z","updated_at":"2026-04-16T11:02:23.294Z","avatar_url":"https://github.com/jeffotoni.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gitwebhooks\n\nGitwebhooks is a service for receiving GHub WebHooks.\n\nGitwebhooks is a microservice, it receives Http requests to be configured in addition to receiving requests from WebHooks.\n\nGitwebhooks receives a POST from WebHooks coming from Github, and it automatically generates a script from a template and runs it on the server.\n\nOur example shows the class call executing all its methods: Handle the rules of WeHooks, generate script.sh, save the script to the disk, run it, remove the server script or not and generate log of the entire execution .\n\nOur microservice has its own route controller and dynamic classes ie you will not need to make new there is an object that will dynamically mount them, using namespace and several other techniques to make it light and thin.\n\n\nThere have been implemented 3 routes so far\n\n```php\n\n/github/webhooks\n    - This endpoint is responsible for receiving the webhooks of Github\n\n/webhooks/repository/add/{name}\n    - This endpoint is responsible for adding new git repository on our server.\n\n/webhooks/status\n - This verifies the status of our microservice.\n\n```\n\nCheck out how the calls are made just below, it receives WebHooks coming from github, creates and executes in real time our script from a template that we can configure it in the way that we think best\n\n```php\n\n$api-\u003eNewRouter()\n    -\u003eMethods(\"POST\")\n    -\u003eHandleFunc(\"/github/webhooks\", function (Response $response, Request $request) use ($api) \n    {\n    \n        $GitHub = $api-\u003eGitHub();\n        $api-\u003eGitHub()\n            \u003eAuthenticateSecretKey()\n            -\u003eEvent(\"push\")\n            -\u003eWScript($api)       \n            -\u003eLoadTemplate(\n                [\n\n                \"REPOSITORY\" =\u003e $GitHub::$REPOSITORY,\n\n                \"PATH\"       =\u003e ARRAY_PROJECT_GIT[$GitHub::$REPOSITORY],\n\n                \"BRANCH\"     =\u003e $GitHub::$BRANCH,\n\n                ]\n            )          \n            -\u003eLoadFileScript()\n            -\u003eSave()\n            -\u003eExecute()\n            -\u003eDelFile()\n            -\u003eLoadLog();\n    }\n\n)-\u003eRun();\n\n```\n\nSometimes we want to add another git repository on our server, we need to create directory, run a git clone etc, this endpoint will do just that.\n\n```php\n\n$api-\u003eNewRouter()\n\n-\u003eMethods(\"GET\")-\u003eHandleFunc(\n\n    '/webhooks/repository/add/{name}', function (Response $response, Request $request) use ($api) {\n    \n        $api-\u003eGitWebHooks()-\u003eAuthenticateMd5();\n\n        \n        $branch     = $request-\u003eGetBranch();\n        \n        $repository = $request-\u003eGetName();\n\n        $gitUser    = $request-\u003eGitUser();\n\n        $api-\u003eWScript()-\u003eAddRepository($gitUser, $repository, $branch);\n    }\n\n)-\u003eRun();\n\n```\n\nYou can extend the entire application, just create your classes to apply them in your need.\n\nWhen creating a class in web/src/MyControler/MyControlerClass.php for example, you can use it for example as follows:\n\n```php\n\n$api-\u003eMyControlerClass()-\u003eMyMethod();\n\n /**\n *              OR\n */\n \n Use web\\src\\MyControler\\MyControlerClass as MyCC;\n \n $myobj = new MyCC();\n \n /**\n *              OR\n */\n \n $objMycc = new web\\src\\MyControler\\MyControlerClass();\n \n /**\n *              dependency injection\n */\n \n $objMycc-\u003eMyMethod($api);\n\n ```\n\nIf you want to access any class of the system just pass $ api as a parameter and with it you can access any class and method available on the platform.\n\nThis way it is easier for you to create your own classes and apply them to the system\n\n\n## Does not use library\n\nThe server is written in php and we do not use any library, the code is simple and very light, we know we can improve and extend the application even more and this is our goal.\n\nWe use psr2, autoloading, namespaces to write our server, our model instantiates class execution time with this technique we can instantiate classes and their methods in a light and practical way and with the possibility of doing dependency injection.\n\n## Git Server Environment\n\nWe know there are hundreds of solutions for implementations, each satisfying a need and reality.\n\nBefore developing a simple and lightweight deployment solution for my need, I ran several tests with git hooks, bitbucket webhooks, gitlab, and github.\n\nGit Hooks is interesting, if you have no problem keeping multiple remote servers is a valid option. To create a git server using the hooks is very simple and practical.\n\nTo know more about git hooks is my reference.\n\n[Git (Hooks)](https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks)\n\nWebHooks is the feature that tools like github, gitlab and bitbucket make available so that we can have a notification for each event in our repository, the events are diverse, all are programmable. You set up the URL that will point to your server, the webhooks sends a POST to your URL, with all the information you need so that you can automate your development process.\n\nTo know a little more about Webhhoks here are the references I've used:\n\n[Github (Webhooks)](https://developer.github.com/webhooks/)\n\n[Gitlab (Webhooks)](https://docs.gitlab.com/ce/user/project/integrations/webhooks.html)\n\n[Bitbucket (Webhooks)](https://bitbucket.org/StephenHowells/webhook)\n\nOur program will receive a POST from Github, the event being what we programmed it will generate a script from a template that we define for our deploy and execute it on our server.\n\nCreating SSH Keys and using git clone on our remote server\n\nFor everything to work, we suggest that you use ssh: // to do the git clone, this way we guarantee that when we run the git commands as www user we will not have to worry about the system request password or user, although we have to cache but Ssh is still the best option for good health of your server.\n\n\n## Installing the program on the server\n\nOur program is done in php, on your web server, can be running apache, ngnix or we can simply run the program with php -S localhost:9001\n\n```sh\n\n$ git clone ssh://git@github.com/jeffotoni/gitwebhooks.git\n\n```\n\nFor this to work well you should do as www user as follows.\n\n```sh\n\n$ sudo -u www-data -H git clone -v ssh: //git@ggithub.com/jeffotoni/gitwebhooks.git\n\n```\n\nFor this to work you will have to do the following steps.\n\n```sh\n\n$ sudo -u www-data mkdir /var/www/.ssh\n\n$ sudo -u www-data ssh-keygen -t rsa -b 4096 -C \"your_email@example.com\"\n\n\n```\n\nIf you want a good reference of a checked here\n\n[Generate SSH Keys (SSH)](https://help.github.com/articles/connecting-to-github-with-ssh)\n\n\nIt will generate the id_rsa, id_rsa.pub, in /var/www/.ssh now just paste your public key and paste it into github settings -\u003e keys -\u003e new SSH Key\n\nCopying the Public Key\n\n```sh\n\n$ sudo -u www-data cat /var/www/.ssh/id_rsa.pub\n\n```\n\nAn example of a structure you might create on your server so that our program works correctly\n\n```sh\n\n$ mkdir /var/www/githtml/beta/repository.git\n\n$ mkdir /var/www/githtml/product/repository.git\n\n```\n\nNow that we have configured our access keys we can use the git clone\n\n```sh\n\n$ sudo -u www-data -H git clone -v ssh: //git@github.com/jeffotoni/gitwebhooks.git\n\n$ sudo -u www-data -H chmod 755  -R Your directory\n\n```\n\n### If you are using apache, do not forget to enable in your virtualhosts [ AllowOverride All ]\n\n```\nRewriteEngine On\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^ test_index.php [QSA,L]\n\n```\n\n### If you are using nginx\n\n```\nserver {\n    listen 80;\n    server_name yoursite.com;\n    index index.php;\n    error_log /path/to/yoursite.error.log;\n    access_log /path/to/yoursite.access.log;\n    root /path/to/public;\n\n    location / {\n        try_files $uri /index.php$is_args$args;\n    }\n\n    location ~ \\.php {\n        try_files $uri =404;\n        fastcgi_split_path_info ^(.+\\.php)(/.+)$;\n        include fastcgi_params;\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n        fastcgi_param SCRIPT_NAME $fastcgi_script_name;\n        fastcgi_index index.php;\n        fastcgi_pass 127.0.0.1:9000;\n    }\n}\n\n```\n\n### If you are using PHP built-in server in production\n\n```\n$ php -S localhost:9001 -t public\n\n```\n\n### If you are using PHP built-in server in Locally\n\n```\n$ cd public\n$ php -S localhost:9001 test_index.php\n\n```\n\n### Now just run \"generate-config.php\" so that your config is generated and you can configure it.\n\n```\n$ cd config\n$ php -q generate-config.php\n\n```\n\n### The program will always check for the POST \n### received by GitHub to determine how it will deploy.\n\n## System structure\n\n```\n \n - public\n    index.php\n    test_index.php\n\n - config/\n    generate-config.php (Run as soon as you put it on your server)\n\n    setenv.conf.php\n\n    setconfig.conf.php (Generated in real time by the program \"generate-config.php\")\n\n    git.repositories.conf.php (In this file is where the paths of your git projects will be configured on your server)\n\n    apache-rewritengine\n\n    nginx-rewritengine\n \n - simulation/\n    curl-test.sh (Executes the curl calls to test and simulate the production environment)\n\n    github.webhooks.json (Json format)\n\n    github.webhooks.form (x-www-form-urlencoded format)\n\n    server-http.go (example to test a deploy using go)\n\n    teste-template-deploy-golang.sh (A deploy to golang)\n \n - templates/\n    simulation-example.sh.php (To perform the tests without actually executing)\n\n    template-script-deploy.sh.php (This script we are using to deploy our applications in php)\n\n    template-script-add-repository.sh.php (Template responsible for adding repository to your server)\n\n    template-script-golang-deploy.sh.php\n\n  - log/\n     github-webooks.log (All actions done on gitwebhooks saved here)\n\n  - web/src/\n        Hooks/\n            Handlers/\n            Hooks/\n            Http/\n            Interfaces/\n            Message/\n                Logs\n\n```\n\n## cURL to perform the tests Locally\n\nFirst under the application with php just to test as follows\n\n```php\n\n$ cd public\n$ php -S localhost:9001 test_index.php\n\n```\n\nNow we can run local tests with curl\n\n```sh\n\n$ cd simulation\n$ sh curl-test.sh\n\n```\n\n## curl-test.sh Source\n\nYou can simulate the submissions to your local machine as if it were github.\n\n\nSending post as if it were github, content type json, we made a file github.webhooks.json, \nwhich is in the same directory, to simulate the sending.\n\nWe are using in this shipment the secret \"X-Hub-Signature\"\n\n```bash\n\ncurl -X POST \\\n     -H \"Content-Type: application/json\" \\\n     -H \"X-Hub-Signature: sha1=9c714dcc8f1f4ba829c88fef184ccd0d090f019d\" \\\n     -H \"X-GitHub-Event: push\" \\\n     -H \"X-GitHub-Delivery: e4cd4180-2c67-11e7-8099-87e86dbb4105\" \\\n     http://localhost.gitwebhooks/github/webhooks \\\n     -d @github.webhooks.json\n\n```\n\nSending post as if it were github, content type x-www-form-urlencoded, we made a \nfile github.webhooks.form, which is in the same directory, to simulate the sending.\n\nIn this submission we are using a parameter in the url itself, \"key\", Discontinued use of the key\n\n```bash\n\n curl -X POST \\\n    -H \"Content-Type: application/x-www-form-urlencoded\" \\\n    -H \"X-GitHub-Event: push\" \\\n    -H \"X-GitHub-Delivery: e4cd4180-2c67-11e7-8099-87e86dbb4105\" \\\n    http://localhost.gitwebhooks/github/webhooks\\?key=b118eda467d926d003f9b4af9c203994 \\\n    -d @github.webhooks.form\n\n```\n\nGitwebhooks is a service so we made a call to another url we created, to return the status of our server.\n\nIt is using an md5 \"GitWebHoos-Authentication\" authentication, but we can implement any of the \"hash_hmac\".\n\n```bash\n\ncurl -X GET \\\n     -H \"Content-Type: application/json\" \\\n     -H \"GitWebHoos-Authentication: md5=827ccb0eea8a706c4c34a16891f84e7b\" \\\n     http://localhost.gitwebhooks/gitwebhooks/status\n\n```\n\nWhen using the PHP built-in server the call can be made so\n\n```bash\n\ncurl -X POST \\\n     -H \"Content-Type: application/json\" \\\n     -H \"GitWebHoos-Authentication: md5=827ccb0eea8a706c4c34a16891f84e7b\" \\\n     -H \"X-GitHub-Event: push\" \\\n     -H \"X-GitHub-Delivery: e4cd4180-2c67-11e7-8099-87e86dbb4105\" \\\n     http://localhost:9001/github/webhooks \\\n     -d @github.webhooks.json\n\n```\n\nAdding repository on server, it git clone on server.\n\n```bash\ncurl -X GET \\\n     -H \"Content-Type: application/json\" \\\n     -H \"GitWebHooks-Authentication: md5=827ccb0eea8a706c4c34a16891f84e7b\" \\\n     -H \"GitWebHooks-Branch: master\" \\\n     -H \"GitWebHooks-GitUser: jeffotoni\" \\\n     http://localhost:9001/webhooks/repository/add/yourproject100\n\n```\n\n## System Settings\n\nWe made an abstraction of our config, now we have a new file setenv.conf.php, it is responsible for generating our config, so your file setconfig.conf.php will be outside the git respository, and you can make your own configurations on the server without affecting It with updates, it will only be generated once.\n\n\n## System Settings, setenv.conf.php\n\nIt is responsible for generating your config once, setconfig.conf.php\n\n```php\n/**\n*\n* @about    project GitHub Webhooks, \n* Application responsible \n* for receiving posts from github webhooks, and automating \n* our production environment by deploying\n* \n* @autor    @jeffotoni\n* @date     25/04/2017\n* @since    Version 0.1\n* \n*/\n\n\n//\n//\n//\n\ndefine(\"ROOT_DIR\", PATHSET_LOCAL);\n\n//\n//\n//\nchdir(ROOT_DIR);\n\n//\n//\n//\n\ndefine(\"GITHUB_SECRET\", \"12345\");\n\n\n//\n//\n//\n\ndefine(\"KEY\", \"b118eda467d926d003f9b4af9c203994\");\n\n//\n//\n//\n\ndefine(\"GITWEBHOOKS_SECRET\", \"827ccb0eea8a706c4c34a16891f84e7b\");\n\n// \n// \n// \n\ndefine(\"PATH_LOG\", ROOT_DIR. \"log/github-webooks.log\");\n\n// \n// \n// \n\ndefine(\"PATH_TEMPLATE\", \"templates/\");\n\n// \n// \n// \n\ndefine(\"PATH_SCRIPT\", \"scripts/\");\n\n\n// \n// \n// \n\ndefine(\"PATH_CLASS\", \"web/src\");\n\n\n// \n// \n// \n\ndefine(\"PATH_CLASS_NAMESPACE\", \"web\\src\");\n\n//\n//\n//\n\n$BRANCH_TEMPLATE_DEPLOY = parse_ini_file(PATH_BRANCH_CREATE);\n\n/** \n *\n * /var/www/gitmyprojects/branch\n *\n * OR\n * \n * /var/www/gitmyprojects/product\n *\n * OR\n * \n * /var/www/gitmyprojects/beta\n *\n * OR\n * \n * /var/www/gitmyprojects/test\n * \n *\n */\n\ndefine(\"BRANCH_TEMPLATE_DEPLOY\", $BRANCH_TEMPLATE_DEPLOY, true);\n\n//\n//\n//\n\ndefine(\"PATH_REPOSITORY\", PATH_REPOSITORY_CREATE);\n\n/** \n *\n * or /var/www/gitmyprojects/\n *\n * example:\n * \n * /var/www/gitmyprojects/beta\n * /var/www/gitmyprojects/product\n * /var/www/gitmyprojects/test\n *\n * /var/www/gitmyprojects/beta/gitproject1.git\n * /var/www/gitmyprojects/beta/gitproject2.git\n * \n * /var/www/gitmyprojects/product/gitproject1.git\n * /var/www/gitmyprojects/product/gitproject2.git\n *\n * Configuring your paths\n * \n * gitwebhooks      =\u003e /var/www/gitmyprojects\n *\n * gitproject1      =\u003e /var/www/gitmyprojects\n *\n * gitproject2      =\u003e /var/www/gitmyprojects\n *\n *  OR\n *  \n * gitwebhooks      =\u003e ../../../../../\n *\n * gitproject1      =\u003e ../../../../../\n *\n * gitproject2      =\u003e ../../../../../\n * \n */\n\n$ARRAY_PROJECT_GIT = parse_ini_file(PATH_REPOSITORY);\n\n//\n//\n//\n\ndefine(\"ARRAY_PROJECT_GIT\", $ARRAY_PROJECT_GIT, true);\n\n```\n\n## Structure of the program\n\nThe $api object is special it can create and instantiate any class that is in the web/src/Hooks structure, so: $api = special object, WScript() = Class, LoadTemplate() == class method.\n\n```php\n/**\n*\n* @about project GitHub Webhooks, \n* Application responsible \n* for receiving posts from github webhooks, and automating \n* our production environment by deploying\n* \n* @autor    @jeffotoni\n* @date     25/04/2017\n* @since  Version 0.1\n*/\n\n// \n// php -S localhost:9001 -t index.php\n// \n// OR\n// \n// php -S localhost:9001 -t test_index.php \n// \n// Apache .htaccess\n// \n// OR\n// \n// Ngnix\n// \n\nrequire_once \"../config/setenv.conf.php\";\n\n/** \n * Various ways of instantiating objects\n *\n * Way one:\n * \n * use web\\src\\Http\\NewRouter;\n * $NewRouter = new NewRouter();\n *\n * Way two:\n *\n * $NewRouter = $api-\u003eNewRouter();\n */\n\n//\n// Class responsible for handling the responses\n//\n\nuse web\\src\\Http\\Response as Response;\n\n\n//\n// Class responsible for handling request \n//\n\nuse web\\src\\Http\\Request as Request;\n\n//\n// Instantiating routes\n//\n\n$api-\u003eNewRouter()\n\n    //\n    //\n    //\n\n    -\u003eMethods(\"POST\")\n\n    //\n    //\n    //\n\n    -\u003eHandleFunc(\n        \n        //\n        // Defining your routes\n        //\n\n        '/github/webhooks', function (Response $response, Request $request) use ($api) {\n\n            //\n            // Class responsible for handling requests coming from github\n            //\n\n            $GitHub = $api-\u003eGitHub();\n\n            $api-\u003eGitHub()\n\n                //\n                //\n                // Authentication coming from github secret\n                // For security reasons we suggest you use this option\n                // \n                //\n\n                -\u003eAuthenticateSecretToken()\n\n\n                //\n                // Authentication of your key, coming from the URL, a GET\n                //\n\n                //-\u003eAuthenticateSecretKey()\n\n                //\n                // Authentication using md5, header\n                //\n\n                //-\u003eAuthenticateMd5()\n\n                //\n                // Setting the event\n                //\n\n                -\u003eEvent(\"push\")\n\n                //\n                // Making the call to run the deploy scripts\n                //\n\n                -\u003eWScript($api)\n\n                // \n                // Loading and running updates\n                // \n        \n                -\u003eLoadTemplate(\n                    [\n\n                    \"REPOSITORY\" =\u003e $GitHub::$REPOSITORY,\n\n                    \"PATH\"       =\u003e ARRAY_PROJECT_GIT[$GitHub::$REPOSITORY],\n\n                    \"BRANCH\"     =\u003e $GitHub::$BRANCH,\n\n                    ]\n                )\n\n                // \n                // Generate script from template\n                //\n          \n                -\u003eLoadFileScript()\n\n                // \n                // Prepares to run, saves script to disk\n                //\n                -\u003eSave()\n\n                // \n                // It runs the script\n                //\n                //\n                -\u003eExecute()\n\n                // \n                // Possibility to delete file, it is not mandatory\n                //\n\n                -\u003eDelFile()\n\n                // \n                // Generates log on disk so you can keep track of all executions\n                //\n                //\n                -\u003eLoadLog();\n\n        }\n    )\n\n    //\n    // It will execute the methods\n    //\n\n    -\u003eRun();\n\n\n//\n// \n//\n\n$api-\u003eNewRouter()\n\n    //\n    //\n    //\n\n    -\u003eMethods(\"GET\")\n\n    //\n    //\n    //\n\n    -\u003eHandleFunc(\n        '/webhooks/status', function (Response $response, Request $request) use ($api) {\n\n            //\n            //\n            //\n\n            $api-\u003eGitWebHooks()\n\n            //\n            //\n            //\n\n                -\u003eAuthenticateMd5();\n\n            //\n            //\n            //\n\n            $arrayJson = [\n            \n            \"status\" =\u003e \"online\",\n            ];\n\n            $response-\u003eWriteJson($arrayJson);\n\n        }\n\n    )-\u003eRun();\n\n\n\n//\n// \n//\n\n$api-\u003eNewRouter()\n\n    //\n    //\n    //\n\n    -\u003eMethods(\"GET\")\n\n    //\n    //\n    //\n\n    -\u003eHandleFunc(\n        '/webhooks/repository/add/{name}', function (Response $response, Request $request) use ($api) {\n\n\n            //\n            // Authentication\n            // \n\n            $api-\u003eGitWebHooks()\n\n                //\n                //\n                //\n                -\u003eAuthenticateMd5();\n\n            //\n            //\n            //\n            \n            $branch     = $request-\u003eGetBranch();\n            \n            $repository = $request-\u003eGetName();\n\n            $gitUser    = $request-\u003eGitUser();\n\n            //\n            //\n            //\n            \n            $api-\u003eWScript()-\u003eAddRepository($gitUser, $repository, $branch);\n\n        }\n        \n    )-\u003eRun();\n    \n```\n\n## Riding our route, the method without comments\n\n\n```php\n$api-\u003eNewRouter()\n\n    -\u003eMethods(\"POST\")\n\n    -\u003eHandleFunc(\"/github/webhooks\", function (Response $response, Request $request) use ($api) {\n            \n            $GitHub = $api-\u003eGitHub();\n\n            $api-\u003eGitHub()\n\n                \u003eAuthenticateSecretKey()\n\n                -\u003eEvent(\"push\")\n\n                -\u003eWScript($api)       \n\n                -\u003eLoadTemplate(\n                    [\n\n                    \"REPOSITORY\" =\u003e $GitHub::$REPOSITORY,\n\n                    \"PATH\"       =\u003e ARRAY_PROJECT_GIT[$GitHub::$REPOSITORY],\n\n                    \"BRANCH\"     =\u003e $GitHub::$BRANCH,\n\n                    ]\n                )          \n                -\u003eLoadFileScript()\n\n                -\u003eSave()\n\n                -\u003eExecute()\n\n                -\u003eDelFile()\n\n                -\u003eLoadLog();\n        }\n    )-\u003eRun();\n\n```\n\n## Structure of the Class WScript\n\n```php\n\n/*\n*\n* @about project GitHub Webhooks, \n* Application responsible \n* for receiving posts from github webhooks, and automating \n* our production environment by deploying\n* \n* @autor    @jeffotoni\n* @date     25/04/2017\n* @since    Version 0.1\n* \n*/\n\n// \n// \n// \n\nnamespace web\\src\\Hooks;\n\n\nclass WScript\n{\n    \n\n    private static $msgconcat = \"\";\n    \n\n    // \n    // \n    // \n\n    private static $show_msg_load = \"\";\n\n    // \n    // \n    // \n\n    private static $msg;\n\n    // \n    // \n    // \n\n    private static $TemplateContent ;\n\n    // \n    // \n    // \n\n    private static $pathTemplate; \n\n    // \n    // \n    // \n\n    private static $pathScript; \n\n\n    //\n    //\n    //\n\n    function __construct()\n    {\n        # code\n    }\n\n    //\n    //\n    //\n\n    private static function GetTemplate()\n    {\n        // \n        // use `self` to access class constants from inside the class definition. \n        // \n\n        return BRANCH_TEMPLATE_DEPLOY;\n    } \n\n    // \n    // \n    // \n\n    public function LoadTemplate($_ARRAY, $modelo = \"beta\") \n    {\n\n\n        // \n        // \n        // \n\n        $path = ROOT_DIR;\n\n        // \n        // \n        // \n\n        $modeloName = isset(self::GetTemplate()[$modelo]) ? self::GetTemplate()[$modelo] : \"beta\";\n\n        // \n        // \n        // \n\n        $file_template = \"{$path}\" . PATH_TEMPLATE . \"{$modeloName}.sh.php\";\n\n        //\n        //\n        //\n\n        if(isset($_ARRAY['BRANCH'], $_ARRAY['PATH']) \u0026\u0026 $_ARRAY['BRANCH'] \u0026\u0026 $_ARRAY['PATH']) {\n\n            self::IsValidBranch($_ARRAY);\n\n        } else {\n\n            \n\n            //\n            //\n            // \n            self::$msgconcat .= \"Error: \" . PHP_EOL;\n            $msg = '{\"msg\":\"Repository, branch and PATH Are mandatory, can not be empty\"}';\n            self::$msgconcat .= $msg;\n\n            //\n            //\n            //\n\n            $this-\u003eLoadLog();\n\n            //\n            //\n            //\n\n            die($msg);\n        }\n\n        // \n        // \n        // \n\n        if (is_file($file_template)) {\n\n            //\n            //\n            //\n            self::$msgconcat .= \"Template Deploy:\" . PHP_EOL;\n            self::$msgconcat .= \"{$modelo}\".PHP_EOL.\"Name Template: {$modeloName}\" . PHP_EOL;\n            self::$msgconcat .= \"\" . PHP_EOL;\n\n            // \n            // \n            // \n\n            $NOME_SCRIPT = $_ARRAY[\"REPOSITORY\"] . \"-\".$modelo;\n\n\n            //\n            //\n            //\n            self::$msgconcat .= \"Repository:\" . PHP_EOL;\n            self::$msgconcat .= \"{$_ARRAY[\"REPOSITORY\"]}\".PHP_EOL;\n            self::$msgconcat .= \"\" . PHP_EOL;\n            \n\n            // \n            // \n            // \n\n            self::$pathScript = PATH_SCRIPT . $NOME_SCRIPT . \".sh\";\n\n            // \n            // \n            // \n\n            self::$pathTemplate = $file_template;\n\n            // \n            // \n            // \n            self::$msgconcat .= \"Create:\" . PHP_EOL;\n            self::$msgconcat .= \"File empty create: [{$file_template}]!\".PHP_EOL;\n            self::$msgconcat .= \"\" . PHP_EOL;\n\n            $content = file_get_contents($file_template);\n\n\n            // \n            // \n            // \n\n            if (is_array($_ARRAY)) {\n\n                // \n                // \n                // \n\n                foreach ($_ARRAY as $key =\u003e $value) {\n                     \n                    // \n                    // \n                    // \n\n                    $content = str_replace('{'.strtoupper($key).'}', $value, $content);\n                }\n\n                // \n                // \n                // \n\n                self::$TemplateContent = $content;\n\n                // \n                // \n                // \n                // \n            } else {\n\n                //\n                //\n                // \n                self::$msgconcat .= \"Error: \" . PHP_EOL;\n                $msg = '{\"msg\":\"erro, not found array LoadTemplate..\"}';\n                self::$msgconcat .= $msg;\n                self::$msgconcat .= \"\" . PHP_EOL;\n\n                //\n                //\n                //\n\n                $this-\u003eLoadLog();\n\n                //\n                //\n                //\n\n                die($msg);\n\n            }\n\n            return $this;\n\n        } else {\n\n            //\n            //\n            // \n            self::$msgconcat .= \"Error: \" . PHP_EOL;\n            $msg = '{\"msg\":\"erro, not found file [' . $file_template . ']..\"}';\n            self::$msgconcat .= $msg;\n            self::$msgconcat .= \"\" . PHP_EOL;\n\n            //\n            //\n            //\n\n            $this-\u003eLoadLog();\n\n            //\n            //\n            //\n\n            die($msg);\n        }\n    }\n\n    // \n    // \n    // \n\n    public function LoadFileScript($show=false) \n    {\n\n        \n        // \n        // \n        // \n\n        if(self::$TemplateContent \u0026\u0026 is_file(self::$pathTemplate)) {\n\n            // \n            // \n            // \n\n            if($show) {\n\n                print_r(self::$TemplateContent); \n            }\n\n            // \n            // \n            // \n\n            $PATH_SCRIPT = ROOT_DIR . self::$pathScript;\n\n            //\n            //\n            //\n\n            self::$pathScript = $PATH_SCRIPT;\n\n            //\n            //\n            //\n\n            self::$msgconcat .= \"Script Path: \" . PHP_EOL;\n            self::$msgconcat .= \"{\".self::$pathScript.\"}\".PHP_EOL;\n            self::$msgconcat .= \"\" . PHP_EOL;\n\n        } else {\n\n            //\n            //\n            // \n            self::$msgconcat .= \"Error: \" . PHP_EOL;\n            $msg = '{\"msg\":\"erro, not found file ['.self::$pathTemplate.']\"}';\n            self::$msgconcat .= $msg;\n            self::$msgconcat .= \"\" . PHP_EOL;\n\n            //\n            //\n            //\n\n            $this-\u003eLoadLog();\n\n            //\n            //\n            //\n\n            die($msg);\n\n        }\n\n        return $this;\n    }\n\n    // \n    // \n    // \n\n    public function Save()\n    {\n\n        // \n        // \n        // \n\n        if(file_put_contents(self::$pathScript, self::$TemplateContent)) {\n\n            // \n            // \n            // \n\n            self::$msg = \"Saved successfully [{\".self::$pathScript.\"}]!\";\n\n            //\n            //\n            //\n            self::$msgconcat .= \"Save: \" . PHP_EOL;\n            self::$msgconcat .= '{\"msg\":\"'.self::$msg.'\"\"}'.PHP_EOL;\n            self::$msgconcat .= \"\".PHP_EOL;\n\n        } else {\n\n            // \n            // \n            // \n\n            self::$msg = \"Error while saving!\";\n\n            self::$msgconcat .= \"Error Save:\" . PHP_EOL;\n            self::$msgconcat .= '{\"msg\":\"'.self::$msg.'\"}'.PHP_EOL;\n            self::$msgconcat .= \"\".PHP_EOL;\n        }\n\n        return $this;\n    }\n\n    // \n    // \n    // \n\n    public function Execute($exec=true)\n    {\n\n\n        if(is_file(self::$pathScript)) {\n\n            //\n            //\n            //\n\n            if($exec) {\n\n                // \n                // \n                // \n\n                $COMANDO = \"/bin/sh \".self::$pathScript.\" 2\u003e\u00261\";\n\n                // \n                // Executes even the generated template\n                // \n\n                $LAST_LINE = shell_exec($COMANDO);\n\n\n                //\n                // \n                //\n                self::$msgconcat .= \"SHELL SCRIPT: \" . PHP_EOL;\n                self::$msgconcat .= \"Execute shell script \" . PHP_EOL;\n                self::$msgconcat .= \"\".$LAST_LINE.\"\".PHP_EOL;\n\n                // \n                // \n                // \n\n                print \"\\n{$LAST_LINE}\\n\";\n            }\n\n            \n        } else {\n\n\n            $msg = '{\"msg\":\"erro, not found file [' . self::$pathScript . ']\"}' . PHP_EOL;\n\n            self::$msgconcat .= \"Error:\" . PHP_EOL;\n            self::$msgconcat .= $msg;\n            self::$msgconcat .= \"\".PHP_EOL;\n\n            //\n            // save log\n            //\n\n            $this-\u003eLoadLog();\n\n            die($msg);\n        }\n\n        return $this;\n    }\n\n    //\n    //\n    //\n\n    public function DelFile()\n    {\n\n        //\n        //\n        //\n\n        if(self::$pathScript \u0026\u0026 is_file(self::$pathScript)) {\n\n            if(@unlink(self::$pathScript)) {\n\n                $filescriptTmp = explode(\"/\", self::$pathScript);\n                $filescript = end($filescriptTmp);\n\n                //\n                //\n                //\n                self::$msgconcat .= \"Script:\" . PHP_EOL;\n                self::$msgconcat .= \"Removed [{$filescript}]!\" . PHP_EOL;\n                self::$msgconcat .= \"\".PHP_EOL;\n\n            } else {\n\n\n                $filescriptTmp = explode(\"/\", self::$pathScript);\n                $filescript = end($filescriptTmp);\n\n                //\n                //\n                //\n                self::$msgconcat .= \"Error:\" . PHP_EOL;\n                self::$msgconcat .= \"Error while trying to remove file:[{$filescript}]\" . PHP_EOL;\n                self::$msgconcat .= \"\".PHP_EOL;\n\n            }\n        }\n\n        return $this;\n    }\n\n    // \n    // \n    // \n\n    public function AddRepository($gitUser, $repository, $branch, $exec = true)\n    {\n\n        //\n        //\n        //\n\n        if(empty($repository) || empty($branch) || empty($gitUser)) {\n\n            //\n            //\n            // \n            self::$msgconcat .= \"Error: \" . PHP_EOL;\n            $msg = '{\"msg\":\"Repository, branch and gitUser Are mandatory, can not be empty\"}';\n            self::$msgconcat .= $msg;\n            self::$msgconcat .= \"\".PHP_EOL;\n\n            //\n            //\n            //\n\n            $this-\u003eLoadLog();\n\n            //\n            //\n            //\n\n            die($msg);\n\n\n        } else if($branch == \"master\") {\n\n            //\n            //\n            // \n            self::$msgconcat .= \"Error: \" . PHP_EOL;\n            $msg = '{\"msg\":\"The branch can not be the master\"}';\n            self::$msgconcat .= $msg;\n            self::$msgconcat .= \"\".PHP_EOL;\n\n            //\n            //\n            //\n\n            $this-\u003eLoadLog();\n\n            //\n            //\n            //\n\n            die($msg);\n\n        } else {\n\n            //\n            //\n            //\n\n            $path_projects = isset(ARRAY_PROJECT_GIT[\"gitwebhooks\"]) ? ARRAY_PROJECT_GIT[\"gitwebhooks\"] : \"\";\n\n            //\n            //\n            //\n\n            if(!$path_projects) {\n\n                //\n                //\n                // \n                self::$msgconcat .= \"Error: \" . PHP_EOL;\n                $msg = '{\"msg\":\"Directory not found, check the git.repositories file!!!\"}';\n                self::$msgconcat .= $msg;\n                self::$msgconcat .= \"\".PHP_EOL;\n\n                //\n                //\n                //\n\n                $this-\u003eLoadLog();\n\n                //\n                //\n                //\n                \n                die($msg);\n            }\n\n            //\n            //\n            //\n\n            $content_config = $repository. ' = '.$path_projects.'';\n\n\n            if(file_put_contents(PATH_REPOSITORY , PHP_EOL . PHP_EOL. $content_config, FILE_APPEND)) {\n\n                self::$msgconcat .= \"Successfully: \" . PHP_EOL;\n                self::$msgconcat .= \"Successfully created content in config git.repositories [$repository]\" . PHP_EOL;\n                self::$msgconcat .= \"\".PHP_EOL;\n\n                // \n                // load file again\n                // \n\n                $ARRAY_PROJECT_GIT = parse_ini_file(PATH_REPOSITORY); // replace again\n             \n                //\n                //\n                //\n\n                define(\"ARRAY_PROJECT_GIT\", $ARRAY_PROJECT_GIT);\n\n            } else {\n\n                //\n                //\n                // \n                self::$msgconcat .= \"error \" . PHP_EOL;\n                $msg = '{\"msg\":\"Error while creating directory ['.$path_projects.']\"}' . PHP_EOL;\n                self::$msgconcat .= $msg;\n                self::$msgconcat .= \"\".PHP_EOL;\n\n                $this-\u003eLoadLog();\n\n                die($msg);\n            }\n\n            // \n            // create line in file conf\n            // \n\n            $path_repository = \"{$path_projects}/{$branch}/{$repository}\";\n        }\n\n\n        $_ARRAY[\"REPOSITORY\"]   = $repository;\n\n        $_ARRAY[\"BRANCH\"]       = $branch;\n        \n        $_ARRAY[\"GITUSER\"]      = $gitUser;\n\n        $_ARRAY[\"PATH\"]         = isset(ARRAY_PROJECT_GIT[$repository]) ? ARRAY_PROJECT_GIT[$repository] : \"Erro, I did not find the repository in the git.repositories.conf.php file\";\n\n       //\n       // Repository can not exist\n       //\n\n       self::IsValidBranch($_ARRAY, true);\n\n       //\n       //\n       //\n\n       $modelo = \"repository\";\n\n       $this-\u003eLoadTemplate($_ARRAY, $modelo)\n         \n                -\u003eLoadFileScript() \n\n                -\u003eSave()\n\n                -\u003eExecute()\n\n                -\u003eLoadLog();\n\n        //\n        //\n        // \n        self::$msgconcat .= \"Created:\" . PHP_EOL;\n        $msg = '{\"msg\":\"Repository successfully created [' . $branch . \"/\" . $repository . ']\"}';\n        self::$msgconcat .= $msg;\n        self::$msgconcat .= \"\".PHP_EOL;\n\n        //\n        //\n        //\n\n        $this-\u003eLoadLog();\n\n        //\n        //\n        //\n        \n        die($msg);\n\n    }\n\n    //\n    //\n    //\n\n    public function IsValidBranch($_ARRAY, $valid=false) {\n\n        //\n        //\n        //\n        \n        $lastpos = strlen($_ARRAY[\"PATH\"]) - 1;\n        \n        if($_ARRAY[\"PATH\"]{$lastpos} != \"/\") {\n\n            $_ARRAY[\"PATH\"] .= \"/\";\n        }\n\n        //\n        //\n        //\n\n        $is_repository_exist = $_ARRAY[\"PATH\"] . \"{$_ARRAY[\"BRANCH\"]}/{$_ARRAY[\"REPOSITORY\"]}\";\n\n        if($valid) {\n\n            if(is_dir($is_repository_exist)) {\n\n                //\n                //\n                // \n                self::$msgconcat .= \"Created:\" . PHP_EOL;\n                $msg = '{\"msg\":\"Repository '.$is_repository_exist.' already exists!\"}';\n                self::$msgconcat .= $msg;\n                self::$msgconcat .= \"\".PHP_EOL;\n\n                //\n                //\n                //\n\n                $this-\u003eLoadLog();\n\n                //\n                //\n                //\n                \n                die($msg);                \n            }\n\n        } else {\n\n            if(!is_dir($is_repository_exist)) {\n\n                //\n                //\n                // \n                self::$msgconcat .= \"Created:\" . PHP_EOL;\n                $msg = '{\"msg\":\"Repository '.$is_repository_exist.' does not exist!\"}';\n                self::$msgconcat .= $msg;\n                self::$msgconcat .= \"\".PHP_EOL;\n\n                //\n                //\n                //\n\n                $this-\u003eLoadLog();\n\n                //\n                //\n                //\n                \n                die($msg);\n            }\n        }\n        \n\n        return null;\n    }\n\n    // \n    // \n    // \n\n    public function LoadLog()\n    {\n\n        //\n        //\n        //\n\n        $IP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : \"\";\n\n        //\n        //\n        //\n\n        $HTTP_USER_AGENT = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : \"\";\n\n        //\n        //\n        //\n        $msgtmp = PHP_EOL . \"--------------------------------------------------- START HERE --------------------------------------------------- \" . PHP_EOL;\n        $msgtmp .= date(\"Y-m-d [H:i]\") . \" [{$IP}] [{$HTTP_USER_AGENT}]\" . PHP_EOL;\n        $msgtmp .= \"\" . PHP_EOL;\n        self::$show_msg_load = $msgtmp . self::$msgconcat . PHP_EOL;\n\n        //\n        //\n        //\n\n        if(!file_put_contents(PATH_LOG, self::$show_msg_load, FILE_APPEND)) {\n\n            //\n            //\n            // \n            self::$msgconcat .= \"Error:\" . PHP_EOL;\n            $msg = '{\"msg\":\"Error writing log [' . PATH_LOG . ']\"}';\n            self::$msgconcat .= $msg;\n            self::$msgconcat .= \"\".PHP_EOL;\n\n            //\n            //\n            //\n\n            //$this-\u003eLoadLog();\n\n            //\n            //\n            //\n            \n            die($msg);\n        }\n\n        //\n        //\n        //\n\n        return $this;\n    }\n\n    // \n    // \n    // \n\n    public function Show()\n    {\n\n        //\n        //\n        //\n\n        if(self::$show_msg_load) {\n\n            //\n            //\n            //\n\n            print \"\\n\";\n            print self::$show_msg_load;\n            print \"\\n\";\n            \n        }\n        \n    }\n}\n\n```\n\n## Template .sh\n\nThe template you can create your own, the way you want, you can assemble your vector to build it dynamically.\n\nStandard Model:\n```php\n  [\n     \"REPOSITORY\" =\u003e $GitHub::$REPOSITORY,\n\n     \"PATH\"       =\u003e ARRAY_PROJECT_GIT[$GitHub::$REPOSITORY],\n\n     \"BRANCH\"     =\u003e $GitHub::$BRANCH,\n]\n\n```\nBut you can extend exactly here what you want for your scripts\n\n```php\n[\n   \"REPOSITORY\" =\u003e $GitHub::$REPOSITORY,\n\n   \"PATH\" =\u003e $ARRAY_PROJECT_GIT [$GitHub::$REPOSITORY],\n\n   \"BRANCH\" =\u003e $GitHub::$BRANCH,\n\n   \"CMD1\" =\u003e \"git checkout deploy\",\n\n   \"CMD2\" =\u003e \"git merge master\",\n\n   \"CMD3\" =\u003e 'echo \"Executing merge!\"'\n]\n\n```\n\nThis is our template, but you can create it the way you want it, confore your real need.\nWe leave some examples.\n\n```sh\n#!/bin/bash\n\n#\n# autor: @jeffotoni\n# about: Script to deploy our applications\n# date:  25/04/2017\n# since: Version 0.1\n#\n\necho \"\\nDeploy being done!!\"\n\n#\n#\n#\ncd `pwd`\n\n#\n#\n#\necho \"{REPOSITORY}\"\n\n#\n#\n#\ncd {PATH}{REPOSITORY}\n\n\n#\n#\n#\necho \"checkout $BRANCH\"\n\n\n#\n#\n#\ngit checkout {BRANCH}\n\n#\n#\ngit reset --hard HEAD\n\n\n#\n#\n#\necho \"Starting pull..\"\n\n#\n#\n#\ngit pull origin {BRANCH}\n\n\necho \"\\End deploy!!\"\necho \" ------------ \"\n\n```\n\nThis template is for deploy in a golang project\n\n\n```sh\n#!/bin/bash\n# autor: @jeffotoni\n# about: Script to deploy our applications\n# date:  25/04/2017\n# since: Version 0.1\n#\n\necho \"\\nDeploy Go(Golang) .. Being done!!\"\n\n#\n#\ncd `pwd`\n\n#\n#\necho \"{REPOSITORY}\"\n\n#\n#\ncd {PATH}{REPOSITORY}\n\n#\n#\necho \"checkout $BRANCH\"\n\n#\n#\n#git checkout {BRANCH}\n\n#\n#\ngit reset --hard HEAD\n\n#\n#\necho \"Starting pull..\"\n\n#\n#\ngit pull origin {BRANCH}\n\n#\n# stop process, id kill of program\n#\necho \"\\nKill all Process program!!\"\n\n#\n#\n#\nfor pid in $(ps -fe | grep {PROGRAM} | grep -v grep | awk '{print $2}'); do\n\n    if [ \"$(echo $pid | grep \"^[ [:digit:] ]*$\")\" ] \n        then\n\n        kill -9 \"$pid\"\n        echo \"\\nKill [$pid]\" \n    fi\ndone\n\n#\n#\n#\nfor pid2 in $(ps -C {PROGRAM} -o pid 2\u003e/dev/null); do\n\n\nif [ \"$(echo $pid2 | grep \"^[ [:digit:] ]*$\")\" ]\n    then\n    kill -9 $pid2\n    echo \"\\nkill [$pid2]\"\nfi\ndone\n\necho \"\\nDone!!!\"\n\necho \"go build {PROGRAM}.go\"\ngo build \"{PROGRAM}.go\"\n\n#echo \"go install {PROGRAM}\"\n#go install \"{PROGRAM}.go\"\n\necho \"\\nExecute {PROGRAM}\"\nexec ./{PROGRAM}\n\necho \"\\End deploy!!\"\necho \" ------------ \"\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeffotoni%2Fgitwebhooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeffotoni%2Fgitwebhooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeffotoni%2Fgitwebhooks/lists"}