Skip to content

Commit cb288ca

Browse files
authored
chore: support approval flow (#78)
* refactor: use protocol api * fix: go mod * chore: update * chore: update * chore: go version * fix: test * chore: golang lint * chore: support approval setting * chore: support approval flow * chore: update * fix: go mod tidy * fix: lint * chore: update * chore: update * chore: update * fix: lint
1 parent d202b2f commit cb288ca

29 files changed

+868
-117
lines changed

.golangci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ linters-settings:
7777
disabled: true
7878
- name: unchecked-type-assertion
7979
disabled: true
80+
- name: redundant-import-alias
81+
disabled: true
8082
gocritic:
8183
disabled-checks:
8284
- ifElseChain

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.2
1+
1.0.3

api/client.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package api
33
import (
44
"context"
55

6-
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
6+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
7+
v1alpha1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
78
)
89

910
// Client is the API message for Bytebase OpenAPI client.
@@ -73,4 +74,16 @@ type Client interface {
7374
DeleteProject(ctx context.Context, projectName string) error
7475
// UndeleteProject undeletes the project.
7576
UndeleteProject(ctx context.Context, projectName string) (*v1pb.Project, error)
77+
78+
// Setting
79+
// ListSettings lists all settings.
80+
ListSettings(ctx context.Context) (*v1pb.ListSettingsResponse, error)
81+
// GetSetting gets the setting by the name.
82+
GetSetting(ctx context.Context, settingName string) (*v1pb.Setting, error)
83+
// UpsertSetting updates or creates the setting.
84+
UpsertSetting(ctx context.Context, upsert *v1pb.Setting, updateMasks []string) (*v1pb.Setting, error)
85+
86+
// Cel
87+
// ParseExpression parse the expression string.
88+
ParseExpression(ctx context.Context, expression string) (*v1alpha1.Expr, error)
7689
}

api/setting.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package api
2+
3+
// SettingName is the Bytebase setting name without settings/ prefix.
4+
type SettingName string
5+
6+
const (
7+
// SettingWorkspaceApproval is the setting name for workspace approval config.
8+
SettingWorkspaceApproval SettingName = "bb.workspace.approval"
9+
// SettingWorkspaceExternalApproval is the setting name for workspace external approval config.
10+
SettingWorkspaceExternalApproval SettingName = "bb.workspace.approval.external"
11+
)
12+
13+
// RiskLevel is the approval risk level.
14+
type RiskLevel string
15+
16+
const (
17+
// RiskLevelDefault is the default risk level, the level number should be 0.
18+
RiskLevelDefault RiskLevel = "DEFAULT"
19+
// RiskLevelLow is the low risk level, the level number should be 100.
20+
RiskLevelLow RiskLevel = "LOW"
21+
// RiskLevelModerate is the moderate risk level, the level number should be 200.
22+
RiskLevelModerate RiskLevel = "MODERATE"
23+
// RiskLevelHigh is the high risk level, the level number should be 300.
24+
RiskLevelHigh RiskLevel = "HIGH"
25+
)
26+
27+
// Int returns the int value for risk.
28+
func (r RiskLevel) Int() int {
29+
switch r {
30+
case RiskLevelLow:
31+
return 100
32+
case RiskLevelModerate:
33+
return 200
34+
case RiskLevelHigh:
35+
return 300
36+
default:
37+
return 0
38+
}
39+
}
40+
41+
// ApprovalNodeType is the type for approval node.
42+
type ApprovalNodeType string
43+
44+
const (
45+
// ApprovalNodeTypeGroup means the approval node is a group.
46+
ApprovalNodeTypeGroup ApprovalNodeType = "GROUP"
47+
// ApprovalNodeTypeRole means the approval node is a role, the value should be role fullname.
48+
ApprovalNodeTypeRole ApprovalNodeType = "ROLE"
49+
// ApprovalNodeTypeExternalNodeID means the approval node is a external node, the value should be the node id.
50+
ApprovalNodeTypeExternalNodeID ApprovalNodeType = "EXTERNAL_NODE"
51+
)

client/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/pkg/errors"
99
"google.golang.org/protobuf/encoding/protojson"
1010

11-
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
11+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
1212
)
1313

1414
// Login will login the user and get the response.

client/cel.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform-plugin-log/tflog"
10+
11+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
12+
"github.com/pkg/errors"
13+
v1alpha1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
14+
"google.golang.org/protobuf/encoding/protojson"
15+
)
16+
17+
// ParseExpression parse the expression string.
18+
func (c *client) ParseExpression(ctx context.Context, expression string) (*v1alpha1.Expr, error) {
19+
payload, err := protojson.Marshal(&v1pb.BatchParseRequest{
20+
Expressions: []string{expression},
21+
})
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/cel/batchParse", c.url, c.version), strings.NewReader(string(payload)))
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
body, err := c.doRequest(req)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
tflog.Debug(ctx, fmt.Sprintf("parse cel response:\n%v", string(body)))
37+
38+
var res v1pb.BatchParseResponse
39+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
40+
return nil, err
41+
}
42+
43+
if len(res.Expressions) != 1 {
44+
return nil, errors.Errorf("failed to parse the cel: %v", expression)
45+
}
46+
47+
return res.GetExpressions()[0], nil
48+
}

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/pkg/errors"
1111

