From 79a585591e79d437fef4eea97eb2068a3624f259 Mon Sep 17 00:00:00 2001 From: iamprecieee Date: Thu, 20 Feb 2025 12:29:02 +0100 Subject: [PATCH 1/2] chore(slack): updated slack url retrieval functionality - added field for slack webhook url to json settings - updated analyzer and telex endpoint to retrieve slack url from settings - updated readme to reflect changes --- README.md | 6 +++--- src/config/integration_config.py | 20 ++++++++++++++------ src/core/analyzer.py | 8 ++++++-- src/routers/telex.py | 4 ++-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 467e830..51457ab 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ Receives commit messages from Telex and sends analysis results to slack. ### Integration Config ``` -GET /api/v1/webhook/telex/integration/json +GET /integration.json ``` Returns integration configuration for Telex. @@ -239,8 +239,8 @@ You can customize the analyzer through Telex integration settings: #### Example Commits ```json { - "feat": "feat(auth): implement OAuth2 with role-based access", - "fix": "fix(api): resolve data race in concurrent requests" + "feat": "feat(auth): implement OAuth2 with role-based access\n\nImplemented OAuth2 protocol with role-based control to enhance security and scalability.", + "fix": "fix(api): resolve data race in concurrent requests\n\nFixed a race condition by adding synchronization mechanisms to prevent concurrent data modifications." } ``` diff --git a/src/config/integration_config.py b/src/config/integration_config.py index 111e5f4..b7ffabd 100644 --- a/src/config/integration_config.py +++ b/src/config/integration_config.py @@ -25,26 +25,34 @@ def generate_json_config(): "Instant notifications when commits need attention", "Easy setup with pre-configured commit patterns" ], + "website": settings.app_logo_url, "author": "iamprecieee", "settings": [ { - "label": "Commit Types", + "label": "slack_url", + "type": "text", + "required": True, + "description": "Slack Webhook URL", + "default": "https://slack.com" + }, + { + "label": "commit_types", "type": "text", - "required": True, + "required": False, "description": "Provide custom commit types mapped to keywords that indicate type of change. Format: {'type': ['keyword1', 'keyword2']}. Example: {'docs': ['document', 'readme']} means commits with 'document' or 'readme' suggest documentation changes.", "default": "{'feat': ['add', 'implement', 'new', 'introduce'], 'fix': ['fix', 'resolve', 'patch', 'address']}" }, { - "label": "Example Commits", + "label": "example_commits", "type": "text", - "required": True, + "required": False, "description": "Set example commits for each custom commit type to guide new devs. These appear in suggestions when similar commits need fixing. Format: {'type1': 'example message1', 'type2': 'example message 2'}.", "default": "{'feat': 'feat(auth): implement OAuth2 with role-based access\n\nImplemented OAuth2 protocol with role-based control to enhance security and scalability.', 'fix': 'fix(api): resolve data race in concurrent requests\n\nFixed a race condition by adding synchronization mechanisms to prevent concurrent data modifications.'}" }, { - "label": "Training Data", + "label": "training_data", "type": "text", - "required": True, + "required": False, "description": "Add custom data to train the analyzer with commits that match preferred style. More examples = better suggestions. Format: {'type1': ['example1', 'example2'], 'type2': ['example3', 'example4']}. The analyzer learns from these to better match preferred conventions.", "default": "{'feat': ['feat(auth): implement OAuth2 with role-based access\n\nImplemented OAuth2 protocol with role-based control to enhance security and scalability.','feat(search): implement elasticsearch integration\n\nIntegrated Elasticsearch to boost search performance and enhance result accuracy.']}" } diff --git a/src/core/analyzer.py b/src/core/analyzer.py index 3db4dee..a3418bf 100644 --- a/src/core/analyzer.py +++ b/src/core/analyzer.py @@ -27,6 +27,8 @@ def __init__(self, settings: list): self.example_commits = example_commits.copy() self.commit_training_data = commit_training_data.copy() self.semantic_patterns = semantic_patterns.copy() + + self.slack_url = None # Retrieved from settings try: self._apply_data_settings() @@ -46,10 +48,12 @@ def _apply_data_settings(self): for setting in self.settings: if setting["label"] == "commit_types": self.commit_types.update(ast.literal_eval(setting["default"])) - elif setting["label"] == "example_commits": + if setting["label"] == "example_commits": self.example_commits.update(ast.literal_eval(setting["default"])) - elif setting["label"] == "training_data": + if setting["label"] == "training_data": self.commit_training_data.update(ast.literal_eval(setting["default"])) + if setting["label"] == "slack_url": + self.slack_url = setting["default"] def _prepare_ml_classifier(self): """ diff --git a/src/routers/telex.py b/src/routers/telex.py index 79e5890..83fdecc 100644 --- a/src/routers/telex.py +++ b/src/routers/telex.py @@ -2,7 +2,6 @@ from ..core.models import TelexTargetPayload from ..core.analyzer import CommitAnalyzer from ..config.integration_config import generate_json_config -from ..config.config import settings from fastapi.responses import JSONResponse from fastapi import status, HTTPException, Query from typing import Annotated @@ -31,6 +30,7 @@ async def telex_webhook( ) try: analyzer = CommitAnalyzer(settings=payload.settings) + slack_url = analyzer.slack_url for commit in commit_message: violations = analyzer.analyze_commit(commit["message"]) if violations: @@ -42,7 +42,7 @@ async def telex_webhook( status_code=status.HTTP_200_OK, ) async with httpx.AsyncClient() as client: - await client.post(settings.slack_url, json=output_message) + await client.post(slack_url, json=output_message) except Exception as e: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, From 1318930b207e0125aa17306d0fd817e617070115 Mon Sep 17 00:00:00 2001 From: iamprecieee Date: Thu, 20 Feb 2025 12:47:16 +0100 Subject: [PATCH 2/2] fix(settings): add check for empty settings values - make no change to data if settings value is empty - update telex test to handle cases of empty value data and include slack url - remove slack_url field from config --- src/core/analyzer.py | 6 +++--- tests/test_telex.py | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/analyzer.py b/src/core/analyzer.py index a3418bf..ad7ab51 100644 --- a/src/core/analyzer.py +++ b/src/core/analyzer.py @@ -47,11 +47,11 @@ def _apply_data_settings(self): """ for setting in self.settings: if setting["label"] == "commit_types": - self.commit_types.update(ast.literal_eval(setting["default"])) + self.commit_types.update(ast.literal_eval(setting["default"])) if setting["default"] else self.commit_types if setting["label"] == "example_commits": - self.example_commits.update(ast.literal_eval(setting["default"])) + self.example_commits.update(ast.literal_eval(setting["default"])) if setting["default"] else self.example_commits if setting["label"] == "training_data": - self.commit_training_data.update(ast.literal_eval(setting["default"])) + self.commit_training_data.update(ast.literal_eval(setting["default"])) if setting["default"] else self.commit_training_data if setting["label"] == "slack_url": self.slack_url = setting["default"] diff --git a/tests/test_telex.py b/tests/test_telex.py index 971ebdb..b0b4b20 100644 --- a/tests/test_telex.py +++ b/tests/test_telex.py @@ -37,12 +37,19 @@ def test_send_from_telex_failure(): json={ "message": '[{"id": "8ce4cf04f4rw6w8600675237350b14b4", "message": "fix(auth): child\n\nFixed a race condcvghdczhjvjhzcvhjvzhjvhjvczjonization mechanisms.", "timestamp": "2025-02-18T10:17:54+01:00", "url": "https://github.com/8", "author": {"name": "test", "email": "test@gmail.com"}}]', "settings": [ + { + "label": "slack_url", + "type": "text", + "required": True, + "description": "Slack Webhook URL", + "default": "https://slack.com" + }, { "label": "commit_types", "type": "text", "description": "Custom commit types and keywords", "required": False, - "default": "{'feat': ['add', 'implement', 'new', 'introduce'], 'fix': ['fix', 'resolve', 'patch', 'address']}", + "default": "", }, { "label": "Example Commits",