qa_dashboard.py 13.3 KB
Newer Older
Laurent Peuch's avatar
Laurent Peuch committed
1
2
3
#!/usr/bin/env python3

import os
4
import anybadge
5
import requests
Laurent Peuch's avatar
Laurent Peuch committed
6
7
import gitlab
import jinja2
8
9
10
import tempfile

from tox.config import parseconfig
Laurent Peuch's avatar
Laurent Peuch committed
11
12
13

gl = gitlab.Gitlab("https://forge.extranet.logilab.fr", oauth_token=os.environ.get("TOKEN"))

Laurent Peuch's avatar
Laurent Peuch committed
14
context = {"cubes": []}
Laurent Peuch's avatar
Laurent Peuch committed
15

16
all_cubes = [x for x in gl.projects.list(all=True) if x.namespace["full_path"] == "cubicweb/cubes"]
Laurent Peuch's avatar
Laurent Peuch committed
17

Laurent Peuch's avatar
Laurent Peuch committed
18
PYTEST_CAPTURE_DEPRECATEDWARNINGS_PACKAGE_URI = "git+https://github.com/psycojoker/pytest-capture-deprecatedwarnings"
19

20
for number, cube in enumerate(all_cubes):
Laurent Peuch's avatar
Laurent Peuch committed
21
22
23
24
    # python3
    # doc8
    # a de la doc
    # safety
Laurent Peuch's avatar
Laurent Peuch committed
25
    # deprecated warnings ?
Laurent Peuch's avatar
Laurent Peuch committed
26

27
28
29
30
31
    if cube.name.startswith("cubicweb-"):
        cube_normalized_name = cube.name.split("-", 1)[1].replace("-", "_")
    else:
        cube_normalized_name = cube.name.replace("-", "_")

Laurent Peuch's avatar
Laurent Peuch committed
32
    try:
33
        cube_files = {x["name"] for x in cube.repository_tree()}
Laurent Peuch's avatar
Laurent Peuch committed
34
    except (gitlab.GitlabHttpError, gitlab.GitlabGetError):
35
36
        # we don't care about empty repositories
        continue
Laurent Peuch's avatar
Laurent Peuch committed
37

38
39
40
    has_tox = "tox.ini" in cube_files
    has_gitlab_ci = ".gitlab-ci.yml" in cube_files
    has_readme_rst = bool({"README.rst", "README.md"} & cube_files)  # set intersection
41
    is_new_format = f"cubicweb_{cube_normalized_name}" in cube_files
Laurent Peuch's avatar
Laurent Peuch committed
42

43
    has_doc = bool({"doc", "docs"} & cube_files)  # set intersection
44
    has_readthedocs = requests.get(f"https://cubicweb-{cube_normalized_name}.readthedocs.io").status_code == 200
45

46
47
    number_of_mrs = len([mr for mr in cube.mergerequests.list(all=True) if mr.state == "opened"])
    number_of_topics = len([x for x in cube.branches.list() if x.name.startswith("topic/")])
48
    # will be set after
Laurent Peuch's avatar
Laurent Peuch committed
49
    has_capture_deprecated_warnings = None
50
    has_pytest = False
51

52
53
    if has_tox:
        tox_content = cube.files.get("tox.ini", ref="branch/default").decode().decode("Utf-8")
Laurent Peuch's avatar
Laurent Peuch committed
54

55
56
57
58
59
60
61
62
63
64
65
66
67
        with tempfile.TemporaryDirectory() as temporary_directory_path:
            # TODO pyproject.toml
            tox_path = os.path.join(temporary_directory_path, "tox.ini")
            open(tox_path, "w").write(tox_content)
            tox_config = parseconfig(["-c", tox_path])

            has_py2_tests = any([x.startswith("py2") for x in tox_config.envlist])
            has_py3_tests = any([x.startswith("py3") for x in tox_config.envlist])
            has_check_manifest = "check-manifest" in tox_config.envlist
            has_black = "black" in tox_config.envlist
            has_mypy = "mypy" in tox_config.envlist
            has_flake8 = "flake8" in tox_config.envlist
            has_from_forge = any(["from-forge" in x for x in tox_config.envlist])
68
69
70
71

            if has_py3_tests:
                env_name = [x for x in tox_config.envconfigs if x.startswith("py3")]
                py3 = tox_config.envconfigs[env_name[0]]
