123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- # SPDX-License-Identifier: AGPL-3.0-or-later
- #
- # Copyright (C) 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
- 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 "<p style=\"background-color: orangered; padding: 10px\">" \
- "an error occured: no projects found</p>"
- # Convert to HTML
- projects_list_html = "<table class=\"laboratory_list\"><tbody>"
- projects_list_html += "<tr>" "<th>Name</th>" \
- "<th>Description</th>" "<th>Owner or group</th>" "<th>Idle</th>" "</tr>"
- for project in projects_list:
- projects_list_html += "<tr><td><a href=\"/%s/%s/\">%s</a></th>" % \
- (instance, project['path_with_namespace'], \
- project['path_with_namespace']) + \
- "<td><a href=\"/%s/%s\">%s</a></td>" % (instance, \
- project['path_with_namespace'], project['description']) + \
- "<td><a href=\"/%s/%s\">%s</a></td>" % (instance, \
- project['path_with_namespace'], \
- project['namespace']['name']) + \
- "<td><a href=\"/%s/%s\">%s</a></td></tr>" % (instance, \
- project['path_with_namespace'], project['last_activity_at'])
- projects_list_html += "</tbody></table>"
- projects_list_html += "<a href=\"?page=%s\">Next →</a>" % (page + 1)
- return projects_list_html
|