43
43
id : process-sql
44
44
if : steps.changed-files.outputs.any_changed == 'true'
45
45
run : |
46
- # Function to make API calls with error handling
47
46
call_api() {
48
47
local url="$1"
49
48
local method="$2"
55
54
echo "URL: $url"
56
55
echo "Method: $method"
57
56
58
- # Store response in a temporary file
59
57
temp_file=$(mktemp)
60
58
http_code=$(curl -s -w "%{http_code}" \
61
59
--request "$method" "$url" \
@@ -76,19 +74,11 @@ jobs:
76
74
return 1
77
75
fi
78
76
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
-
86
77
cat "$temp_file"
87
78
rm "$temp_file"
88
79
return 0
89
80
}
90
81
91
- # Find the manifest.toml once at the start
92
82
MANIFEST_PATH=""
93
83
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
94
84
DIR_PATH=$(dirname "$file")
@@ -110,23 +100,11 @@ jobs:
110
100
echo "Manifest contents:"
111
101
cat "$MANIFEST_PATH"
112
102
113
- # Parse manifest.toml with error handling
114
103
read_toml() {
115
104
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', ''))"
127
106
}
128
107
129
- # Read each value with error handling
130
108
PROJECT=$(read_toml "project") || exit 1
131
109
INSTANCE=$(read_toml "instance") || exit 1
132
110
DATABASE=$(read_toml "database") || exit 1
@@ -139,56 +117,43 @@ except Exception as e:
139
117
echo "Format: $FORMAT"
140
118
echo "==========================="
141
119
142
- # Process each SQL file
143
120
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
144
121
echo "Processing $file"
145
-
146
- # Read SQL content and encode to base64
147
122
SQL_CONTENT=$(base64 < "$file")
148
-
149
- # Generate UUID for step ID
150
123
STEP_ID=$(python3 -c "import uuid; print(str(uuid.uuid4()))")
151
-
152
124
BASE_URL="${{ steps.bytebase-login.outputs.api_url }}"
153
-
154
- # 1. Create Sheet
125
+
155
126
sheet_data=$(call_api \
156
127
"$BASE_URL/v1/projects/$PROJECT/sheets" \
157
128
"POST" \
158
129
"{\"title\":\"\",\"content\":\"$SQL_CONTENT\",\"type\":\"TYPE_SQL\",\"source\":\"SOURCE_BYTEBASE_ARTIFACT\",\"visibility\":\"VISIBILITY_PUBLIC\"}" \
159
- " Create Sheet" ) || exit 1
160
-
130
+ "Create Sheet")
131
+
161
132
SHEET_NAME=$(echo "$sheet_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['name'])")
162
-
163
- # 2. Create Plan
133
+
164
134
plan_data=$(call_api \
165
135
"$BASE_URL/v1/projects/$PROJECT/plans" \
166
136
"POST" \
167
137
"{\"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
+
170
140
PLAN_NAME=$(echo "$plan_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['name'])")
171
-
172
- # 3. Create Issue
141
+
173
142
issue_data=$(call_api \
174
143
"$BASE_URL/v1/projects/$PROJECT/issues" \
175
144
"POST" \
176
145
"{\"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
+
180
148
rollout_data=$(call_api \
181
149
"$BASE_URL/v1/projects/$PROJECT/rollouts" \
182
150
"POST" \
183
151
"{\"plan\":\"$PLAN_NAME\"}" \
184
- " Create Rollout" ) || exit 1
185
-
186
- # Extract issue link for PR comment
152
+ "Create Rollout")
153
+
187
154
ISSUE_NUMBER=$(echo "$issue_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['name'].split('/')[-1])")
188
155
ISSUE_LINK="${{ secrets.BYTEBASE_URL }}/projects/$PROJECT/issues/$ISSUE_NUMBER"
189
156
echo "ISSUE_LINK=$ISSUE_LINK" >> $GITHUB_ENV
190
-
191
- echo "Successfully processed $file"
192
157
done
193
158
194
159
- name : Comment on PR
@@ -210,7 +175,7 @@ except Exception as e:
210
175
commentBody += `None`;
211
176
}
212
177
213
- commentBody += '\n\n**Status:** ${process.env.STATUS || 'Completed'}` ;
178
+ commentBody += '\n\n**Status:** ${process.env.STATUS || 'Completed'}' ;
214
179
215
180
await github.rest.issues.createComment({
216
181
...context.repo,
0 commit comments