Skip to content

Commit 22e0877

Browse files
authored
Notifications: respect project.versioning_scheme for links (#265)
This is a workaround for now since the backend API is not returning this URLs due to readthedocs/readthedocs-ops#1323. However, we cannot fix that quickly. I'm re-implementing a small version of the resolver for this by generating the URL based on the `project.versioning_scheme` for now. We should come back to this once the API returns the URLs we need. Closes #243
1 parent 0b06297 commit 22e0877

File tree

6 files changed

+113
-34
lines changed

6 files changed

+113
-34
lines changed

dist/readthedocs-addons.js

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/readthedocs-addons.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/_/readthedocs-addons.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,22 @@
6767
}
6868
],
6969
"current": {
70+
"aliases": [],
7071
"slug": "v1.0",
7172
"type": "external",
7273
"downloads": {
7374
"pdf": "https://localhost:8000/_/downloads/en/v1.0/pdf/"
75+
},
76+
"urls": {
77+
"documentation": "https://example.readthedocs.io/en/v1.0/"
7478
}
7579
}
7680
},
7781
"builds": {
7882
"current": {
83+
"id": 1,
7984
"urls": {
80-
"build": "https://localhost:8000/projects/example/builds/1234/"
85+
"build": "https://readthedocs.org/dashboard/example/builds/1/"
8186
}
8287
}
8388
},

src/data-validation.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,11 @@ const addons_notifications = {
311311
properties: {
312312
urls: {
313313
type: "object",
314+
required: ["build"],
314315
properties: {
315-
build: { type: "string" },
316+
build: {
317+
type: "string",
318+
},
316319
},
317320
},
318321
},
@@ -375,11 +378,23 @@ const addons_notifications = {
375378
},
376379
current: {
377380
type: "object",
378-
required: ["slug", "type", "aliases"],
381+
required: ["slug", "urls", "type", "aliases"],
379382
properties: {
383+
aliases: { type: "array" },
380384
slug: { type: "string" },
381385
type: { enum: ["branch", "tag", "external"] },
382-
aliases: { type: "array" },
386+
urls: {
387+
type: "object",
388+
required: ["documentation", "vcs"],
389+
properties: {
390+
documentation: {
391+
type: "string",
392+
},
393+
vcs: {
394+
type: "string",
395+
},
396+
},
397+
},
383398
},
384399
},
385400
},

src/notification.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,9 @@ export class NotificationElement extends LitElement {
5353
this.config.addons.external_version_warning.enabled &&
5454
this.config.versions.current.type === "external"
5555
) {
56-
// TODO: this URL should come from the backend API.
57-
// Doing a simple replacement for now to solve the most common cases.
58-
const vcs_external_url = this.config.projects.current.repository.url
59-
.replace(".git", "")
60-
.replace("[email protected]:", "https://github.com/");
61-
6256
this.urls = {
63-
build: this.config.builds.current.urls.build,
64-
external: `${vcs_external_url}/pull/${this.config.versions.current.slug}`,
57+
build: config.builds.current.urls.build,
58+
external: config.versions.current.urls.vcs,
6559
};
6660
}
6761

tests/notification.test.html

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@
6464
],
6565
current: {
6666
slug: "stable",
67-
type: "branch",
6867
aliases: [],
68+
type: "branch",
69+
urls: {
70+
documentation: "https://project.readthedocs.io/en/stable/",
71+
vcs: "https://github.com/readthedocs/project/",
72+
},
6973
},
7074
},
7175
};
@@ -74,8 +78,15 @@
7478
describe("Notification addon", () => {
7579
it("on stable version", async () => {
7680
// Set current version as non-stable and non-latest
77-
config.versions.current.slug = "v3.x";
78-
config.versions.current.type = "tag";
81+
config.versions.current = {
82+
slug: "v3.x",
83+
aliases: [],
84+
type: "tag",
85+
urls: {
86+
documentation: "https://project.readthedocs.io/en/v3.x/",
87+
vcs: "https://github.com/readthedocs/project/",
88+
},
89+
};
7990

8091
const addon = new notification.NotificationAddon(config);
8192
const element = document.querySelector("readthedocs-notification");
@@ -89,7 +100,7 @@
89100
);
90101
});
91102

92-
it("on latest version", async () => {
103+
it("on latest version (multiple_versions_with_translations)", async () => {
93104
config.versions.current.slug = "latest";
94105

95106
const addon = new notification.NotificationAddon(config);
@@ -98,10 +109,64 @@
98109
// We need to wait for the element to renders/updates before querying it
99110
await elementUpdated(element);
100111

101-
const title = element.shadowRoot.querySelector("div.title");
102-
expect(title.innerText).to.be.equal(
103-
"This is the latest development version",
104-
);
112+
expect(element).shadowDom.to.equal(`
113+
<div>
114+
<div class="title">
115+
This is the
116+
<span>
117+
latest development version
118+
</span>
119+
<a
120+
class="right"
121+
href="#"
122+
>
123+
</a>
124+
</div>
125+
<div class="content">
126+
Some features may not yet be available in the published stable
127+
version. Read the
128+
<a href="https://project.readthedocs.io/en/stable/">
129+
stable version of this documentation
130+
</a>
131+
.
132+
</div>
133+
</div>
134+
`);
135+
});
136+
137+
it("on latest version (multiple_versions_without_translations)", async () => {
138+
config.versions.current.slug = "latest";
139+
config.projects.current.versioning_scheme =
140+
"multiple_versions_without_translations";
141+
142+
const addon = new notification.NotificationAddon(config);
143+
const element = document.querySelector("readthedocs-notification");
144+
145+
// We need to wait for the element to renders/updates before querying it
146+
await elementUpdated(element);
147+
148+
expect(element).shadowDom.to.equal(`
149+
<div>
150+
<div class="title">
151+
This is the
152+
<span>
153+
latest development version
154+
</span>
155+
<a
156+
class="right"
157+
href="#"
158+
>
159+
</a>
160+
</div>
161+
<div class="content">
162+
Some features may not yet be available in the published stable
163+
version. Read the
164+
<a href="https://project.readthedocs.io/en/stable/">
165+
stable version of this documentation
166+
</a>
167+
.
168+
</div>
169+
`);
105170
});
106171

107172
it("on external version", async () => {

0 commit comments

Comments
 (0)