|
| 1 | +# Git Commit Quality Monitor |
| 2 | + |
| 3 | +A smart Telex integration that helps teams maintain high-quality git commit messages using ML-powered analysis and real-time feedback. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Git Commit Quality Monitor analyzes commit messages in real-time, providing instant feedback on commit quality and suggestions for improvement. It uses machine learning to understand commit patterns and provides customized suggestions based on conventional commit standards and the development team's preferences. |
| 8 | + |
| 9 | +### Key Features |
| 10 | + |
| 11 | +- ⚡️ Real-time feedback through Slack |
| 12 | +- 🎯 Customizable commit rules/conventions |
| 13 | +- 🔄 GitHub webhook integration |
| 14 | +- 🎨 Telex integration support |
| 15 | +- 🤖 Smart ML-powered commit message analysis and suggestions |
| 16 | + |
| 17 | +## Quick Start Guide |
| 18 | + |
| 19 | +### Prerequisites |
| 20 | + |
| 21 | +- Python 3.8+ |
| 22 | +- FastAPI |
| 23 | +- scikit-learn |
| 24 | +- Git (for development) |
| 25 | +- Telex account |
| 26 | +- GitHub repository access |
| 27 | +- Slack workspace (for notifications) |
| 28 | + |
| 29 | +### Basic Setup |
| 30 | + |
| 31 | +1. Clone the repository: |
| 32 | +```bash |
| 33 | +git clone <repository-url> |
| 34 | +cd commit-quality-monitor |
| 35 | +``` |
| 36 | + |
| 37 | +2. Set up a virtual environment: |
| 38 | +```bash |
| 39 | +python -m venv venv |
| 40 | +source venv/bin/activate # Linux/Mac |
| 41 | +# or |
| 42 | +.\venv\Scripts\activate # Windows |
| 43 | +``` |
| 44 | + |
| 45 | +3. Install dependencies: |
| 46 | +```bash |
| 47 | +pip install -r requirements.txt |
| 48 | +``` |
| 49 | + |
| 50 | +4. Configure environment variables: |
| 51 | +```bash |
| 52 | +cp .env.example .env |
| 53 | +# Edit .env with your configurations |
| 54 | +``` |
| 55 | + |
| 56 | +### Integration Setup |
| 57 | + |
| 58 | +1. Configure repository webhooks with specific settings: |
| 59 | + - Go to your repository settings |
| 60 | + - Add webhook: `<your-app-url>/api/v1/webhook/github/{telex_channel_id}` |
| 61 | + - Set content type to `application/json` |
| 62 | + - Select "Just the push event" |
| 63 | + |
| 64 | +2. Configure Slack notifications: |
| 65 | + - Create a Slack app |
| 66 | + - Add Incoming Webhooks |
| 67 | + - Add the generated webhook URL to your environment variables |
| 68 | + |
| 69 | +## Detailed Configuration |
| 70 | + |
| 71 | +| Variable | Description | Example | |
| 72 | +|----------|-------------|---------| |
| 73 | +| ALLOWED_ORIGINS | Comma-separated list of allowed origins | https://github.com,http://localhost:8000 | |
| 74 | +| ALLOWED_HOSTS | Comma-separated list of allowed hosts | github.com,localhost | |
| 75 | +| HOST | Server host | 0.0.0.0 | |
| 76 | +| PORT | Server port | 8000 | |
| 77 | +| TELEX_WEBHOOK_URL | Telex webhook URL | https://ping.telex.im/v1/webhooks | |
| 78 | +| SLACK_URL | Slack webhook URL for notifications | https://hooks.slack.com/... | |
| 79 | +| APP_LOGO_URL | URL for app logo | https://example.com/logo.png | |
| 80 | +| APP_URL | Application URL | https://your-app.com | |
| 81 | +| TARGET_URL | Telex target URL | https://your-app.com/webhook/telex | |
| 82 | + |
| 83 | +## System Architecture |
| 84 | + |
| 85 | +### Project Structure |
| 86 | + |
| 87 | +``` |
| 88 | +project_root/ |
| 89 | +├── src/ |
| 90 | +│ ├── core/ # Core Analysis Engine |
| 91 | +│ │ ├── analyzer.py # ML-based commit analysis logic |
| 92 | +│ │ └── models.py # Data models and structure |
| 93 | +│ │ |
| 94 | +│ ├── config/ # Configuration Management |
| 95 | +│ │ ├── data.py # Training data, patterns, and examples |
| 96 | +│ │ ├── config.py # Environment settings management |
| 97 | +│ │ ├── integration_config.py # Telex integration configuration |
| 98 | +│ │ └── middleware.py # CORS and trusted host middleware |
| 99 | +│ │ |
| 100 | +│ ├── routers/ # API Routing Layer |
| 101 | +│ │ ├── github.py # GitHub webhook endpoint handling |
| 102 | +│ │ ├── telex.py # Telex webhook and integration |
| 103 | +│ │ └── router.py # Main router configuration |
| 104 | +│ │ |
| 105 | +│ └── utils/ # Utility Functions |
| 106 | +│ └── telex_utils.py # Telex communication helpers |
| 107 | +│ |
| 108 | +├── tests/ # Test Suite |
| 109 | +│ ├── __init__.py # Test configuration |
| 110 | +│ ├── test_github.py # GitHub integration tests |
| 111 | +│ └── test_telex.py # Telex integration tests |
| 112 | +│ |
| 113 | +├── .env.example # Environment variable template |
| 114 | +├── main.py # Application entry point |
| 115 | +├── requirements.txt # Project dependencies |
| 116 | +└── README.md # Project documentation |
| 117 | +``` |
| 118 | + |
| 119 | +### Core Analysis Engine |
| 120 | + |
| 121 | +The system implements a multi-step process to evaluate the quality of commit messages: |
| 122 | + |
| 123 | +#### Direct Pattern Matching |
| 124 | + - Matches against predefined commit types |
| 125 | + - Provides immediate classification |
| 126 | + - Optimized for standard conventions |
| 127 | + |
| 128 | +```python |
| 129 | +commit_types = { |
| 130 | + "feat": ["add", "implement", "new", "introduce"], |
| 131 | + "fix": ["fix", "resolve", "patch", "address"], |
| 132 | + "docs": ["document", "update docs", "readme"], |
| 133 | + "refactor": ["refactor", "restructure", "simplify"] |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +#### Machine Learning Classification |
| 138 | + - Uses TF-IDF vectorization to transform commit messages into numerical vectors |
| 139 | + - Maintains a training dataset of exemplar commits |
| 140 | + - Employs cosine similarity analysis to compute similarity scores against known patterns |
| 141 | + - Suggests types based on highest similarity matches |
| 142 | + |
| 143 | +```python |
| 144 | +def _prepare_ml_classifier(self): |
| 145 | + x_train = [] # Commit messages |
| 146 | + y_train = [] # Corresponding types |
| 147 | + |
| 148 | + for commit_type, messages in self.commit_training_data.items(): |
| 149 | + x_train.extend(messages) |
| 150 | + y_train.extend([commit_type] * len(messages)) |
| 151 | + |
| 152 | + self.vectorizer.fit(x_train) |
| 153 | + self.x_train_vectorized = self.vectorizer.transform(x_train) |
| 154 | +``` |
| 155 | + |
| 156 | +#### Semantic Analysis |
| 157 | + - Recognizes complex patterns |
| 158 | + - Understands contextual relationships |
| 159 | + - Handles non-standard commits |
| 160 | + |
| 161 | +```python |
| 162 | +semantic_patterns = { |
| 163 | + "feat": [ |
| 164 | + ("create", "new"), |
| 165 | + ("add", "feature"), |
| 166 | + ("implement", "support") |
| 167 | + ], |
| 168 | + "fix": [ |
| 169 | + ("resolve", "issue"), |
| 170 | + ("patch", "vulnerability"), |
| 171 | + ("correct", "behavior") |
| 172 | + ] |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +#### Content Quality |
| 177 | +- It verifies that the commit message contains enough words. Messages with fewer than 5 words are flagged with a high-severity warning, while those with 5–9 words are flagged with a medium-severity warning. |
| 178 | +- Scans the commit message for words that might be gibberish. |
| 179 | + |
| 180 | +#### Context Evaluation |
| 181 | +- Ensures that the commit message provides adequate context. It looks for a clear separation between the subject line and the detailed body (detected via a double newline \n\n). If this separation is missing, the method suggests splitting the message to improve clarity. |
| 182 | + |
| 183 | +## API Documentation |
| 184 | + |
| 185 | +### GitHub Webhook Endpoint |
| 186 | +```http |
| 187 | +POST /api/v1/webhook/github/{telex_channel_id} |
| 188 | +Content-Type: application/json |
| 189 | +
|
| 190 | +{ |
| 191 | + "pusher": {"name": "username"}, |
| 192 | + "commits": [ |
| 193 | + { |
| 194 | + "id": "commit_hash", |
| 195 | + "message": "commit_message", |
| 196 | + "timestamp": "iso_timestamp" |
| 197 | + } |
| 198 | + ] |
| 199 | +} |
| 200 | +``` |
| 201 | +Receives GitHub push events and forwards to Telex. |
| 202 | + |
| 203 | +### Telex Integration Endpoint |
| 204 | +```http |
| 205 | +POST /api/v1/webhook/telex |
| 206 | +Content-Type: application/json |
| 207 | +
|
| 208 | +{ |
| 209 | + "message": "serialized_commit_data", |
| 210 | + "settings": [ |
| 211 | + { |
| 212 | + "label": "commit_types", |
| 213 | + "type": "text", |
| 214 | + "default": "{...}" |
| 215 | + } |
| 216 | + ] |
| 217 | +} |
| 218 | +``` |
| 219 | +Receives commit messages from Telex and sends analysis results to slack. |
| 220 | + |
| 221 | +### Integration Config |
| 222 | +``` |
| 223 | +GET /integration.json |
| 224 | +``` |
| 225 | +Returns integration configuration for Telex. |
| 226 | + |
| 227 | +### Customizing Commit Analysis |
| 228 | + |
| 229 | +You can customize the analyzer through Telex integration settings: |
| 230 | + |
| 231 | +#### Commit Types |
| 232 | +```json |
| 233 | +{ |
| 234 | + "feat": ["add", "implement", "new"], |
| 235 | + "fix": ["fix", "resolve", "patch"] |
| 236 | +} |
| 237 | +``` |
| 238 | + |
| 239 | +#### Example Commits |
| 240 | +```json |
| 241 | +{ |
| 242 | + "feat": "feat(auth): implement OAuth2 with role-based access\n\nImplemented OAuth2 protocol with role-based control to enhance security and scalability.", |
| 243 | + "fix": "fix(api): resolve data race in concurrent requests\n\nFixed a race condition by adding synchronization mechanisms to prevent concurrent data modifications." |
| 244 | +} |
| 245 | +``` |
| 246 | + |
| 247 | +#### Training Data |
| 248 | +```json |
| 249 | +{ |
| 250 | + "feat": [ |
| 251 | + "feat(ui): add dark mode toggle", |
| 252 | + "feat(api): implement rate limiting" |
| 253 | + ] |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +## Development Guide |
| 258 | + |
| 259 | +### Running the Application |
| 260 | +```bash |
| 261 | +uvicorn main:app --reload |
| 262 | +``` |
| 263 | + |
| 264 | +### Testing |
| 265 | +```bash |
| 266 | +pytest tests/ |
| 267 | +``` |
| 268 | + |
| 269 | +### Contributing |
| 270 | + |
| 271 | +To contribute to the project: |
| 272 | +1. Fork the repository |
| 273 | +2. Create a feature branch |
| 274 | +3. Commit changes following our guidelines |
| 275 | +4. Push to your branch |
| 276 | +5. Submit a Pull Request |
| 277 | + |
| 278 | + |
| 279 | + |
| 280 | +--- |
| 281 | + |
| 282 | +📦 GitHub Push → 🔄 Telex → 🧠 Analysis/Suggestions Pipeline (🔍 → 🤖 → 🎯) → 📱 Slack Alert |
0 commit comments