72
73
74

                has_pytest = any(("pytest" in x for x in py3.commands))

75
76
77
                deps = [str(x) for x in py3.deps]
                uses_dev_requirements = bool([x for x in deps if x.startswith("-r") and x.endswith("dev-requirements.txt")])

Laurent Peuch's avatar
Laurent Peuch committed
78
79
                if PYTEST_CAPTURE_DEPRECATEDWARNINGS_PACKAGE_URI in deps:
                    has_capture_deprecated_warnings = True
80
81
82
83
84
85
                else:
                    for requirements_file in {x for x in cube_files if "requirements" in x}:
                        requirements = [x.strip().lower() for x in cube.files.get(requirements_file, ref="branch/default").decode().decode("Utf-8").split("\n") if not x.strip().startswith("#")]
                        if PYTEST_CAPTURE_DEPRECATEDWARNINGS_PACKAGE_URI in requirements:
                            has_capture_deprecated_warnings = True
                            break
86
87

            else:
Laurent Peuch's avatar
Laurent Peuch committed
88
                has_capture_deprecated_warnings = False
89

90
91
92
93
94
                if has_py2_tests:
                    env_name = [x for x in tox_config.envconfigs if x.startswith("py2")]
                    py2 = tox_config.envconfigs[env_name[0]]
                    has_pytest = any(("pytest" in x for x in py2.commands))

95
96
            # has_doc8, check if has doc first
            # has_pytest_html
Laurent Peuch's avatar
Laurent Peuch committed
97
            # has_pytest_capture_deprecatedwarnings
98
99
100
101
102
103
104
105
106
    else:
        has_py2_tests = False
        has_py3_tests = False
        has_check_manifest = False
        has_black = False
        has_mypy = False
        has_flake8 = False
        has_from_forge = False

107
    markers = {
Laurent Peuch's avatar
Laurent Peuch committed
108
        "cube": cube.attributes,
109
        "has_doc": has_doc,
110
        "has_readthedocs": has_readthedocs,
111
        "is_new_format": is_new_format,
Laurent Peuch's avatar
Laurent Peuch committed
112
113
114
        "has_tox": has_tox,
        "has_gitlab_ci": has_gitlab_ci,
        "has_readme_rst": has_readme_rst,
115
116
        "has_py2_tests": has_py2_tests,
        "has_py3_tests": has_py3_tests,
117
        "has_pytest": has_pytest,
118
119
120
121
122
        "has_check_manifest": has_check_manifest,
        "has_black": has_black,
        "has_mypy": has_mypy,
        "has_flake8": has_flake8,
        "has_from_forge": has_from_forge,
Laurent Peuch's avatar
Laurent Peuch committed
123
        "has_capture_deprecated_warnings": has_capture_deprecated_warnings,
124
125
        "number_of_mrs": number_of_mrs,
        "number_of_topics": number_of_topics,
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    }

    badges_path = f"public/qa/badges/{cube.id}/"
    if not os.path.exists(badges_path):
        os.makedirs(badges_path)

    for key, value in markers.items():
        if key in ("cube", "number_of_mrs", "number_of_topics"):
            continue

        name = key.replace("has_", "")
        path = os.path.join(badges_path, name + ".svg")
        text = name.replace("_", " ")

        if value:
            color = "green"
            displayed_value = "yes"
        elif value is None:
            color = "white"
            displayed_value = " "
        elif not value:
            color = "red"
            displayed_value = "no"

        anybadge.Badge(text, displayed_value, default_color=color).write_badge(path, overwrite=True)

    context["cubes"].append(markers)
Laurent Peuch's avatar
Laurent Peuch committed
153
154

context["cubes"] = sorted(context["cubes"], key=lambda x: x["cube"]["name"].lower())
Laurent Peuch's avatar
Laurent Peuch committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

os.system("mkdir -p public")  # XXX lazy
result = jinja2.Template(open("template/qa.html", "r").read()).render(**context)
open("public/qa.html", "w").write(result)

