link.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. # pylint: disable=too-many-arguments
  8. from __future__ import unicode_literals, absolute_import
  9. import re
  10. import pagure.lib.query
  11. import pagure.exceptions
  12. import pagure.utils
  13. from pagure.config import config as pagure_config
  14. FIXES = [
  15. re.compile(r"(?:.*\s+)?{0}?[sd]?:?\s*?#(\d+)".format(kw), re.I)
  16. for kw in ["fixe", "merge", "close"]
  17. ]
  18. FIXES += [
  19. re.compile(
  20. r"(?:.*\s+)?{0}?[sd]?:?\s*?{1}"
  21. r"(/.*?/(?:issue|pull-request)/\d+)".format(
  22. kw, pagure_config["APP_URL"].rstrip("/")
  23. ),
  24. re.I,
  25. )
  26. for kw in ["fixe", "merge", "close"]
  27. ]
  28. RELATES = [
  29. re.compile(r"(?:.*\s+)?{0}?[sd]?:?\s*?(?:to)?\s*?#(\d+)".format(kw), re.I)
  30. for kw in ["relate"]
  31. ]
  32. RELATES += [
  33. re.compile(
  34. r"(?:.*\s+)?{0}?[sd]?:?\s*?(?:to)?\s*?{1}(/.*?/issue/\d+)".format(
  35. kw, pagure_config["APP_URL"].rstrip("/")
  36. ),
  37. re.I,
  38. )
  39. for kw in ["relate"]
  40. ]
  41. def get_relation(
  42. session,
  43. reponame,
  44. username,
  45. namespace,
  46. text,
  47. reftype="relates",
  48. include_prs=False,
  49. ):
  50. """ For a given text, searches using regex if the text contains
  51. reference to another issue in this project or another one.
  52. Returns the list of issues referenced (possibly empty).
  53. If include_prs=True, it may also contain pull requests (may still
  54. be empty).
  55. By default it searches for references of type: `relates`, for example:
  56. ``this commits relates to #2``.
  57. Another reference type is: `fixes` refering to text including for
  58. example: ``this commits fixes #3``.
  59. """
  60. repo = pagure.lib.query.get_authorized_project(
  61. session, reponame, user=username, namespace=namespace
  62. )
  63. if not repo:
  64. return []
  65. regex = RELATES
  66. if reftype == "fixes":
  67. regex = FIXES
  68. relations = []
  69. for motif in regex:
  70. relid = None
  71. project = None
  72. got_match = motif.match(text)
  73. if got_match:
  74. relid = got_match.group(1)
  75. if not relid.isdigit():
  76. (
  77. username,
  78. namespace,
  79. reponame,
  80. objtype,
  81. relid,
  82. ) = pagure.utils.parse_path(relid)
  83. repo = pagure.lib.query.get_authorized_project(
  84. session, reponame, user=username, namespace=namespace
  85. )
  86. if not repo:
  87. continue
  88. if relid:
  89. relation = pagure.lib.query.search_issues(
  90. session, repo=repo, issueid=relid
  91. )
  92. if relation is None and include_prs:
  93. relation = pagure.lib.query.search_pull_requests(
  94. session, project_id=repo.id, requestid=relid
  95. )
  96. if relation is None or relation.project.name not in [
  97. project,
  98. repo.name,
  99. ]:
  100. continue
  101. if relation not in relations:
  102. relations.append(relation)
  103. return relations