Skip to content

Commit 7a63ad5

Browse files
authored
chore: support vcs (#80)
1 parent 2ac2bae commit 7a63ad5

38 files changed

+2007
-283
lines changed

api/client.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,28 @@ type Client interface {
8686
// Cel
8787
// ParseExpression parse the expression string.
8888
ParseExpression(ctx context.Context, expression string) (*v1alpha1.Expr, error)
89+
90+
// VCS Provider
91+
// ListVCSProvider will returns all vcs providers.
92+
ListVCSProvider(ctx context.Context) (*v1pb.ListVCSProvidersResponse, error)
93+
// GetVCSProvider gets the vcs by id.
94+
GetVCSProvider(ctx context.Context, name string) (*v1pb.VCSProvider, error)
95+
// CreateVCSProvider creates the vcs provider.
96+
CreateVCSProvider(ctx context.Context, vcsID string, vcs *v1pb.VCSProvider) (*v1pb.VCSProvider, error)
97+
// UpdateVCSProvider updates the vcs provider.
98+
UpdateVCSProvider(ctx context.Context, patch *v1pb.VCSProvider, updateMasks []string) (*v1pb.VCSConnector, error)
99+
// DeleteVCSProvider deletes the vcs provider.
100+
DeleteVCSProvider(ctx context.Context, name string) error
101+
102+
// VCS Connector
103+
// ListVCSConnector will returns all vcs connector in a project.
104+
ListVCSConnector(ctx context.Context, projectName string) (*v1pb.ListVCSConnectorsResponse, error)
105+
// GetVCSConnector gets the vcs connector by id.
106+
GetVCSConnector(ctx context.Context, name string) (*v1pb.VCSConnector, error)
107+
// CreateVCSConnector creates the vcs connector in a project.
108+
CreateVCSConnector(ctx context.Context, projectName, connectorID string, connector *v1pb.VCSConnector) (*v1pb.VCSConnector, error)
109+
// UpdateVCSConnector updates the vcs connector.
110+
UpdateVCSConnector(ctx context.Context, patch *v1pb.VCSConnector, updateMasks []string) (*v1pb.VCSConnector, error)
111+
// DeleteVCSConnector deletes the vcs provider.
112+
DeleteVCSConnector(ctx context.Context, name string) error
89113
}

client/common.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,76 @@
11
package client
22

33
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
49
"google.golang.org/protobuf/encoding/protojson"
10+
"google.golang.org/protobuf/reflect/protoreflect"
511
)
612

713
// ProtojsonUnmarshaler is the unmarshal for protocol.
814
var ProtojsonUnmarshaler = protojson.UnmarshalOptions{DiscardUnknown: true}
15+
16+
// deleteResource deletes the resource by name.
17+
func (c *client) deleteResource(ctx context.Context, name string) error {
18+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", c.url, c.version, name), nil)
19+
if err != nil {
20+
return err
21+
}
22+
23+
if _, err := c.doRequest(req); err != nil {
24+
return err
25+
}
26+
return nil
27+
}
28+
29+
// undeleteResource undeletes the resource by name.
30+
func (c *client) undeleteResource(ctx context.Context, name string) ([]byte, error) {
31+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/%s:undelete", c.url, c.version, name), nil)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
body, err := c.doRequest(req)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return body, nil
42+
}
43+
44+
// deleteResource deletes the resource by name.
45+
func (c *client) updateResource(ctx context.Context, name string, patch protoreflect.ProtoMessage, updateMasks []string, allowMissing bool) ([]byte, error) {
46+
payload, err := protojson.Marshal(patch)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?update_mask=%s&allow_missing=%v", c.url, c.version, name, strings.Join(updateMasks, ","), allowMissing), strings.NewReader(string(payload)))
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
body, err := c.doRequest(req)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return body, nil
62+
}
63+
64+
// getResource gets the resource by name.
65+
func (c *client) getResource(ctx context.Context, name string) ([]byte, error) {
66+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, name), nil)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
body, err := c.doRequest(req)
72+
if err != nil {
73+
return nil, err
74+
}
75+
return body, nil
76+
}

client/database.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8-
"strings"
98

109
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
11-
"google.golang.org/protobuf/encoding/protojson"
1210
)
1311

