[modules/gitlab] add module

This commit is contained in:
Clemens Beck 2023-02-19 02:37:13 +01:00
parent f77f5552ae
commit 7161ef211c
6 changed files with 127 additions and 0 deletions

View file

@ -0,0 +1,78 @@
# pylint: disable=C0111,R0903
"""
Displays the GitLab todo count:
* https://docs.gitlab.com/ee/user/todos.html
* https://docs.gitlab.com/ee/api/todos.html
Uses `xdg-open` or `x-www-browser` to open web-pages.
Requires the following library:
* requests
Errors:
if the GitLab todo query failed, the shown value is `n/a`
Parameters:
* gitlab.token: GitLab personal access token, the token needs to have the "read_api" scope.
* gitlab.host: Host of the GitLab instance, default is "gitlab.com".
* 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 core.decorators
import core.input
class Module(core.module.Module):
@core.decorators.every(minutes=5)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.gitlab))
self.background = True
self.__label = ""
self.__host = self.parameter("host", "gitlab.com")
self.__actions = []
actions = self.parameter("actions", "")
if actions:
self.__actions = util.format.aslist(actions)
self.__requests = requests.Session()
self.__requests.headers.update({"PRIVATE-TOKEN": self.parameter("token", "")})
cmd = "xdg-open"
if not shutil.which(cmd):
cmd = "x-www-browser"
core.input.register(
self,
button=core.input.LEFT_MOUSE,
cmd="{cmd} https:/{host}//dashboard/todos".format(
cmd=cmd, host=self.__host
),
)
def gitlab(self, _):
return self.__label
def update(self):
try:
url = "https://{host}/api/v4/todos".format(host=self.__host)
response = self.__requests.get(url)
todos = response.json()
if self.__actions:
todos = [t for t in todos if t["action_name"] in self.__actions]
self.__label = str(len(todos))
except Exception as e:
self.__label = "n/a"
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -0,0 +1 @@
requests

BIN
screenshots/gitlab.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

@ -0,0 +1,42 @@
import pytest
from unittest import TestCase, mock
import core.config
import core.widget
import modules.contrib.gitlab
from requests import Session
from requests.models import Response
pytest.importorskip("requests")
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: [
{"action_name": "assigned"},
{"action_name": "assigned"},
{"action_name": "approval_required"},
]
res.status_code = 200
return res
class TestGitlabUnit(TestCase):
def test_load_module(self):
__import__("modules.contrib.gitlab")
@mock.patch.object(Session, "get", return_value=mock_todo_api_response())
def test_unfiltered(self, _):
module = build_gitlab_module()
module.update()
assert module.widgets()[0].full_text() == "3"
@mock.patch.object(Session, "get", return_value=mock_todo_api_response())
def test_filtered(self, _):
module = build_gitlab_module(actions="approval_required")
module.update()
assert module.widgets()[0].full_text() == "1"

View file

@ -309,6 +309,9 @@
"github": {
"prefix": "github"
},
"gitlab": {
"prefix": "gitlab"
},
"deezer": {
"prefix": ""
},

View file

@ -581,6 +581,9 @@
"github": {
"prefix": "  "
},
"gitlab": {
"prefix": ""
},
"deezer": {
"prefix": "  "
},