gitlab.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Engine to search in collaborative software platforms based on GitLab_ with
  3. the `GitLab REST API`_.
  4. .. _GitLab: https://about.gitlab.com/install/
  5. .. _GitLab REST API: https://docs.gitlab.com/ee/api/
  6. Configuration
  7. =============
  8. The engine has the following mandatory setting:
  9. - :py:obj:`base_url`
  10. Optional settings are:
  11. - :py:obj:`api_path`
  12. .. code:: yaml
  13. - name: gitlab
  14. engine: gitlab
  15. base_url: https://gitlab.com
  16. shortcut: gl
  17. about:
  18. website: https://gitlab.com/
  19. wikidata_id: Q16639197
  20. - name: gnome
  21. engine: gitlab
  22. base_url: https://gitlab.gnome.org
  23. shortcut: gn
  24. about:
  25. website: https://gitlab.gnome.org
  26. wikidata_id: Q44316
  27. Implementations
  28. ===============
  29. """
  30. from urllib.parse import urlencode
  31. from dateutil import parser
  32. about = {
  33. "website": None,
  34. "wikidata_id": None,
  35. "official_api_documentation": "https://docs.gitlab.com/ee/api/",
  36. "use_official_api": True,
  37. "require_api_key": False,
  38. "results": "JSON",
  39. }
  40. categories = ['it', 'repos']
  41. paging = True
  42. base_url: str = ""
  43. """Base URL of the GitLab host."""
  44. api_path: str = 'api/v4/projects'
  45. """The path the `project API <https://docs.gitlab.com/ee/api/projects.html>`_.
  46. The default path should work fine usually.
  47. """
  48. def request(query, params):
  49. args = {'search': query, 'page': params['pageno']}
  50. params['url'] = f"{base_url}/{api_path}?{urlencode(args)}"
  51. return params
  52. def response(resp):
  53. results = []
  54. for item in resp.json():
  55. results.append(
  56. {
  57. 'template': 'packages.html',
  58. 'url': item.get('web_url'),
  59. 'title': item.get('name'),
  60. 'content': item.get('description'),
  61. 'thumbnail': item.get('avatar_url'),
  62. 'package_name': item.get('name'),
  63. 'maintainer': item.get('namespace', {}).get('name'),
  64. 'publishedDate': parser.parse(item.get('last_activity_at') or item.get("created_at")),
  65. 'tags': item.get('tag_list', []),
  66. 'popularity': item.get('star_count'),
  67. 'homepage': item.get('readme_url'),
  68. 'source_code_url': item.get('http_url_to_repo'),
  69. }
  70. )
  71. return results