# reference:
# In [71]: project.attributes
# Out[71]:
# {'id': 385,
#  'description': 'https://cubicweb.readthedocs.io/',
#  'vcs_type': 'hg_git',
#  'name': 'cubicweb',
#  'name_with_namespace': 'cubicweb / cubicweb',
#  'path': 'cubicweb',
#  'path_with_namespace': 'cubicweb/cubicweb',
#  'created_at': '2020-04-10T13:39:55.623Z',
#  'default_branch': 'branch/default',
#  'tag_list': ['cw-core'],
#  'ssh_url_to_repo': 'ssh://git@forge.extranet.logilab.fr/cubicweb/cubicweb',
#  'http_url_to_repo': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb',
#  'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb',
#  'readme_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/-/blob/branch/default/README',
#  'avatar_url': 'https://forge.extranet.logilab.fr/uploads/-/system/project/avatar/385/cubicweb_logo.png',
#  'star_count': 2,
#  'forks_count': 0,
#  'last_activity_at': '2020-07-22T02:01:43.427Z',
#  'namespace': {'id': 23,
#   'name': 'cubicweb',
#   'path': 'cubicweb',
#   'kind': 'group',
#   'full_path': 'cubicweb',
#   'parent_id': None,
#   'avatar_url': '/uploads/-/system/group/avatar/23/cubicweb.png',
#   'web_url': 'https://forge.extranet.logilab.fr/groups/cubicweb'},
#  '_links': {'self': 'https://forge.extranet.logilab.fr/api/v4/projects/385',
#   'issues': 'https://forge.extranet.logilab.fr/api/v4/projects/385/issues',
#   'merge_requests': 'https://forge.extranet.logilab.fr/api/v4/projects/385/merge_requests',
#   'repo_branches': 'https://forge.extranet.logilab.fr/api/v4/projects/385/repository/branches',
#   'labels': 'https://forge.extranet.logilab.fr/api/v4/projects/385/labels',
#   'events': 'https://forge.extranet.logilab.fr/api/v4/projects/385/events',
#   'members': 'https://forge.extranet.logilab.fr/api/v4/projects/385/members'},
#  'empty_repo': False,
#  'archived': False,
#  'visibility': 'public',
#  'resolve_outdated_diff_discussions': False,
#  'container_registry_enabled': True,
#  'issues_enabled': True,
#  'merge_requests_enabled': True,
#  'wiki_enabled': True,
#  'jobs_enabled': True,
#  'snippets_enabled': True,
#  'can_create_merge_request_in': True,
#  'issues_access_level': 'enabled',
#  'repository_access_level': 'enabled',
#  'merge_requests_access_level': 'enabled',
#  'forking_access_level': 'enabled',
#  'wiki_access_level': 'enabled',
#  'builds_access_level': 'enabled',
#  'snippets_access_level': 'enabled',
#  'pages_access_level': 'enabled',
#  'emails_disabled': False,
#  'shared_runners_enabled': True,
#  'lfs_enabled': True,
#  'creator_id': 19,
#  'import_status': 'none',
#  'open_issues_count': 38,
#  'ci_default_git_depth': 50,
#  'public_jobs': True,
#  'build_timeout': 14400,
#  'auto_cancel_pending_pipelines': 'enabled',
#  'build_coverage_regex': '',
#  'ci_config_path': '',
#  'shared_with_groups': [{'group_id': 21,
#    'group_name': 'logilab',
#    'group_full_path': 'logilab',
#    'group_access_level': 40,
#    'expires_at': None}],
#  'only_allow_merge_if_pipeline_succeeds': False,
#  'request_access_enabled': True,
#  'only_allow_merge_if_all_discussions_are_resolved': False,
#  'remove_source_branch_after_merge': None,
#  'printing_merge_request_link_enabled': True,
#  'merge_method': 'ff',
#  'suggestion_commit_message': None,
#  'auto_devops_enabled': False,
#  'auto_devops_deploy_strategy': 'continuous',
#  'autoclose_referenced_issues': True,
#  'permissions': {'project_access': {'access_level': 30,
#    'notification_level': 3},
#   'group_access': {'access_level': 40, 'notification_level': 2}}}
#
# In [171]: mr.attributes
# Out[171]:
# {'assignees': [],
#  'labels': ['Need Revision'],
#  'id': 2098,
#  'iid': 40,
#  'project_id': 385,
#  'title': 'fix: UX when migractions failed to get its connection',
#  'description': "It was a mess to debug because from the exception we couldn't get the right information to understand what was happening.",
#  'state': 'opened',
#  'created_at': '2020-07-08T13:43:26.081Z',
#  'updated_at': '2020-07-22T02:01:50.582Z',
#  'merged_by': None,
#  'merged_at': None,
#  'closed_by': None,
#  'closed_at': None,
#  'target_branch': 'topic/default/fix-yams-str-only-api',
#  'source_branch': 'topic/default/ux-migractions-get-connections',
#  'user_notes_count': 4,
#  'upvotes': 0,
#  'downvotes': 0,
#  'assignee': None,
#  'author': {'id': 52,
#   'name': 'Laurent Peuch',
#   'username': 'bram',
#   'state': 'active',
#   'avatar_url': 'https://forge.extranet.logilab.fr/uploads/-/system/user/avatar/52/avatar.png',
#   'web_url': 'https://forge.extranet.logilab.fr/bram'},
#  'source_project_id': 385,
#  'target_project_id': 385,
#  'work_in_progress': False,
#  'milestone': None,
#  'merge_when_pipeline_succeeds': False,
#  'merge_status': 'cannot_be_merged',
#  'sha': 'b082d6abc2dd0eff89c5b2d5a200afed87f871bd',
#  'merge_commit_sha': None,
#  'squash_commit_sha': None,
#  'discussion_locked': None,
#  'should_remove_source_branch': True,
#  'force_remove_source_branch': True,
#  'reference': '!40',
#  'references': {'short': '!40',
#   'relative': '!40',
#   'full': 'cubicweb/cubicweb!40'},
#  'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/-/merge_requests/40',
#  'time_stats': {'time_estimate': 0,
#   'total_time_spent': 0,
#   'human_time_estimate': None,
#   'human_total_time_spent': None},
#  'squash': False,
#  'task_completion_status': {'count': 0, 'completed_count': 0},
#  'has_conflicts': True,
#  'blocking_discussions_resolved': True}
#
# In [173]: mr.pipelines()
# Out[173]:
# [{'id': 10456,
#   'sha': 'b082d6abc2dd0eff89c5b2d5a200afed87f871bd',
#   'ref': 'topic/default/ux-migractions-get-connections',
#   'status': 'failed',
#   'created_at': '2020-07-22T02:01:45.244Z',
#   'updated_at': '2020-07-22T02:49:01.167Z',
#   'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/pipelines/10456'},
#  {'id': 10172,
#   'sha': 'ecd6627517e6d933fed6142251ac33e01feadb7d',
#   'ref': 'topic/default/ux-migractions-get-connections',
#   'status': 'success',
#   'created_at': '2020-07-15T19:14:46.111Z',
#   'updated_at': '2020-07-15T19:49:18.689Z',
#   'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/pipelines/10172'},
#  {'id': 10139,
#   'sha': '80da4968dbd33dc3c595f34ed42f363c09afe93b',
#   'ref': 'topic/default/ux-migractions-get-connections',
#   'status': 'success',
#   'created_at': '2020-07-15T13:48:35.325Z',
#   'updated_at': '2020-07-15T14:23:44.391Z',
#   'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/pipelines/10139'},
#  {'id': 9981,
#   'sha': '43ba55bcb3b09008ea180f9fb13e38871c61d72a',
#   'ref': 'topic/default/ux-migractions-get-connections',
#   'status': 'failed',
#   'created_at': '2020-07-08T21:26:50.427Z',
#   'updated_at': '2020-07-08T22:05:34.317Z',
#   'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/pipelines/9981'},
#  {'id': 9963,
#   'sha': '927761a999636ce7a8707ce5e320d0aa1f840c77',
#   'ref': 'topic/default/ux-migractions-get-connections',
#   'status': 'failed',
#   'created_at': '2020-07-08T13:40:32.447Z',
#   'updated_at': '2020-07-08T14:16:23.240Z',
#   'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/pipelines/9963'}]
#
# In [174]: project.labels.list(all=True)[0].attributes
# Out[174]:
# {'id': 945,
#  'name': 'Doing',
#  'color': '#5CB85C',
#  'description': 'Issue in progress',
#  'description_html': 'Issue in progress',
#  'text_color': '#FFFFFF',
#  'subscribed': True,
#  'priority': None,
#  'is_project_label': True,
#  'project_id': 385}