Skip to content

Commit cb0b59a

Browse files
committed
using redis to store fetched state to ensure no versions are skipped
1 parent dbad956 commit cb0b59a

5 files changed

Lines changed: 138 additions & 7 deletions

File tree

assets/check

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
# vim: set ft=sh
33

4-
set -ex
4+
set -e
55

66
exec 3>&1 # make stdout available as fd 3 for the result
77
exec 1>&2 # redirect all output to stderr for logging
@@ -87,13 +87,18 @@ get_branch_refs() {
8787
local branch_ref
8888
local branch
8989

90+
local ancestry_prefix="${redis_prefix:+"${redis_prefix}:"}ancestry:"
91+
local fetched_prefix="${redis_prefix:+"${redis_prefix}:"}fetched:"
92+
93+
9094
# Get the redis connection options
9195
if [ -n "$redis_host" ] ; then
9296
db_opt="${redis_dbnum:+" -n $redis_dbnum"}"
9397
pw_opt="${redis_password:+" -a '$redis_password'"}"
94-
key="${redis_prefix:+"${redis_prefix}-"}${last_refs}"
98+
ancestry_key="${ancestry_prefix}${last_refs%% *}"
99+
fetched_key="${fetched_prefix}${last_refs%% *}"
95100

96-
historic_branches="$(eval "redis-cli -h '${redis_host}'${db_opt}${pw_opt} get '$key'" | jq -Rr .)"
101+
historic_branches="$(eval "redis-cli -h '${redis_host}'${db_opt}${pw_opt} get '$ancestry_key'" | jq -Rr .)"
97102
case $historic_branches in
98103
*"NOAUTH Authentication required"* )
99104
echo "Could not authenticate on redis server: $historic_branches" >&2
@@ -105,6 +110,14 @@ get_branch_refs() {
105110
;;
106111
esac
107112

113+
# Check if the last references was fetched
114+
last_ref_fetched="$(eval "redis-cli -h '${redis_host}'${db_opt}${pw_opt} --raw get '$fetched_key'" | jq -Rr .)"
115+
if [ -n "$last_refs" -a "$last_ref_fetched" != "true" ]
116+
then
117+
echo nothing new >&2
118+
return 0
119+
fi
120+
108121
last_refs="${last_refs}${historic_branches:+" $historic_branches"}"
109122
fi
110123

@@ -150,12 +163,18 @@ INLINE
150163
echo "$results" | awk '{print $1 ":" "'"$changed_branch${remaining_branches:+" $remaining_branches"}"'" }'
151164
else
152165
for ref in $results; do
153-
key="${redis_prefix:+"${redis_prefix}-"}${ref}:${changed_branch}"
154-
redis_result="$(eval "redis-cli -h '${redis_host}'${db_opt}${pw_opt} set '${key}' '$remaining_branches'")"
166+
ancestry_key="${ancestry_prefix}${ref}:${changed_branch}"
167+
fetched_key="${fetched_prefix}${ref}:${changed_branch}"
168+
redis_result="$(eval "redis-cli -h '${redis_host}'${db_opt}${pw_opt} set '${ancestry_key}' '$remaining_branches'")"
155169
if [ "${redis_result}" != "OK" ] ; then
156170
echo "Could not store historic branches: $redis_result" >&2
157171
exit 1
158172
fi
173+
redis_result="$(eval "redis-cli -h '${redis_host}'${db_opt}${pw_opt} set '${fetched_key}' 'false'")"
174+
if [ "${redis_result}" != "OK" ] ; then
175+
echo "Could not record ref not fetched: $redis_result" >&2
176+
exit 1
177+
fi
159178
done
160179

161180
echo "$results" | awk '{print $1 ":" "'"$changed_branch"'" }'

assets/in

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ depth=$(jq -r '(.params.depth // 0)' < $payload)
3232
fetch=$(jq -r '(.params.fetch // [])[]' < $payload)
3333
submodules=$(jq -r '(.params.submodules // "all")' < $payload)
3434

35+
## Redis stuff to support cleaner multibranch wait-for-previous behaviour
36+
redis_host=$(jq -r '.source.redis.host // ""' < $payload)
37+
if [ -n "$redis_host" ] ; then
38+
redis_password=$(jq -r '.source.redis.password // ""' < $payload)
39+
redis_dbnum=$(jq -r '.source.redis.db_number // "0"' < $payload)
40+
redis_prefix=$(jq -r '.source.redis.prefix // ""' < $payload)
41+
fi
42+
3543
[[ -z "$fetch" ]] && single_branch_flag="--single-branch " || single_branch_flag=''
3644

3745
if [ -z "$uri" ]
@@ -101,6 +109,18 @@ do
101109
fi
102110
done
103111

112+
if [ -n "$redis_host" ]
113+
then
114+
db_opt="${redis_dbnum:+" -n $redis_dbnum"}"
115+
pw_opt="${redis_password:+" -a '$redis_password'"}"
116+
key="${redis_prefix:+"${redis_prefix}:"}fetched:${ref}:${branch}"
117+
redis_result="$(eval "redis-cli -h '${redis_host}'${db_opt}${pw_opt} set '${key}' 'true'")"
118+
if [ "${redis_result}" != "OK" ] ; then
119+
echo "Could not record that ref has been fetched: $redis_result" >&2
120+
exit 1
121+
fi
122+
fi
123+
104124
new_ref="$(echo "$(git rev-parse HEAD)$ref_extras" | jq -R .)"
105125
jq -n "{
106126
version: {ref: $new_ref},

test/check_multi.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,25 @@ it_can_find_successive_branches_with_multiple_commits_with_redis() {
220220
echo "ref7:another $ref7"
221221
echo "ref8:another $ref8"
222222

223+
test_check uri $repo branches '.*' from "$ref1:master $ref2:bogus $ref3:another" redis "testing" | jq -e "
224+
. == []
225+
"
226+
223227
if [ "$ref6" \< "$ref8" ] ; then
224228

225229
echo "master is least commit"
230+
231+
echo "---------> TEST M1"
232+
set_ref_fetched "testing" $ref1:master
226233
test_check uri $repo branches '.*' from "$ref1:master $ref2:bogus $ref3:another" redis "testing" | jq -e "
227234
. == [
228235
{ref: $(echo "$ref5:master" | jq -R .)},
229236
{ref: $(echo "$ref6:master" | jq -R .)}
230237
]
231238
"
239+
240+
echo "---------> TEST M2"
241+
set_ref_fetched "testing" $ref6:master
232242
test_check uri $repo branches '.*' from "$ref6:master" redis "testing" | jq -e "
233243
. == [
234244
{ref: $(echo "$ref4:another" | jq -R .)},
@@ -237,6 +247,8 @@ it_can_find_successive_branches_with_multiple_commits_with_redis() {
237247
]
238248
"
239249

250+
echo "---------> TEST M3"
251+
set_ref_fetched "testing" $ref8:another
240252
test_check uri $repo branches '.*' from "$ref8:another" redis "testing" | jq -e "
241253
. == []
242254
"
@@ -249,19 +261,29 @@ it_can_find_successive_branches_with_multiple_commits_with_redis() {
249261

250262
else
251263
echo "another is least commit"
264+
265+
echo "---------> TEST A1"
266+
267+
set_ref_fetched "different-key" $ref1:master
252268
test_check uri $repo branches '.*' from "$ref1:master $ref2:bogus $ref3:another" redis "different-key" | jq -e "
253269
. == [
254270
{ref: $(echo "$ref4:another" | jq -R .)},
255271
{ref: $(echo "$ref7:another" | jq -R .)},
256272
{ref: $(echo "$ref8:another" | jq -R .)}
257273
]
258274
"
275+
276+
echo "---------> TEST A2"
277+
set_ref_fetched "different-key" $ref8:another
259278
test_check uri $repo branches '.*' from "$ref8:another" redis "different-key"| jq -e "
260279
. == [
261280
{ref: $(echo "$ref5:master" | jq -R .)},
262281
{ref: $(echo "$ref6:master" | jq -R .)}
263282
]
264283
"
284+
285+
echo "---------> TEST A3"
286+
set_ref_fetched "different-key" $ref6:master
265287
test_check uri $repo branches '.*' from "$ref6:master" redis "different-key" | jq -e "
266288
. == []
267289
"

test/get.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,46 @@ it_can_get_from_url_at_multibranch_ref() {
205205

206206
}
207207

208+
it_can_get_from_url_at_multibranch_ref_with_redis() {
209+
210+
set +u
211+
if [ "$ENABLE_REDIS_TESTS" != "TRUE" ] ; then
212+
echo "Skipping Redis tests because \$ENABLE_REDIS_TESTS not set to 'TRUE'"
213+
return 0
214+
elif [ "$(redis-cli ping)" != "PONG" ] ; then
215+
echo "Skipping Redis tests because redis not installed or not reachable"
216+
return 0
217+
fi
218+
set -u
219+
220+
local repo=$(init_repo)
221+
local ref1=$(make_commit_to_branch $repo branch-a)
222+
local ref2=$(make_commit_to_branch $repo branch-b)
223+
224+
local dest=$TMPDIR/destination
225+
226+
test_get $dest uri $repo ref "$ref1:branch-a" redis "testing" | jq -e "
227+
.version == {ref: $(echo "$ref1:branch-a" | jq -R .)}
228+
"
229+
230+
test_ref_fetched "testing" $ref2:branch-a
231+
232+
test -e $dest/some-file
233+
test "$(git -C $dest rev-parse HEAD)" = $ref1
234+
235+
rm -rf $dest
236+
237+
test_get $dest uri $repo ref "$ref2:branch-b" redis "testing" | jq -e "
238+
.version == {ref: $(echo "$ref2:branch-b" | jq -R .)}
239+
"
240+
241+
test_ref_fetched "testing" $ref2:branch-b
242+
243+
test -e $dest/some-file
244+
test "$(git -C $dest rev-parse HEAD)" = $ref2
245+
246+
}
247+
208248
run it_can_get_from_url
209249
run it_can_get_from_url_at_ref
210250
run it_can_get_from_url_from_a_multibranch_ref
@@ -214,3 +254,4 @@ run it_can_get_multiple_branches_using_fetch
214254
run it_honors_the_depth_flag
215255
run it_honors_the_depth_flag_for_submodules
216256
run it_can_get_from_url_at_multibranch_ref
257+
run it_can_get_from_url_at_multibranch_ref_with_redis

test/helpers.sh

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ test_get() {
282282
}")"
283283
shift;;
284284

285+
"redis" )
286+
addition="$(jq -n "{
287+
source: {
288+
redis: {
289+
host: $(echo "localhost" | jq -R '.'),
290+
prefix: $(echo "$1" | jq -R '.')
291+
}
292+
}
293+
}")"
294+
shift;;
295+
285296
* )
286297
if [ -z ${destination+is_set} ] ; then
287298
destination=$arg
@@ -308,9 +319,9 @@ test_get() {
308319
}
309320

310321
test_redis() {
311-
result="$(redis-cli get "${1:+${1}-}$2")"
322+
result="$(redis-cli get "${1:+${1}:}ancestry:$2")"
312323
if [ "$result" != "$3" ] ; then
313-
echo "Expected redis-cli get \"${1:+${1}-}$2\" to return:"
324+
echo "Expected redis-cli get \"${1:+${1}:}$2\" to return:"
314325
echo "$3"
315326
echo ""
316327
echo "Instead, it returned:"
@@ -320,6 +331,24 @@ test_redis() {
320331
fi
321332
}
322333

334+
set_ref_fetched() {
335+
redis-cli set "${1:+${1}:}fetched:$2" "${3:-true}"
336+
}
337+
338+
test_ref_fetched() {
339+
expected="${3:-true}"
340+
result="$(redis-cli --raw get "${1:+${1}:}fetched:$2")"
341+
if [ "$result" != "${expected}" ] ; then
342+
echo "Expected redis-cli get \"${1:+${1}:}fetched:$2\" to return:"
343+
echo "${expected}"
344+
echo ""
345+
echo "Instead, it returned:"
346+
echo "$result"
347+
echo ""
348+
return 1
349+
fi
350+
}
351+
323352
test_put() {
324353
echo ''
325354
}

0 commit comments

Comments
 (0)