-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
161 lines (138 loc) · 4.86 KB
/
action.yaml
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright 2023 Chris Lee (chrisyxlee)
# SPDX-License-Identifier: Apache-2.0
name: 'files-changed-action'
description: |
This action finds all modified files between the source and destination SHA
and pattern matches against a set of regex.
inputs:
include:
description: |
A list of patterns to match against.
required: false
default: |
.*
exclude:
description: |
A list of patterns to exclude from the match. The diffs will be filtered
prior to checking the include list. If this list is empty, nothing will
be filtered out.
required: false
base_ref:
description: |
The reference to the commit that contains the base changes to diff against.
required: false
head_ref:
description: |
The reference to the commit that contains the latest changes.
required: false
output_diffs:
description: |
Whether to log information about what happens after each filtering step.
required: false
default: 'true'
outputs:
modified:
description: |
Whether files between the base_ref and head_ref commit were modified after excluding
and looking for included files.
value: ${{ steps.files-changed.outputs.modified }}
runs:
using: "composite"
steps:
- name: Determine commits to compare
id: get-commits
shell: bash
run: |
set -u
set -o pipefail
cd "${GITHUB_WORKSPACE}" || exit 1
echo "${{ github.event.pull_request }}"
echo "${{ github.event.pull_request.head.sha }}"
echo "${{ github.event.pull_request.base.sha }}"
if [ -n "${{ inputs.head_ref }}" ]; then
echo "User defined head_ref."
echo "head_ref=${{ inputs.head_ref }}" >> $GITHUB_OUTPUT
elif [ -n "${{ github.event.pull_request.head.sha }}" ]; then
echo "Detecting pull request, using github.event.pull_request.head.sha"
echo "head_ref=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
else
echo "No input for head_ref. Setting head to GITHUB_SHA."
echo "head_ref=${GITHUB_SHA}" >> $GITHUB_OUTPUT
fi
if [ -n "${{ inputs.base_ref }}" ]; then
echo "User defined base_ref."
echo "base_ref=${{ inputs.base_ref }}" >> $GITHUB_OUTPUT
elif [ -n "${{ github.event.pull_request.base.sha }}" ]; then
echo "Detecting pull request, using github.event.pull_request.base.sha"
echo "base_ref=${{ github.event.pull_request.base.sha }}" >> $GITHUB_OUTPUT
else
echo "No input for base_ref. Diffing against last commit."
echo "base_ref=HEAD~1" >> $GITHUB_OUTPUT
fi
- name: Check files changed
id: files-changed
shell: bash
run: |
set +e
set -u
set -o pipefail
cd "${GITHUB_WORKSPACE}" || exit 1
DIFFS=$(git diff --name-only ${{ steps.get-commits.outputs.head_ref }} ${{ steps.get-commits.outputs.base_ref }})
if [ $? -ne 0 ]; then
>&2 echo "You may need 'fetch-depth: 2' for your checkout action."
exit 1
fi
DIFFS=$(echo "${DIFFS}" | xargs)
if [ "${{ inputs.output_diffs }}" = 'true' ]; then
echo '::group:: All files changed.'
echo "$(echo "${DIFFS}" | tr ' ' '\n')"
echo '::endgroup::'
fi
EXCLUDED=$(cat <<EOF
${{ inputs.exclude }}
EOF
)
EXCLUDED=$(echo "${EXCLUDED}" | xargs)
if [ "${{ inputs.output_diffs }}" = 'true' ]; then
echo '::group:: Exclusion group.'
echo "$(echo "${EXCLUDED}" | tr ' ' '\n')"
echo '::endgroup::'
fi
if [ -n "${EXCLUDED}" ]; then
echo "${EXCLUDED}" | tr ' ' '\n' | while read EXC; do
DIFFS=$(echo "${DIFFS}" | tr ' ' '\n' | grep -v "${EXC}")
done
fi
if [ "${{ inputs.output_diffs }}" = 'true' ]; then
echo '::group:: After filtering out excluded.'
echo "$(echo "${DIFFS}" | tr ' ' '\n')"
echo '::endgroup::'
fi
INCLUDED=$(cat <<EOF
${{ inputs.include }}
EOF
)
INCLUDED=$(echo "${INCLUDED}" | xargs)
if [ "${{ inputs.output_diffs }}" = 'true' ]; then
echo '::group:: Inclusion group.'
echo "$(echo "${INCLUDED}" | tr ' ' '\n')"
echo '::endgroup::'
fi
echo "${INCLUDED}" | tr ' ' '\n' | while read INC; do
if [ -n "${INC}" ]; then
MATCH=$(echo "${DIFFS}" | tr ' ' '\n' | grep "${INC}")
if [ $? -eq 0 ]; then
if [ "${{ inputs.output_diffs }}" = 'true' ]; then
echo "::group:: Matched on pattern ${INC}."
echo "$(echo "${MATCH}" | tr ' ' '\n')"
echo '::endgroup::'
fi
echo "Setting modified to true."
echo 'modified=true' >> $GITHUB_OUTPUT
break
fi
fi
done
# Explicitly set the exit code, otherwise the script will return the exit
# code of whatever last ran.
exit 0