# SPDX-License-Identifier: AGPL-3.0-or-later # # Copyright (C) 2023 Ferass El Hafidi from markupsafe import escape from pycmarkgfm import gfm_to_html as markdown from base64 import b64decode # For some reason GitLab encodes file contents import requests """ This API "library" basically turns JSON returned by the GitLab API into valid HTML5 that is then embedded in Laboratory templates. """ def api_call(url): return requests.get(url).json() def errcheck(instance, repo = None, group = None, username = None): # TODO: Better error handling try: requests.get('https://%s/api/v4/' % instance) except requests.exceptions.ConnectionError: return 404 # ...what? if repo != None: try: api_call('https://%s/api/v4/projects/%s' \ % (instance, repo))['name'] except KeyError: return 404 if group != None: try: api_call('https://%s/api/v4/groups/%s' \ % (instance, group))['name'] except KeyError: return 404 if username != None: try: api_call('https://%s/api/v4/users?username=%s' \ % (instance, username))[0] except IndexError: return 404 except KeyError: return 404 return 200 def get_projects_list(instance, search_query = None, group = None, page = 1): if search_query is None and group is None: projects_list = api_call('https://%s/api/v4/projects?page=%s' \ % (instance, page)) elif group is not None and search_query is None: # Groups projects_list = api_call('https://%s/api/v4/groups/%s?page=%s' \ % (instance, group, page))['projects'] elif group is not None and search_query is not None: # Groups projects_list = api_call('https://%s/api/v4/groups/%s?search=%s&page=%s' \ % (instance, group, search_query, page))['projects'] else: # Search projects_list = api_call(\ 'https://%s/api/v4/projects?search_namespaces=true&search=%s&page=%s' \ % (instance, search_query, page)) if projects_list == []: return "

" \ "an error occured: no projects found

" # Convert to HTML projects_list_html = "" projects_list_html += "" "" \ "" "" "" "" for project in projects_list: projects_list_html += "" % (instance, \ project['path_with_namespace'], project['description']) + \ "" % (instance, \ project['path_with_namespace'], \ project['namespace']['name']) + \ "" % (instance, \ project['path_with_namespace'], project['last_activity_at']) projects_list_html += "
NameDescriptionOwner or groupIdle
%s" % \ (instance, project['path_with_namespace'], \ project['path_with_namespace']) + \ "%s%s%s
" projects_list_html += "Next →" % (page + 1) return projects_list_html