4343 id : process-sql
4444 if : steps.changed-files.outputs.any_changed == 'true'
4545 run : |
46- # Function to make API calls with error handling
4746 call_api() {
4847 local url="$1"
4948 local method="$2"
5554 echo "URL: $url"
5655 echo "Method: $method"
5756
58- # Store response in a temporary file
5957 temp_file=$(mktemp)
6058 http_code=$(curl -s -w "%{http_code}" \
6159 --request "$method" "$url" \
@@ -76,19 +74,11 @@ jobs:
7674 return 1
7775 fi
7876
79- # Validate JSON response using a simple Python command
80- if ! python3 -c "import json; json.load(open('$temp_file'))" 2>/dev/null; then
81- echo "Error: Invalid JSON response"
82- rm "$temp_file"
83- return 1
84- fi
85-
8677 cat "$temp_file"
8778 rm "$temp_file"
8879 return 0
8980 }
9081
91- # Find the manifest.toml once at the start
9282 MANIFEST_PATH=""
9383 for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
9484 DIR_PATH=$(dirname "$file")
@@ -110,23 +100,11 @@ jobs:
110100 echo "Manifest contents:"
111101 cat "$MANIFEST_PATH"
112102
113- # Parse manifest.toml with error handling
114103 read_toml() {
115104 local key="$1"
116- python3 -c "
117- import sys
118- import tomllib
119- try :
120- with open('$MANIFEST_PATH', 'rb') as f :
121- data = tomllib.load(f)
122- print(data.get('$key', ''))
123- except Exception as e :
124- print(f'Error reading $key : {str(e)}', file=sys.stderr)
125- sys.exit(1)
126- "
105+ python3 -c "import tomllib; print(tomllib.load(open('$MANIFEST_PATH', 'rb')).get('$key', ''))"
127106 }
128107
129- # Read each value with error handling
130108 PROJECT=$(read_toml "project") || exit 1
131109 INSTANCE=$(read_toml "instance") || exit 1
132110 DATABASE=$(read_toml "database") || exit 1
@@ -139,56 +117,43 @@ except Exception as e:
139117 echo "Format: $FORMAT"
140118 echo "==========================="
141119
142- # Process each SQL file
143120 for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
144121 echo "Processing $file"
145-
146- # Read SQL content and encode to base64
147122 SQL_CONTENT=$(base64 < "$file")
148-
149- # Generate UUID for step ID
150123 STEP_ID=$(python3 -c "import uuid; print(str(uuid.uuid4()))")
151-
152124 BASE_URL="${{ steps.bytebase-login.outputs.api_url }}"
153-
154- # 1. Create Sheet
125+
155126 sheet_data=$(call_api \
156127 "$BASE_URL/v1/projects/$PROJECT/sheets" \
157128 "POST" \
158129 "{\"title\":\"\",\"content\":\"$SQL_CONTENT\",\"type\":\"TYPE_SQL\",\"source\":\"SOURCE_BYTEBASE_ARTIFACT\",\"visibility\":\"VISIBILITY_PUBLIC\"}" \
159- " Create Sheet" ) || exit 1
160-
130+ "Create Sheet")
131+
161132 SHEET_NAME=$(echo "$sheet_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['name'])")
162-
163- # 2. Create Plan
133+
164134 plan_data=$(call_api \
165135 "$BASE_URL/v1/projects/$PROJECT/plans" \
166136 "POST" \
167137 "{\"steps\":[{\"specs\":[{\"id\":\"$STEP_ID\",\"export_data_config\":{\"target\":\"/instances/$INSTANCE/databases/$DATABASE\",\"format\":\"$FORMAT\",\"sheet\":\"$SHEET_NAME\"}}]}],\"title\":\"Export data from $DATABASE\",\"description\":\"EXPORT\"}" \
168- " Create Plan" ) || exit 1
169-
138+ "Create Plan")
139+
170140 PLAN_NAME=$(echo "$plan_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['name'])")
171-
172- # 3. Create Issue
141+
173142 issue_data=$(call_api \
174143 "$BASE_URL/v1/projects/$PROJECT/issues" \
175144 "POST" \
176145 "{\"approvers\":[],\"approvalTemplates\":[],\"subscribers\":[],\"title\":\"Issue: Export data from instances/$INSTANCE/databases/$DATABASE\",\"description\":\"SQL request from GitHub\",\"type\":\"DATABASE_DATA_EXPORT\",\"assignee\":\"\",\"plan\":\"$PLAN_NAME\"}" \
177- " Create Issue" ) || exit 1
178-
179- # 4. Create Rollout
146+ "Create Issue")
147+
180148 rollout_data=$(call_api \
181149 "$BASE_URL/v1/projects/$PROJECT/rollouts" \
182150 "POST" \
183151 "{\"plan\":\"$PLAN_NAME\"}" \
184- " Create Rollout" ) || exit 1
185-
186- # Extract issue link for PR comment
152+ "Create Rollout")
153+
187154 ISSUE_NUMBER=$(echo "$issue_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['name'].split('/')[-1])")
188155 ISSUE_LINK="${{ secrets.BYTEBASE_URL }}/projects/$PROJECT/issues/$ISSUE_NUMBER"
189156 echo "ISSUE_LINK=$ISSUE_LINK" >> $GITHUB_ENV
190-
191- echo "Successfully processed $file"
192157 done
193158
194159 - name : Comment on PR
@@ -210,7 +175,7 @@ except Exception as e:
210175 commentBody += `None`;
211176 }
212177
213- commentBody += '\n\n**Status:** ${process.env.STATUS || 'Completed'}` ;
178+ commentBody += '\n\n**Status:** ${process.env.STATUS || 'Completed'}' ;
214179
215180 await github.rest.issues.createComment({
216181 ...context.repo,
0 commit comments