Skip to content

Commit be2922b

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 be2922b

File tree

1 file changed

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

1 file changed

+28
-4
lines changed

readthedocs/vcs_support/backends/git.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from readthedocs.builds.constants import EXTERNAL
1212
from readthedocs.config import ALL
13+
from readthedocs.integrations.models import Integration
1314
from readthedocs.projects.constants import (
1415
GITHUB_BRAND,
1516
GITHUB_PR_PULL_PATTERN,
@@ -150,6 +151,28 @@ def use_shallow_clone(self):
150151
from readthedocs.projects.models import Feature
151152
return not self.project.has_feature(Feature.DONT_SHALLOW_CLONE)
152153

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

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

174198
code, stdout, stderr = self.run(*cmd)
175199
return code, stdout, stderr

0 commit comments

Comments
 (0)