gwh


Namegwh JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/ahebrank/gitlab-webhook-handler
SummaryWebhook Handler for GitLab
upload_time2023-01-18 21:03:19
maintainer
docs_urlNone
authorAndy Hebrank
requires_python
licenseApache License, Version 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flask webhook for GitLab

A very simple post-receive web hook handler for GitLab based on a project for Github: [razius/github-webhook-handler](https://github.com/razius/github-webhook-handler)

It will optionally verify that the POST request originated from a particular IP address.

## Getting started

### Installation

```bash
pip install gwh
```

### Repository Configuration

Create a JSON config file (e.g., `repos.json`) to configure repositories. Each repository must be keyed by its GitLab homepage.

```json
{
    "https://gitlab.com/pal/spm-batching": {
        "private_token": "xxxxxxxxxxxxx",
        "webhook_token": "xxxxxxxxxxxxx",
        "push": {
            "master": {
                "path": "/home/spm-batching/deploy",
                "actions": [
                  "git checkout master",
                  "git pull"
                ]
            },
            "other": {
                "actions": [
                    "echo A non-master branch was pushed."
                ]
            }
        }
    },
        "issue": {
            "user_notify": [
                "\\*\\*Sender\\*\\*\\: ([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+)",
                "@gitlabuser"
            ],
            "labels": []
        }
    }
}
```

This example handles several types of webhooks.

1. `push`: After a push event to the repo `master` branch, it executes a couple of command in the local shell to pull in the master HEAD. The special branch key `other` will run if the pushed branch is not otherwise matched to a key in the `push` hash.
2. `issue.user_notify`: After a new issue event, the handler runs a regex match on the issue body and adds an issue comment to @mention the user by email.
3. `issue.labels`: Parse the commit message to handle adding or removing labels based on commit messages. For instance, "address #53, #72: carousel accessibility fixes; -browser compat, +accessibility, ~Pending" will add an "accessibility" label (if it already exists for the project) to issues 53 and 72, remove label "browser compat", and add label "Pending" (presumably a list label) while removing other list labels (effectively moving an issue on the GitLab boards kanban).

The issue hook uses a [GitLab personal access token](https://docs.gitlab.com/ee/api/#personal-access-tokens) (`private_token`) to run API commands to lookup a username by email and add an issue comment. The push hook does not require the private token because it does not use the GitLab API.

`webhook_token` allows authorization by matching against a [secret token included in the payload headers](https://docs.gitlab.com/ce/user/project/integrations/webhooks.html#secret-token).

### Run

```
python -m gwh --help
```

## Example usage:

Assume a self-hosted GitLab server on 192.168.1.44 and a production webserver on 192.168.1.99.  The idea is that a push to the master branch for your project should trigger the production machine to pull in a new copy of the repo.

On your webhost (192.168.1.99), bring up the webhook handler:

```
python -m gwh -p 8080 --allow 192.168.1.44 repos.json &
```

Then in your GitLab server (on 192.168.1.44) project settings, create a new webhook with URL http://192.168.1.99:8080 that's triggered on push events.

## Test

```
curl -i -X POST -H "Content-Type: application/json" --data "@test.json" http://localhost:8080
```




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ahebrank/gitlab-webhook-handler",
    "name": "gwh",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Andy Hebrank",
    "author_email": "ahebrank@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c2/98/ecfb324c3d3531df68e8e07c2fcf58295499d4fb772a3fde0e8cf7cbe505/gwh-1.0.2.tar.gz",
    "platform": null,
    "description": "# Flask webhook for GitLab\n\nA very simple post-receive web hook handler for GitLab based on a project for Github: [razius/github-webhook-handler](https://github.com/razius/github-webhook-handler)\n\nIt will optionally verify that the POST request originated from a particular IP address.\n\n## Getting started\n\n### Installation\n\n```bash\npip install gwh\n```\n\n### Repository Configuration\n\nCreate a JSON config file (e.g., `repos.json`) to configure repositories. Each repository must be keyed by its GitLab homepage.\n\n```json\n{\n    \"https://gitlab.com/pal/spm-batching\": {\n        \"private_token\": \"xxxxxxxxxxxxx\",\n        \"webhook_token\": \"xxxxxxxxxxxxx\",\n        \"push\": {\n            \"master\": {\n                \"path\": \"/home/spm-batching/deploy\",\n                \"actions\": [\n                  \"git checkout master\",\n                  \"git pull\"\n                ]\n            },\n            \"other\": {\n                \"actions\": [\n                    \"echo A non-master branch was pushed.\"\n                ]\n            }\n        }\n    },\n        \"issue\": {\n            \"user_notify\": [\n                \"\\\\*\\\\*Sender\\\\*\\\\*\\\\: ([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\\\.[a-zA-Z0-9-.]+)\",\n                \"@gitlabuser\"\n            ],\n            \"labels\": []\n        }\n    }\n}\n```\n\nThis example handles several types of webhooks.\n\n1. `push`: After a push event to the repo `master` branch, it executes a couple of command in the local shell to pull in the master HEAD. The special branch key `other` will run if the pushed branch is not otherwise matched to a key in the `push` hash.\n2. `issue.user_notify`: After a new issue event, the handler runs a regex match on the issue body and adds an issue comment to @mention the user by email.\n3. `issue.labels`: Parse the commit message to handle adding or removing labels based on commit messages. For instance, \"address #53, #72: carousel accessibility fixes; -browser compat, +accessibility, ~Pending\" will add an \"accessibility\" label (if it already exists for the project) to issues 53 and 72, remove label \"browser compat\", and add label \"Pending\" (presumably a list label) while removing other list labels (effectively moving an issue on the GitLab boards kanban).\n\nThe issue hook uses a [GitLab personal access token](https://docs.gitlab.com/ee/api/#personal-access-tokens) (`private_token`) to run API commands to lookup a username by email and add an issue comment. The push hook does not require the private token because it does not use the GitLab API.\n\n`webhook_token` allows authorization by matching against a [secret token included in the payload headers](https://docs.gitlab.com/ce/user/project/integrations/webhooks.html#secret-token).\n\n### Run\n\n```\npython -m gwh --help\n```\n\n## Example usage:\n\nAssume a self-hosted GitLab server on 192.168.1.44 and a production webserver on 192.168.1.99.  The idea is that a push to the master branch for your project should trigger the production machine to pull in a new copy of the repo.\n\nOn your webhost (192.168.1.99), bring up the webhook handler:\n\n```\npython -m gwh -p 8080 --allow 192.168.1.44 repos.json &\n```\n\nThen in your GitLab server (on 192.168.1.44) project settings, create a new webhook with URL http://192.168.1.99:8080 that's triggered on push events.\n\n## Test\n\n```\ncurl -i -X POST -H \"Content-Type: application/json\" --data \"@test.json\" http://localhost:8080\n```\n\n\n\n",
    "bugtrack_url": null,
    "license": "Apache License, Version 2.0",
    "summary": "Webhook Handler for GitLab",
    "version": "1.0.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "38ea7a0bfce559ea4e66c8cd22f1de4623d9e1a42eeb18bd8e73bdcb77a8c40b",
                "md5": "132d96bac71fedbc0669fc36badb371b",
                "sha256": "a41cb4d5d7990f0734e6fe0ddad1f37732e443d86a8f35a044221eac9627bfbf"
            },
            "downloads": -1,
            "filename": "gwh-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "132d96bac71fedbc0669fc36badb371b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7187,
            "upload_time": "2023-01-18T21:03:17",
            "upload_time_iso_8601": "2023-01-18T21:03:17.667518Z",
            "url": "https://files.pythonhosted.org/packages/38/ea/7a0bfce559ea4e66c8cd22f1de4623d9e1a42eeb18bd8e73bdcb77a8c40b/gwh-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c298ecfb324c3d3531df68e8e07c2fcf58295499d4fb772a3fde0e8cf7cbe505",
                "md5": "dd4f8ad258ba6ab2d08e098c01ab07ef",
                "sha256": "69c2f151289a6b8015adacff34c64a984c5c8e6abf6cb22cb61f2e08611fed9c"
            },
            "downloads": -1,
            "filename": "gwh-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "dd4f8ad258ba6ab2d08e098c01ab07ef",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6312,
            "upload_time": "2023-01-18T21:03:19",
            "upload_time_iso_8601": "2023-01-18T21:03:19.133173Z",
            "url": "https://files.pythonhosted.org/packages/c2/98/ecfb324c3d3531df68e8e07c2fcf58295499d4fb772a3fde0e8cf7cbe505/gwh-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-18 21:03:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "ahebrank",
    "github_project": "gitlab-webhook-handler",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "gwh"
}
        
Elapsed time: 0.02924s