Skip to content

Commit f230881

Browse files
committed
Git backend: Improve heuristic checking if GITLAB_MR_PULL_PATTERN is needed
This commit is an incremental improvement toward having full support for self-hosted GitLab instance. It ensures that the expected sources are fetched when a build is triggered after a merge request event is processed in the context of a GitLab integration webhook associated with a self-hosted GitLab instance.
1 parent 524c24c commit f230881

File tree

1 file changed

+27
-4
lines changed
  • readthedocs/vcs_support/backends

1 file changed

+27
-4
lines changed

readthedocs/vcs_support/backends/git.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ def use_shallow_clone(self):
150150
from readthedocs.projects.models import Feature
151151
return not self.project.has_feature(Feature.DONT_SHALLOW_CLONE)
152152

153+
@staticmethod
154+
def _is_trigger_integration_gitlab(project):
155+
# If there is only one integration associated with the project, we
156+
# can unambiguously conclude that the external build was triggered by
157+
# a merge request.
158+
#
159+
# https://github.com/readthedocs/readthedocs.org/issues/9464
160+
161+
# To avoid error like the following, the value corresponding to
162+
# Integration.GITLAB_WEBHOOK is hardcoded.
163+
#
164+
# ImportError: cannot import name 'Project' from partially initialized module
165+
# 'readthedocs.projects.models' (most likely due to a circular import)
166+
_gitlab_webhook = "gitlab_webhook" # Integration.GITLAB_WEBHOOK
167+
168+
if (
169+
len(project.integrations) == 1
170+
and project.integrations[0].integration_type == _gitlab_webhook
171+
):
172+
return True
173+
return False
174+
153175
def fetch(self):
154176
# --force lets us checkout branches that are not fast-forwarded
155177
# https://github.com/readthedocs/readthedocs.org/issues/6097
@@ -166,10 +188,11 @@ def fetch(self):
166188
GITHUB_PR_PULL_PATTERN.format(id=self.verbose_name)
167189
)
168190

169-
if self.project.git_provider_name == GITLAB_BRAND:
170-
cmd.append(
171-
GITLAB_MR_PULL_PATTERN.format(id=self.verbose_name)
172-
)
191+
if (
192+
self.project.git_provider_name == GITLAB_BRAND
193+
or self._is_trigger_integration_gitlab(self.project)
194+
):
195+
cmd.append(GITLAB_MR_PULL_PATTERN.format(id=self.verbose_name))
173196

174197
code, stdout, stderr = self.run(*cmd)
175198
return code, stdout, stderr

0 commit comments

Comments
 (0)