-
Notifications
You must be signed in to change notification settings - Fork 2
183 lines (154 loc) · 6.42 KB
/
4-bb-export.yml
File metadata and controls
183 lines (154 loc) · 6.42 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
name: Bytebase Export SQL
on:
pull_request:
types: [closed]
branches:
- main
paths:
- 'export/**'
workflow_dispatch:
jobs:
bytebase-export:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Login Bytebase
id: bytebase-login
uses: bytebase/login-action@0.0.2
with:
bytebase-url: ${{ secrets.BYTEBASE_URL }}
service-key: ${{ secrets.BYTEBASE_SERVICE_KEY }}
service-secret: ${{ secrets.BYTEBASE_SERVICE_SECRET }}
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v42
with:
files: |
export/**/*.sql
since_last_remote_commit: true
- name: Install jq
run: sudo apt-get install -y jq
- name: Process SQL files
id: process-sql
if: steps.changed-files.outputs.any_changed == 'true'
run: |
call_api() {
local url="$1"
local method="$2"
local data="$3"
local description="$4"
echo "Calling API: $description"
echo "URL: $url"
temp_file=$(mktemp)
http_code=$(curl -s -w "%{http_code}" \
--request "$method" "$url" \
--header "Authorization: Bearer ${{ steps.bytebase-login.outputs.token }}" \
--header "Content-Type: application/json" \
--data "$data" \
-o "$temp_file")
echo "Response (Status $http_code):"
cat "$temp_file" | jq '.'
echo "------------------------"
cat "$temp_file"
}
MANIFEST_PATH=""
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
DIR_PATH=$(dirname "$file")
while [[ "$DIR_PATH" == export* ]]; do
if [[ -f "$DIR_PATH/manifest.toml" ]]; then
MANIFEST_PATH="$DIR_PATH/manifest.toml"
break 2
fi
DIR_PATH=$(dirname "$DIR_PATH")
done
done
if [[ -z "$MANIFEST_PATH" ]]; then
echo "Error: No manifest.toml found in the export directory"
exit 1
fi
echo "Found manifest file at: $MANIFEST_PATH"
echo "Manifest contents:"
cat "$MANIFEST_PATH"
read_toml() {
local key="$1"
python3 -c "import tomllib; print(tomllib.load(open('$MANIFEST_PATH', 'rb')).get('$key', ''))"
}
PROJECT=$(read_toml "project") || exit 1
INSTANCE=$(read_toml "instance") || exit 1
DATABASE=$(read_toml "database") || exit 1
FORMAT=$(read_toml "format") || FORMAT="JSON"
echo "=== Parsed Configuration ==="
echo "Project: $PROJECT"
echo "Instance: $INSTANCE"
echo "Database: $DATABASE"
echo "Format: $FORMAT"
echo "==========================="
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "Processing $file"
SQL_CONTENT=$(base64 < "$file")
STEP_ID=$(python3 -c "import uuid; print(str(uuid.uuid4()))")
BASE_URL="${{ steps.bytebase-login.outputs.api_url }}"
# Create Sheet
sheet_data=$(call_api \
"$BASE_URL/projects/$PROJECT/sheets" \
"POST" \
"{\"title\":\"\",\"content\":\"$SQL_CONTENT\",\"type\":\"TYPE_SQL\",\"source\":\"SOURCE_BYTEBASE_ARTIFACT\",\"visibility\":\"VISIBILITY_PUBLIC\"}" \
"Create Sheet")
SHEET_NAME=$(echo "$sheet_data" | jq -r '.name')
echo "Sheet name: $SHEET_NAME"
# Create Plan
plan_data=$(call_api \
"$BASE_URL/projects/$PROJECT/plans" \
"POST" \
"{\"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\"}" \
"Create Plan")
PLAN_NAME=$(echo "$plan_data" | jq -r '.name')
echo "Plan name: $PLAN_NAME"
# Create Issue
issue_data=$(call_api \
"$BASE_URL/projects/$PROJECT/issues" \
"POST" \
"{\"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\"}" \
"Create Issue")
ISSUE_NUMBER=$(echo "$issue_data" | jq -r '.name | split("/")[-1]')
ISSUE_LINK="${{ secrets.BYTEBASE_URL }}/projects/$PROJECT/issues/$ISSUE_NUMBER"
echo "Issue link: $ISSUE_LINK"
# Create Rollout
rollout_data=$(call_api \
"$BASE_URL/projects/$PROJECT/rollouts" \
"POST" \
"{\"plan\":\"$PLAN_NAME\"}" \
"Create Rollout")
echo "Rollout created: $(echo "$rollout_data" | jq -r '.name')"
done
- name: Comment on PR
uses: actions/github-script@v7
if: always()
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
with:
script: |
const changedFiles = process.env.CHANGED_FILES || '';
let commentBody = `### SQL Export Summary\n\n`;
commentBody += `✅ **PR Status:** Merged\n\n`;
commentBody += `📝 **Processed SQL Files:**\n\n`;
if (changedFiles.trim()) {
commentBody += changedFiles.split(' ').map(f => `- ${f}`).join('\n');
} else {
commentBody += `None`;
}
commentBody += `\n\n**Status:** ${process.env.STATUS || 'Completed'}`;
await github.rest.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: commentBody
});