Skip to content

Commit

Permalink
Merge pull request koderover#615 from 27149chen/add_webhook_rollback
Browse files Browse the repository at this point in the history
webhook rollback
  • Loading branch information
lilianzhu authored Nov 15, 2021
2 parents 99b2cc6 + bee8e70 commit f27fd3e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
33 changes: 29 additions & 4 deletions pkg/cli/upgradeassistant/cmd/migrate/170.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package migrate

import (
internalmongodb "github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/repository/mongodb"
"github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/upgradepath"
"github.com/koderover/zadig/pkg/microservice/aslan/config"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/mongodb"
gitservice "github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/git"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/github"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/gitlab"
"github.com/koderover/zadig/pkg/setting"
Expand All @@ -36,7 +38,7 @@ func init() {
func V160ToV170() error {
log.Info("Migrating data from 1.6.0 to 1.7.0")

err := refreshWebHookSecret()
err := refreshWebHookSecret(gitservice.GetHookSecret())
if err != nil {
log.Errorf("Failed to refresh webhook secret, err: %s", err)
return err
Expand All @@ -47,14 +49,27 @@ func V160ToV170() error {

func V170ToV160() error {
log.Info("Rollback data from 1.7.0 to 1.6.0")

token := getWebHookTokenFromOrganization()
if token == "" {
return nil
}

log.Info("Start to rollback all webhook secrets")
err := refreshWebHookSecret(token)
if err != nil {
log.Errorf("Failed to refresh webhook secret, err: %s", err)
return err
}

return nil
}

type hookRefresher interface {
RefreshWebHookSecret(owner, repo, hookID string) error
RefreshWebHookSecret(secret, owner, repo, hookID string) error
}

func refreshWebHookSecret() error {
func refreshWebHookSecret(secret string) error {
hooks, err := mongodb.NewWebHookColl().List()
if err != nil {
log.Errorf("Failed to list webhooks, err: %s", err)
Expand Down Expand Up @@ -89,7 +104,7 @@ func refreshWebHookSecret() error {
continue
}

if err = cl.RefreshWebHookSecret(hook.Owner, hook.Repo, hook.HookID); err != nil {
if err = cl.RefreshWebHookSecret(secret, hook.Owner, hook.Repo, hook.HookID); err != nil {
log.Warnf("Failed to refresh webhook secret for hook %d in %s/%s, err: %s", hook.HookID, hook.Owner, hook.Repo, err)
continue
}
Expand All @@ -114,3 +129,13 @@ func getCodeHostByAddressAndOwner(address, owner string, all []*systemconfig.Cod

return nil
}

func getWebHookTokenFromOrganization() string {
org, found, err := internalmongodb.NewOrganizationColl().Get(1)
if err != nil || !found {
log.Warnf("Failed to get organization, err: %s", err)
return ""
}

return org.Token
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type Organization struct {
ID int `bson:"id" json:"id"`
Token string `bson:"token,omitempty" json:"token,omitempty"`
}

func (Organization) TableName() string {
return "organization"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package mongodb

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"

"github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/repository/models"
"github.com/koderover/zadig/pkg/config"
mongotool "github.com/koderover/zadig/pkg/tool/mongo"
)

type OrganizationColl struct {
*mongo.Collection

coll string
}

func NewOrganizationColl() *OrganizationColl {
name := models.Organization{}.TableName()
return &OrganizationColl{
Collection: mongotool.Database(config.MongoDatabase()).Collection(name),
coll: name,
}
}

func (c *OrganizationColl) Get(id int) (*models.Organization, bool, error) {
org := &models.Organization{}
query := bson.M{"id": id}

err := c.FindOne(context.TODO(), query).Decode(org)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, false, nil
}
return nil, false, err
}
return org, true, nil
}
4 changes: 2 additions & 2 deletions pkg/microservice/aslan/core/common/service/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func (c *Client) DeleteWebHook(owner, repo, hookID string) error {
return c.DeleteHook(context.TODO(), owner, repo, hookIDInt)
}

func (c *Client) RefreshWebHookSecret(owner, repo, hookID string) error {
func (c *Client) RefreshWebHookSecret(secret, owner, repo, hookID string) error {
hookIDInt, err := strconv.ParseInt(hookID, 10, 64)
if err != nil {
return err
}
_, err = c.UpdateHook(context.TODO(), owner, repo, hookIDInt, &git.Hook{
URL: config.WebHookURL(),
Secret: gitservice.GetHookSecret(),
Secret: secret,
})

return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/microservice/aslan/core/common/service/gitlab/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func (c *Client) DeleteWebHook(owner, repo, hookID string) error {
return c.DeleteProjectHook(owner, repo, hookIDInt)
}

func (c *Client) RefreshWebHookSecret(owner, repo, hookID string) error {
func (c *Client) RefreshWebHookSecret(secret, owner, repo, hookID string) error {
hookIDInt, err := strconv.Atoi(hookID)
if err != nil {
return err
}
_, err = c.UpdateProjectHook(owner, repo, hookIDInt, &git.Hook{
URL: config.WebHookURL(),
Secret: gitservice.GetHookSecret(),
Secret: secret,
})

return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func prEventToPipelineTasks(event *github.PullRequestEvent, requestID string, lo
commitMessage = *event.PullRequest.Title
)

address, err := util.GetAddress(event.Repo.GetURL())
address, err := util.GetAddress(event.Repo.GetHTMLURL())
if err != nil {
log.Errorf("GetAddress failed, url: %s, err: %s", event.Repo.GetURL(), err)
return nil, err
Expand Down

0 comments on commit f27fd3e

Please sign in to comment.