Skip to content

Commit a0959d3

Browse files
authored
Updated docMapper logic (#263)
1 parent c65b58f commit a0959d3

3 files changed

Lines changed: 146 additions & 10 deletions

File tree

library/components/atomic-design/templates/Header/Header.cy.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const runtimeConfig: IRuntimeConfig = {
2626
ADMIN: undefined,
2727
},
2828
API: {},
29-
VERSIONS: { v: undefined },
29+
VERSIONS: { v: undefined, orchestrator: "v3.1.0-dev-f478801" },
3030
DOCUMENTATION: [],
3131
};
3232

@@ -87,7 +87,9 @@ describe("<Header/>", () => {
8787
OBSERVABILITY_URL: "",
8888
MFE: {},
8989
API: {},
90-
VERSIONS: {},
90+
VERSIONS: {
91+
orchestrator: "v3.1.0-dev-f478801",
92+
},
9193
};
9294
window.__RUNTIME_CONFIG__ = cfg;
9395
cy.mount(

library/components/atomic-design/templates/Header/docMapper.ts

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,63 @@
55

66
import { RuntimeConfig, stripTrailingSlash } from "@orch-ui/utils";
77

8+
/**
9+
* Extracts major.minor version from orchestrator version string
10+
* @param {string} version - Version string like "v3.0.1-dev-b21fb28"
11+
* @returns {string} - Major.minor version like "3.0" or "main" if invalid
12+
*/
13+
function extractDocumentationVersion(version: string) {
14+
let docVersion = "main";
15+
16+
if (!version) {
17+
return docVersion;
18+
}
19+
20+
// Remove "v" prefix if present
21+
let cleanVersion = version;
22+
if (version.startsWith("v")) {
23+
cleanVersion = version.substring(1); // removes "v" prefix
24+
} else {
25+
// if orchestrator version is received other than "v" format return default
26+
return docVersion;
27+
}
28+
29+
// Split by "-" to separate version from metadata
30+
const versionParts = cleanVersion.split("-"); //returns [3.0.1, dev, b21fb28]
31+
32+
if (versionParts.length > 0) {
33+
// Split the version numbers by "."
34+
const numbers = versionParts[0].split("."); //returns [3, 0, 1]
35+
36+
// Check if we have at least major and minor version
37+
if (numbers.length >= 2) {
38+
docVersion = `${numbers[0]}.${numbers[1]}`; // returns `3.0`
39+
}
40+
}
41+
42+
return docVersion;
43+
}
44+
845
/**
946
* Method to transform from url to doc link
1047
* @param url - pathname without search params (location.pathname)
1148
*/
1249
export const getDocsForUrl = (url: string) => {
50+
// this is where documentation is hoisted and same value is received
51+
// ...in documentationUrl but concatenated with `/main`
52+
const docHost = "https://docs.openedgeplatform.intel.com/edge-manage-docs";
1353
const urlParts = url.substring(1).split("/");
14-
15-
const docsUrl = stripTrailingSlash(RuntimeConfig.documentationUrl);
54+
let docVersion;
55+
if (
56+
window.__RUNTIME_CONFIG__?.VERSIONS.orchestrator &&
57+
RuntimeConfig.getComponentVersion("orchestrator")
58+
) {
59+
docVersion = extractDocumentationVersion(
60+
RuntimeConfig.getComponentVersion("orchestrator"),
61+
);
62+
}
63+
let docsUrl = stripTrailingSlash(RuntimeConfig.documentationUrl);
1664
let docsMapper = RuntimeConfig.documentation;
17-
1865
// looking for matches part (url segment) by part
1966
// takes browser url, e.g. /test/aa/bb/cc and test against mapper values, segment by segment
2067
// if values from the same segment index from url and mapper key are different we dont grab this mapping
@@ -31,7 +78,6 @@ export const getDocsForUrl = (url: string) => {
3178
[urlParts[index], "*"].includes(part),
3279
),
3380
);
34-
3581
// if we get more than one match it means that we get match against static address and segment with path parameter
3682
// e.g. /test/aa/bb and /test/*/bb or /test/aa/*
3783
// then we need to pick key with the least number of path parameters (*)
@@ -56,11 +102,25 @@ export const getDocsForUrl = (url: string) => {
56102
];
57103
}
58104

59-
// when mapper contains only one entry, we are ready to read the docs address
105+
try {
106+
const docsUrlObj = new URL(docsUrl);
107+
const docHostObj = new URL(docHost);
108+
// fail safe check to ensure documentation host
109+
if (
110+
docsUrlObj.host === docHostObj.host &&
111+
docsUrlObj.protocol === docHostObj.protocol &&
112+
docVersion
113+
) {
114+
// concatenating the release version with docsUrl
115+
docsUrl = `${docHost}/${docVersion}`;
116+
}
117+
} catch (e) {
118+
// fallback: do nothing, keep docsUrl as is
119+
}
120+
60121
if (docsMapper.length === 1) {
61122
return `${docsUrl}${docsMapper[0].dest}`;
62123
}
63-
64124
// default option
65125
const defaultDocsAddress =
66126
RuntimeConfig.documentation[0]?.dest ?? `${window.location.origin}/docs`;

library/components/atomic-design/templates/Header/docsMapper.cy.tsx

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ const testCases = [
5454
},
5555
];
5656

