|
| 1 | +const actionConfig = require('./action-config') |
| 2 | +const argHelper = require('./arg-helper') |
| 3 | +const debugHelper = require('./debug-helper') |
| 4 | +const fs = require('fs') |
| 5 | + |
| 6 | +async function main() { |
| 7 | + try { |
| 8 | + // Command line args |
| 9 | + const args = getArgs() |
| 10 | + |
| 11 | + // Get the action config file |
| 12 | + const file = actionConfig.getFilePath(args.owner, args.repo) |
| 13 | + debugHelper.debug(`file: ${file}`) |
| 14 | + |
| 15 | + // Load the config |
| 16 | + const config = await actionConfig.loadFromPath(file) |
| 17 | + |
| 18 | + // Add ignore tags |
| 19 | + if (!config.ignoreTags) { |
| 20 | + config.ignoreTags = [] |
| 21 | + } |
| 22 | + |
| 23 | + // Add new patterns (avoid duplicates) |
| 24 | + for (const pattern of args.ignoreTags) { |
| 25 | + if (!config.ignoreTags.includes(pattern)) { |
| 26 | + config.ignoreTags.push(pattern) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Write config back |
| 31 | + await fs.promises.writeFile(file, JSON.stringify(config, null, ' ')) |
| 32 | + console.log(`Updated config file: ${file}`) |
| 33 | + console.log(` ignoreTags: ${JSON.stringify(config.ignoreTags)}`) |
| 34 | + } |
| 35 | + catch (err) { |
| 36 | + // Help |
| 37 | + if (err.code === argHelper.helpCode) { |
| 38 | + printUsage() |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + // Arg error? |
| 43 | + if (err.code === argHelper.errorCode) { |
| 44 | + printUsage() |
| 45 | + console.error('') |
| 46 | + } |
| 47 | + |
| 48 | + // Print error |
| 49 | + debugHelper.debug(err.stack) |
| 50 | + console.error(`ERROR: ${err.message}`) |
| 51 | + process.exitCode = 1 |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class Args { |
| 56 | + owner = '' |
| 57 | + repo = '' |
| 58 | + ignoreTags = [] |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Get the command line args |
| 63 | + * @returns {Args} |
| 64 | + */ |
| 65 | +function getArgs() { |
| 66 | + const parsedArgs = argHelper.parse([], ['ignore-tags']) |
| 67 | + const result = new Args() |
| 68 | + |
| 69 | + // Validate ignore-tags is provided |
| 70 | + if (!parsedArgs.options['ignore-tags']) { |
| 71 | + argHelper.throwError('--ignore-tags is required') |
| 72 | + } |
| 73 | + |
| 74 | + // Parse ignore-tags (comma-separated version prefixes like v1,v2) |
| 75 | + const prefixes = parsedArgs.options['ignore-tags'].split(',').map(t => t.trim()).filter(t => t) |
| 76 | + for (const prefix of prefixes) { |
| 77 | + // Convert simple version prefix like "v1" to regex pattern "^v1(\\..*)?$" |
| 78 | + // This matches "v1", "v1.0", "v1.0.0", etc. |
| 79 | + const escapedPrefix = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 80 | + result.ignoreTags.push(`^${escapedPrefix}(\\..*)?$`) |
| 81 | + } |
| 82 | + |
| 83 | + // Validate exactly one arg |
| 84 | + if (parsedArgs.arguments.length !== 1) { |
| 85 | + argHelper.throwError('Expected exactly one arg (nwo)') |
| 86 | + } |
| 87 | + |
| 88 | + const nwo = parsedArgs.arguments[0] |
| 89 | + const splitNwo = nwo.split('/') |
| 90 | + if (splitNwo.length !== 2 || !splitNwo[0] || !splitNwo[1]) { |
| 91 | + argHelper.throwError(`Invalid nwo '${nwo}'`) |
| 92 | + } |
| 93 | + result.owner = splitNwo[0] |
| 94 | + result.repo = splitNwo[1] |
| 95 | + |
| 96 | + return result |
| 97 | +} |
| 98 | + |
| 99 | +function printUsage() { |
| 100 | + console.error('USAGE: add-ignore-tags.sh --ignore-tags versions nwo') |
| 101 | + console.error(` --ignore-tags Comma-separated version prefixes to ignore. For example: v1,v2`) |
| 102 | + console.error(` nwo Name with owner. For example: actions/checkout`) |
| 103 | +} |
| 104 | + |
| 105 | +main() |
0 commit comments