-
Notifications
You must be signed in to change notification settings - Fork 545
77 lines (75 loc) · 2.92 KB
/
changed_files.yml
File metadata and controls
77 lines (75 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
on:
workflow_call:
inputs:
files:
required: false
type: string
default: "**/*"
files_ignore:
required: false
type: string
default: ""
dir_names:
required: false
type: boolean
default: false
outputs:
any_changed:
description: "Whether any files changed"
value: ${{ jobs.changed-files.outputs.any_changed }}
changed_files:
description: "All changed files"
value: ${{ jobs.changed-files.outputs.changed_files }}
permissions:
contents: read
jobs:
changed-files:
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
any_changed: ${{ fromJson(steps.changed-files.outputs.result).any_changed }}
changed_files: ${{ fromJson(steps.changed-files.outputs.result).changed_files }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Install micromatch
run: npm i -D micromatch@4.0.8
- uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
id: changed-files
env:
files: ${{ inputs.files }}
files_ignore: ${{ inputs.files_ignore }}
dir_names: ${{ inputs.dir_names }}
with:
script: |
const path = require('path')
const micromatch = require('micromatch')
let base, head
if (context.eventName === 'pull_request') {
base = context.payload.pull_request.base.sha
head = context.payload.pull_request.head.sha
} else if (context.eventName === 'push') {
base = context.payload.before
head = context.payload.after
} else {
throw new Error('Unsupported event type: ' + context.eventName)
}
const patternsToMatch = process.env.files.split('\n').filter(Boolean)
const patternsToIgnore = process.env.files_ignore.split('\n').filter(Boolean)
console.log(`Getting changed files between ${base} and ${head}`)
const { data: { files } } = await github.rest.repos.compareCommits({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
base,
head,
})
const onlyNames = [...new Set(files.map(file => process.env.dir_names == 'true' ? path.dirname(file.filename) : file.filename))]
console.log('All changed files:', onlyNames)
const allPatterns = [...patternsToMatch, ...patternsToIgnore.map(pattern => `!${pattern}`)]
console.log('All patterns:', allPatterns)
const afterPatterns = micromatch(onlyNames, allPatterns, { dot: true })
console.log('After patterns:', afterPatterns)
return {
any_changed: afterPatterns.length > 0,
changed_files: afterPatterns.join(' '),
}