|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Detect code changes for CI/CD pipeline |
| 5 | + * |
| 6 | + * This script detects what types of files have changed between two commits |
| 7 | + * and outputs the results for use in GitHub Actions workflow conditions. |
| 8 | + * |
| 9 | + * Key behavior: |
| 10 | + * - For PRs: compares PR head against base branch |
| 11 | + * - For pushes: compares HEAD against HEAD^ |
| 12 | + * - Excludes certain folders and file types from "code changes" detection |
| 13 | + * |
| 14 | + * Excluded from code changes (don't require changesets): |
| 15 | + * - Markdown files (*.md) in any folder |
| 16 | + * - .changeset/ folder (changeset metadata) |
| 17 | + * - docs/ folder (documentation) |
| 18 | + * - experiments/ folder (experimental scripts) |
| 19 | + * - examples/ folder (example scripts) |
| 20 | + * |
| 21 | + * Usage: |
| 22 | + * node scripts/detect-code-changes.mjs |
| 23 | + * bun scripts/detect-code-changes.mjs |
| 24 | + * |
| 25 | + * Environment variables (set by GitHub Actions): |
| 26 | + * - GITHUB_EVENT_NAME: 'pull_request' or 'push' |
| 27 | + * - GITHUB_BASE_SHA: Base commit SHA for PR |
| 28 | + * - GITHUB_HEAD_SHA: Head commit SHA for PR |
| 29 | + * |
| 30 | + * Outputs (written to GITHUB_OUTPUT): |
| 31 | + * - java-changed: 'true' if any .java files changed |
| 32 | + * - pom-changed: 'true' if pom.xml changed |
| 33 | + * - mjs-changed: 'true' if any .mjs files changed |
| 34 | + * - docs-changed: 'true' if any .md files changed |
| 35 | + * - workflow-changed: 'true' if any .github/workflows/ files changed |
| 36 | + * - any-code-changed: 'true' if any code files changed (excludes docs, changesets, experiments, examples) |
| 37 | + */ |
| 38 | + |
| 39 | +import { execSync } from 'child_process'; |
| 40 | +import { appendFileSync } from 'fs'; |
| 41 | + |
| 42 | +/** |
| 43 | + * Execute a shell command and return trimmed output |
| 44 | + * @param {string} command - The command to execute |
| 45 | + * @returns {string} - The trimmed command output |
| 46 | + */ |
| 47 | +function exec(command) { |
| 48 | + try { |
| 49 | + return execSync(command, { encoding: 'utf-8' }).trim(); |
| 50 | + } catch (error) { |
| 51 | + console.error(`Error executing command: ${command}`); |
| 52 | + console.error(error.message); |
| 53 | + return ''; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Write output to GitHub Actions output file |
| 59 | + * @param {string} name - Output name |
| 60 | + * @param {string} value - Output value |
| 61 | + */ |
| 62 | +function setOutput(name, value) { |
| 63 | + const outputFile = process.env.GITHUB_OUTPUT; |
| 64 | + if (outputFile) { |
| 65 | + appendFileSync(outputFile, `${name}=${value}\n`); |
| 66 | + } |
| 67 | + console.log(`${name}=${value}`); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Get the list of changed files between two commits |
| 72 | + * @returns {string[]} Array of changed file paths |
| 73 | + */ |
| 74 | +function getChangedFiles() { |
| 75 | + const eventName = process.env.GITHUB_EVENT_NAME || 'local'; |
| 76 | + |
| 77 | + if (eventName === 'pull_request') { |
| 78 | + const baseSha = process.env.GITHUB_BASE_SHA; |
| 79 | + const headSha = process.env.GITHUB_HEAD_SHA; |
| 80 | + |
| 81 | + if (baseSha && headSha) { |
| 82 | + console.log(`Comparing PR: ${baseSha}...${headSha}`); |
| 83 | + try { |
| 84 | + // Ensure we have the base commit |
| 85 | + try { |
| 86 | + execSync(`git cat-file -e ${baseSha}`, { stdio: 'ignore' }); |
| 87 | + } catch { |
| 88 | + console.log('Base commit not available locally, attempting fetch...'); |
| 89 | + execSync(`git fetch origin ${baseSha}`, { stdio: 'inherit' }); |
| 90 | + } |
| 91 | + const output = exec(`git diff --name-only ${baseSha} ${headSha}`); |
| 92 | + return output ? output.split('\n').filter(Boolean) : []; |
| 93 | + } catch (error) { |
| 94 | + console.error(`Git diff failed: ${error.message}`); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // For push events or fallback |
| 100 | + console.log('Comparing HEAD^ to HEAD'); |
| 101 | + try { |
| 102 | + const output = exec('git diff --name-only HEAD^ HEAD'); |
| 103 | + return output ? output.split('\n').filter(Boolean) : []; |
| 104 | + } catch { |
| 105 | + // If HEAD^ doesn't exist (first commit), list all files in HEAD |
| 106 | + console.log('HEAD^ not available, listing all files in HEAD'); |
| 107 | + const output = exec('git ls-tree --name-only -r HEAD'); |
| 108 | + return output ? output.split('\n').filter(Boolean) : []; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Check if a file should be excluded from code changes detection |
| 114 | + * @param {string} filePath - The file path to check |
| 115 | + * @returns {boolean} True if the file should be excluded |
| 116 | + */ |
| 117 | +function isExcludedFromCodeChanges(filePath) { |
| 118 | + // Exclude markdown files in any folder |
| 119 | + if (filePath.endsWith('.md')) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + // Exclude specific folders from code changes |
| 124 | + const excludedFolders = ['.changeset/', 'docs/', 'experiments/', 'examples/']; |
| 125 | + |
| 126 | + for (const folder of excludedFolders) { |
| 127 | + if (filePath.startsWith(folder)) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return false; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Main function to detect changes |
| 137 | + */ |
| 138 | +function detectChanges() { |
| 139 | + console.log('Detecting file changes for CI/CD...\n'); |
| 140 | + |
| 141 | + const changedFiles = getChangedFiles(); |
| 142 | + |
| 143 | + console.log('Changed files:'); |
| 144 | + if (changedFiles.length === 0) { |
| 145 | + console.log(' (none)'); |
| 146 | + } else { |
| 147 | + changedFiles.forEach((file) => console.log(` ${file}`)); |
| 148 | + } |
| 149 | + console.log(''); |
| 150 | + |
| 151 | + // Detect .java file changes |
| 152 | + const javaChanged = changedFiles.some((file) => file.endsWith('.java')); |
| 153 | + setOutput('java-changed', javaChanged ? 'true' : 'false'); |
| 154 | + |
| 155 | + // Detect pom.xml changes |
| 156 | + const pomChanged = changedFiles.some((file) => file === 'pom.xml'); |
| 157 | + setOutput('pom-changed', pomChanged ? 'true' : 'false'); |
| 158 | + |
| 159 | + // Detect .mjs file changes (scripts) |
| 160 | + const mjsChanged = changedFiles.some((file) => file.endsWith('.mjs')); |
| 161 | + setOutput('mjs-changed', mjsChanged ? 'true' : 'false'); |
| 162 | + |
| 163 | + // Detect documentation changes (any .md file) |
| 164 | + const docsChanged = changedFiles.some((file) => file.endsWith('.md')); |
| 165 | + setOutput('docs-changed', docsChanged ? 'true' : 'false'); |
| 166 | + |
| 167 | + // Detect workflow changes |
| 168 | + const workflowChanged = changedFiles.some((file) => |
| 169 | + file.startsWith('.github/workflows/') |
| 170 | + ); |
| 171 | + setOutput('workflow-changed', workflowChanged ? 'true' : 'false'); |
| 172 | + |
| 173 | + // Detect code changes (excluding docs, changesets, experiments, examples folders, and markdown files) |
| 174 | + const codeChangedFiles = changedFiles.filter( |
| 175 | + (file) => !isExcludedFromCodeChanges(file) |
| 176 | + ); |
| 177 | + |
| 178 | + console.log('\nFiles considered as code changes:'); |
| 179 | + if (codeChangedFiles.length === 0) { |
| 180 | + console.log(' (none)'); |
| 181 | + } else { |
| 182 | + codeChangedFiles.forEach((file) => console.log(` ${file}`)); |
| 183 | + } |
| 184 | + console.log(''); |
| 185 | + |
| 186 | + // Check if any code files changed (.java, .mjs, .xml, .yml, .yaml, or workflow files) |
| 187 | + const codePattern = /\.(java|mjs|xml|yml|yaml|properties)$|\.github\/workflows\//; |
| 188 | + const codeChanged = codeChangedFiles.some((file) => codePattern.test(file)); |
| 189 | + setOutput('any-code-changed', codeChanged ? 'true' : 'false'); |
| 190 | + |
| 191 | + console.log('\nChange detection completed.'); |
| 192 | +} |
| 193 | + |
| 194 | +// Run the detection |
| 195 | +detectChanges(); |
0 commit comments