Skip to content

Commit 648ed49

Browse files
authored
fix user can create duplicated name webhook in workflow (#4590)
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
1 parent 7ec4b4f commit 648ed49

File tree

4 files changed

+13
-30
lines changed

4 files changed

+13
-30
lines changed

pkg/microservice/aslan/core/common/repository/mongodb/workflow_v4_general_hook.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,7 @@ func (c *WorkflowV4GeneralHookColl) Get(ctx *internalhandler.Context, workflowNa
127127
}
128128

129129
func (c *WorkflowV4GeneralHookColl) Exists(ctx *internalhandler.Context, workflowName, hookName string) (bool, error) {
130-
if err := c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}); err != nil {
131-
if err.Err() == mongo.ErrNoDocuments {
132-
return false, nil
133-
}
134-
return false, err.Err()
135-
}
136-
137-
return true, nil
130+
return singleResultExists(c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}))
138131
}
139132

140133
func (c *WorkflowV4GeneralHookColl) Update(ctx *internalhandler.Context, id string, obj *models.WorkflowV4GeneralHook) error {

pkg/microservice/aslan/core/common/repository/mongodb/workflow_v4_git_hook.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,17 @@ func (c *WorkflowV4GitHookColl) Get(ctx *internalhandler.Context, workflowName,
127127
}
128128

129129
func (c *WorkflowV4GitHookColl) Exists(ctx *internalhandler.Context, workflowName, hookName string) (bool, error) {
130-
if err := c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}); err != nil {
131-
if err.Err() == mongo.ErrNoDocuments {
132-
return false, nil
133-
}
134-
return false, err.Err()
135-
}
130+
return singleResultExists(c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}))
131+
}
136132

133+
func singleResultExists(result *mongo.SingleResult) (bool, error) {
134+
err := result.Err()
135+
if err == mongo.ErrNoDocuments {
136+
return false, nil
137+
}
138+
if err != nil {
139+
return false, err
140+
}
137141
return true, nil
138142
}
139143

pkg/microservice/aslan/core/common/repository/mongodb/workflow_v4_jira_hook.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,7 @@ func (c *WorkflowV4JiraHookColl) Create(ctx *internalhandler.Context, obj *model
9999
}
100100

101101
func (c *WorkflowV4JiraHookColl) Exists(ctx *internalhandler.Context, workflowName, hookName string) (bool, error) {
102-
if err := c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}); err != nil {
103-
if err.Err() == mongo.ErrNoDocuments {
104-
return false, nil
105-
}
106-
return false, err.Err()
107-
}
108-
109-
return true, nil
102+
return singleResultExists(c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}))
110103
}
111104

112105
func (c *WorkflowV4JiraHookColl) List(ctx *internalhandler.Context, workflowName string) ([]*models.WorkflowV4JiraHook, error) {

pkg/microservice/aslan/core/common/repository/mongodb/workflow_v4_meego_hook.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,7 @@ func (c *WorkflowV4MeegoHookColl) Create(ctx *internalhandler.Context, obj *mode
9999
}
100100

101101
func (c *WorkflowV4MeegoHookColl) Exists(ctx *internalhandler.Context, workflowName, hookName string) (bool, error) {
102-
if err := c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}); err != nil {
103-
if err.Err() == mongo.ErrNoDocuments {
104-
return false, nil
105-
}
106-
return false, err.Err()
107-
}
108-
109-
return true, nil
102+
return singleResultExists(c.Collection.FindOne(ctx, bson.M{"workflow_name": workflowName, "name": hookName}))
110103
}
111104

112105
func (c *WorkflowV4MeegoHookColl) List(ctx *internalhandler.Context, workflowName string) ([]*models.WorkflowV4MeegoHook, error) {

0 commit comments

Comments
 (0)