Skip to content

Commit 82fdc83

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

File tree

24 files changed

+2008
-7
lines changed

24 files changed

+2008
-7
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,11 @@ 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+
}
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+
WorkItemType string `bson:"work_item_type" json:"work_item_type"`
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2022 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+
"github.com/koderover/zadig/v2/pkg/types"
21+
"go.mongodb.org/mongo-driver/bson/primitive"
22+
)
23+
24+
type LarkPluginWorkitemTask struct {
25+
ID primitive.ObjectID `bson:"_id,omitempty" yaml:"-" json:"id"`
26+
LarkWorkItemTypeKey string `bson:"lark_workitem_type_key" yaml:"lark_workitem_type_key" json:"lark_workitem_type_key"`
27+
LarkWorkItemID string `bson:"lark_workitem_id" yaml:"lark_workitem_id" json:"lark_workitem_id"`
28+
WorkflowName string `bson:"workflow_name" yaml:"workflow_name" json:"workflow_name"`
29+
WorkflowTaskID int64 `bson:"workflow_task_id" yaml:"workflow_task_id" json:"workflow_task_id"`
30+
Hash string `bson:"hash" yaml:"hash" json:"hash"`
31+
Creator types.UserBriefInfo `bson:"creator" yaml:"creator" json:"creator"`
32+
CreateTime int64 `bson:"create_time" yaml:"create_time" json:"create_time"`
33+
}
34+
35+
func (LarkPluginWorkitemTask) TableName() string {
36+
return "lark_plugin_workitem_task"
37+
}

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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 LarkPluginWorkflowConfigColl struct {
32+
*mongo.Collection
33+
34+
coll string
35+
}
36+
37+
func NewLarkPluginWorkflowConfigColl() *LarkPluginWorkflowConfigColl {
38+
name := models.LarkPluginWorkflowConfig{}.TableName()
39+
return &LarkPluginWorkflowConfigColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name}
40+
}
41+
42+
func (c *LarkPluginWorkflowConfigColl) GetCollectionName() string {
43+
return c.coll
44+
}
45+
46+
func (c *LarkPluginWorkflowConfigColl) EnsureIndex(ctx context.Context) error {
47+
mod := []mongo.IndexModel{
48+
{
49+
Keys: bson.D{
50+
bson.E{Key: "workspace_id", Value: 1},
51+
bson.E{Key: "work_item_type", Value: 1},
52+
},
53+
Options: options.Index().SetUnique(true),
54+
},
55+
}
56+
57+
_, err := c.Indexes().CreateMany(ctx, mod)
58+
59+
return err
60+
}
61+
62+
func (c *LarkPluginWorkflowConfigColl) Get(workspaceID string) ([]*models.LarkPluginWorkflowConfig, error) {
63+
resp := make([]*models.LarkPluginWorkflowConfig, 0)
64+
cursor, err := c.Collection.Find(context.TODO(), bson.M{"workspace_id": workspaceID})
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
err = cursor.All(context.TODO(), &resp)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
return resp, nil
75+
}
76+
77+
func (c *LarkPluginWorkflowConfigColl) GetWorkItemTypeConfig(workspaceID, workItemType string) (*models.LarkPluginWorkflowConfig, error) {
78+
resp := new(models.LarkPluginWorkflowConfig)
79+
80+
query := bson.M{"workspace_id": workspaceID, "work_item_type": workItemType}
81+
err := c.Collection.FindOne(context.TODO(), query).Decode(resp)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
return resp, nil
87+
}
88+
89+
func (c *LarkPluginWorkflowConfigColl) Update(configs []*models.LarkPluginWorkflowConfig) error {
90+
if len(configs) == 0 {
91+
return nil
92+
}
93+
94+
// 构建批量操作
95+
operations := make([]mongo.WriteModel, 0, len(configs))
96+
for _, config := range configs {
97+
// 使用 WorkspaceID 和 WorkItemType 作为唯一标识字段进行 upsert 操作
98+
query := bson.M{"workspace_id": config.WorkspaceID, "work_item_type": config.WorkItemType}
99+
operation := mongo.NewReplaceOneModel().
100+
SetFilter(query).
101+
SetReplacement(config).
102+
SetUpsert(true)
103+
operations = append(operations, operation)
104+
}
105+
106+
// 执行批量操作
107+
opts := options.BulkWrite().SetOrdered(false)
108+
_, err := c.BulkWrite(context.TODO(), operations, opts)
109+
110+
return err
111+
}

0 commit comments

Comments
 (0)