12-
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
12+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
1313

1414
"github.com/bytebase/terraform-provider-bytebase/api"
1515
)

client/database.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net/url"
88
"strings"
99

10-
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
10+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
1111
"google.golang.org/protobuf/encoding/protojson"
1212
)
1313

@@ -24,8 +24,7 @@ func (c *client) GetDatabase(ctx context.Context, databaseName string) (*v1pb.Da
2424
}
2525

2626
var res v1pb.Database
27-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
28-
if err != nil {
27+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
2928
return nil, err
3029
}
3130

@@ -50,8 +49,7 @@ func (c *client) ListDatabase(ctx context.Context, instanceID, filter string) (*
5049
}
5150

5251
var res v1pb.ListDatabasesResponse
53-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
54-
if err != nil {
52+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
5553
return nil, err
5654
}
5755

@@ -65,14 +63,6 @@ func (c *client) UpdateDatabase(ctx context.Context, patch *v1pb.Database, updat
6563
return nil, err
6664
}
6765

68-
// updateMask := []string{}
69-
// if patch.Project != nil {
70-
// updateMask = append(updateMask, "project")
71-
// }
72-
// if patch.Labels != nil {
73-
// updateMask = append(updateMask, "labels")
74-
// }
75-
7666
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)))
7767
if err != nil {
7868
return nil, err
@@ -84,8 +74,7 @@ func (c *client) UpdateDatabase(ctx context.Context, patch *v1pb.Database, updat
8474
}
8575

8676
var res v1pb.Database
87-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
88-
if err != nil {
77+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
8978
return nil, err
9079
}
9180

client/environment.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77
"strings"
88

9-
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
9+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
1010
"google.golang.org/protobuf/encoding/protojson"
1111
)
1212

@@ -28,8 +28,7 @@ func (c *client) CreateEnvironment(ctx context.Context, environmentID string, cr
2828
}
2929

3030
var env v1pb.Environment
31-
err = ProtojsonUnmarshaler.Unmarshal(body, &env)
32-
if err != nil {
31+
if err := ProtojsonUnmarshaler.Unmarshal(body, &env); err != nil {
3332
return nil, err
3433
}
3534

@@ -49,8 +48,7 @@ func (c *client) GetEnvironment(ctx context.Context, environmentName string) (*v
4948
}
5049

5150
var env v1pb.Environment
52-
err = ProtojsonUnmarshaler.Unmarshal(body, &env)
53-
if err != nil {
51+
if err := ProtojsonUnmarshaler.Unmarshal(body, &env); err != nil {
5452
return nil, err
5553
}
5654

@@ -70,8 +68,7 @@ func (c *client) ListEnvironment(ctx context.Context, showDeleted bool) (*v1pb.L
7068
}
7169

7270
var res v1pb.ListEnvironmentsResponse
73-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
74-
if err != nil {
71+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
7572
return nil, err
7673
}
7774

@@ -96,8 +93,7 @@ func (c *client) UpdateEnvironment(ctx context.Context, patch *v1pb.Environment,
9693
}
9794

9895
var env v1pb.Environment
99-
err = ProtojsonUnmarshaler.Unmarshal(body, &env)
100-
if err != nil {
96+
if err := ProtojsonUnmarshaler.Unmarshal(body, &env); err != nil {
10197
return nil, err
10298
}
10399

@@ -130,8 +126,7 @@ func (c *client) UndeleteEnvironment(ctx context.Context, environmentName string
130126
}
131127

132128
var res v1pb.Environment
133-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
134-
if err != nil {
129+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
135130
return nil, err
136131
}
137132

client/instance.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77
"strings"
88

9-
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
9+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
1010
"google.golang.org/protobuf/encoding/protojson"
1111
)
1212

@@ -23,8 +23,7 @@ func (c *client) ListInstance(ctx context.Context, showDeleted bool) (*v1pb.List
2323
}
2424

2525
var res v1pb.ListInstancesResponse
26-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
27-
if err != nil {
26+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
2827
return nil, err
2928
}
3029

@@ -44,8 +43,7 @@ func (c *client) GetInstance(ctx context.Context, instanceName string) (*v1pb.In
4443
}
4544

4645
var res v1pb.Instance
47-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
48-
if err != nil {
46+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
4947
return nil, err
5048
}
5149

@@ -71,8 +69,7 @@ func (c *client) CreateInstance(ctx context.Context, instanceID string, instance
7169
}
7270

7371
var res v1pb.Instance
74-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
75-
if err != nil {
72+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
7673
return nil, err
7774
}
7875

@@ -98,8 +95,7 @@ func (c *client) UpdateInstance(ctx context.Context, patch *v1pb.Instance, updat
9895
}
9996

10097
var res v1pb.Instance
101-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
102-
if err != nil {
98+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
10399
return nil, err
104100
}
105101

@@ -132,8 +128,7 @@ func (c *client) UndeleteInstance(ctx context.Context, instanceName string) (*v1
132128
}
133129

134130
var res v1pb.Instance
135-
err = ProtojsonUnmarshaler.Unmarshal(body, &res)
136-
if err != nil {
131+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
137132
return nil, err
138133
}
139134

0 commit comments

Comments
 (0)