Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Verify repo revision #46

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/validate-manifests.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Manifests Validation
on: # yamllint disable-line rule:truthy
push:
branches:
- "*"
pull_request:
branches:
- "*"
Expand Down Expand Up @@ -78,6 +75,15 @@ jobs:
echo
echo "Charts successfully validated!"

validate-git-config:
runs-on: ubuntu-latest
steps:
- name: Code Checkout
uses: actions/checkout@v4
- name: Validate Repo and Revision
run: |
./scripts/validate_git_config.sh -g

check-spelling:
runs-on: ubuntu-latest
steps:
Expand Down
77 changes: 76 additions & 1 deletion scripts/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,81 @@ wait_for_openshift_gitops(){
done
}

get_cluster_branch() {
if [ -z "$1" ]; then
echo "No patch file supplied."
exit 1
else
PATCH_FILE=$1
fi

if [ -z "$2" ]; then
RETURN_LINE_NUMBER=false
else
RETURN_LINE_NUMBER=$2
fi

PATH_VALUE="/spec/source/targetRevision"

BRANCH=$(get_patch_value ${PATCH_FILE} ${PATH_VALUE} ${RETURN_LINE_NUMBER})

echo ${BRANCH}
}

get_cluster_repo() {
if [ -z "$1" ]; then
echo "No patch file supplied."
exit 1
else
PATCH_FILE=$1
fi

if [ -z "$2" ]; then
RETURN_LINE_NUMBER=false
else
RETURN_LINE_NUMBER=$2
fi

PATH_VALUE="/spec/source/repoURL"

REPO=$(get_patch_value ${PATCH_FILE} ${PATH_VALUE} ${RETURN_LINE_NUMBER})

echo ${REPO}
}

get_patch_value() {
if [ -z "$1" ]; then
echo "No patch file supplied."
exit 1
else
PATCH_FILE=$1
fi

if [ -z "$2" ]; then
echo "No path value supplied."
exit 1
else
PATH_VALUE=$2
fi

if [ -z "$3" ]; then
RETURN_LINE_NUMBER=false
else
RETURN_LINE_NUMBER=$3
fi

if ${RETURN_LINE_NUMBER}; then
query=".[] | select(.path == \"${PATH_VALUE}\") | .value | line"
else
query=".[] | select(.path == \"${PATH_VALUE}\") | .value"
fi

VALUE=$(yq -r "${query}" ${PATCH_FILE})

echo ${VALUE}
}


check_branch(){
if [ -z "$1" ]; then
echo "No cluster overlay supplied."
Expand All @@ -193,7 +268,7 @@ check_branch(){
echo "yq could not be found. We are unable to verify the branch of your repo."
else
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
APP_BRANCH=$(yq -r ".[1].value" ${APP_PATCH_FILE})
APP_BRANCH=$(get_cluster_branch ${APP_PATCH_FILE})
if [[ ${GIT_BRANCH} == ${APP_BRANCH} ]] ; then
echo "Your working branch ${GIT_BRANCH}, matches your cluster overlay branch ${APP_BRANCH}"
else
Expand Down
138 changes: 138 additions & 0 deletions scripts/validate_git_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/bin/bash
set -e

source "$(dirname "$0")/functions.sh"

CLUSTERS_FOLDER="clusters/overlays/*"
GIT_PATCH_FILE="patch-application-repo-revision.yaml"
EXPECTED_REPO="https://github.com/redhat-ai-services/ai-accelerator.git"
EXPECTED_BRANCH="main"

DEBUG=false
GITHUB=false

ERROR_DETECTED=false

help() {
echo "A script to help validate the repo and branch for the cluster."
echo
echo "usage:"
echo " $0 [-h|g]"
echo "options:"
echo "-h Print this help menu."
echo "-g Report error messages using the GitHub Actions annotation format."
}

verifiy_patch_file() {
if [ -z "$1" ]; then
echo "No patch file supplied."
exit 1
else
PATCH_FILE=$1
fi

if ${DEBUG}; then
echo "Verifying if ${PATCH_FILE} exists"
fi

if [ ! -f "${PATCH_FILE}" ]; then
echo "${PATCH_FILE} was not found."
ERROR_DETECTED=true
fi
}

verify_branch() {
if [ -z "$1" ]; then
echo "No patch file supplied."
exit 1
else
PATCH_FILE=$1
fi

if [ -z "$2" ]; then
echo "No expected branch supplied."
exit 1
else
EXPECTED_BRANCH=$2
fi

if ${DEBUG}; then
echo "Verifying if ${PATCH_FILE} is set to ${EXPECTED_BRANCH}"
fi

CLUSTER_BRANCH=$(get_cluster_branch ${PATCH_FILE})

if [[ "${CLUSTER_BRANCH}" != "${EXPECTED_BRANCH}" ]]; then

if ${GITHUB}; then
line_number=$(get_cluster_branch ${PATCH_FILE} true)
message="Expected \`${EXPECTED_BRANCH}\` but got \`${CLUSTER_BRANCH}\`"
echo "::error file=${PATCH_FILE},line=${line_number},col=10,title=Incorrect Branch::${message}"
else
echo "Expected ${PATCH_FILE} to be set to \`${EXPECTED_BRANCH}\` but got \`${CLUSTER_BRANCH}\`"
fi

ERROR_DETECTED=true
fi
}

verify_repo() {
if [ -z "$1" ]; then
echo "No patch file supplied."
exit 1
else
PATCH_FILE=$1
fi

if [ -z "$2" ]; then
echo "No expected repo supplied."
exit 1
else
EXPECTED_REPO=$2
fi

if ${DEBUG}; then
echo "Verifying if ${PATCH_FILE} is set to ${EXPECTED_REPO}"
fi

CLUSTER_REPO=$(get_cluster_repo ${PATCH_FILE})

if [[ "${CLUSTER_REPO}" != "${EXPECTED_REPO}" ]]; then

if ${GITHUB}; then
line_number=$(get_cluster_repo ${PATCH_FILE} true)
message="Expected \`${EXPECTED_REPO}\` but got \`${CLUSTER_REPO}\`"
echo "::error file=${PATCH_FILE},line=${line_number},col=10,title=Incorrect Repo URL::${message}"
else
echo "Expected ${PATCH_FILE} to be set to \`${EXPECTED_REPO}\` but got \`${CLUSTER_REPO}\`"
fi

ERROR_DETECTED=true
fi
}

main() {
for cluster in ${CLUSTERS_FOLDER}; do
if [ -d "${cluster}" ]; then
verifiy_patch_file "${cluster}/${GIT_PATCH_FILE}"
verify_branch "${cluster}/${GIT_PATCH_FILE}" ${EXPECTED_BRANCH}
verify_repo "${cluster}/${GIT_PATCH_FILE}" ${EXPECTED_REPO}

if ${ERROR_DETECTED}; then
exit 1
fi
fi
done
}

while getopts ":hg" option; do
case $option in
h) # display Help
help
exit;;
g)
GITHUB=true
esac
done

main