From b77f8f08db14082bfbc3b51341734aebaa309fe7 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Mon, 29 Jan 2024 16:26:22 +0100
Subject: [PATCH] Run shellcheck in Ci for gitlab-tofu

---
 .gitlab-ci.yml                  | 14 ++++++++++++++
 .gitlab/scripts/fetch-states.sh |  2 +-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ceee10c..47299e3 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -111,6 +111,20 @@ check-backports:
     - make backports
     - git diff --exit-code
 
+shellcheck:
+  stage: test
+  needs: []
+  image: koalaman/shellcheck-alpine:v0.9.0
+  script:
+    - shellcheck ./src/gitlab-tofu.sh
+    - shellcheck ./.gitlab/scripts/*.sh
+  rules:
+    - changes:
+        - src/gitlab-tofu.sh
+        - .gitlab/scripts/*.sh
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+
 container_scanning:
   extends: .opentofu-versions
   rules:
diff --git a/.gitlab/scripts/fetch-states.sh b/.gitlab/scripts/fetch-states.sh
index 693739a..4a29828 100755
--- a/.gitlab/scripts/fetch-states.sh
+++ b/.gitlab/scripts/fetch-states.sh
@@ -34,7 +34,7 @@ while $has_next_page; do
     data=$(curl --silent --show-error --fail -H "Authorization: Bearer $GITLAB_TOKEN" -H "Content-Type: application/json" \
         "$CI_API_GRAPHQL_URL" \
         -X POST \
-        --data '[{ "operationName": "getStates", "variables": { "projectPath": "'$CI_PROJECT_PATH'", "first": 100, "after":'"$after"', "last": null, "before": null }, "query": "query getStates($projectPath: ID!, $first: Int, $last: Int, $before: String, $after: String) { project(fullPath: $projectPath) { id terraformStates(first: $first, last: $last, before: $before, after: $after) { count nodes { name updatedAt } pageInfo { hasNextPage endCursor } } } } " } ] '
+        --data '[{ "operationName": "getStates", "variables": { "projectPath": "'"$CI_PROJECT_PATH"'", "first": 100, "after":'"$after"', "last": null, "before": null }, "query": "query getStates($projectPath: ID!, $first: Int, $last: Int, $before: String, $after: String) { project(fullPath: $projectPath) { id terraformStates(first: $first, last: $last, before: $before, after: $after) { count nodes { name updatedAt } pageInfo { hasNextPage endCursor } } } } " } ] '
     )
 
     echo "$data" | jq --arg date "$FETCH_OLDER_THAN" -r '.[0]["data"]["project"]["terraformStates"]["nodes"][] | select(.updatedAt < $date) | "\(.name),\(.updatedAt)"'
-- 
GitLab