Skip to content

Commit 06d1239

Browse files
authored
Fix develop changelog parsing (#28232)
* Fix develop changelog parsing and DRY it Signed-off-by: Michael Telatynski <[email protected]> * duh Signed-off-by: Michael Telatynski <[email protected]> * Improve coverage Signed-off-by: Michael Telatynski <[email protected]> * Typeguards! Signed-off-by: Michael Telatynski <[email protected]> * Discard changes to test/unit-tests/components/views/dialogs/__snapshots__/ChangelogDialog-test.tsx.snap --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 6771bd6 commit 06d1239

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

src/components/views/dialogs/ChangelogDialog.tsx

+29-10
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import Spinner from "../elements/Spinner";
1515
import Heading from "../typography/Heading";
1616

1717
interface IProps {
18-
newVersion: string;
19-
version: string;
18+
newVersion: DevelopVersionString;
19+
version: DevelopVersionString;
2020
onFinished: (success: boolean) => void;
2121
}
2222

@@ -32,6 +32,28 @@ interface Commit {
3232

3333
const REPOS = ["element-hq/element-web", "matrix-org/matrix-js-sdk"] as const;
3434

35+
export type DevelopVersionString = string & { _developVersionString: never };
36+
37+
/*
38+
* Parse a version string is compatible with the Changelog dialog ([element-version]-js-[js-sdk-version])
39+
*/
40+
export function parseVersion(version: string): Record<(typeof REPOS)[number], string> | null {
41+
const parts = version.split("-");
42+
if (parts.length === 3 && parts[1] === "js") {
43+
const obj: Record<string, string> = {};
44+
for (let i = 0; i < REPOS.length; i++) {
45+
const commit = parts[2 * i];
46+
obj[REPOS[i]] = commit;
47+
}
48+
return obj;
49+
}
50+
return null;
51+
}
52+
53+
export function checkVersion(version: string): version is DevelopVersionString {
54+
return parseVersion(version) !== null;
55+
}
56+
3557
export default class ChangelogDialog extends React.Component<IProps, State> {
3658
public constructor(props: IProps) {
3759
super(props);
@@ -58,14 +80,11 @@ export default class ChangelogDialog extends React.Component<IProps, State> {
5880
}
5981

6082
public componentDidMount(): void {
61-
const version = this.props.newVersion.split("-");
62-
const version2 = this.props.version.split("-");
63-
if (version == null || version2 == null) return;
64-
// parse versions of form: [vectorversion]-react-[react-sdk-version]-js-[js-sdk-version]
65-
for (let i = 0; i < REPOS.length; i++) {
66-
const oldVersion = version2[2 * i];
67-
const newVersion = version[2 * i];
68-
this.fetchChanges(REPOS[i], oldVersion, newVersion);
83+
const commits = parseVersion(this.props.version)!;
84+
const newCommits = parseVersion(this.props.newVersion)!;
85+
86+
for (const repo of REPOS) {
87+
this.fetchChanges(repo, commits[repo], newCommits[repo]);
6988
}
7089
}
7190

src/toasts/UpdateToast.tsx

+1-10
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,12 @@ import SdkConfig from "../SdkConfig";
1313
import GenericToast from "../components/views/toasts/GenericToast";
1414
import ToastStore from "../stores/ToastStore";
1515
import QuestionDialog from "../components/views/dialogs/QuestionDialog";
16-
import ChangelogDialog from "../components/views/dialogs/ChangelogDialog";
16+
import ChangelogDialog, { checkVersion } from "../components/views/dialogs/ChangelogDialog";
1717
import PlatformPeg from "../PlatformPeg";
1818
import Modal from "../Modal";
1919

2020
const TOAST_KEY = "update";
2121

22-
/*
23-
* Check a version string is compatible with the Changelog
24-
* dialog ([element-version]-react-[react-sdk-version]-js-[js-sdk-version])
25-
*/
26-
function checkVersion(ver: string): boolean {
27-
const parts = ver.split("-");
28-
return parts.length === 5 && parts[1] === "react" && parts[3] === "js";
29-
}
30-
3122
function installUpdate(): void {
3223
PlatformPeg.get()?.installUpdate();
3324
}

test/unit-tests/components/views/dialogs/ChangelogDialog-test.tsx

+27-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import React from "react";
1010
import fetchMock from "fetch-mock-jest";
1111
import { render, screen, waitForElementToBeRemoved } from "jest-matrix-react";
1212

13-
import ChangelogDialog from "../../../../../src/components/views/dialogs/ChangelogDialog";
13+
import ChangelogDialog, {
14+
DevelopVersionString,
15+
parseVersion,
16+
} from "../../../../../src/components/views/dialogs/ChangelogDialog";
1417

1518
describe("<ChangelogDialog />", () => {
1619
it("should fetch github proxy url for each repo with old and new version strings", async () => {
@@ -64,8 +67,8 @@ describe("<ChangelogDialog />", () => {
6467
files: [],
6568
});
6669

67-
const newVersion = "newsha1-js-newsha3";
68-
const oldVersion = "oldsha1-js-oldsha3";
70+
const newVersion = "newsha1-js-newsha3" as DevelopVersionString;
71+
const oldVersion = "oldsha1-js-oldsha3" as DevelopVersionString;
6972
const { asFragment } = render(
7073
<ChangelogDialog newVersion={newVersion} version={oldVersion} onFinished={jest.fn()} />,
7174
);
@@ -78,3 +81,24 @@ describe("<ChangelogDialog />", () => {
7881
expect(asFragment()).toMatchSnapshot();
7982
});
8083
});
84+
85+
describe("parseVersion", () => {
86+
it("should return null for old-style version strings", () => {
87+
expect(parseVersion("aaaabbbb-react-ccccdddd-js-eeeeffff")).toBeNull();
88+
});
89+
90+
it("should return null for invalid version strings", () => {
91+
expect(parseVersion("aaaabbbb-react-ccccdddd")).toBeNull();
92+
});
93+
94+
it("should return null for release version strings", () => {
95+
expect(parseVersion("v1.22.33")).toBeNull();
96+
});
97+
98+
it("should return mapping for develop version string", () => {
99+
expect(parseVersion("aaaabbbb-js-eeeeffff")).toEqual({
100+
"element-hq/element-web": "aaaabbbb",
101+
"matrix-org/matrix-js-sdk": "eeeeffff",
102+
});
103+
});
104+
});

0 commit comments

Comments
 (0)