Skip to content

Commit 8810d4d

Browse files
authored
Lint and test service before pushing to main (#111)
* feat: Lint and test service SDK before pushing * feat: Beautify echo prints
1 parent fc09ddc commit 8810d4d

File tree

6 files changed

+51
-38
lines changed

6 files changed

+51
-38
lines changed

scripts/download-oas.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fi
2020
# Create temp directory to clone OAS repo
2121
work_dir=$(mktemp -d)
2222
if [[ ! ${work_dir} || -d {work_dir} ]]; then
23-
echo "Unable to create temporary directory"
23+
echo "! Unable to create temporary directory"
2424
exit 1
2525
fi
2626
trap "rm -rf ${work_dir}" EXIT # Delete temp directory on exit
@@ -33,7 +33,7 @@ fi
3333
# Move oas to root level
3434
mkdir ${ROOT_DIR}/oas
3535
cd ${work_dir}
36-
git clone ${OAS_REPO}
36+
git clone ${OAS_REPO} --quiet
3737

3838
for service_dir in ${work_dir}/${OAS_REPO_NAME}/services/*; do
3939
max_version_dir=""
@@ -55,8 +55,8 @@ for service_dir in ${work_dir}/${OAS_REPO_NAME}/services/*; do
5555
if [[ ${version} == *alpha* ]]; then
5656
# To support initial integrations of the IaaS API in an Alpha state, we will temporarily use it to generate an IaaS Alpha SDK module
5757
# This check can be removed once the IaaS API moves all endpoints to Beta
58-
if [[ ${service} == "iaas" ]]; then
59-
mv -f ${dir}/*.json ${ROOT_DIR}/oas/iaasalpha.json
58+
if [[ ${service} == "iaas" ]]; then
59+
mv -f ${dir}/*.json ${ROOT_DIR}/oas/iaasalpha.json
6060
fi
6161
if [[ ${ALLOW_ALPHA} != "true" ]]; then
6262
continue

scripts/generate-sdk/generate-sdk.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,30 @@ jar_path="${GENERATOR_PATH}/openapi-generator-cli.jar"
6565
if [ -e ${jar_path} ] && [ $(java -jar ${jar_path} version) == ${GENERATOR_VERSION_NUMBER} ]; then
6666
:
6767
else
68-
echo "Downloading OpenAPI generator (version ${GENERATOR_VERSION}) to ${GENERATOR_PATH}..."
68+
echo "Downloading OpenAPI generator (version ${GENERATOR_VERSION})..."
6969
mkdir -p ${GENERATOR_PATH}
70-
wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${GENERATOR_VERSION_NUMBER}/openapi-generator-cli-${GENERATOR_VERSION_NUMBER}.jar -O ${jar_path}
70+
wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${GENERATOR_VERSION_NUMBER}/openapi-generator-cli-${GENERATOR_VERSION_NUMBER}.jar -O ${jar_path} --quiet
7171
echo "Download done."
7272
fi
7373

7474
# Generate SDK for the specified language
7575
case "${LANGUAGE}" in
7676
go)
77-
echo -e "\nGenerating the Go SDK...\n"
77+
echo -e "\n>> Generating the Go SDK..."
7878

7979
source ${LANGUAGE_GENERATORS_FOLDER_PATH}/${LANGUAGE}.sh
8080
# Usage: generate_go_sdk GENERATOR_PATH GIT_HOST GIT_USER_ID [GIT_REPO_ID] [SDK_REPO_URL]
8181
generate_go_sdk ${jar_path} ${GIT_HOST} ${GIT_USER_ID} ${GIT_REPO_ID} ${SDK_REPO_URL}
8282
;;
8383
python)
84-
echo -e "\nGenerating the Python SDK...\n"
84+
echo -e "\n>> Generating the Python SDK..."
8585

8686
source ${LANGUAGE_GENERATORS_FOLDER_PATH}/${LANGUAGE}.sh
8787
# Usage: generate_python_sdk GENERATOR_PATH GIT_HOST GIT_USER_ID [GIT_REPO_ID] [SDK_REPO_URL]
8888
generate_python_sdk ${jar_path} ${GIT_HOST} ${GIT_USER_ID} ${GIT_REPO_ID} ${SDK_REPO_URL}
8989
;;
9090
*)
91-
echo "SDK language not supported."
91+
echo "! SDK language not supported."
9292
exit 1
9393
;;
9494
esac

scripts/generate-sdk/languages/go.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ generate_go_sdk() {
6262
if type -p go >/dev/null; then
6363
:
6464
else
65-
echo "Go not installed, unable to proceed."
65+
echo "! Go not installed, unable to proceed."
6666
exit 1
6767
fi
6868

@@ -80,7 +80,7 @@ generate_go_sdk() {
8080
# Backup of the current state of the SDK services dir (services/)
8181
sdk_services_backup_dir=$(mktemp -d)
8282
if [[ ! ${sdk_services_backup_dir} || -d {sdk_services_backup_dir} ]]; then
83-
echo "Unable to create temporary directory"
83+
echo "! Unable to create temporary directory"
8484
exit 1
8585
fi
8686
cleanup() {
@@ -128,7 +128,7 @@ generate_go_sdk() {
128128
exit 1
129129
fi
130130

131-
echo -e "\nGenerating \"${service}\" service..."
131+
echo -e "\n>> Generating \"${service}\" service..."
132132
cd ${ROOT_DIR}
133133

134134
GO_POST_PROCESS_FILE="gofmt -w" \
@@ -215,5 +215,4 @@ generate_go_sdk() {
215215
cd ${SDK_REPO_LOCAL_PATH}
216216
goimports -w ${SERVICES_FOLDER}/
217217
make sync-tidy
218-
219218
}

scripts/generate-sdk/languages/python.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ generate_python_sdk() {
2424

2525
# Check required parameters
2626
if [[ -z ${GIT_HOST} ]]; then
27-
echo "GIT_HOST not specified."
27+
echo "! GIT_HOST not specified."
2828
exit 1
2929
fi
3030

3131
if [[ -z ${GIT_USER_ID} ]]; then
32-
echo "GIT_USER_ID id not specified."
32+
echo "! GIT_USER_ID id not specified."
3333
exit 1
3434
fi
3535

@@ -46,7 +46,7 @@ generate_python_sdk() {
4646

4747
# Prepare folders
4848
if [[ ! -d $SERVICES_FOLDER ]]; then
49-
mkdir -p "$SERVICES_FOLDER"
49+
mkdir -p "$SERVICES_FOLDER"
5050
fi
5151

5252
# Clone SDK repo
@@ -63,7 +63,7 @@ generate_python_sdk() {
6363
# Backup of the current state of the SDK services dir (services/)
6464
sdk_services_backup_dir=$(mktemp -d)
6565
if [[ ! ${sdk_services_backup_dir} || -d {sdk_services_backup_dir} ]]; then
66-
echo "Unable to create temporary directory"
66+
echo "! Unable to create temporary directory"
6767
exit 1
6868
fi
6969
cleanup() {
@@ -75,7 +75,7 @@ generate_python_sdk() {
7575
trap cleanup EXIT
7676

7777
# Remove old contents of services dir (services/)
78-
rm -rf ${SERVICES_FOLDER}
78+
rm -rf ${SERVICES_FOLDER}
7979

8080
# Generate SDK for each service
8181
for service_json in ${ROOT_DIR}/oas/*.json; do
@@ -89,7 +89,7 @@ generate_python_sdk() {
8989
service=$(echo "${service}" | tr '[:upper:]' '[:lower:]') # convert upper case letters to lower case
9090
service=$(echo "${service}" | tr -d -c '[:alnum:]') # remove non-alphanumeric characters
9191

92-
echo "Generating \"${service}\" service..."
92+
echo ">> Generating \"${service}\" service..."
9393
cd ${ROOT_DIR}
9494

9595
mkdir -p "${SERVICES_FOLDER}/${service}/"
@@ -113,11 +113,11 @@ generate_python_sdk() {
113113
rm -r "${SERVICES_FOLDER}/${service}/.openapi-generator/"
114114
rm "${SERVICES_FOLDER}/${service}/stackit/__init__.py"
115115
rm "${SERVICES_FOLDER}/${service}/.github/workflows/python.yml"
116-
116+
117117
# Create source layout
118118
mkdir "${SERVICES_FOLDER}/${service}/src"
119119
mv "${SERVICES_FOLDER}/${service}/stackit/" "${SERVICES_FOLDER}/${service}/src/"
120-
120+
121121
# If the service has a wait package files, move them inside the service folder
122122
if [ -d ${sdk_services_backup_dir}/${service}/src/wait ]; then
123123
echo "Found ${service} \"wait\" package"
@@ -165,6 +165,6 @@ generate_python_sdk() {
165165
isort .
166166
autoimport --ignore-init-modules .
167167
black .
168-
168+
169169
done
170170
}

scripts/project.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ elif [ "$action" = "tools" ]; then
2727
elif [ "${LANGUAGE}" == "python" ]; then
2828
pip install black==24.8.0 isort~=5.13.2 autoimport~=1.6.1
2929
else
30-
echo "Invalid language: `$LANGUAGE`, please use $0 help for help"
30+
echo "! Invalid language: $($LANGUAGE), please use $0 help for help"
3131
fi
3232
else
33-
echo "Invalid action: '$action', please use $0 help for help"
33+
echo "! Invalid action: '$action', please use $0 help for help"
3434
fi

scripts/sdk-create-pr.sh

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ BRANCH_PREFIX=$1
1111
COMMIT_INFO=$2
1212

1313
if [ $# -lt 2 ]; then
14-
echo "Not enough arguments supplied. Required: 'branch-prefix' 'commit-info'"
14+
echo "! Not enough arguments supplied. Required: 'branch-prefix' 'commit-info'"
1515
exit 1
1616
fi
1717

1818
if type -p go >/dev/null; then
1919
:
2020
else
21-
echo "Go not installed, unable to proceed."
21+
echo "! Go not installed, unable to proceed."
2222
exit 1
2323
fi
2424

2525
if [ ! -d ${SDK_REPO_LOCAL_PATH} ]; then
26-
echo "sdk to commit not found in root. Please run make generate-sdk"
26+
echo "! SDK to commit not found in root. Please run make generate-sdk"
2727
exit 1
2828
fi
2929

@@ -41,7 +41,8 @@ else
4141
fi
4242

4343
# Create temp directory to work on
44-
work_dir=$(mktemp -d)
44+
work_dir="$ROOT_DIR/temp"
45+
mkdir -p ${work_dir}
4546
if [[ ! ${work_dir} || -d {work_dir} ]]; then
4647
echo "Unable to create temporary directory"
4748
exit 1
@@ -51,7 +52,7 @@ fi
5152
trap "rm -rf ${work_dir}" EXIT
5253

5354
mkdir ${work_dir}/git_repo # Where the git repo will be created
54-
mkdir ${work_dir}/sdk_backup # Backup of the SDK to check for new modules
55+
mkdir ${work_dir}/sdk_backup # Backup of the SDK to check for new modules
5556
mkdir ${work_dir}/sdk_to_push # Copy of SDK to push
5657

5758
# Prepare SDK to push
@@ -60,7 +61,7 @@ rm -rf ${work_dir}/sdk_to_push/.git
6061

6162
# Initialize git repo
6263
cd ${work_dir}/git_repo
63-
git clone ${REPO_URL_SSH} ./
64+
git clone ${REPO_URL_SSH} ./ --quiet
6465
git config user.name "${COMMIT_NAME}"
6566
git config user.email "${COMMIT_EMAIL}"
6667

@@ -74,37 +75,50 @@ for service_path in ${work_dir}/sdk_to_push/services/*; do
7475
# Removal of pulled data is necessary because the old version may have files
7576
# that were deleted in the new version
7677
rm -rf ./services/$service/*
77-
cp -a ${work_dir}/sdk_to_push/services/$service/. ./services/$service
78+
cp -a ${work_dir}/sdk_to_push/services/$service/. ./services/$service
7879

7980
# Check for changes in the specific folder compared to main
8081
service_changes=$(git status --porcelain "services/$service")
8182

8283
if [[ -n "$service_changes" ]]; then
83-
echo "Committing changes for $service"
84+
echo -e "\n>> Detected changes in $service service"
85+
86+
# If lint or test fails for a service, we skip it and continue to the next one
87+
make lint skip-non-generated-files=true service=$service || {
88+
echo "! Linting failed for $service. THE UPDATE OF THIS SERVICE WILL BE SKIPPED."
89+
continue
90+
}
91+
make test skip-non-generated-files=true service=$service || {
92+
echo "! Testing failed for $service. THE UPDATE OF THIS SERVICE WILL BE SKIPPED."
93+
continue
94+
}
95+
8496
if [[ "$BRANCH_PREFIX" != "main" ]]; then
8597
git switch main # This is needed to create a new branch for the service without including the previously committed files
8698
branch="$BRANCH_PREFIX/$service"
8799
git switch -c "$branch"
88100
else
89101
branch=$BRANCH_PREFIX
90-
fi
91-
102+
fi
103+
92104
git add services/${service}/
93105
if [ "${LANGUAGE}" == "go" ] && [ ! -d "${work_dir}/sdk_backup/services/${service}/" ]; then # Check if it is a newly added SDK module
94106
# go work use -r adds a use directive to the go.work file for dir, if it exists, and removes the use directory if the argument directory doesn’t exist
95107
# the -r flag examines subdirectories of dir recursively
96108
# this prevents errors if there is more than one new module in the SDK generation
97-
go work use -r .
109+
go work use -r .
98110
git add go.work
99111
fi
100-
112+
101113
if [[ "$branch" != "main" ]]; then
114+
echo ">> Creating PR for $service"
102115
git commit -m "Generate $service"
103116
git push origin "$branch"
104117
gh pr create --title "Generator: Update SDK /services/$service" --body "$COMMIT_INFO" --head "$branch" --base "main"
105118
else
119+
echo ">> Pushing changes for $service service..."
106120
git commit -m "Generate $service: $COMMIT_INFO"
107121
git push origin "$branch"
108-
fi
122+
fi
109123
fi
110-
done
124+
done

0 commit comments

Comments
 (0)