|
| 1 | +name: Get changed files |
| 2 | +description: Outputs a stringified JSON array of changed files for a PR |
| 3 | +inputs: |
| 4 | + github-token: |
| 5 | + description: GitHub token |
| 6 | + required: true |
| 7 | + pattern: |
| 8 | + description: "The glob pattern to use to check for changed files" |
| 9 | + required: true |
| 10 | + default: "${{ github.workspace }}/**/*" |
| 11 | + exclude: |
| 12 | + description: "A stringified JSON array of files to exclude" |
| 13 | + required: false |
| 14 | + default: "[]" |
| 15 | +outputs: |
| 16 | + files: |
| 17 | + description: Stringified JSON array of changed file paths |
| 18 | + value: ${{ steps.changed-files.outputs.files }} |
| 19 | +runs: |
| 20 | + using: "composite" |
| 21 | + steps: |
| 22 | + - name: Get changed files |
| 23 | + id: changed-files |
| 24 | + uses: actions/github-script@v7 |
| 25 | + env: |
| 26 | + PATTERN: ${{ inputs.pattern }} |
| 27 | + EXCLUDE: ${{ inputs.exclude }} |
| 28 | + with: |
| 29 | + github-token: ${{ inputs.github-token }} |
| 30 | + script: | |
| 31 | + const exclude = JSON.parse(process.env['EXCLUDE']); |
| 32 | + const path = require('path'); |
| 33 | + const pr = context.payload.pull_request; |
| 34 | + if (!pr) { |
| 35 | + core.setOutput('files', JSON.stringify([])); |
| 36 | + return; |
| 37 | + } |
| 38 | + const files = await github.paginate( |
| 39 | + github.rest.pulls.listFiles, |
| 40 | + { |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + pull_number: pr.number, |
| 44 | + per_page: 100 |
| 45 | + } |
| 46 | + ); |
| 47 | + const results = files |
| 48 | + .filter(f => path.matchesGlob( |
| 49 | + f.filename, process.env['PATTERN'] |
| 50 | + ) && !exclude.includes(f.filename)) |
| 51 | + .map(f => f.filename); |
| 52 | + console.debug(results); |
| 53 | + core.setOutput('files', JSON.stringify(results)); |
0 commit comments