-
Notifications
You must be signed in to change notification settings - Fork 2
224 lines (194 loc) · 7.72 KB
/
diff.yaml
File metadata and controls
224 lines (194 loc) · 7.72 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
on:
pull_request:
branches: [main]
name: Crossplane Dynamic Diff
jobs:
pr:
name: prepare
runs-on: ubuntu-latest
steps:
- name: Setup QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: all
- name: Check out current commit
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v4
- name: Install yq
run: |
sudo add-apt-repository ppa:rmescandon/yq
sudo apt-get update
sudo apt-get install yq
- name: Install Crossplane CLI
run: go install github.com/crossplane/crossplane/cmd/crank@latest
# Discover YAML files and their annotations
- name: Discover YAML files
run: |
EXAMPLES_PATH="examples"
YAML_FILES=($(find $EXAMPLES_PATH -type f -name '*.yaml'))
INPUT_FILES=""
for file in "${YAML_FILES[@]}"; do
COMPOSITION=$(yq eval '.metadata.annotations."crossplane.io/render-composition-path"' $file)
FUNCTION=$(yq eval '.metadata.annotations."crossplane.io/render-function-path"' $file)
# Skip files where both annotations are null
if [[ "$COMPOSITION" == "null" && "$FUNCTION" == "null" ]]; then
continue
fi
INPUT_FILES+="$file,$COMPOSITION,$FUNCTION "
done
echo "INPUT_FILES=${INPUT_FILES}" >> $GITHUB_ENV
# Run a dynamic job for each input file
- name: Run Dynamic Jobs
run: |
IFS=' ' read -ra INPUT_FILES <<< "${{ env.INPUT_FILES }}"
for files in "${INPUT_FILES[@]}"; do
IFS=',' read -ra FILE_ARRAY <<< "$files"
FILE="${FILE_ARRAY[0]}"
COMPOSITION="${FILE_ARRAY[1]}"
FUNCTION="${FILE_ARRAY[2]}"
# Skip files where both annotations are null
if [[ "$COMPOSITION" == "null" && "$FUNCTION" == "null" ]]; then
continue
fi
crank beta render $FILE $COMPOSITION $FUNCTION >> diff-pr-${FILE//\//-}.yaml
done
- name: Install Python
uses: actions/setup-python@v4
# Sort YAML files
- name: Sort YAML files
run: |
for file in diff-pr-*.yaml; do
python3 -c "import yaml; data = list(yaml.load_all(open('$file'), Loader=yaml.FullLoader)); data.sort(key=lambda x: (x.get('kind', ''), str(x.get('metadata', {}).get('annotations', '')))); yaml.dump_all(data, open('$file', 'w'), default_flow_style=False)"
done
# Store outputs as artifacts
- name: Store outputs
uses: actions/upload-artifact@v3
with:
name: diff-pr-${{ github.sha }}
path: diff-*.yaml
main:
name: diff
needs: [pr]
runs-on: ubuntu-latest
steps:
- name: Setup QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: all
- name: Install Go
uses: actions/setup-go@v4
- name: Install yq
run: |
sudo add-apt-repository ppa:rmescandon/yq
sudo apt-get update
sudo apt-get install yq
- name: Install Crossplane CLI
run: go install github.com/crossplane/crossplane/cmd/crank@latest
- name: Check out main
uses: actions/checkout@v4
with:
ref: main
# Download artifacts from the dynamic job
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: diff-pr-${{ github.sha }}
# Discover YAML files and their annotations
- name: Discover YAML files
run: |
EXAMPLES_PATH="examples"
YAML_FILES=($(find $EXAMPLES_PATH -type f -name '*.yaml'))
INPUT_FILES=""
for file in "${YAML_FILES[@]}"; do
COMPOSITION=$(yq eval '.metadata.annotations."crossplane.io/render-composition-path"' $file)
FUNCTION=$(yq eval '.metadata.annotations."crossplane.io/render-function-path"' $file)
# Skip files where both annotations are null
if [[ "$COMPOSITION" == "null" && "$FUNCTION" == "null" ]]; then
continue
fi
INPUT_FILES+="$file,$COMPOSITION,$FUNCTION "
done
echo "INPUT_FILES=${INPUT_FILES}" >> $GITHUB_ENV
# Run a dynamic job for each input file
- name: Run Dynamic Jobs
run: |
IFS=' ' read -ra INPUT_FILES <<< "${{ env.INPUT_FILES }}"
for files in "${INPUT_FILES[@]}"; do
IFS=',' read -ra FILE_ARRAY <<< "$files"
FILE="${FILE_ARRAY[0]}"
COMPOSITION="${FILE_ARRAY[1]}"
FUNCTION="${FILE_ARRAY[2]}"
# Skip files where both annotations are null
if [[ "$COMPOSITION" == "null" && "$FUNCTION" == "null" ]]; then
continue
fi
crank beta render $FILE $COMPOSITION $FUNCTION >> diff-main-${FILE//\//-}.yaml
done
- name: Install Python
uses: actions/setup-python@v4
# Sort YAML files
- name: Sort YAML files
run: |
for file in diff-main-*.yaml; do
python3 -c "import yaml; data = list(yaml.load_all(open('$file'), Loader=yaml.FullLoader)); data.sort(key=lambda x: (x.get('kind', ''), str(x.get('metadata', {}).get('annotations', '')))); yaml.dump_all(data, open('$file', 'w'), default_flow_style=False)"
done
# Store outputs as artifacts
- name: Store outputs
uses: actions/upload-artifact@v2
with:
name: diff-main-${{ github.sha }}
path: diff-main-*.yaml
# Diff the outputs between PR and main
- name: Diff
id: diff_rev
run: |
IFS=',' read -ra INPUT_FILES <<< "$INPUT_FILES"
for FILE in "${INPUT_FILES[@]}"; do
IFS=' ' read -ra FILE_PATHS <<< "$FILE"
for SUB_FILE in "${FILE_PATHS[@]}"; do
MAIN_FILE="diff-main-${SUB_FILE//\//-}.yaml"
PR_FILE="diff-pr-${SUB_FILE//\//-}.yaml"
echo "Checking differences for file: $SUB_FILE"
echo "Main file path: $MAIN_FILE"
echo "PR file path: $PR_FILE"
if [ -f "$MAIN_FILE" ] && [ -f "$PR_FILE" ]; then
DELTA_NAME=$(echo "DELTA_${SUB_FILE//[\./]/_}" | tr '[:upper:]' '[:lower:]' | sed 's/\//_/g')
DELTA_FILE="$DELTA_NAME.diff"
echo "Setting DELTA_NAME: $DELTA_NAME"
DELTA=$(diff -u "$MAIN_FILE" "$PR_FILE" || true)
echo "$DELTA" > "$DELTA_FILE"
echo "Differences found for $SUB_FILE. Diff saved in $DELTA_FILE"
else
echo "Either $MAIN_FILE or $PR_FILE does not exist!"
fi
done
done
- name: Generate Comment
id: generate_comment
run: |
ALL_DELTAS=""
# Iterate over all diff files
for DELTA_FILE in $(find . -name '*.diff'); do
echo "Processing diff file: $DELTA_FILE"
# Debugging statement
cat "$DELTA_FILE"
# Ensure the file exists and is not empty
if [ -s "$DELTA_FILE" ]; then
# Use printf to format newlines properly
ALL_DELTAS="${ALL_DELTAS}$(printf '\n```diff\n%s\n```' "$(cat "$DELTA_FILE")")"
echo "ALL_DELTAS so far: $ALL_DELTAS"
else
echo "Diff file $DELTA_FILE is either empty or not found!"
fi
done
# Debugging statement
echo "Final ALL_DELTAS: $ALL_DELTAS"
# Write the content to a file
echo -e "$ALL_DELTAS" > diff_content.txt
- name: comment PR
uses: machine-learning-apps/pr-comment@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
path: diff_content.txt