|
63 | 63 | HTML_ONLY = getattr(settings, 'HTML_ONLY_PROJECTS', ())
|
64 | 64 |
|
65 | 65 |
|
66 |
| -class UpdateDocsTask(Task): |
| 66 | +class SyncRepositoryMixin(object): |
| 67 | + |
| 68 | + """ |
| 69 | + Mixin that handles the VCS sync/update. |
| 70 | + """ |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def get_version(project=None, version_pk=None): |
| 74 | + """ |
| 75 | + Retrieve version data from the API. |
| 76 | +
|
| 77 | + :param project: project object to sync |
| 78 | + :type project: projects.models.Project |
| 79 | + :param version_pk: version pk to sync |
| 80 | + :type version_pk: int |
| 81 | + :returns: a data-complete version object |
| 82 | + :rtype: builds.models.APIVersion |
| 83 | + """ |
| 84 | + assert (project or version_pk), 'project or version_pk is needed' |
| 85 | + if version_pk: |
| 86 | + version_data = api_v2.version(version_pk).get() |
| 87 | + else: |
| 88 | + version_data = (api_v2 |
| 89 | + .version(project.slug) |
| 90 | + .get(slug=LATEST)['objects'][0]) |
| 91 | + return APIVersion(**version_data) |
| 92 | + |
| 93 | + def sync_repo(self): |
| 94 | + """ |
| 95 | + Checkout/update the project's repository and hit ``sync_versions`` API. |
| 96 | + """ |
| 97 | + # Make Dirs |
| 98 | + if not os.path.exists(self.project.doc_path): |
| 99 | + os.makedirs(self.project.doc_path) |
| 100 | + |
| 101 | + if not self.project.vcs_repo(): |
| 102 | + raise RepositoryError( |
| 103 | + _('Repository type "{repo_type}" unknown').format( |
| 104 | + repo_type=self.project.repo_type, |
| 105 | + ), |
| 106 | + ) |
| 107 | + |
| 108 | + with self.project.repo_nonblockinglock( |
| 109 | + version=self.version, |
| 110 | + max_lock_age=getattr(settings, 'REPO_LOCK_SECONDS', 30)): |
| 111 | + |
| 112 | + # Get the actual code on disk |
| 113 | + try: |
| 114 | + before_vcs.send(sender=self.version) |
| 115 | + self._log( |
| 116 | + 'Checking out version {slug}: {identifier}'.format( |
| 117 | + slug=self.version.slug, |
| 118 | + identifier=self.version.identifier, |
| 119 | + ), |
| 120 | + ) |
| 121 | + version_repo = self.project.vcs_repo( |
| 122 | + self.version.slug, |
| 123 | + # When called from ``SyncRepositoryTask.run`` we don't have |
| 124 | + # a ``setup_env`` so we use just ``None`` and commands won't |
| 125 | + # be recorded |
| 126 | + getattr(self, 'setup_env', None), |
| 127 | + ) |
| 128 | + version_repo.checkout(self.version.identifier) |
| 129 | + finally: |
| 130 | + after_vcs.send(sender=self.version) |
| 131 | + |
| 132 | + # Update tags/version |
| 133 | + version_post_data = {'repo': version_repo.repo_url} |
| 134 | + |
| 135 | + if version_repo.supports_tags: |
| 136 | + version_post_data['tags'] = [ |
| 137 | + {'identifier': v.identifier, |
| 138 | + 'verbose_name': v.verbose_name, |
| 139 | + } for v in version_repo.tags |
| 140 | + ] |
| 141 | + |
| 142 | + if version_repo.supports_branches: |
| 143 | + version_post_data['branches'] = [ |
| 144 | + {'identifier': v.identifier, |
| 145 | + 'verbose_name': v.verbose_name, |
| 146 | + } for v in version_repo.branches |
| 147 | + ] |
| 148 | + |
| 149 | + try: |
| 150 | + # Hit the API ``sync_versions`` which may trigger a new build |
| 151 | + # for the stable version |
| 152 | + api_v2.project(self.project.pk).sync_versions.post(version_post_data) |
| 153 | + except HttpClientError: |
| 154 | + log.exception('Sync Versions Exception') |
| 155 | + except Exception: |
| 156 | + log.exception('Unknown Sync Versions Exception') |
| 157 | + |
| 158 | + |
| 159 | +class SyncRepositoryTask(SyncRepositoryMixin, Task): |
| 160 | + |
| 161 | + """ |
| 162 | + Entry point to synchronize the VCS documentation. |
| 163 | + """ |
| 164 | + |
| 165 | + max_retries = 5 |
| 166 | + default_retry_delay = (7 * 60) |
| 167 | + name = __name__ + '.sync_repository' |
| 168 | + |
| 169 | + def run(self, version_pk): |
| 170 | + """ |
| 171 | + Run the VCS synchronization. |
| 172 | +
|
| 173 | + :param version_pk: version pk to sync |
| 174 | + :type version_pk: int |
| 175 | + :returns: whether or not the task ended successfully |
| 176 | + :rtype: bool |
| 177 | + """ |
| 178 | + try: |
| 179 | + self.version = self.get_version(version_pk=version_pk) |
| 180 | + self.project = self.version.project |
| 181 | + self.sync_repo() |
| 182 | + return True |
| 183 | + # Catch unhandled errors when syncing |
| 184 | + except Exception: |
| 185 | + log.exception( |
| 186 | + 'An unhandled exception was raised during VCS syncing', |
| 187 | + ) |
| 188 | + return False |
| 189 | + |
| 190 | + |
| 191 | +class UpdateDocsTask(SyncRepositoryMixin, Task): |
67 | 192 |
|
68 | 193 | """
|
69 | 194 | The main entry point for updating documentation.
|
@@ -106,8 +231,7 @@ def _log(self, msg):
|
106 | 231 |
|
107 | 232 | # pylint: disable=arguments-differ
|
108 | 233 | def run(self, pk, version_pk=None, build_pk=None, record=True,
|
109 |
| - docker=False, search=True, force=False, localmedia=True, |
110 |
| - sync_only=False, **__): |
| 234 | + docker=False, search=True, force=False, localmedia=True, **__): |
111 | 235 | """
|
112 | 236 | Run a documentation sync or sync n' build.
|
113 | 237 |
|
@@ -145,10 +269,6 @@ def run(self, pk, version_pk=None, build_pk=None, record=True,
|
145 | 269 | self.build_force = force
|
146 | 270 | self.config = None
|
147 | 271 |
|
148 |
| - if sync_only: |
149 |
| - self.sync_repo() |
150 |
| - return True |
151 |
| - |
152 | 272 | setup_successful = self.run_setup(record=record)
|
153 | 273 | if not setup_successful:
|
154 | 274 | return False
|
@@ -301,17 +421,6 @@ def get_project(project_pk):
|
301 | 421 | project_data = api_v2.project(project_pk).get()
|
302 | 422 | return APIProject(**project_data)
|
303 | 423 |
|
304 |
| - @staticmethod |
305 |
| - def get_version(project, version_pk): |
306 |
| - """Ensure we're using a sane version.""" |
307 |
| - if version_pk: |
308 |
| - version_data = api_v2.version(version_pk).get() |
309 |
| - else: |
310 |
| - version_data = (api_v2 |
311 |
| - .version(project.slug) |
312 |
| - .get(slug=LATEST)['objects'][0]) |
313 |
| - return APIVersion(**version_data) |
314 |
| - |
315 | 424 | @staticmethod
|
316 | 425 | def get_build(build_pk):
|
317 | 426 | """
|
@@ -342,70 +451,6 @@ def setup_vcs(self):
|
342 | 451 | if commit:
|
343 | 452 | self.build['commit'] = commit
|
344 | 453 |
|
345 |
| - def sync_repo(self): |
346 |
| - """ |
347 |
| - Checkout/update the project's repository and hit ``sync_versions`` API. |
348 |
| - """ |
349 |
| - # Make Dirs |
350 |
| - if not os.path.exists(self.project.doc_path): |
351 |
| - os.makedirs(self.project.doc_path) |
352 |
| - |
353 |
| - if not self.project.vcs_repo(): |
354 |
| - raise RepositoryError( |
355 |
| - _('Repository type "{repo_type}" unknown').format( |
356 |
| - repo_type=self.project.repo_type, |
357 |
| - ), |
358 |
| - ) |
359 |
| - |
360 |
| - with self.project.repo_nonblockinglock( |
361 |
| - version=self.version, |
362 |
| - max_lock_age=getattr(settings, 'REPO_LOCK_SECONDS', 30)): |
363 |
| - |
364 |
| - # Get the actual code on disk |
365 |
| - try: |
366 |
| - before_vcs.send(sender=self.version) |
367 |
| - self._log( |
368 |
| - 'Checking out version {slug}: {identifier}'.format( |
369 |
| - slug=self.version.slug, |
370 |
| - identifier=self.version.identifier, |
371 |
| - ), |
372 |
| - ) |
373 |
| - version_repo = self.project.vcs_repo( |
374 |
| - self.version.slug, |
375 |
| - # When ``sync_only`` we don't a setup_env |
376 |
| - getattr(self, 'setup_env', None), |
377 |
| - ) |
378 |
| - version_repo.checkout(self.version.identifier) |
379 |
| - finally: |
380 |
| - after_vcs.send(sender=self.version) |
381 |
| - |
382 |
| - # Update tags/version |
383 |
| - version_post_data = {'repo': version_repo.repo_url} |
384 |
| - |
385 |
| - if version_repo.supports_tags: |
386 |
| - version_post_data['tags'] = [ |
387 |
| - {'identifier': v.identifier, |
388 |
| - 'verbose_name': v.verbose_name, |
389 |
| - } for v in version_repo.tags |
390 |
| - ] |
391 |
| - |
392 |
| - if version_repo.supports_branches: |
393 |
| - version_post_data['branches'] = [ |
394 |
| - {'identifier': v.identifier, |
395 |
| - 'verbose_name': v.verbose_name, |
396 |
| - } for v in version_repo.branches |
397 |
| - ] |
398 |
| - |
399 |
| - try: |
400 |
| - # Hit the API ``sync_versions`` which may trigger a new build |
401 |
| - # for the stable version |
402 |
| - api_v2.project(self.project.pk).sync_versions.post(version_post_data) |
403 |
| - except HttpClientError: |
404 |
| - log.exception('Sync Versions Exception') |
405 |
| - except Exception: |
406 |
| - log.exception('Unknown Sync Versions Exception') |
407 |
| - |
408 |
| - |
409 | 454 | def get_env_vars(self):
|
410 | 455 | """Get bash environment variables used for all builder commands."""
|
411 | 456 | env = {
|
|
0 commit comments