Skip to content

Commit 96e927c

Browse files
humitosagjohnson
andauthored
Add project: skip config step if YAML file is present (#11540)
* Add project: skip config step if YAML file is present This commit checks if the repository contains a `.readthedocs.yaml` testing different possible names, and skip the "Add a configuration file to your project" setp if it's present. It only works for GitHub for now, but we can probably add GitLab as well. Related readthedocs/ext-theme#445 * Show a Django message if we detected a .yaml file * Use OAuth session to support YAML check on private repositories * Update readthedocs/projects/views/private.py Co-authored-by: Anthony <[email protected]> * Logging filename * Typo --------- Co-authored-by: Anthony <[email protected]>
1 parent f0c0df1 commit 96e927c

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

readthedocs/projects/views/private.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
from readthedocs.integrations.models import HttpExchange, Integration
5050
from readthedocs.invitations.models import Invitation
5151
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
5354
from readthedocs.oauth.tasks import attach_webhook
5455
from readthedocs.oauth.utils import update_webhook
5556
from readthedocs.projects.filters import ProjectListFilterSet
@@ -347,7 +348,57 @@ def get_form_kwargs(self, step=None):
347348

348349
def get_template_names(self):
349350
"""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)
351402

352403
def done(self, form_list, **kwargs):
353404
"""

0 commit comments

Comments
 (0)