57+
const createTestConfig = (
58+
overrides: Partial<IRuntimeConfig> = {},
59+
): IRuntimeConfig => ({
60+
DOCUMENTATION_URL:
61+
"https://docs.openedgeplatform.intel.com/edge-manage-docs/main",
62+
DOCUMENTATION: [
63+
{
64+
src: "/dashboard",
65+
dest: "/user_guide/monitor_deployments/index.html",
66+
},
67+
],
68+
AUTH: "false",
69+
KC_URL: "",
70+
KC_REALM: "",
71+
KC_CLIENT_ID: "",
72+
TITLE: "",
73+
SESSION_TIMEOUT: 1800,
74+
OBSERVABILITY_URL: "",
75+
MFE: {},
76+
API: {},
77+
VERSIONS: {
78+
orchestrator: "v3.1.0-dev-f478801",
79+
},
80+
...overrides,
81+
});
82+
5783
describe("test mapping url to docs link", () => {
5884
beforeEach(() => {
5985
const cfg: IRuntimeConfig = {
@@ -101,7 +127,9 @@ describe("test mapping url to docs link", () => {
101127
OBSERVABILITY_URL: "",
102128
MFE: {},
103129
API: {},
104-
VERSIONS: {},
130+
VERSIONS: {
131+
orchestrator: "v3.1.0-dev-f478801",
132+
},
105133
};
106134
window.__RUNTIME_CONFIG__ = cfg;
107135
});
@@ -127,9 +155,55 @@ describe("test no mapping", () => {
127155
OBSERVABILITY_URL: "",
128156
MFE: {},
129157
API: {},
130-
VERSIONS: {},
158+
VERSIONS: {
159+
orchestrator: "v3.1.0-dev-f478801",
160+
},
131161
};
132162
window.__RUNTIME_CONFIG__ = cfg;
133163
expect(getDocsForUrl("/random")).to.equal("http://localhost:8000/docs");
134164
});
135165
});
166+
167+
describe("doc URL tests for orchestrator versions", () => {
168+
it("Should construct URL with release version 3.1", () => {
169+
window.__RUNTIME_CONFIG__ = createTestConfig({
170+
VERSIONS: {
171+
orchestrator: "v3.1.0-dev-f478801",
172+
},
173+
});
174+
expect(getDocsForUrl("/dashboard")).to.equal(
175+
"https://docs.openedgeplatform.intel.com/edge-manage-docs/3.1/user_guide/monitor_deployments/index.html",
176+
);
177+
});
178+
179+
it("Should construct URL with release version 3.0", () => {
180+
window.__RUNTIME_CONFIG__ = createTestConfig({
181+
VERSIONS: {
182+
orchestrator: "v3.0.0-dev-f478801",
183+
},
184+
});
185+
expect(getDocsForUrl("/dashboard")).to.equal(
186+
"https://docs.openedgeplatform.intel.com/edge-manage-docs/3.0/user_guide/monitor_deployments/index.html",
187+
);
188+
});
189+
190+
it("Should construct URL with mainline doc version if orchestrator version is not available", () => {
191+
window.__RUNTIME_CONFIG__ = createTestConfig({
192+
VERSIONS: {},
193+
});
194+
expect(getDocsForUrl("/dashboard")).to.equal(
195+
"https://docs.openedgeplatform.intel.com/edge-manage-docs/main/user_guide/monitor_deployments/index.html",
196+
);
197+
});
198+
199+
it("Should construct URL with mainline doc version if orchestrator version is received in unknown format", () => {
200+
window.__RUNTIME_CONFIG__ = createTestConfig({
201+
VERSIONS: {
202+
orchestrator: "3.0.0",
203+
},
204+
});
205+
expect(getDocsForUrl("/dashboard")).to.equal(
206+
"https://docs.openedgeplatform.intel.com/edge-manage-docs/main/user_guide/monitor_deployments/index.html",
207+
);
208+
});
209+
});

0 commit comments

Comments
 (0)