-
Notifications
You must be signed in to change notification settings - Fork 2
/
bytebase.go
194 lines (172 loc) · 5.49 KB
/
bytebase.go
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
184
185
186
187
188
189
190
191
192
193
194
package sink
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/bytebase/relay/payload"
"github.com/bytebase/relay/service"
flag "github.com/spf13/pflag"
)
var (
_ Sinker = (*bytebaseSinker)(nil)
)
var (
bytebaseURL string
bytebaseServiceAccount string
bytebaseServiceKey string
// hard code for demo
issueNameTemplate string = "[%s] %s"
filePathTemplate string = "{{PROJECT_KEY}}/{{ENV_NAME}}/{{DB_NAME}}##{{VERSION}}##{{TYPE}}##{{DESCRIPTION}}.sql"
placeholderRegexp string = `[^\\/?%*:|"<>]+`
placeholderList = []string{
"PROJECT_KEY",
"ENV_NAME",
"VERSION",
"DB_NAME",
"TYPE",
"DESCRIPTION",
}
)
func init() {
flag.StringVar(&bytebaseURL, "bytebase-url", "http://localhost:8080", "The Bytebase service URL")
flag.StringVar(&bytebaseServiceAccount, "bytebase-service-account", "", "The Bytebase service account name")
flag.StringVar(&bytebaseServiceKey, "bytebase-service-key", "", "The Bytebase service account key")
}
// NewBytebase creates a Bytebase sinker
func NewBytebase() Sinker {
return &bytebaseSinker{}
}
type bytebaseSinker struct {
bytebaseService *service.BytebaseService
}
type migrationInfo struct {
Type payload.MigrationType
Version string
Database string
Environment string
Description string
Project string
Name string
}
func (sinker *bytebaseSinker) Mount() error {
if bytebaseURL == "" {
fmt.Printf("--bytebase-url is missing, Bytebase sinker will not be able to process any events.\n")
return nil
}
if bytebaseServiceAccount == "" {
fmt.Printf("--bytebase-service-account is missing, Bytebase sinker will not be able to process any events.\n")
return nil
}
if bytebaseServiceKey == "" {
fmt.Printf("---bytebase-service-key is missing, Bytebase sinker will not be able to process any events.\n")
return nil
}
sinker.bytebaseService = service.NewBytebase(bytebaseURL, bytebaseServiceAccount, bytebaseServiceKey)
return nil
}
func (sinker *bytebaseSinker) Process(c context.Context, _ string, pi interface{}) error {
if bytebaseURL == "" {
return fmt.Errorf("--bytebase-url is required")
}
if bytebaseServiceAccount == "" {
return fmt.Errorf("--bytebase-service-account is required")
}
if bytebaseServiceKey == "" {
return fmt.Errorf("---bytebase-service-key is required")
}
change := pi.(payload.GerritFileChangeMessage)
for _, file := range change.Files {
mi, err := parseMigrationInfo(file.FileName, filePathTemplate)
if err != nil {
return err
}
issueName := fmt.Sprintf(issueNameTemplate, mi.Name, file.FileName)
issueCreate := &payload.IssueCreate{
ProjectKey: mi.Project,
Database: mi.Database,
Environment: mi.Environment,
Name: issueName,
Description: mi.Description,
MigrationType: mi.Type,
Statement: file.Content,
SchemaVersion: mi.Version,
}
if err := sinker.bytebaseService.CreateIssue(c, issueCreate); err != nil {
return err
}
}
return nil
}
// parseMigrationInfo matches filePath against filePathTemplate
func parseMigrationInfo(filePath, filePathTemplate string) (*migrationInfo, error) {
// Escape "." characters to match literals instead of using it as a wildcard.
filePathRegex := strings.ReplaceAll(filePathTemplate, `.`, `\.`)
filePathRegex = strings.ReplaceAll(filePathRegex, `/*/`, `/[^/]+/`)
filePathRegex = strings.ReplaceAll(filePathRegex, `**`, `.*`)
for _, placeholder := range placeholderList {
filePathRegex = strings.ReplaceAll(filePathRegex, fmt.Sprintf("{{%s}}", placeholder), fmt.Sprintf(`(?P<%s>%s)`, placeholder, placeholderRegexp))
}
myRegex, err := regexp.Compile(filePathRegex)
if err != nil {
return nil, fmt.Errorf("invalid file path template: %q", filePathTemplate)
}
if !myRegex.MatchString(filePath) {
// File path does not match file path template.
return nil, nil
}
mi := &migrationInfo{
Type: payload.Migrate,
}
matchList := myRegex.FindStringSubmatch(filePath)
for _, placeholder := range placeholderList {
index := myRegex.SubexpIndex(placeholder)
if index >= 0 {
switch placeholder {
case "PROJECT_KEY":
mi.Project = matchList[index]
case "ENV_NAME":
mi.Environment = matchList[index]
case "VERSION":
mi.Version = matchList[index]
case "DB_NAME":
mi.Database = matchList[index]
case "TYPE":
switch matchList[index] {
case "data", "dml":
mi.Type = payload.Data
mi.Type = payload.Data
mi.Name = "Change data"
case "migrate", "ddl":
mi.Type = payload.Migrate
mi.Type = payload.Migrate
mi.Name = "Alter schema"
default:
return nil, fmt.Errorf("file path %q contains invalid migration type %q, must be 'migrate'('ddl') or 'data'('dml')", filePath, matchList[index])
}
case "DESCRIPTION":
mi.Description = matchList[index]
}
}
}
if mi.Version == "" {
return nil, fmt.Errorf("file path %q does not contain {{VERSION}}, configured file path template %q", filePath, filePathTemplate)
}
if mi.Database == "" {
return nil, fmt.Errorf("file path %q does not contain {{DB_NAME}}, configured file path template %q", filePath, filePathTemplate)
}
if mi.Description == "" {
switch mi.Type {
case payload.Data:
mi.Description = fmt.Sprintf("Create %s data change", mi.Database)
default:
mi.Description = fmt.Sprintf("Create %s schema migration", mi.Database)
}
} else {
// Replace _ with space
mi.Description = strings.ReplaceAll(mi.Description, "_", " ")
// Capitalize first letter
mi.Description = strings.ToUpper(mi.Description[:1]) + mi.Description[1:]
}
return mi, nil
}