forked from vanderbilt-redcap/redcap_rsvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_feature_changes.sh
More file actions
283 lines (233 loc) · 7.01 KB
/
Copy pathfind_feature_changes.sh
File metadata and controls
283 lines (233 loc) · 7.01 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
set -e
# Load environment variables from .env file
if [ -f .env ]; then
source .env
fi
# Default values
prompt_mode=false
upload=false
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
--cur_tag)
CUR_TAG="$2"
shift 2
;;
--comp_tag)
COMP_TAG="$2"
shift 2
;;
--start_date)
START_DATE="$2"
shift 2
;;
--end_date)
END_DATE="$2"
shift 2
;;
--prompt)
prompt_mode=true
shift
;;
--upload)
upload=true
shift
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
done
# Handle prompt mode
if [[ "$prompt_mode" = true ]]; then
echo "Choose Mode"
echo "1) Tags"
echo "2) Dates"
read -rp "Enter 1 or 2: " choice
case "$choice" in
1)
read -rp "Enter current tag: " CUR_TAG
read -rp "Enter comparison tag: " COMP_TAG
;;
2)
read -rp "Enter start date (YYYY-MM-DD): " START_DATE
read -rp "Enter end date (YYYY-MM-DD): " END_DATE
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac
fi
awk_script='
{
orig_path = $3
path = orig_path
old_path = ""
if (path ~ /\{.*=>.*\}/) {
prefix = substr(path, 1, match(path, /\{/) - 1)
inside = substr(path, match(path, /\{/) + 1)
inside = substr(inside, 1, length(inside) - 1)
split(inside, parts, /=>/)
old_part = parts[1]
new_part = parts[2]
gsub(/^[ \t]+|[ \t]+$/, "", old_part)
gsub(/^[ \t]+|[ \t]+$/, "", new_part)
old_path = prefix old_part
path = prefix new_part
old_paths[old_path] = 1
}
if (path ~ /\.feature$/) {
added[path] += $1
deleted[path] += $2
files[path] = 1
}
}
#We ingest a temporary file of all features here so we do not miss any
BEGIN {
while ((getline line < "all_features.txt.tmp") > 0) {
all_files[line] = 1
}
}
END {
count = 1
# Remove any files that were old paths in renames
for (op in old_paths) {
delete files[op]
delete added[op]
delete deleted[op]
}
total_added = 0
total_deleted = 0
printf "%10s %10s %10s %s\n", "Added", "Deleted", "Total", "File"
for (file in all_files) {
file_added = added[file]
file_deleted = deleted[file]
file_total = file_added + file_deleted
total_added += file_added
total_deleted += file_deleted
n = split(file, path_parts, "/")
base = path_parts[n]
sub(/\.feature$/, "", base)
split(base, parts, "\\.")
letter = toupper(parts[1])
part1 = parts[2]
part2 = parts[3]
part3 = sprintf("%04d", parts[4])
if (letter != "A" && letter != "B" && letter != "C") continue
feature = letter "." part1 "." part2 "." part3 "."
mod = (file_added < file_deleted ? file_added : file_deleted)
pure_added = file_added - mod
pure_deleted = file_deleted - mod
#Outputting this is helpful so we know how many features we have
printf count
count++
if (upload == "true") {
#Skip these files for upload
# A.999.999.999 is just an example. If we need to skip features going forward we should consider skipping those where NewManual records exist.
if( feature == "A.999.999.999"){
#Do nothing here because these records do not exist within the VUMC REDCap project for one reason or another
#NOTE: This skip block will probably change with each new version of LTS tested
} else {
if(file_added == 0 && file_deleted == 0){
printf feature
cmd = "sh push_lines_changes.sh \"" feature "\" " 0 " " 0 " " 0 " " 0 " " 0 " " 0
return_code = system(cmd)
} else {
cmd = "sh push_lines_changes.sh \"" feature "\" " \
file_added " " \
pure_added " " \
file_deleted " " \
pure_deleted " " \
file_total " " \
mod
return_code = system(cmd)
}
}
if (return_code != 0) {
print "push_lines_changes.sh failed on " feature
exit 1
}
printf "%10d %10d %10d %s - UPLOADED\n", file_added, file_deleted, file_total, file
} else {
printf "%10d %10d %10d %s\n", file_added, file_deleted, file_total, file
}
}
printf "%s\n", "---------------------------------------------------------------"
printf " %10d %10d %10d %s\n", total_added, total_deleted, total_added + total_deleted, "TOTAL"
}'
# Validate input
if [[ -n "$CUR_TAG" && -n "$COMP_TAG" ]]; then
echo "Using tags: CURRENT_TAG=$CUR_TAG vs COMPARISON_TAG=$COMP_TAG"
#Only if empty Start Date
if [ -z $START_DATE ]; then
START_DATE=$(git log -1 --format=%ad --date=short $COMP_TAG)
fi
#Only if empty End Date
if [ -z $END_DATE ]; then
END_DATE=$(git log -1 --format=%ad --date=short $CUR_TAG)
fi
elif [[ -n "$START_DATE" && -n "$END_DATE" ]]; then
echo "Using dates: START_DATE=$START_DATE => END_DATE=$END_DATE"
else
#Only if empty Current Tag
if [ -z $CUR_TAG ]; then
CUR_TAG=$(git tag --sort=-v:refname | head -n 1)
fi
#Only if empty Comparison Tag
if [ -z $COMP_TAG ]; then
COMP_TAG=$(git tag --sort=-creatordate | sed -n 2p)
fi
#Only if empty Start Date
if [ -z $START_DATE ]; then
START_DATE=$(git log -1 --format=%ad --date=short $COMP_TAG)
fi
#Only if empty End Date
if [ -z $END_DATE ]; then
END_DATE=$(git log -1 --format=%ad --date=short $CUR_TAG)
fi
printf "DEFAULT MODE: Takes the most recent tag and compares it to the previous tag.\n"
fi
if [ "$upload" = true ]; then
if [ -z "$REDCAP_API_TOKEN" ]; then
echo "Environment variable REDCAP_API_TOKEN is not set. Exiting."
exit 1
fi
if [ -z $CUR_TAG ]; then
echo "Tags must be used when uploading to ensure we're uploading to the matching REDCap project."
exit
fi
CURL=`which curl`
if [ "$REDCAP_API_URL" == "https://redcap.loc/api/" ]; then
CURL="$CURL --ssl-revoke-best-effort"
fi
PROJECT_LTS_VERSION=`$CURL -X POST "$REDCAP_API_URL" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "token=$REDCAP_API_TOKEN" \
-d "content=project" \
-d "format=json" \
-d "returnFormat=json" |cut -d'"' -f 6|cut -d' ' -f 6`
if [[ "$CUR_TAG" != "V$PROJECT_LTS_VERSION-ABC" ]]; then
echo "Tag and project LTS version do not match ($CUR_TAG vs. $PROJECT_LTS_VERSION)"
exit
fi
fi
#Generate a temporary file of all features
ALL_FEATURE_FILES=$(git ls-files '*.feature')
echo "$ALL_FEATURE_FILES" > all_features.txt.tmp
# Get the line changes for .feature files between the dates
git log --since="$START_DATE" --until="$END_DATE" --pretty=tformat: --numstat --ignore-space-change | awk -F'\t' -v upload="$upload" "$awk_script"
#Remove the temporary file
rm all_features.txt.tmp
if [ -z $CUR_TAG ]; then
echo ""
else
printf "\nTAGS: \n CURRENT_TAG: $CUR_TAG \n COMPARISON_TAG: $COMP_TAG\n"
fi
printf "DATES:\n START_DATE: $START_DATE \n END_DATE: $END_DATE\n"
if [ "$upload" = false ]; then
echo
echo "Add the '--upload' argument to upload these results."
fi