Skip to content

Commit 30c1e23

Browse files
authored
Allow for returning more than just the latest version in a check (#346)
* Allow for returning more than just the latest version in a check Signed-off-by: Aaron George <[email protected]>
1 parent d567f58 commit 30c1e23

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Tracks the commits in a [git](http://git-scm.com/) repository.
117117
*MUST* be included in commit messages for the commit to not be
118118
skipped
119119

120+
* `version_depth`: *Optional.* The number of versions to return when performing a check
121+
120122
### Example
121123

122124
Resource configuration for a private repo with an HTTPS proxy:

assets/check

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ref=$(jq -r '.version.ref // ""' < $payload)
3131
skip_ci_disabled=$(jq -r '.source.disable_ci_skip // false' < $payload)
3232
filter_whitelist=$(jq -r '.source.commit_filter.include // []' < $payload)
3333
filter_blacklist=$(jq -r '.source.commit_filter.exclude // []' < $payload)
34+
version_depth=$(jq -r '.source.version_depth // 1' < $payload)
3435
reverse=false
3536

3637
configure_git_global "${git_config_payload}"
@@ -126,7 +127,7 @@ get_commit(){
126127
#if no range is selected just grab the last commit that fits the filter
127128
if [ -z "$log_range" ]
128129
then
129-
list_command+="| git rev-list --stdin --date-order --no-walk=unsorted -1"
130+
list_command+="| git rev-list --stdin --date-order --no-walk=unsorted -$version_depth --reverse"
130131
fi
131132

132133
if [ "$reverse" == "true" ]
@@ -147,7 +148,7 @@ if [ -n "$tag_filter" ]; then
147148
if [ -n "$branch" ]; then
148149
branch_flag="--merged $branch"
149150
fi
150-
tag=$(git tag --list "$tag_filter" --sort=creatordate $branch_flag | tail -1)
151+
tag=$(git tag --list "$tag_filter" --sort=creatordate $branch_flag | tail -$version_depth)
151152
get_commit $tag
152153
fi
153154
} | jq -s "map(.)" >&3
@@ -164,7 +165,7 @@ elif [ -n "$tag_regex" ]; then
164165
if [ -n "$branch" ]; then
165166
branch_flag="--merged $branch"
166167
fi
167-
tag=$(git tag --list --sort=creatordate $branch_flag | grep -Ex "$tag_regex" | tail -1)
168+
tag=$(git tag --list --sort=creatordate $branch_flag | grep -Ex "$tag_regex" | tail -$version_depth)
168169
get_commit $tag
169170
fi
170171
} | jq -s "map(.)" >&3

test/check.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,42 @@ it_can_check_a_repo_having_multiple_root_commits() {
902902
"
903903
}
904904

905+
it_checks_with_version_depth() {
906+
local repo=$(init_repo)
907+
local ref1=$(make_commit_to_future $repo)
908+
local ref2=$(make_commit $repo)
909+
local ref3=$(make_commit $repo)
910+
check_uri_with_version_depth $repo 2 | jq -e "
911+
. == [
912+
{ref: $(echo $ref2 | jq -R .)},
913+
{ref: $(echo $ref3 | jq -R .)}
914+
]
915+
"
916+
}
917+
918+
it_checks_uri_with_tag_filter_and_version_depth() {
919+
local repo=$(init_repo)
920+
local ref1=$(make_commit $repo)
921+
make_annotated_tag $repo "test.tag.1" "tag ref1"
922+
local ref2=$(make_commit $repo)
923+
make_annotated_tag $repo "test.tag.2" "tag ref2"
924+
local ref3=$(make_commit $repo)
925+
make_annotated_tag $repo "test.badtag.3" "tag ref3"
926+
local ref4=$(make_commit $repo)
927+
make_annotated_tag $repo "test.tag.4" "tag ref4"
928+
check_uri_with_tag_filter_and_version_depth $repo 2 "test.tag.*" | jq -e "
929+
. == [
930+
{
931+
ref: \"test.tag.2\",
932+
commit: $(echo $ref2 | jq -R .)
933+
},
934+
{
935+
ref: \"test.tag.4\",
936+
commit: $(echo $ref4 | jq -R .)
937+
}
938+
]"
939+
}
940+
905941
run it_can_check_from_head
906942
run it_can_check_from_a_ref
907943
run it_can_check_from_a_first_commit_in_repo
@@ -947,3 +983,6 @@ run it_can_check_with_tag_filter_given_branch_first_ref
947983
run it_can_check_with_tag_regex_given_branch_first_ref
948984
run it_checks_lastest_commit
949985
run it_can_check_a_repo_having_multiple_root_commits
986+
run it_checks_with_version_depth
987+
run it_checks_uri_with_tag_filter_and_version_depth
988+

test/helpers.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,25 @@ check_uri_from() {
377377
}" | ${resource_dir}/check | tee /dev/stderr
378378
}
379379

380+
check_uri_with_version_depth() {
381+
jq -n "{
382+
source: {
383+
uri: $(echo $1 | jq -R .),
384+
version_depth: $(echo $2 | jq -R .)
385+
}
386+
}" | ${resource_dir}/check | tee /dev/stderr
387+
}
388+
389+
check_uri_with_tag_filter_and_version_depth() {
390+
jq -n "{
391+
source: {
392+
uri: $(echo $1 | jq -R .),
393+
version_depth: $(echo $2 | jq -R .),
394+
tag_filter: $(echo $3 | jq -R .)
395+
}
396+
}" | ${resource_dir}/check | tee /dev/stderr
397+
}
398+
380399
check_uri_from_ignoring() {
381400
local uri=$1
382401
local ref=$2

0 commit comments

Comments
 (0)