summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/templates/Jobs/Secret-Detection.gitlab-ci.yml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/ci/templates/Jobs/Secret-Detection.gitlab-ci.yml')
-rw-r--r--lib/gitlab/ci/templates/Jobs/Secret-Detection.gitlab-ci.yml27
1 files changed, 17 insertions, 10 deletions
diff --git a/lib/gitlab/ci/templates/Jobs/Secret-Detection.gitlab-ci.yml b/lib/gitlab/ci/templates/Jobs/Secret-Detection.gitlab-ci.yml
index 42487cc0c67..4e4f96bc7c7 100644
--- a/lib/gitlab/ci/templates/Jobs/Secret-Detection.gitlab-ci.yml
+++ b/lib/gitlab/ci/templates/Jobs/Secret-Detection.gitlab-ci.yml
@@ -32,15 +32,22 @@ secret_detection:
- if [ -n "$CI_COMMIT_TAG" ]; then echo "Skipping Secret Detection for tags. No code changes have occurred."; exit 0; fi
- if [ "$CI_COMMIT_BRANCH" = "$CI_DEFAULT_BRANCH" ]; then echo "Running Secret Detection on default branch."; /analyzer run; exit 0; fi
- |
- git fetch origin $CI_DEFAULT_BRANCH $CI_COMMIT_REF_NAME
- git log --left-right --cherry-pick --pretty=format:"%H" refs/remotes/origin/${CI_DEFAULT_BRANCH}..refs/remotes/origin/${CI_COMMIT_REF_NAME} >${CI_COMMIT_SHA}_commit_list.txt
- if [[ $(wc -l <${CI_COMMIT_SHA}_commit_list.txt) -eq "0" ]]; then
- # if git log produces 0 or 1 commits we should scan $CI_COMMIT_SHA only
- export SECRET_DETECTION_COMMITS=$CI_COMMIT_SHA
- else
- # +1 because busybox wc only countsĀ \n and there is no trailing \n
- echo "scanning $(($(wc -l <${CI_COMMIT_SHA}_commit_list.txt) + 1)) commits"
- export SECRET_DETECTION_COMMITS_FILE=${CI_COMMIT_SHA}_commit_list.txt
- fi
+ # we don't need the whole history when excluding in the next `git fetch` line,
+ # so git depth=1
+ git fetch origin --depth=1 $CI_DEFAULT_BRANCH
+ # shallow clone $CI_COMMIT_REF_NAME to get commits associated with MR or push
+ git fetch --shallow-exclude=${CI_DEFAULT_BRANCH} origin $CI_COMMIT_REF_NAME
+ # determine what commits we need to scan using "git log A..B"
+ git log --no-merges --pretty=format:"%H" refs/remotes/origin/${CI_DEFAULT_BRANCH}..refs/remotes/origin/${CI_COMMIT_REF_NAME} >${CI_COMMIT_SHA}_commit_list.txt
+
+ # we need to extend the git fetch depth to the number of commits + 2 for the following reasons:
+ # because busybox wc only counts \n and there is no trailing \n (+1)
+ # include the parent commit of the base commit in this MR/Push event. This is needed because
+ # `git diff -p` needs something to compare changes in that commit against (+1)
+ git fetch --depth=$(($(wc -l <${CI_COMMIT_SHA}_commit_list.txt) + 2)) origin $CI_COMMIT_REF_NAME
+
+ # +1 because busybox wc only counts \n and there is no trailing \n
+ echo "scanning $(($(wc -l <${CI_COMMIT_SHA}_commit_list.txt) + 1)) commits"
+ export SECRET_DETECTION_COMMITS_FILE=${CI_COMMIT_SHA}_commit_list.txt
- /analyzer run
- rm "$CI_COMMIT_SHA"_commit_list.txt