Updated gitlab module to have state of warning when there is at least 1 notification, just like the github module

This commit is contained in:
Duarte Figueiredo 2023-05-06 10:56:17 +01:00
parent 1dd39a4e43
commit a6de61b751
2 changed files with 46 additions and 9 deletions

View file

@ -20,15 +20,15 @@ Parameters:
* gitlab.actions: Comma separated actions to be parsed (e.g.: gitlab.actions=assigned,approval_required)
"""
import json
import requests
import shutil
import util
import core.module
import core.widget
import requests
import core.decorators
import core.input
import core.module
import core.widget
import util
class Module(core.module.Module):
@ -74,5 +74,14 @@ class Module(core.module.Module):
except Exception as e:
self.__label = "n/a"
def state(self, widget):
state = []
try:
if int(self.__label) > 0:
state.append("warning")
except ValueError:
pass
return state
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,13 +1,12 @@
import pytest
from unittest import TestCase, mock
import pytest
from requests import Session
import core.config
import core.widget
import modules.contrib.gitlab
from requests import Session
from requests.models import Response
pytest.importorskip("requests")
@ -15,6 +14,7 @@ def build_gitlab_module(actions=""):
config = core.config.Config(["-p", "gitlab.actions={}".format(actions)])
return modules.contrib.gitlab.Module(config=config, theme=None)
def mock_todo_api_response():
res = mock.Mock()
res.json = lambda: [
@ -25,6 +25,7 @@ def mock_todo_api_response():
res.status_code = 200
return res
class TestGitlabUnit(TestCase):
def test_load_module(self):
__import__("modules.contrib.gitlab")
@ -40,3 +41,30 @@ class TestGitlabUnit(TestCase):
module = build_gitlab_module(actions="approval_required")
module.update()
assert module.widgets()[0].full_text() == "1"
@mock.patch.object(Session, "get", return_value=mock_todo_api_response())
def test_state_warning(self, _):
module = build_gitlab_module(actions="approval_required")
module.update()
assert module.state(None) == ["warning"]
@mock.patch.object(Session, "get", return_value=mock_todo_api_response())
def test_state_normal(self, _):
module = build_gitlab_module(actions="empty_filter")
module.update()
assert module.state(None) == []
@mock.patch.object(Session, "get", return_value=mock_todo_api_response())
def test_state_normal_before_update(self, _):
module = build_gitlab_module(actions="approval_required")
assert module.state(None) == []
@mock.patch.object(Session, "get", side_effect=Exception("Something went wrong"))
def test_state_normal_if_na(self, _):
module = build_gitlab_module(actions="approval_required")
module.update()
assert module.state(None) == []