Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gatsby/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ function branchToVersion(repo: Repo, branch: string) {
switch (repo) {
case Repo.tidb:
case Repo.operator: {
const devBranch = repo === Repo.operator ? "main" : "master";
const stable = CONFIG.docs[repo].stable;
switch (branch) {
case "master":
case devBranch:
Comment on lines +90 to +93

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic is duplicated in other files. Please see my comment on gatsby/utils.ts for a suggestion to centralize this into a reusable utility function to improve maintainability.

return "dev";
case stable:
return "stable";
Expand Down
25 changes: 12 additions & 13 deletions gatsby/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@ export function getStable(doc: Repo) {
return undefined;
}

function renameVersion(version: string, stable: string | undefined) {
switch (version) {
case "master":
return "dev";
case stable:
return "stable";
default:
return version.replace("release-", "v");
}
}

export function renameVersionByDoc(doc: Repo, version: string) {
switch (doc) {
case "tidb":
case "tidb-data-migration":
case "tidb-in-kubernetes":
return renameVersion(version, getStable(doc));
case "tidb-in-kubernetes": {
const devBranch = doc === "tidb-in-kubernetes" ? "main" : "master";
const stable = getStable(doc);
switch (version) {
case devBranch:
return "dev";
case stable:
return "stable";
default:
return version.replace("release-", "v");
}
}
Comment on lines +28 to +39

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this change is functionally correct, the logic for determining the dev branch and converting it to a version name is now duplicated in at least three places (gatsby/path.ts, gatsby/utils.ts, and src/shared/utils/index.ts).

To improve maintainability and follow the DRY (Don't Repeat Yourself) principle, I recommend extracting this logic into a single, reusable utility function. You could re-introduce a function similar to the renameVersion that was removed, but make it more generic and export it so it can be used across the codebase.

For example:

// Could be placed in a shared utils file
export function getVersionFromBranch(branch: string, repo: Repo, stable: string | undefined) {
  const devBranch = repo === 'tidb-in-kubernetes' ? 'main' : 'master';
  switch (branch) {
    case devBranch:
      return 'dev';
    case stable:
      return 'stable';
    default:
      return branch.replace('release-', 'v');
  }
}

Using this helper would simplify the code in all affected locations.

case "tidbcloud":
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ export const TIDB_ZH_VERSIONS = CONFIG["docs"]["tidb"]["languages"]["zh"][

export const OP_EN_VERSIONS = CONFIG["docs"]["tidb-in-kubernetes"]["languages"][
"en"
]["versions"].map((d) => convertVersionName(d, OP_EN_STABLE_VERSION));
]["versions"].map((d) => convertVersionName(d, OP_EN_STABLE_VERSION, "tidb-in-kubernetes"));

export const CLOUD_EN_VERSIONS = [];

Expand Down
8 changes: 5 additions & 3 deletions src/shared/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ function branchToVersion(repo: Repo, branch: string) {
switch (repo) {
case Repo.tidb:
case Repo.operator: {
const devBranch = repo === Repo.operator ? "main" : "master";
const stable = CONFIG.docs[repo].stable;
switch (branch) {
case "master":
case devBranch:
Comment on lines +187 to +190

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic is duplicated in other files. Please see my comment on gatsby/utils.ts for a suggestion to centralize this into a reusable utility function to improve maintainability.

return "dev";
case stable:
return "stable";
Expand Down Expand Up @@ -214,9 +215,10 @@ export const AllVersion = Object.keys(CONFIG.docs).reduce((acc, val) => {
return acc;
}, {} as Record<Repo, Record<Locale, (string | null)[]>>);

export function convertVersionName(version: string, stable: string) {
export function convertVersionName(version: string, stable: string, repo?: string) {
const devBranch = repo === "tidb-in-kubernetes" ? "main" : "master";
switch (version) {
case "master":
case devBranch:
Comment on lines +219 to +221

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for determining the dev branch is duplicated here as well. While the default case of this switch is different, the dev branch logic could still be shared. Please see my main comment on gatsby/utils.ts for a refactoring suggestion.

return "dev";
case stable:
return "stable";
Expand Down