Skip to content

Commit 229c965

Browse files
committed
support lark plugin feature
Signed-off-by: Patrick Zhao <[email protected]>
1 parent d00f952 commit 229c965

File tree

21 files changed

+1734
-12
lines changed

21 files changed

+1734
-12
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ require (
253253
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
254254
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
255255
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
256+
github.com/larksuite/project-oapi-sdk-golang v1.0.15 // indirect
256257
github.com/leodido/go-urn v1.2.4 // indirect
257258
github.com/lib/pq v1.10.9 // indirect
258259
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ github.com/larksuite/oapi-sdk-go/v3 v3.0.10 h1:yf/0GBgpHtGpAZCzJBhOIQVHxe8gpveXi
795795
github.com/larksuite/oapi-sdk-go/v3 v3.0.10/go.mod h1:FKi8vBgtkBt/xNRQUwdWvoDmsPh7/wP75Sn5IBIBQLk=
796796
github.com/larksuite/oapi-sdk-go/v3 v3.4.20 h1:Ul1NWAHXYzbXBHFmUxMTSZ9v2ahy/O8EthYOQnLvPo0=
797797
github.com/larksuite/oapi-sdk-go/v3 v3.4.20/go.mod h1:ZEplY+kwuIrj/nqw5uSCINNATcH3KdxSN7y+UxYY5fI=
798+
github.com/larksuite/project-oapi-sdk-golang v1.0.15 h1:5GqWotqVDTWk3Sg+YjTtaocRx/0dHQabU6F2Qd7MUSk=
799+
github.com/larksuite/project-oapi-sdk-golang v1.0.15/go.mod h1:M4gZ6QA4sa6U9iukFsSVQ58LQwlWO8eqk13nArHRHCk=
798800
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
799801
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
800802
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=

pkg/config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,14 @@ func RedisPassword() string {
295295
func RedisCommonCacheTokenDB() int {
296296
return viper.GetInt(setting.ENVRedisCommonCacheDB)
297297
}
298+
299+
func LarkPluginID() string {
300+
return viper.GetString(setting.ENVLarkPluginID)
301+
}
302+
303+
func LarkPluginSecret() string {
304+
return viper.GetString(setting.ENVLarkPluginSecret)
305+
}
306+
func LarkPluginAccessTokenType() int {
307+
return viper.GetInt(setting.ENVLarkPluginAccessTokenType)
308+
}

pkg/microservice/aslan/core/common/repository/models/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ type ServiceWithModule struct {
200200
ServiceModule string `bson:"service_module" json:"service_module" yaml:"service_module"`
201201
}
202202

203+
func (s *ServiceWithModule) GetKey() string {
204+
return s.ServiceName + "-" + s.ServiceModule
205+
}
206+
203207
type ServiceModuleTargetBase struct {
204208
ProductName string `json:"product_name"`
205209
ServiceWithModule `json:",inline"`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2025 The KodeRover Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package models
18+
19+
import (
20+
"go.mongodb.org/mongo-driver/bson/primitive"
21+
)
22+
23+
type LarkPluginAuthConfig struct {
24+
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
25+
WorkspaceID string `bson:"workspace_id" json:"workspace_id"`
26+
ZadigAddress string `bson:"zadig_address" json:"zadig_address"`
27+
ApiToken string `bson:"api_token" json:"api_token"`
28+
UpdateTime int64 `bson:"update_time" json:"update_time"`
29+
}
30+
31+
func (LarkPluginAuthConfig) TableName() string {
32+
return "lark_plugin_auth_config"
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2025 The KodeRover Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package models
18+
19+
import (
20+
"go.mongodb.org/mongo-driver/bson/primitive"
21+
)
22+
23+
type LarkPluginWorkflowConfig struct {
24+
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
25+
WorkspaceID string `bson:"workspace_id" json:"workspace_id"`
26+
WorkItemTypeKey string `bson:"work_item_type_key" json:"work_item_type_key"`
27+
Nodes []*LarkPluginWorkflowConfigNode `bson:"nodes" json:"nodes"`
28+
UpdateTime int64 `bson:"update_time" json:"update_time"`
29+
}
30+
31+
type LarkPluginWorkflowConfigNode struct {
32+
ID string `bson:"id" json:"id"`
33+
Name string `bson:"name" json:"name"`
34+
ProjectKey string `bson:"project_key" json:"project_key"`
35+
WorkflowName string `bson:"workflow_name" json:"workflow_name"`
36+
}
37+
38+
func (LarkPluginWorkflowConfig) TableName() string {
39+
return "lark_plugin_workflow_config"
40+
}

pkg/microservice/aslan/core/common/repository/models/wokflow_task_v4.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type WorkflowTask struct {
6464
Hash string `bson:"hash" json:"hash"`
6565
ApprovalTicketID string `bson:"approval_ticket_id" json:"approval_ticket_id"`
6666
ApprovalID string `bson:"approval_id" json:"approval_id"`
67+
68+
LarkWorkItemTypeKey string `bson:"lark_workitem_type_key" json:"lark_workitem_type_key"`
69+
LarkWorkItemID string `bson:"lark_workitem_id" json:"lark_workitem_id"`
6770
}
6871

6972
func (WorkflowTask) TableName() string {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2024 The KodeRover Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mongodb
18+
19+
import (
20+
"context"
21+
22+
"go.mongodb.org/mongo-driver/bson"
23+
"go.mongodb.org/mongo-driver/mongo"
24+
"go.mongodb.org/mongo-driver/mongo/options"
25+
26+
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
27+
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
28+
mongotool "github.com/koderover/zadig/v2/pkg/tool/mongo"
29+
)
30+
31+
type LarkPluginAuthConfigColl struct {
32+
*mongo.Collection
33+
34+
coll string
35+
}
36+
37+
func NewLarkPluginAuthConfigColl() *LarkPluginAuthConfigColl {
38+
name := models.LarkPluginAuthConfig{}.TableName()
39+
return &LarkPluginAuthConfigColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name}
40+
}
41+
42+
func (c *LarkPluginAuthConfigColl) GetCollectionName() string {
43+
return c.coll
44+
}
45+
46+
func (c *LarkPluginAuthConfigColl) EnsureIndex(ctx context.Context) error {
47+
mod := []mongo.IndexModel{
48+
{
49+
Keys: bson.D{
50+
bson.E{Key: "workspace_id", Value: 1},
51+
},
52+
Options: options.Index().SetUnique(true),
53+
},
54+
}
55+
56+
_, err := c.Indexes().CreateMany(ctx, mod)
57+
58+
return err
59+
}
60+
61+
func (c *LarkPluginAuthConfigColl) Get(workspaceID string) (*models.LarkPluginAuthConfig, error) {
62+
resp := new(models.LarkPluginAuthConfig)
63+
cursor, err := c.Collection.Find(context.TODO(), bson.M{"workspace_id": workspaceID})
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
err = cursor.All(context.TODO(), &resp)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
return resp, nil
74+
}
75+
76+
func (c *LarkPluginAuthConfigColl) Update(config *models.LarkPluginAuthConfig) error {
77+
if config == nil {
78+
return nil
79+
}
80+
81+
query := bson.M{"workspace_id": config.WorkspaceID}
82+
opts := options.Replace().SetUpsert(true)
83+
_, err := c.ReplaceOne(context.TODO(), query, config, opts)
84+
85+
return err
86+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright 2024 The KodeRover Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mongodb
18+
19+
import (
20+
"context"
21+
"time"
22+
23+
"go.mongodb.org/mongo-driver/bson"
24+
"go.mongodb.org/mongo-driver/mongo"
25+
"go.mongodb.org/mongo-driver/mongo/options"
26+
27+
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
28+
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
29+
mongotool "github.com/koderover/zadig/v2/pkg/tool/mongo"
30+
)
31+
32+
type LarkPluginWorkflowConfigColl struct {
33+
*mongo.Collection
34+
35+
coll string
36+
}
37+
38+
func NewLarkPluginWorkflowConfigColl() *LarkPluginWorkflowConfigColl {
39+
name := models.LarkPluginWorkflowConfig{}.TableName()
40+
return &LarkPluginWorkflowConfigColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name}
41+
}
42+
43+
func (c *LarkPluginWorkflowConfigColl) GetCollectionName() string {
44+
return c.coll
45+
}
46+
47+
func (c *LarkPluginWorkflowConfigColl) EnsureIndex(ctx context.Context) error {
48+
mod := []mongo.IndexModel{
49+
{
50+
Keys: bson.D{
51+
bson.E{Key: "workspace_id", Value: 1},
52+
bson.E{Key: "work_item_type_key", Value: 1},
53+
},
54+
Options: options.Index().SetUnique(true),
55+
},
56+
}
57+
58+
_, err := c.Indexes().CreateMany(ctx, mod)
59+
60+
return err
61+
}
62+
63+
func (c *LarkPluginWorkflowConfigColl) Get(workspaceID string) ([]*models.LarkPluginWorkflowConfig, error) {
64+
resp := make([]*models.LarkPluginWorkflowConfig, 0)
65+
cursor, err := c.Collection.Find(context.TODO(), bson.M{"workspace_id": workspaceID})
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
err = cursor.All(context.TODO(), &resp)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
return resp, nil
76+
}
77+
78+
func (c *LarkPluginWorkflowConfigColl) GetWorkItemTypeConfig(workspaceID, workItemType string) (*models.LarkPluginWorkflowConfig, error) {
79+
resp := new(models.LarkPluginWorkflowConfig)
80+
81+
query := bson.M{"workspace_id": workspaceID, "work_item_type_key": workItemType}
82+
err := c.Collection.FindOne(context.TODO(), query).Decode(resp)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
return resp, nil
88+
}
89+
90+
func (c *LarkPluginWorkflowConfigColl) Update(configs []*models.LarkPluginWorkflowConfig) error {
91+
if len(configs) == 0 {
92+
return nil
93+
}
94+
95+
// 构建批量操作
96+
operations := make([]mongo.WriteModel, 0, len(configs))
97+
for _, config := range configs {
98+
config.UpdateTime = time.Now().Unix()
99+
// 使用 WorkspaceID 和 WorkItemType 作为唯一标识字段进行 upsert 操作
100+
query := bson.M{"workspace_id": config.WorkspaceID, "work_item_type_key": config.WorkItemTypeKey}
101+
operation := mongo.NewReplaceOneModel().
102+
SetFilter(query).
103+
SetReplacement(config).
104+
SetUpsert(true)
105+
operations = append(operations, operation)
106+
}
107+
108+
// 执行批量操作
109+
opts := options.BulkWrite().SetOrdered(false)
110+
_, err := c.BulkWrite(context.TODO(), operations, opts)
111+
112+
return err
113+
}
114+
115+
func (c *LarkPluginWorkflowConfigColl) Delete(workItemTypeKeys []string) error {
116+
if len(workItemTypeKeys) == 0 {
117+
return nil
118+
}
119+
120+
query := bson.M{"work_item_type_key": bson.M{"$in": workItemTypeKeys}}
121+
_, err := c.DeleteMany(context.TODO(), query)
122+
return err
123+
}

0 commit comments

Comments
 (0)