|
| 1 | +// validate-version-labels.js |
| 2 | +// Script to validate version labels for CI workflows |
| 3 | +// |
| 4 | +// Usage: |
| 5 | +// node validate-version-labels.js <labels-file> |
| 6 | +// echo -e "version:type/foo:v1.2.3\nversion:type/bar:v2.0.0\nother" | node scripts/internal-ci/validate-version-labels.js |
| 7 | +// |
| 8 | +// Inputs: |
| 9 | +// - Newline-separated list of label strings (from file or stdin) |
| 10 | +// |
| 11 | +// Outputs: |
| 12 | +// - Writes to $GITHUB_OUTPUT variable: |
| 13 | +// isValid: true/false |
| 14 | +// validationMessage: detailed message of validation errors |
| 15 | +// invalidVersionLabels: comma-separated list of invalid version labels |
| 16 | +// hasUntrackedVersion: true/false if untracked version label is present |
| 17 | +// componentVersionLabels: comma-separated list of valid component version labels |
| 18 | +// - Exits with code 1 on validation failure, 0 on success |
| 19 | + |
| 20 | +const fs = require('fs'); |
| 21 | + |
| 22 | +// Returns an array of normalized, trimmed, non-empty label strings |
| 23 | +function getLabelArray(rawLabels) { |
| 24 | + return rawLabels |
| 25 | + .split('\n') |
| 26 | + .map(l => l.trim().toLowerCase()) |
| 27 | + .filter(Boolean); |
| 28 | +} |
| 29 | + |
| 30 | +const versionLabelPrefix = 'version:'; |
| 31 | +const untrackedLabel = `${versionLabelPrefix}untracked`; // Special label for untracked versions, 'version:untracked' |
| 32 | +const semverPattern = 'v\\d+\\.\\d+\\.\\d+'; // Semantic versioning pattern vX.Y.Z, e.g., v1.2.3 |
| 33 | +// Pattern for valid version label: version:component-type/component-name:vX.Y.Z where type and name correspond to the path of the component under the ./github directory |
| 34 | +const componentVersionRegEx = new RegExp( |
| 35 | + `^${versionLabelPrefix}[a-z0-9_-]+\/[a-z0-9_-]+:${semverPattern}$` |
| 36 | +); |
| 37 | + |
| 38 | +function getVersionLabelArray(labels) { |
| 39 | + return labels.filter(label => label.startsWith(versionLabelPrefix)); |
| 40 | +} |
| 41 | + |
| 42 | +function getInvalidVersionLabels(versionLabels) { |
| 43 | + return versionLabels.filter( |
| 44 | + label => label !== untrackedLabel && !componentVersionRegEx.test(label) |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +function getComponentVersionLabels(versionLabels) { |
| 49 | + return versionLabels.filter(label => componentVersionRegEx.test(label)); |
| 50 | +} |
| 51 | + |
| 52 | +function basicValidate(invalidVersionLabels, componentVersionLabels, hasUntrackedVersion) { |
| 53 | + const hasVersionLabels = |
| 54 | + invalidVersionLabels.length + componentVersionLabels.length + (hasUntrackedVersion ? 1 : 0) > 0; |
| 55 | + const hasInvalidVersionLabels = invalidVersionLabels.length > 0; |
| 56 | + const hasConflictingVersions = hasUntrackedVersion && componentVersionLabels.length > 0; |
| 57 | + |
| 58 | + let basicValidationMessage = ''; |
| 59 | + if (!hasVersionLabels) { |
| 60 | + basicValidationMessage = '- No version labels found. At least one version label is required.'; |
| 61 | + } |
| 62 | + if (hasInvalidVersionLabels) { |
| 63 | + basicValidationMessage += '- Invalid version labels found.\n'; |
| 64 | + } |
| 65 | + if (hasConflictingVersions) { |
| 66 | + basicValidationMessage += |
| 67 | + '- Conflicting version labels found: both untracked and component version labels present.\n'; |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + isValid: hasVersionLabels && !hasInvalidVersionLabels && !hasConflictingVersions, |
| 72 | + basicValidationMessage: basicValidationMessage.trim(), |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +// Main entry |
| 77 | +if (require.main === module) { |
| 78 | + // Accept labels from stdin or as a file argument |
| 79 | + let rawLabels = ''; |
| 80 | + if (process.argv.length > 2) { |
| 81 | + rawLabels = fs.readFileSync(process.argv[2], 'utf8'); |
| 82 | + } else { |
| 83 | + rawLabels = fs.readFileSync(0, 'utf8'); // Read from stdin |
| 84 | + } |
| 85 | + |
| 86 | + const labels = getLabelArray(rawLabels); |
| 87 | + console.log('Normalized Labels:', labels); |
| 88 | + |
| 89 | + const versionLabels = getVersionLabelArray(labels); |
| 90 | + |
| 91 | + const invalidVersionLabels = getInvalidVersionLabels(versionLabels); |
| 92 | + const componentVersionLabels = getComponentVersionLabels(versionLabels); |
| 93 | + const hasUntrackedVersion = versionLabels.includes(untrackedLabel); |
| 94 | + |
| 95 | + // Validate |
| 96 | + let { isValid, basicValidationMessage } = basicValidate( |
| 97 | + invalidVersionLabels, |
| 98 | + componentVersionLabels, |
| 99 | + hasUntrackedVersion |
| 100 | + ); |
| 101 | + |
| 102 | + const validationMessage = ( |
| 103 | + basicValidationMessage ?? '✅ Version labels validation passed.' |
| 104 | + ).replace(/\n/g, '\\n'); |
| 105 | + |
| 106 | + // write outputs for use in later steps |
| 107 | + const githubOutput = process.env.GITHUB_OUTPUT; |
| 108 | + if (githubOutput) { |
| 109 | + fs.appendFileSync(githubOutput, `isValid=${isValid}\n`); |
| 110 | + fs.appendFileSync(githubOutput, `validationMessage=${validationMessage}\n`); |
| 111 | + fs.appendFileSync(githubOutput, `invalidVersionLabels=${invalidVersionLabels.join(',')}\n`); |
| 112 | + fs.appendFileSync(githubOutput, `hasUntrackedVersion=${hasUntrackedVersion}\n`); |
| 113 | + fs.appendFileSync(githubOutput, `componentVersionLabels=${componentVersionLabels.join(',')}\n`); |
| 114 | + } |
| 115 | + |
| 116 | + // Set exit code based on validation |
| 117 | + if (!isValid) { |
| 118 | + console.error('::error title=Version Label Validation::' + validationMessage); |
| 119 | + process.exit(1); |
| 120 | + } |
| 121 | + |
| 122 | + console.log(validationMessage); |
| 123 | + process.exit(0); |
| 124 | +} |
| 125 | + |
| 126 | +module.exports = { |
| 127 | + getLabelArray, |
| 128 | + getVersionLabelArray, |
| 129 | + getInvalidVersionLabels, |
| 130 | + getComponentVersionLabels, |
| 131 | + basicValidate, |
| 132 | + versionLabelPrefix, |
| 133 | + untrackedLabel, |
| 134 | + componentVersionRegEx, |
| 135 | +}; |
0 commit comments