|
| 1 | +// Copyright 2024 TODO Group. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +const Result = require('../lib/result') |
| 5 | +// eslint-disable-next-line no-unused-vars |
| 6 | +const FileSystem = require('../lib/file_system') |
| 7 | +const GitHelper = require('../lib/git_helper') |
| 8 | + |
| 9 | +/** |
| 10 | + * @param {string} flags |
| 11 | + * @returns {regexMatchFactory~regexFn} |
| 12 | + * @ignore |
| 13 | + */ |
| 14 | +function regexMatchFactory(flags) { |
| 15 | + /** |
| 16 | + * @param {string} value |
| 17 | + * @param {string} pattern |
| 18 | + * @returns {object} |
| 19 | + * @ignore |
| 20 | + */ |
| 21 | + const regexFn = function (value, pattern) { |
| 22 | + return value.match(new RegExp(pattern, flags)) |
| 23 | + } |
| 24 | + return regexFn |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @param {string[]} tagNames |
| 29 | + * @param {object} options The rule configuration |
| 30 | + * @param {string[]=} options.allowlist |
| 31 | + * @param {string[]=} options.denylist |
| 32 | + * @param {boolean=} options.ignoreCase |
| 33 | + * @returns {Result} |
| 34 | + * @ignore |
| 35 | + */ |
| 36 | +function validateAgainstAllowlist(tagNames, options) { |
| 37 | + const targets = [] |
| 38 | + const allowlist = options.allowlist |
| 39 | + console.log(options.ignoreCase) |
| 40 | + const regexMatch = regexMatchFactory(options.ignoreCase ? 'i' : '') |
| 41 | + |
| 42 | + for (const tagName of tagNames) { |
| 43 | + let matched = false |
| 44 | + for (const allowRegex of allowlist) { |
| 45 | + if (regexMatch(tagName, allowRegex) !== null) { |
| 46 | + matched = true |
| 47 | + break // tag name passed at least one allowlist entry. |
| 48 | + } |
| 49 | + } |
| 50 | + if (!matched) { |
| 51 | + // Tag name did not pass any allowlist entries |
| 52 | + const message = [ |
| 53 | + `The tag name for tag "${tagName}" does not match any regex in allowlist.\n`, |
| 54 | + `\tAllowlist: ${allowlist.join(', ')}` |
| 55 | + ].join('\n') |
| 56 | + |
| 57 | + targets.push({ |
| 58 | + passed: false, |
| 59 | + message, |
| 60 | + path: tagName |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (targets.length <= 0) { |
| 66 | + const message = [ |
| 67 | + `Tag names comply with regex allowlist.\n`, |
| 68 | + `\tAllowlist: ${allowlist.join(', ')}` |
| 69 | + ].join('\n') |
| 70 | + return new Result(message, [], true) |
| 71 | + } |
| 72 | + return new Result('', targets, false) |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * @param {string[]} tagNames |
| 77 | + * @param {object} options The rule configuration |
| 78 | + * @param {string[]=} options.allowlist |
| 79 | + * @param {string[]=} options.denylist |
| 80 | + * @param {boolean=} options.ignoreCase |
| 81 | + * @returns {Result} |
| 82 | + * @ignore |
| 83 | + */ |
| 84 | +function validateAgainstDenylist(tagNames, options) { |
| 85 | + const targets = [] |
| 86 | + const denylist = options.denylist |
| 87 | + const regexMatch = regexMatchFactory(options.ignoreCase ? 'i' : '') |
| 88 | + |
| 89 | + for (const tagName of tagNames) { |
| 90 | + for (const denyRegex of denylist) { |
| 91 | + if (regexMatch(tagName, denyRegex) !== null) { |
| 92 | + // Tag name matches a denylist entry |
| 93 | + const message = [ |
| 94 | + `The tag name for tag "${tagName}" matched a regex in denylist.\n`, |
| 95 | + `\tDenylist: ${denylist.join(', ')}` |
| 96 | + ].join('\n') |
| 97 | + |
| 98 | + targets.push({ |
| 99 | + passed: false, |
| 100 | + message, |
| 101 | + path: tagName |
| 102 | + }) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + if (targets.length <= 0) { |
| 107 | + const message = [ |
| 108 | + `No denylisted regex found in any tag names.\n`, |
| 109 | + `\tDenylist: ${denylist.join(', ')}` |
| 110 | + ].join('\n') |
| 111 | + return new Result(message, [], true) |
| 112 | + } |
| 113 | + return new Result('', targets, false) |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * |
| 118 | + * @param {FileSystem} fs A filesystem object configured with filter paths and target directories |
| 119 | + * @param {object} options The rule configuration |
| 120 | + * @param {string[]=} options.allowlist |
| 121 | + * @param {string[]=} options.denylist |
| 122 | + * @param {boolean=} options.ignoreCase |
| 123 | + * @returns {Result} The lint rule result |
| 124 | + * @ignore |
| 125 | + */ |
| 126 | +function gitRegexTagNames(fs, options) { |
| 127 | + if (options.allowlist && options.denylist) { |
| 128 | + throw new Error('"allowlist" and "denylist" cannot be both set.') |
| 129 | + } else if (!options.allowlist && !options.denylist) { |
| 130 | + throw new Error('missing "allowlist" or "denylist".') |
| 131 | + } |
| 132 | + const tagNames = GitHelper.gitAllTagNames(fs.targetDir) |
| 133 | + |
| 134 | + // Allowlist |
| 135 | + if (options.allowlist) { |
| 136 | + return validateAgainstAllowlist(tagNames, options) |
| 137 | + } else if (options.denylist) { |
| 138 | + return validateAgainstDenylist(tagNames, options) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = gitRegexTagNames |
0 commit comments