|
| 1 | +name: 'Get Changed Files' |
| 2 | +description: 'Gets the list of files changed in a pull request or push event' |
| 3 | +inputs: |
| 4 | + filter: |
| 5 | + description: 'Optional filter pattern (e.g., "*.md" for markdown files, ".github/" for GitHub files)' |
| 6 | + required: false |
| 7 | + default: '' |
| 8 | + event-types: |
| 9 | + description: 'Comma-separated list of event types to support (pull_request, push)' |
| 10 | + required: false |
| 11 | + default: 'pull_request' |
| 12 | +outputs: |
| 13 | + files: |
| 14 | + description: 'JSON array of changed file paths' |
| 15 | + value: ${{ steps.get-files.outputs.files }} |
| 16 | + count: |
| 17 | + description: 'Number of changed files' |
| 18 | + value: ${{ steps.get-files.outputs.count }} |
| 19 | +runs: |
| 20 | + using: 'composite' |
| 21 | + steps: |
| 22 | + - name: Get changed files |
| 23 | + id: get-files |
| 24 | + uses: actions/github-script@v7 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const eventTypes = '${{ inputs.event-types }}'.split(',').map(t => t.trim()); |
| 28 | + const filter = '${{ inputs.filter }}'; |
| 29 | + let changedFiles = []; |
| 30 | +
|
| 31 | + if (eventTypes.includes('pull_request') && context.eventName === 'pull_request') { |
| 32 | + console.log(`Getting files changed in PR #${context.payload.pull_request.number}`); |
| 33 | +
|
| 34 | + // Fetch all files changed in the PR with pagination |
| 35 | + let allFiles = []; |
| 36 | + let page = 1; |
| 37 | + let fetchedCount; |
| 38 | +
|
| 39 | + do { |
| 40 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + pull_number: context.payload.pull_request.number, |
| 44 | + per_page: 100, |
| 45 | + page: page |
| 46 | + }); |
| 47 | +
|
| 48 | + allFiles = allFiles.concat(files); |
| 49 | + fetchedCount = files.length; |
| 50 | + page++; |
| 51 | + } while (fetchedCount === 100); |
| 52 | +
|
| 53 | + if (allFiles.length >= 100) { |
| 54 | + console.log(`Note: This PR has ${allFiles.length} changed files. All files fetched using pagination.`); |
| 55 | + } |
| 56 | +
|
| 57 | + changedFiles = allFiles |
| 58 | + .filter(file => file.status === 'added' || file.status === 'modified' || file.status === 'renamed') |
| 59 | + .map(file => file.filename); |
| 60 | +
|
| 61 | + } else if (eventTypes.includes('push') && context.eventName === 'push') { |
| 62 | + console.log(`Getting files changed in push to ${context.ref}`); |
| 63 | +
|
| 64 | + const { data: comparison } = await github.rest.repos.compareCommits({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + base: context.payload.before, |
| 68 | + head: context.payload.after, |
| 69 | + }); |
| 70 | +
|
| 71 | + changedFiles = comparison.files |
| 72 | + .filter(file => file.status === 'added' || file.status === 'modified' || file.status === 'renamed') |
| 73 | + .map(file => file.filename); |
| 74 | +
|
| 75 | + } else { |
| 76 | + core.setFailed(`Unsupported event type: ${context.eventName}. Supported types: ${eventTypes.join(', ')}`); |
| 77 | + return; |
| 78 | + } |
| 79 | +
|
| 80 | + // Apply filter if provided |
| 81 | + if (filter) { |
| 82 | + const filterLower = filter.toLowerCase(); |
| 83 | + const beforeFilter = changedFiles.length; |
| 84 | + changedFiles = changedFiles.filter(file => { |
| 85 | + const fileLower = file.toLowerCase(); |
| 86 | + // Support simple patterns like "*.md" or ".github/" |
| 87 | + if (filterLower.startsWith('*.')) { |
| 88 | + const ext = filterLower.substring(1); |
| 89 | + return fileLower.endsWith(ext); |
| 90 | + } else { |
| 91 | + return fileLower.startsWith(filterLower); |
| 92 | + } |
| 93 | + }); |
| 94 | + console.log(`Filter '${filter}' applied: ${beforeFilter} → ${changedFiles.length} files`); |
| 95 | + } |
| 96 | +
|
| 97 | + // Calculate simple hash for verification |
| 98 | + const crypto = require('crypto'); |
| 99 | + const filesJson = JSON.stringify(changedFiles.sort()); |
| 100 | + const hash = crypto.createHash('sha256').update(filesJson).digest('hex').substring(0, 8); |
| 101 | +
|
| 102 | + // Log changed files in a collapsible group |
| 103 | + core.startGroup(`Changed Files (${changedFiles.length} total, hash: ${hash})`); |
| 104 | + if (changedFiles.length > 0) { |
| 105 | + changedFiles.forEach(file => console.log(` - ${file}`)); |
| 106 | + } else { |
| 107 | + console.log(' (no files changed)'); |
| 108 | + } |
| 109 | + core.endGroup(); |
| 110 | +
|
| 111 | + console.log(`Found ${changedFiles.length} changed files`); |
| 112 | + core.setOutput('files', JSON.stringify(changedFiles)); |
| 113 | + core.setOutput('count', changedFiles.length); |
| 114 | +
|
| 115 | +branding: |
| 116 | + icon: 'file-text' |
| 117 | + color: 'blue' |
0 commit comments