@immobiliarelabs/backstage-plugin-gitlab
Version:
82 lines (79 loc) • 2.75 kB
JavaScript
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import duration from 'dayjs/plugin/duration';
dayjs.extend(relativeTime);
dayjs.extend(duration);
const getElapsedTime = (start) => {
return dayjs(start).fromNow();
};
const getDuration = (start, end) => {
if (!end || !start) {
return "NA";
}
const end_time = dayjs(end);
const start_time = dayjs(start);
const duration2 = dayjs.duration(
end_time.diff(start_time, "seconds"),
"seconds"
);
const days = duration2.days();
const hours = duration2.hours();
const minutes = duration2.minutes();
const seconds = duration2.seconds();
const output = `${days ? days + "d " : ""}${hours ? hours + "h " : ""}${minutes ? minutes + "m " : ""}${seconds ? seconds + "s" : ""}`;
if (!output) return "0s";
return output;
};
const parseCodeOwners = (str) => {
try {
const lines = str.replace(/\r/g, "").split("\n");
const owned = [];
for (const line of lines) {
if (!line || line.startsWith("#")) {
continue;
}
owned.push(parseCodeOwnerLine(line));
}
return owned;
} catch (error) {
console.log(`failed to load codeowners`, error);
throw error;
}
};
const parseCodeOwnerLine = (rule) => {
const parts = rule.split(/\s+/);
const path = parts[0];
let teamNames = [];
if (parts.length > 1) {
teamNames = parts.slice(1, parts.length);
for (const name of teamNames) {
if (!codeOwnerRegex.test(name)) {
throw new Error(
`${name} is not a valid owner name in rule ${rule}`
);
}
}
}
return {
rule,
path,
owners: teamNames
};
};
const codeOwnersEmailRegex = /(^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$)/;
const codeOwnersGitlabUsernameRegex = /(^@[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]{0,254}[a-zA-Z0-9_\-]|[a-zA-Z0-9_]$)/;
const codeOwnerRegex = new RegExp(
codeOwnersGitlabUsernameRegex.source + "|" + codeOwnersEmailRegex.source
);
const parseGitLabReadme = (readme) => {
const lines = readme.split("\n");
const modifiedLines = lines.map((line) => {
if (/^\[TOC\]|\[\[_TOC_\]\]$/.test(line.trim())) {
return `## <!-- injected_toc -->`;
}
return line;
});
return modifiedLines.join("\n");
};
export { getDuration, getElapsedTime, parseCodeOwners, parseGitLabReadme };
//# sourceMappingURL=utils.esm.js.map