Skip to content

Commit

Permalink
upgrade webhook (koderover#102)
Browse files Browse the repository at this point in the history
* upgrade webhook

Signed-off-by: lou <[email protected]>

* update workflow&pipeline hook name

Signed-off-by: lou <[email protected]>

* update after review

Signed-off-by: lou <[email protected]>
  • Loading branch information
27149chen authored Jul 21, 2021
1 parent 106d41f commit fbc313b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
4 changes: 4 additions & 0 deletions pkg/microservice/aslan/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/webhook"
environmentservice "github.com/koderover/zadig/pkg/microservice/aslan/core/environment/service"
systemservice "github.com/koderover/zadig/pkg/microservice/aslan/core/system/service"
webhookservice "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/service/webhook"
workflowservice "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/service/workflow"
"github.com/koderover/zadig/pkg/setting"
"github.com/koderover/zadig/pkg/tool/log"
Expand Down Expand Up @@ -87,6 +88,9 @@ func Start(ctx context.Context) {
environmentservice.ResetProductsStatus()

go StartControllers(ctx.Done())

// 仅用于升级 release v1.2.1, 将在下一版本移除
webhookservice.SyncWebHooks()
}

func Stop(ctx context.Context) {
Expand Down
74 changes: 74 additions & 0 deletions pkg/microservice/aslan/core/workflow/service/webhook/syncer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2021 The KodeRover Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import (
"context"
"fmt"

"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/mongodb"
commonservice "github.com/koderover/zadig/pkg/microservice/aslan/core/common/service"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/webhook"
"github.com/koderover/zadig/pkg/tool/log"
)

func SyncWebHooks() {
logger := log.SugaredLogger()

count, _ := mongodb.NewWebHookColl().EstimatedDocumentCount(context.TODO())
if count > 0 {
return
}

workflows, _ := mongodb.NewWorkflowColl().List(&mongodb.ListWorkflowOption{})
for _, workflow := range workflows {
logger.Debugf("Processing workflow %s", workflow.Name)
if workflow.HookCtl != nil {
for i, h := range workflow.HookCtl.Items {
h.MainRepo.Name = fmt.Sprintf("trigger%d", i+1)
workflow.HookCtl.Items[i] = h
}
if err := mongodb.NewWorkflowColl().Replace(workflow); err != nil {
log.Errorf("Failed to update workflow, err: %s", err)
continue
}
}

if err := commonservice.ProcessWebhook(workflow.HookCtl.Items, nil, webhook.WorkflowPrefix+workflow.Name, logger); err != nil {
log.Errorf("Failed to process webhook, err: %s", err)
}
}

pipelines, _ := mongodb.NewPipelineColl().List(&mongodb.PipelineListOption{})
for _, pipeline := range pipelines {
logger.Debugf("Processing pipeline %s", pipeline.Name)
if pipeline.Hook != nil {
for i, h := range pipeline.Hook.GitHooks {
h.Name = fmt.Sprintf("trigger%d", i+1)
pipeline.Hook.GitHooks[i] = h
}
if err := mongodb.NewPipelineColl().Upsert(pipeline); err != nil {
log.Errorf("Failed to update pipeline, err: %s", err)
continue
}
}

if err := commonservice.ProcessWebhook(pipeline.Hook.GitHooks, nil, webhook.PipelinePrefix+pipeline.Name, logger); err != nil {
log.Errorf("Failed to process webhook, err: %s", err)
}
}
}
18 changes: 0 additions & 18 deletions pkg/microservice/aslan/server/rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,10 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) {
// ---------------------------------------------------------------------------------------
public := router.Group("/api")
{
// new webhook url for all
public.POST("/webhook", func(c *gin.Context) {
c.Request.URL.Path = "/api/workflow/webhook"
s.HandleContext(c)
})
// 内部路由重定向。接口路由更新后,需要保证旧路由可用,否则github、gitlab的配置需要手动更新。
public.POST("/ci/webhook", func(c *gin.Context) {
c.Request.URL.Path = "/api/workflow/webhook"
s.HandleContext(c)
})
public.POST("/githubWebHook", func(c *gin.Context) {
c.Request.URL.Path = "/api/workflow/webhook"
s.HandleContext(c)
})
public.POST("/gitlabhook", func(c *gin.Context) {
c.Request.URL.Path = "/api/workflow/webhook"
s.HandleContext(c)
})
public.POST("/gerritHook", func(c *gin.Context) {
c.Request.URL.Path = "/api/workflow/webhook"
s.HandleContext(c)
})
public.GET("/health", commonhandler.Health)
}

Expand Down

0 comments on commit fbc313b

Please sign in to comment.