Skip to content

Commit bd77150

Browse files
authored
fix: Update the alerting capitalization functionality (#21)
1 parent 1402189 commit bd77150

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

grafana_api/legacy_alerting.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
import re
34

45
from .model import APIModel, APIEndpoints, RequestsMethods
56
from .api import Api
@@ -114,11 +115,21 @@ def get_alert_by_id(self, id: int) -> dict:
114115
api_call (dict): Returns an alert
115116
"""
116117

118+
def _to_camel_case(input_value: str) -> str:
119+
content = re.findall('[A-Z][^A-Z]*', input_value)
120+
if content != list():
121+
if len(content) != 1:
122+
return content[0].lower() + "".join(content[1:])
123+
else:
124+
return content[0].lower()
125+
return input_value
126+
117127
if id != 0:
118-
api_call: dict = Api(self.grafana_api_model).call_the_api(
128+
api_call_raw: dict = Api(self.grafana_api_model).call_the_api(
119129
f"{APIEndpoints.LEGACY_ALERTS.value}/{id}",
120130
RequestsMethods.GET,
121131
)
132+
api_call: dict = {_to_camel_case(k): v for k, v in api_call_raw.items()}
122133

123134
if api_call == dict() or api_call.get("id") is None:
124135
logging.error(f"Check the error: {api_call}.")

tests/unittests/test_legacy_alerting.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ def test_get_alert_by_id(self, call_the_api_mock):
7979

8080
self.assertEqual(dict({"id": "test"}), alerting.get_alert_by_id(1))
8181

82+
@patch("grafana_api.api.Api.call_the_api")
83+
def test_get_alert_by_id_upper_case_id_result(self, call_the_api_mock):
84+
# ISSUE: https://github.com/grafana/grafana/issues/51141
85+
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
86+
alerting: Alerting = Alerting(grafana_api_model=model)
87+
88+
call_the_api_mock.return_value = dict({"Id": "test", "PanelId": 112, "NewStateDate": "2022-06-17T09:34:08Z"})
89+
90+
self.assertEqual(dict({"id": "test", "panelId": 112, "newStateDate": "2022-06-17T09:34:08Z"}),
91+
alerting.get_alert_by_id(1))
92+
8293
def test_get_alert_by_id_no_id(self):
8394
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
8495
alerting: Alerting = Alerting(grafana_api_model=model)

0 commit comments

Comments
 (0)