Merge pull request #968 from Duarte-Figueiredo/main

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:
tobi-wan-kenobi 2023-05-06 12:01:24 +02:00 committed by GitHub
commit bdfc4fdab4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) * gitlab.actions: Comma separated actions to be parsed (e.g.: gitlab.actions=assigned,approval_required)
""" """
import json
import requests
import shutil import shutil
import util
import core.module import requests
import core.widget
import core.decorators import core.decorators
import core.input import core.input
import core.module
import core.widget
import util
class Module(core.module.Module): class Module(core.module.Module):
@ -74,5 +74,14 @@ class Module(core.module.Module):
except Exception as e: except Exception as e:
self.__label = "n/a" 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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,13 +1,12 @@
import pytest
from unittest import TestCase, mock from unittest import TestCase, mock
import pytest
from requests import Session
import core.config import core.config
import core.widget import core.widget
import modules.contrib.gitlab import modules.contrib.gitlab
from requests import Session
from requests.models import Response
pytest.importorskip("requests") pytest.importorskip("requests")
@ -15,6 +14,7 @@ def build_gitlab_module(actions=""):
config = core.config.Config(["-p", "gitlab.actions={}".format(actions)]) config = core.config.Config(["-p", "gitlab.actions={}".format(actions)])
return modules.contrib.gitlab.Module(config=config, theme=None) return modules.contrib.gitlab.Module(config=config, theme=None)
def mock_todo_api_response(): def mock_todo_api_response():
res = mock.Mock() res = mock.Mock()
res.json = lambda: [ res.json = lambda: [
@ -25,6 +25,7 @@ def mock_todo_api_response():
res.status_code = 200 res.status_code = 200
return res return res
class TestGitlabUnit(TestCase): class TestGitlabUnit(TestCase):
def test_load_module(self): def test_load_module(self):
__import__("modules.contrib.gitlab") __import__("modules.contrib.gitlab")
@ -40,3 +41,30 @@ class TestGitlabUnit(TestCase):
module = build_gitlab_module(actions="approval_required") module = build_gitlab_module(actions="approval_required")
module.update() module.update()
assert module.widgets()[0].full_text() == "1" 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) == []