|
49 | 49 | from readthedocs.integrations.models import HttpExchange, Integration
|
50 | 50 | from readthedocs.invitations.models import Invitation
|
51 | 51 | from readthedocs.notifications.models import Notification
|
52 |
| -from readthedocs.oauth.services import registry |
| 52 | +from readthedocs.oauth.constants import GITHUB |
| 53 | +from readthedocs.oauth.services import GitHubService, registry |
53 | 54 | from readthedocs.oauth.tasks import attach_webhook
|
54 | 55 | from readthedocs.oauth.utils import update_webhook
|
55 | 56 | from readthedocs.projects.filters import ProjectListFilterSet
|
@@ -347,7 +348,57 @@ def get_form_kwargs(self, step=None):
|
347 | 348 |
|
348 | 349 | def get_template_names(self):
|
349 | 350 | """Return template names based on step name."""
|
350 |
| - return "projects/import_{}.html".format(self.steps.current) |
| 351 | + return f"projects/import_{self.steps.current}.html" |
| 352 | + |
| 353 | + def process_step(self, form): |
| 354 | + # pylint: disable=too-many-nested-blocks |
| 355 | + if isinstance(form, ProjectBasicsForm): |
| 356 | + remote_repository = form.cleaned_data.get("remote_repository") |
| 357 | + if remote_repository and remote_repository.vcs_provider == GITHUB: |
| 358 | + remote_repository_relations = ( |
| 359 | + remote_repository.remote_repository_relations.filter( |
| 360 | + user=self.request.user, |
| 361 | + account__isnull=False, |
| 362 | + ) |
| 363 | + .select_related("account", "user") |
| 364 | + .only("user", "account") |
| 365 | + ) |
| 366 | + for relation in remote_repository_relations: |
| 367 | + service = GitHubService(relation.user, relation.account) |
| 368 | + session = service.get_session() |
| 369 | + |
| 370 | + for yaml in [ |
| 371 | + ".readthedocs.yaml", |
| 372 | + ".readthedocs.yml", |
| 373 | + "readthedocs.yaml", |
| 374 | + "readthedocs.yml", |
| 375 | + ]: |
| 376 | + try: |
| 377 | + response = session.head( |
| 378 | + f"https://api.github.com/repos/{remote_repository.full_name}/contents/{yaml}", |
| 379 | + timeout=1, |
| 380 | + ) |
| 381 | + if response.ok: |
| 382 | + log.info( |
| 383 | + "Read the Docs YAML file found for this repository.", |
| 384 | + filename=yaml, |
| 385 | + ) |
| 386 | + messages.success( |
| 387 | + self.request, |
| 388 | + _( |
| 389 | + "We detected a configuration file in your repository and started your project's first build." |
| 390 | + ), |
| 391 | + ) |
| 392 | + self.form_list.pop("config") |
| 393 | + break |
| 394 | + except Exception: |
| 395 | + log.warning( |
| 396 | + "Failed when hitting GitHub API to check for .readthedocs.yaml file.", |
| 397 | + filename=yaml, |
| 398 | + ) |
| 399 | + continue |
| 400 | + |
| 401 | + return super().process_step(form) |
351 | 402 |
|
352 | 403 | def done(self, form_list, **kwargs):
|
353 | 404 | """
|
|
0 commit comments