ensure both changeset short link matchers are using the same regex

This commit is contained in:
Sebastian Sdorra
2020-07-02 11:30:26 +02:00
parent 3a393f6238
commit e08f2f2260
2 changed files with 19 additions and 17 deletions

View File

@@ -25,26 +25,27 @@
import React from "react";
import { Link } from "react-router-dom";
import { Changeset } from "@scm-manager/ui-types";
import { Replacement } from "@scm-manager/ui-components";
import { Replacement, changesetShortLinkRegex } from "@scm-manager/ui-components";
const ChangesetShortLink: (changeset: Changeset, value: string) => Replacement[] = (changeset, value) => {
const linkRegExp = "([A-Za-z0-9\.\-_]+)\/([A-Za-z0-9\.\-_]+)@([a-f0-9]+)";
const matches = value.match(new RegExp(linkRegExp, "g"));
if (!matches) {
return [];
const regex = new RegExp(changesetShortLinkRegex, "g")
const replacements: Replacement[] = [];
let m = regex.exec(value);
while (m) {
const namespace = m[1];
const name = m[2];
const revision = m[3];
const link = `/repo/${namespace}/${name}/code/changeset/${revision}`;
replacements.push({
textToReplace: m[0],
replacement: <Link to={link}>{m[0]}</Link>
});
m = regex.exec(value);
}
return matches.map(value => {
const groups = value.match(new RegExp(linkRegExp));
const namespace = groups[1];
const name = groups[2];
const revision = groups[3];
const link = `/repo/${namespace}/${name}/changeset/${revision}`;
const replacement: Replacement = {
textToReplace: value,
replacement: <Link to={link}>{value}</Link>
};
return replacement;
});
return replacements;
};
export default ChangesetShortLink;