Skip to content

Commit 89662fa

Browse files
committed
Builds: save readthedocs-build.yaml into database
I added a `Version.build_data` field that may be used from `/_/readthedocs-config.json` to extend with data generated by the doctool at build time if necessary.
1 parent 2ad30cc commit 89662fa

File tree

4 files changed

+36
-71
lines changed

4 files changed

+36
-71
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 3.2.18 on 2023-03-13 15:15
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("builds", "0047_build_default_triggered"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="version",
15+
name="build_data",
16+
field=models.JSONField(
17+
default=None,
18+
null=True,
19+
verbose_name="Data generated at build time by the doctool (`readthedocs-build.yaml`).",
20+
),
21+
),
22+
]

readthedocs/builds/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ class Version(TimeStampedModel):
186186
),
187187
)
188188

189+
build_data = models.JSONField(
190+
_("Data generated at build time by the doctool (`readthedocs-build.yaml`)."),
191+
default=None,
192+
null=True,
193+
)
194+
189195
objects = VersionManager.from_queryset(VersionQuerySet)()
190196
# Only include BRANCH, TAG, UNKNOWN type Versions.
191197
internal = InternalVersionManager.from_queryset(partial(VersionQuerySet, internal_only=True))()

readthedocs/doc_builder/director.py

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import structlog
55
import yaml
66
from django.conf import settings
7-
from django.template.loader import render_to_string
8-
from django.utils import timezone
97
from django.utils.translation import gettext_lazy as _
108

119
from readthedocs.builds.constants import EXTERNAL
@@ -644,79 +642,17 @@ def generate_readthedocs_data_javascript(self):
644642
log.warning(yaml_path)
645643
data = yaml.safe_load(open(yaml_path, "r"))
646644
except Exception:
647-
648-
# NOTE: skip this work for now until we decide whether or not this YAML file is required
645+
# NOTE: skip this work for now until we decide whether or not this YAML file is required.
646+
# NOTE: decide whether or not we want this file to be mandatory and raise an exception here.
647+
log.info("No `readthedocs-build.yaml` was provided.")
649648
return
650649

651-
# TODO: Improve this message
652-
raise BuildUserError(
653-
"The required 'readthedocs-build.yaml' file was not provided."
654-
)
655-
656650
log.info("readthedocs-build.yaml loaded.", path=yaml_path)
657651

658652
# TODO: validate the YAML generated by the user
659653
# self._validate_readthedocs_data_yaml(data)
660654

661-
# Populate the data provided by the doctool with data we have from the build context
662-
# TODO: this is the new structure that has to be defined
663-
# context = {
664-
# "project": {
665-
# "slug": self.data.project.slug,
666-
# },
667-
# "version": {
668-
# "slug": self.data.version.slug,
669-
# },
670-
# "build": {
671-
# "id": self.data.build["pk"],
672-
# },
673-
# }
674-
675-
# NOTE: this is the OLD structure.
676-
# This will be changed for a new structure that we have to decide yet.
677-
# I'm using the old one for now just to keep compatibility.
678-
context = {
679-
"ad_free": False,
680-
"api_host": "http://devthedocs.org",
681-
"build_date": timezone.now(),
682-
"builder": "$DOCTOOL_NAME",
683-
"canonical_url": self.data.project.canonical_url,
684-
"commit": self.data.build["commit"],
685-
"docroot": "$DOCTOOL_DOCROOT",
686-
"language": self.data.project.language,
687-
"page": "$DOCTOOL_PAGE", # Can we define this dynamically via Javascript?
688-
"programming_language": self.data.project.programming_language,
689-
"project": self.data.project.slug,
690-
"version": self.data.version.slug,
691-
"source_suffix": "$DOCTOOL_SOURCE_SUFFIX",
692-
"theme": "$DOCTOOL_THEME",
693-
# These can be removed
694-
"user_analytics_code": None,
695-
"global_analytics_code": None,
696-
"proxied_api_host": f"/{settings.DOC_PATH_PREFIX}",
697-
"subprojects": None,
698-
# TODO: remove the following ones, they are just my own tests
699-
# NOTE: eventually, some of these settings should be enabled/disabled by the reader
700-
"build": {
701-
"id": self.data.build_pk,
702-
"external_version": self.data.version.type == EXTERNAL,
703-
},
704-
"repository_url": self.data.project.repo,
705-
}
706-
707-
# Update user's generated data with our own data.
708-
data.update(context)
709-
710-
js_path = os.path.join(
711-
self.data.project.artifact_path(
712-
version=self.data.version.slug, type_="html"
713-
),
714-
"readthedocs-data.html",
715-
)
716-
content = render_to_string(
717-
template_name="doc_builder/readthedocs-data.html",
718-
context=dict(data=context),
719-
)
720-
with open(js_path, "w") as f:
721-
f.write(content)
722-
log.info("readthedocs-data.html written.", path=js_path)
655+
# Copy the YAML data into `Version.build_data`.
656+
# It will be saved when the API is hit.
657+
# This data will be used by the `/_/readthedocs-config.json` API endpoint.
658+
self.data.version.build_data = data

readthedocs/projects/tasks/builds.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ def on_success(self, retval, task_id, args, kwargs):
598598
"has_pdf": "pdf" in valid_artifacts,
599599
"has_epub": "epub" in valid_artifacts,
600600
"has_htmlzip": "htmlzip" in valid_artifacts,
601+
"build_data": self.data.version.build_data,
601602
}
602603
)
603604
except HttpClientError:

0 commit comments

Comments
 (0)