14-
// GetDatabase gets the database by environment resource id, instance resource id and the database name.
12+
// GetDatabase gets the database by the database name.
1513
func (c *client) GetDatabase(ctx context.Context, databaseName string) (*v1pb.Database, error) {
16-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, databaseName), nil)
17-
if err != nil {
18-
return nil, err
19-
}
20-
21-
body, err := c.doRequest(req)
14+
body, err := c.getResource(ctx, databaseName)
2215
if err != nil {
2316
return nil, err
2417
}
@@ -58,17 +51,7 @@ func (c *client) ListDatabase(ctx context.Context, instanceID, filter string) (*
5851

5952
// UpdateDatabase patches the database.
6053
func (c *client) UpdateDatabase(ctx context.Context, patch *v1pb.Database, updateMasks []string) (*v1pb.Database, error) {
61-
payload, err := protojson.Marshal(patch)
62-
if err != nil {
63-
return nil, err
64-
}
65-
66-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?update_mask=%s", c.url, c.version, patch.Name, strings.Join(updateMasks, ",")), strings.NewReader(string(payload)))
67-
if err != nil {
68-
return nil, err
69-
}
70-
71-
body, err := c.doRequest(req)
54+
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
7255
if err != nil {
7356
return nil, err
7457
}

client/environment.go

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ func (c *client) CreateEnvironment(ctx context.Context, environmentID string, cr
3737

3838
// GetEnvironment gets the environment by id.
3939
func (c *client) GetEnvironment(ctx context.Context, environmentName string) (*v1pb.Environment, error) {
40-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, environmentName), nil)
41-
if err != nil {
42-
return nil, err
43-
}
44-
45-
body, err := c.doRequest(req)
40+
body, err := c.getResource(ctx, environmentName)
4641
if err != nil {
4742
return nil, err
4843
}
@@ -76,18 +71,8 @@ func (c *client) ListEnvironment(ctx context.Context, showDeleted bool) (*v1pb.L
7671
}
7772

7873
// UpdateEnvironment updates the environment.
79-
func (c *client) UpdateEnvironment(ctx context.Context, patch *v1pb.Environment, updateMask []string) (*v1pb.Environment, error) {
80-
payload, err := protojson.Marshal(patch)
81-
if err != nil {
82-
return nil, err
83-
}
84-
85-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?update_mask=%s", c.url, c.version, patch.Name, strings.Join(updateMask, ",")), strings.NewReader(string(payload)))
86-
if err != nil {
87-
return nil, err
88-
}
89-
90-
body, err := c.doRequest(req)
74+
func (c *client) UpdateEnvironment(ctx context.Context, patch *v1pb.Environment, updateMasks []string) (*v1pb.Environment, error) {
75+
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
9176
if err != nil {
9277
return nil, err
9378
}
@@ -102,25 +87,12 @@ func (c *client) UpdateEnvironment(ctx context.Context, patch *v1pb.Environment,
10287

10388
// DeleteEnvironment deletes the environment.
10489
func (c *client) DeleteEnvironment(ctx context.Context, environmentName string) error {
105-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", c.url, c.version, environmentName), nil)
106-
if err != nil {
107-
return err
108-
}
109-
110-
if _, err := c.doRequest(req); err != nil {
111-
return err
112-
}
113-
return nil
90+
return c.deleteResource(ctx, environmentName)
11491
}
11592

11693
// UndeleteEnvironment undeletes the environment.
11794
func (c *client) UndeleteEnvironment(ctx context.Context, environmentName string) (*v1pb.Environment, error) {
118-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/%s:undelete", c.url, c.version, environmentName), nil)
119-
if err != nil {
120-
return nil, err
121-
}
122-
123-
body, err := c.doRequest(req)
95+
body, err := c.undeleteResource(ctx, environmentName)
12496
if err != nil {
12597
return nil, err
12698
}

client/instance.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ func (c *client) ListInstance(ctx context.Context, showDeleted bool) (*v1pb.List
3232

3333
// GetInstance gets the instance by id.
3434
func (c *client) GetInstance(ctx context.Context, instanceName string) (*v1pb.Instance, error) {
35-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, instanceName), nil)
36-
if err != nil {
37-
return nil, err
38-
}
39-
40-
body, err := c.doRequest(req)
35+
body, err := c.getResource(ctx, instanceName)
4136
if err != nil {
4237
return nil, err
4338
}
@@ -78,18 +73,7 @@ func (c *client) CreateInstance(ctx context.Context, instanceID string, instance
7873

7974
// UpdateInstance updates the instance.
8075
func (c *client) UpdateInstance(ctx context.Context, patch *v1pb.Instance, updateMasks []string) (*v1pb.Instance, error) {
81-
payload, err := protojson.Marshal(patch)
82-
if err != nil {
83-
return nil, err
84-
}
85-
86-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?update_mask=%s", c.url, c.version, patch.Name, strings.Join(updateMasks, ",")), strings.NewReader(string(payload)))
87-
88-
if err != nil {
89-
return nil, err
90-
}
91-
92-
body, err := c.doRequest(req)
76+
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
9377
if err != nil {
9478
return nil, err
9579
}
@@ -104,25 +88,12 @@ func (c *client) UpdateInstance(ctx context.Context, patch *v1pb.Instance, updat
10488

10589
// DeleteInstance deletes the instance.
10690
func (c *client) DeleteInstance(ctx context.Context, instanceName string) error {
107-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", c.url, c.version, instanceName), nil)
108-
if err != nil {
109-
return err
110-
}
111-
112-
if _, err := c.doRequest(req); err != nil {
113-
return err
114-
}
115-
return nil
91+
return c.deleteResource(ctx, instanceName)
11692
}
11793

11894
// UndeleteInstance undeletes the instance.
11995
func (c *client) UndeleteInstance(ctx context.Context, instanceName string) (*v1pb.Instance, error) {
120-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/%s:undelete", c.url, c.version, instanceName), nil)
121-
if err != nil {
122-
return nil, err
123-
}
124-
125-
body, err := c.doRequest(req)
96+
body, err := c.undeleteResource(ctx, instanceName)
12697
if err != nil {
12798
return nil, err
12899
}

client/policy.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
"strings"
87

98
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
10-
"google.golang.org/protobuf/encoding/protojson"
119
)
1210

1311
// ListPolicies lists policies in a specific resource.
@@ -38,12 +36,7 @@ func (c *client) ListPolicies(ctx context.Context, parent string) (*v1pb.ListPol
3836

3937
// GetPolicy gets a policy in a specific resource.
4038
func (c *client) GetPolicy(ctx context.Context, policyName string) (*v1pb.Policy, error) {
41-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, policyName), nil)
42-
if err != nil {
43-
return nil, err
44-
}
45-
46-
body, err := c.doRequest(req)
39+
body, err := c.getResource(ctx, policyName)
4740
if err != nil {
4841
return nil, err
4942
}
@@ -58,17 +51,7 @@ func (c *client) GetPolicy(ctx context.Context, policyName string) (*v1pb.Policy
5851

5952
// UpsertPolicy creates or updates the policy.
6053
func (c *client) UpsertPolicy(ctx context.Context, policy *v1pb.Policy, updateMasks []string) (*v1pb.Policy, error) {
61-
payload, err := protojson.Marshal(policy)
62-
if err != nil {
63-
return nil, err
64-
}
65-
66-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?allow_missing=true&update_mask=%s", c.url, c.version, policy.Name, strings.Join(updateMasks, ",")), strings.NewReader(string(payload)))
67-
if err != nil {
68-
return nil, err
69-
}
70-
71-
body, err := c.doRequest(req)
54+
body, err := c.updateResource(ctx, policy.Name, policy, updateMasks, true /* allow missing = true*/)
7255
if err != nil {
7356
return nil, err
7457
}
@@ -83,13 +66,5 @@ func (c *client) UpsertPolicy(ctx context.Context, policy *v1pb.Policy, updateMa
8366

8467
// DeletePolicy deletes the policy.
8568
func (c *client) DeletePolicy(ctx context.Context, policyName string) error {
86-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", c.url, c.version, policyName), nil)
87-
if err != nil {
88-
return err
89-
}
90-
91-
if _, err := c.doRequest(req); err != nil {
92-
return err
93-
}
94-
return nil
69+
return c.deleteResource(ctx, policyName)
9570
}

client/project.go

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ import (
1212

1313
// GetProject gets the project by resource id.
1414
func (c *client) GetProject(ctx context.Context, projectName string) (*v1pb.Project, error) {
15-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, projectName), nil)
16-
if err != nil {
17-
return nil, err
18-
}
19-
20-
body, err := c.doRequest(req)
15+
body, err := c.getResource(ctx, projectName)
2116
if err != nil {
2217
return nil, err
2318
}
@@ -77,19 +72,8 @@ func (c *client) CreateProject(ctx context.Context, projectID string, project *v
7772
}
7873

7974
// UpdateProject updates the project.
80-
func (c *client) UpdateProject(ctx context.Context, patch *v1pb.Project, updateMask []string) (*v1pb.Project, error) {
81-
payload, err := protojson.Marshal(patch)
82-
if err != nil {
83-
return nil, err
84-
}
85-
86-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?update_mask=%s", c.url, c.version, patch.Name, strings.Join(updateMask, ",")), strings.NewReader(string(payload)))
87-
88-
if err != nil {
89-
return nil, err
90-
}
91-
92-
body, err := c.doRequest(req)
75+
func (c *client) UpdateProject(ctx context.Context, patch *v1pb.Project, updateMasks []string) (*v1pb.Project, error) {
76+
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
9377
if err != nil {
9478
return nil, err
9579
}
@@ -104,25 +88,12 @@ func (c *client) UpdateProject(ctx context.Context, patch *v1pb.Project, updateM
10488

10589
// DeleteProject deletes the project.
10690
func (c *client) DeleteProject(ctx context.Context, projectName string) error {
107-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", c.url, c.version, projectName), nil)
108-
if err != nil {
109-
return err
110-
}
111-
112-
if _, err := c.doRequest(req); err != nil {
113-
return err
114-
}
115-
return nil
91+
return c.deleteResource(ctx, projectName)
11692
}
11793

11894
// UndeleteProject undeletes the project.
11995
func (c *client) UndeleteProject(ctx context.Context, projectName string) (*v1pb.Project, error) {
120-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/%s:undelete", c.url, c.version, projectName), nil)
121-
if err != nil {
122-
return nil, err
123-
}
124-
125-
body, err := c.doRequest(req)
96+
body, err := c.undeleteResource(ctx, projectName)
12697
if err != nil {
12798
return nil, err
12899
}

0 commit comments

Comments
 (0)