Skip to content

Commit

Permalink
added exceptions to the multi-versions rule
Browse files Browse the repository at this point in the history
  • Loading branch information
stsfartz committed Oct 2, 2024
1 parent 791ba2e commit 2c3d9b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
9 changes: 8 additions & 1 deletion functions/multiVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
import { isObject } from '../util/funcUtils.js';
import getVersion from '../util/getVersion.js';

// Corner cases of API paths that are indeed versions on the url BUT should not be flagged by the rule
const PATH_EXCEPTIONS = [
// Meraki: /networks/{networkId}/switch/dhcp/v4/servers/seen
"dhcp/v1" ,
];


/**
* Checks if there is only one version in server urls and paths.
* @param {string} targetVal The string to lint
Expand Down Expand Up @@ -79,7 +86,7 @@ export default function (targetVal, opts) {
let pathFirstVersion = '';

for (const { path } of getAllPaths(paths)) {
const version = getVersion(path);
const version = getVersion(path, PATH_EXCEPTIONS);

if (version === '') {
continue;
Expand Down
20 changes: 16 additions & 4 deletions util/getVersion.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@

export default function getVersion(str) {
export default function getVersion(str, exceptionList) {

// Exclude paths fragments that are considered exceptions
if (exceptionList) {
for (const exception of exceptionList) {
if (str.includes(exception)) {
str = str.replace(exception, 'EXCEPTION');
}
}
}

// The version regex is pretty complex to address most use cases at Cisco, check this asana issue for context:
// The regexp requires to remove leading or trailing '/' if any.
const versionRegex = /\b\/?v\d+(\.\d+)*\/?(\b|$)/;

let version = '';
const parts = str.match(versionRegex);

if (parts) {
version = parts[0];
version = parts[0];

// Remove leading or trailing '/'
version = version.charAt(0) === '/' ? version.slice(1) : version;
version = version.charAt(version.length - 1) === '/' ? version.slice(0, -1) : version;
}

return version;
}

0 comments on commit 2c3d9b8

Please sign in to comment.