pipelines_status.py 8.83 KB
Newer Older
Laurent Peuch's avatar
init  
Laurent Peuch committed
1
2
3
4
5
#!/usr/bin/env python3

import os
import gitlab
import jinja2
6
7
8
9
import logging

logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s:%(name)s] %(message)s")
logger = logging.getLogger("dashboard")
Laurent Peuch's avatar
init  
Laurent Peuch committed
10
11
12
13
14

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

all_core_projects = [x for x in gl.projects.list(all=True) if "cw-core" in x.tag_list]

Laurent Peuch's avatar
Laurent Peuch committed
15
context = {"projects": [], "cubes": []}
Laurent Peuch's avatar
init  
Laurent Peuch committed
16

17
logger.info("parsing all core projects...")
Laurent Peuch's avatar
init  
Laurent Peuch committed
18
for project in all_core_projects:
19
    logger.info(f"working on {project.name} {project.web_url}")
Laurent Peuch's avatar
init  
Laurent Peuch committed
20
21
22
23
24

    # grab the pipelines for default branch that hasn't been triggered from another pipeline
    pipeline_for_default = None

    for pipeline in project.pipelines.list(as_list=False):
25
        logger.info(f"working on pipeline #{pipeline.id} {pipeline.web_url}")
Laurent Peuch's avatar
init  
Laurent Peuch committed
26
27
28
29
30
31
32
33
34
35
36
        if pipeline.ref != "branch/default":
            continue

        if "TRIGGERED_FROM_OTHER_PROJECT" in {x.key for x in pipeline.variables.list()}:
            continue

        pipeline_for_default = pipeline
        break

    assert pipeline_for_default is not None

Laurent Peuch's avatar
Laurent Peuch committed
37
    context["projects"].append({
Laurent Peuch's avatar
init  
Laurent Peuch committed
38
39
40
        "project": project.attributes,
        "pipeline": pipeline.attributes,
        "jobs": [job.attributes for job in pipeline_for_default.jobs.list(all=True)],
Laurent Peuch's avatar
Laurent Peuch committed
41
42
43
    })

context["projects"] = sorted(context["projects"], key=lambda x: x["project"]["name"].lower())
Laurent Peuch's avatar
init  
Laurent Peuch committed
44

Laurent Peuch's avatar
Laurent Peuch committed
45
46
all_cubes = [x for x in gl.projects.list(all=True) if x.namespace["name"] == "cubes"]

47
logger.info("parsing all cubes...")
Laurent Peuch's avatar
Laurent Peuch committed
48
for cube in all_cubes:
49
    logger.info(f"working on {cube.name} {cube.web_url}")
Laurent Peuch's avatar
Laurent Peuch committed
50
51
52
53
54

    # grab the pipelines for default branch that hasn't been triggered from another pipeline
    pipeline_for_default = None

    for pipeline in cube.pipelines.list(as_list=False, all=True):
55
        logger.info(f"working on pipeline #{pipeline.id} {pipeline.web_url}")
Laurent Peuch's avatar
Laurent Peuch committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        if pipeline.ref != "branch/default":
            continue

        if "TRIGGERED_FROM_OTHER_PROJECT" in {x.key for x in pipeline.variables.list()}:
            continue

        pipeline_for_default = pipeline
        break

    if pipeline_for_default:
        jobs = [job.attributes for job in pipeline_for_default.jobs.list(all=True)]
    else:
        jobs = None

Laurent Peuch's avatar
Laurent Peuch committed
70
    context["cubes"].append({
Laurent Peuch's avatar
Laurent Peuch committed
71
72
73
        "project": cube.attributes,
        "pipeline": pipeline.attributes,
        "jobs": jobs,
Laurent Peuch's avatar
Laurent Peuch committed
74
75
76
    })

context["cubes"] = sorted(context["cubes"], key=lambda x: x["project"]["name"].lower())
Laurent Peuch's avatar
Laurent Peuch committed
77

78
79
80
os.system("mkdir -p public")  # XXX lazy
result = jinja2.Template(open("template/pipelines.html", "r").read()).render(**context)
open("public/pipelines.html", "w").write(result)
Laurent Peuch's avatar
init  
Laurent Peuch committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
153
154
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

# 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 [73]: pipeline.attributes
# Out[73]:
# {'id': 10509,
#  'sha': 'cc394e8b84ebeb3e27ab7ddbc5c92746e975abdf',
#  'ref': 'branch/default',
#  'status': 'success',
#  'created_at': '2020-07-22T17:41:55.591Z',
#  'updated_at': '2020-07-22T18:11:24.533Z',
#  'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/pipelines/10509',
#  'project_id': 385}
#
# In [74]: job.attributes
# Out[74]:
# {'artifacts': [{'file_type': 'trace',
#    'size': 3770,
#    'filename': 'job.log',
#    'file_format': None}],
#  'id': 40326,
#  'status': 'success',
#  'stage': 'lint',
#  'name': 'lint_py',
#  'ref': 'branch/default',
#  'tag': False,
#  'coverage': None,
#  'allow_failure': False,
#  'created_at': '2020-07-22T17:41:55.614Z',
#  'started_at': '2020-07-22T17:42:25.406Z',
#  'finished_at': '2020-07-22T17:43:55.065Z',
#  'duration': 89.659301,
#  'user': {'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',
#   'created_at': '2019-08-07T15:08:00.657Z',
#   'bio': '',
#   'location': 'Brussels',
#   'public_email': '',
#   'skype': '',
#   'linkedin': '',
#   'twitter': '',
#   'website_url': '',
#   'organization': '',
#   'job_title': '',
#   'work_information': None},
#  'commit': {'id': 'cc394e8b84ebeb3e27ab7ddbc5c92746e975abdf',
#   'short_id': 'cc394e8b',
#   'created_at': '2020-07-10T16:40:36.000+02:00',
#   'parent_ids': ['5cd9e6f4133f0cef28e80b6bfb359a5d4166d28e'],
#   'title': 'docs(wip): refactoring in progress',
#   'message': 'docs(wip): refactoring in progress\n',
#   'author_name': 'Simon Chabot',
#   'author_email': 'simon.chabot@logilab.fr',
#   'authored_date': '2020-07-10T16:40:36.000+02:00',
#   'committer_name': 'Simon Chabot',
#   'committer_email': 'simon.chabot@logilab.fr',
#   'committed_date': '2020-07-10T16:40:36.000+02:00',
#   'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/-/commit/cc394e8b84ebeb3e27ab7ddbc5c92746e975abdf'},
#  'pipeline': {'id': 10509,
#   'sha': 'cc394e8b84ebeb3e27ab7ddbc5c92746e975abdf',
#   'ref': 'branch/default',
#   'status': 'success',
#   'created_at': '2020-07-22T17:41:55.591Z',
#   'updated_at': '2020-07-22T18:11:24.533Z',
#   'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/pipelines/10509'},
#  'web_url': 'https://forge.extranet.logilab.fr/cubicweb/cubicweb/-/jobs/40326',
#  'runner': {'id': 59,
#   'description': 'heptapod-runner-gitlab-runner-665c6d66bf-gxgxx',
#   'ip_address': '92.154.88.244',
#   'active': True,
#   'is_shared': True,
#   'name': 'gitlab-runner',
#   'online': True,
#   'status': 'online'},
#  'artifacts_expire_at': None,
#  'project_id': 385,
#  'pipeline_id': 10509}