-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsdk-create-pr.sh
More file actions
executable file
·159 lines (136 loc) · 6.1 KB
/
sdk-create-pr.sh
File metadata and controls
executable file
·159 lines (136 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
# This script pushes the generated SDK to its repo and creates a PR
set -eo pipefail
ROOT_DIR=$(git rev-parse --show-toplevel)
COMMIT_NAME="SDK Generator Bot"
COMMIT_EMAIL="noreply@stackit.de"
SDK_REPO_LOCAL_PATH="${ROOT_DIR}/sdk-repo-updated" # Comes from generate-sdk.sh
BRANCH_PREFIX=$1
COMMIT_INFO=$2
if [ $# -lt 2 ]; then
echo "! Not enough arguments supplied. Required: 'branch-prefix' 'commit-info'"
exit 1
fi
if type -p go >/dev/null; then
:
else
echo "! Go not installed, unable to proceed."
exit 1
fi
if [ ! -d "${SDK_REPO_LOCAL_PATH}" ]; then
echo "! SDK to commit not found in root. Please run make generate-sdk"
exit 1
fi
if [[ -z $3 ]]; then
REPO_URL_SSH="git@github.com:stackitcloud/stackit-sdk-go.git"
else
REPO_URL_SSH=$3
fi
if [[ -z $4 ]]; then
echo "LANGUAGE not specified, default will be used."
LANGUAGE="go"
else
LANGUAGE=$4
fi
# Create temp directory to work on
work_dir="$ROOT_DIR/temp"
mkdir -p "${work_dir}"
if [[ ! ${work_dir} || -d {work_dir} ]]; then
echo "Unable to create temporary directory"
exit 1
fi
# Delete temp directory on exit
trap "rm -rf ${work_dir}" EXIT
mkdir "${work_dir}/git_repo" # Where the git repo will be created
mkdir "${work_dir}/sdk_backup" # Backup of the SDK to check for new modules
mkdir "${work_dir}/sdk_to_push" # Copy of SDK to push
# Prepare SDK to push
cp -a "${SDK_REPO_LOCAL_PATH}/." "${work_dir}/sdk_to_push"
rm -rf "${work_dir}/sdk_to_push/.git"
# Initialize git repo
cd "${work_dir}/git_repo"
git clone "${REPO_URL_SSH}" ./ --quiet
git config user.name "${COMMIT_NAME}"
git config user.email "${COMMIT_EMAIL}"
cp -a . "${work_dir}/sdk_backup"
# Create PR for each new SDK module if there are changes
for service_path in ${work_dir}/sdk_to_push/services/*; do
service="${service_path##*/}"
# Replace old SDK with new one
# Removal of pulled data is necessary because the old version may have files
# that were deleted in the new version
rm -rf "./services/${service}"
cp -a "${work_dir}/sdk_to_push/services/${service}/." "./services/${service}"
# Check for changes in the specific folder compared to main
service_changes=$(git status --porcelain "services/$service")
if [[ -n "$service_changes" ]]; then
echo -e "\n>> Detected changes in $service service"
if [[ "$BRANCH_PREFIX" != "main" ]]; then
git switch main # This is needed to create a new branch for the service without including the previously committed files
branch="$BRANCH_PREFIX/$service"
git switch -c "$branch"
else
branch=$BRANCH_PREFIX
fi
if [ "${LANGUAGE}" == "go" ] && [ ! -d "${work_dir}/sdk_backup/services/${service}/" ]; then # Check if it is a newly added SDK module
# 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
# the -r flag examines subdirectories of dir recursively
# this prevents errors if there is more than one new module in the SDK generation
go work use -r .
fi
# If the target branch is main, we run the linter and tests in order to have code which can be compiled.
# If the target branch is not main, we skip the linter and tests, because in the create PRs the CI pipeline will check run them as well.
# This prevents unrecognized api changes, because we will see them within the PR itself.
if [[ "$branch" == "main" ]]; then
# If lint or test fails for a service, we skip it and continue to the next one
make lint skip-non-generated-files=true service="$service" || {
echo "! Linting failed for $service. THE UPDATE OF THIS SERVICE WILL BE SKIPPED."
continue
}
# Our unit test template fails because it doesn't support fields with validations,
# such as the UUID component used by IaaS. We introduce this hardcoded skip until we fix it
if [ "${service}" = "iaas" ] || [ "${service}" = "iaasalpha" ]; then
echo ">> Skipping tests of $service service"
else
make test skip-non-generated-files=true service="$service" || {
echo "! Testing failed for $service. THE UPDATE OF THIS SERVICE WILL BE SKIPPED."
continue
}
fi
fi
# write OAS commit hash of service into source tree, create compare link
echo ">> Updating OAS commit for $service"
old_commit=""
if [[ -f "services/${service}/oas_commit" ]]; then
old_commit=$(cat "services/${service}/oas_commit")
echo "old commit: ${old_commit}"
fi
new_commit=""
if [[ -f "${ROOT_DIR}/oas/legacy/oas_commits" ]]; then
new_commit=$(grep "${service}=" "${ROOT_DIR}/oas/legacy/oas_commits" | cut -d = -f 2)
echo "new commit: ${new_commit}"
fi
compare_link=""
if [[ -n "${old_commit}" ]] && [[ -n "${new_commit}" ]]; then
compare_link=https://github.com/stackitcloud/stackit-api-specifications/compare/${old_commit}...${new_commit}
echo "compare link: ${compare_link}"
fi
if [[ -n "${new_commit}" ]]; then
echo "${new_commit}" > "services/${service}/oas_commit"
fi
git add "services/${service}/"
if [ "${LANGUAGE}" == "go" ] && [ ! -d "${work_dir}/sdk_backup/services/${service}/" ]; then # Check if it is a newly added SDK module
git add go.work
fi
if [[ "$branch" != "main" ]]; then
echo ">> Creating PR for $service"
git commit -m "Generate $service"
git push origin "$branch"
echo -e "$COMMIT_INFO\nspec comparison: ${compare_link}" | gh pr create --title "Generator: Update SDK /services/$service" --body-file - --head "$branch" --base "main"
else
echo ">> Pushing changes for $service service..."
git commit -m "Generate $service: $COMMIT_INFO"
git push origin "$branch"
fi
fi
done