Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rjw57/pydantic-gitlab-webhooks
Pydantic models for GitLab webhook events
https://github.com/rjw57/pydantic-gitlab-webhooks
gitlab pydantic pydantic-models python webhook
Last synced: about 1 month ago
JSON representation
Pydantic models for GitLab webhook events
- Host: GitHub
- URL: https://github.com/rjw57/pydantic-gitlab-webhooks
- Owner: rjw57
- License: mit
- Created: 2024-11-15T11:20:49.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-26T14:07:25.000Z (about 1 month ago)
- Last Synced: 2024-11-26T15:23:25.376Z (about 1 month ago)
- Topics: gitlab, pydantic, pydantic-models, python, webhook
- Language: Python
- Homepage: https://rjw57.github.io/pydantic-gitlab-webhooks/
- Size: 223 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Pydantic models for GitLab Webhooks
[![PyPI - Version](https://img.shields.io/pypi/v/pydantic-gitlab-webhooks)](https://pypi.org/p/pydantic-gitlab-webhooks/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pydantic-gitlab-webhooks)
[![GitHub Release](https://img.shields.io/github/v/release/rjw57/pydantic-gitlab-webhooks)](https://github.com/rjw57/pydantic-gitlab-webhooks/releases)
[![Test suite status](https://github.com/rjw57/pydantic-gitlab-webhooks/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/rjw57/pydantic-gitlab-webhooks/actions/workflows/main.yml?query=branch%3Amain)Module containing Pydantic models for validating bodies from [GitLab webhook
requests](https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html).## Documentation
The project documentation including an API reference can be found at
[https://rjw57.github.io/pydantic-gitlab-webhooks/](https://rjw57.github.io/pydantic-gitlab-webhooks/).## Usage example
Intended usage is via a single `validate_event_header_and_body` function which will
validate an incoming webhook's `X-Gitlab-Event` header and the body after being parsed
into a Python dict.```py
from pydantic import ValidationError
from pydantic_gitlab_webhooks import (
validate_event_body_dict,
validate_event_header_and_body_dict,
)event_body = {
"object_kind": "access_token",
"group": {
"group_name": "Twitter",
"group_path": "twitter",
"group_id": 35,
"full_path": "twitter"
},
"object_attributes": {
"user_id": 90,
"created_at": "2024-01-24 16:27:40 UTC",
"id": 25,
"name": "acd",
"expires_at": "2024-01-26"
},
"event_name": "expiring_access_token"
}# Use the value of the "X-Gitlab-Event" header and event body to validate
# the incoming event.
parsed_event = validate_event_header_and_body_dict(
"Resource Access Token Hook",
event_body
)
assert parsed_event.group.full_path == "twitter"# Invalid event bodies or hook headers raise Pydantic validation errors
try:
validate_event_header_and_body_dict("invalid hook", event_body)
except ValidationError:
pass # ok - expected error raised
else:
assert False, "ValidationError was not raised"# Event bodies can be parsed without the header hook if necessary although using
# the hook header is more efficient.
parsed_event = validate_event_body_dict(event_body)
assert parsed_event.group.full_path == "twitter"# Event models may be imported individually. For example:
from pydantic_gitlab_webhooks.events import GroupAccessTokenEventparsed_event = GroupAccessTokenEvent.model_validate(event_body)
```