• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uwefladrich / scriptengine-tasks-ecearth / 14861206140

06 May 2025 01:41PM UTC coverage: 96.771% (-0.4%) from 97.128%
14861206140

push

github

web-flow
Gitlab presentation task (#95)

* Gitlab presentation task

- creates issues on git.smhi.se
- file upload, issue creation, and issue modification work as with Redmine
- modified create_anchor() to work with Gitlab
- added new dependency for python-gitlab
- added some tests for the new task
- Update documentation for Gitlab task

* Reordered presentation task documentation

- ordered tasks alphabetically (Gitlab is probably most used)
- moved custom visualization options to the end (closes #104)

* Update CHANGES

* More explicit server connection treatment

- make the URL and project ID module constants as suggested by @uwefladrich
- the `gl` object was not used outside of `get_project_and_issue`, so I moved its creation into the function.
This way, the repo information are used in the same place, which is easier to understand I think

125 of 137 new or added lines in 2 files covered. (91.24%)

2188 of 2261 relevant lines covered (96.77%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.04
/tests/test_gitlab.py
1
"""Tests for monitoring/gitlab.py"""
2

3
from unittest.mock import Mock, patch
1✔
4

5
import gitlab
1✔
6
import jinja2.exceptions
1✔
7
import pytest
1✔
8
import scriptengine.exceptions
1✔
9

10
from monitoring.gitlab import Gitlab, create_anchor
1✔
11

12

13
def test_gitlab_presentation_list(tmp_path):
1✔
14
    init = {
1✔
15
        "src": str(tmp_path),
16
        "local_dst": str(tmp_path),
17
        "template": str(tmp_path / "gitlab.txt.j2"),
18
        "subject": "Test Issue",
19
        "api_key": "Invalid Key",
20
    }
21
    redmine_task = Gitlab(init)
1✔
22
    test_sources = ["path.txt", {"path": "path.txt"}, "path.yml"]
1✔
23
    with patch.object(redmine_task, "log_warning") as mock:
1✔
24
        result = redmine_task.get_presentation_list(test_sources, str(tmp_path))
1✔
25
    mock.assert_called_with("Can not present diagnostic: File not found: path.yml")
1✔
26
    assert result == []
1✔
27

28

29
def test_gitlab_connection_error(tmp_path):
1✔
30
    init = {
1✔
31
        "src": str(tmp_path),
32
        "local_dst": str(tmp_path),
33
        "template": str(tmp_path / "gitlab.txt.j2"),
34
        "subject": "Test Issue",
35
        "api_key": "Invalid Key",
36
    }
37
    gitlab_task = Gitlab(init)
1✔
38
    pytest.raises(
1✔
39
        scriptengine.exceptions.ScriptEngineTaskRunError,
40
        gitlab_task.get_project_and_issue,
41
        init["api_key"],
42
        init["subject"],
43
    )
44

45

46
def test_gitlab_auth_error(tmp_path):
1✔
47
    init = {
1✔
48
        "src": str(tmp_path),
49
        "local_dst": str(tmp_path),
50
        "template": str(tmp_path / "gitlab.txt.j2"),
51
        "subject": "Test Issue",
52
        "api_key": "Invalid Key",
53
    }
54
    gitlab_task = Gitlab(init)
1✔
55
    pytest.raises(
1✔
56
        scriptengine.exceptions.ScriptEngineTaskRunError,
57
        gitlab_task.get_project_and_issue,
58
        init["api_key"],
59
        init["subject"],
60
    )
61

62

63
def test_gitlab_get_template(tmp_path):
1✔
64
    init = {
1✔
65
        "src": str(tmp_path),
66
        "local_dst": str(tmp_path),
67
        "template": str(tmp_path / "gitlab.txt.j2"),
68
        "subject": "Test Issue",
69
        "api_key": "Invalid Key",
70
    }
71
    gitlab_task = Gitlab(init)
1✔
72
    with pytest.raises(jinja2.exceptions.TemplateNotFound):
1✔
73
        gitlab_task.get_template(init, init["template"])
1✔
74

75

76
class MockTemplateOrIssue:
1✔
77
    def __init__(self):
1✔
78
        self.globals = {}
1✔
79
        self.web_url = ""
1✔
80

81
    def render(self, **kwargs):
1✔
82
        return str(kwargs)
1✔
83

84
    def save(self):
1✔
85
        return None
1✔
86

87
    def upload(self, file_name, filepath=""):
1✔
NEW
88
        return f"![{file_name}]({filepath})"
×
89

90

91
def test_gitlab_run(tmp_path):
1✔
92
    init = {
1✔
93
        "src": str(tmp_path),
94
        "local_dst": str(tmp_path),
95
        "template": str(tmp_path / "redmine.txt.j2"),
96
        "subject": "Test Issue",
97
        "api_key": "Invalid Key",
98
    }
99
    gitlab_task = Gitlab(init)
1✔
100
    gitlab_task.get_presentation_list = Mock(return_value=[])
1✔
101
    mock_object = MockTemplateOrIssue()
1✔
102
    gitlab_task.get_template = Mock(return_value=mock_object)
1✔
103
    gitlab_task.get_project_and_issue = Mock(return_value=(mock_object, mock_object))
1✔
104
    with patch.object(gitlab_task, "log_debug") as mock:
1✔
105
        gitlab_task.run(init)
1✔
106
    mock.assert_called_with("Saving issue.")
1✔
107

108

109
def test_create_anchor():
1✔
110
    test_string = "Precipitation - Evaporation (Annual Mean Climatology)"
1✔
111
    gitlab_anchor = "precipitation-evaporation-annual-mean-climatology"
1✔
112
    assert create_anchor(test_string) == gitlab_anchor
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc