Skip to content

Commit

Permalink
fix: update1
Browse files Browse the repository at this point in the history
Signed-off-by: Gosha <[email protected]>
  • Loading branch information
gosharo committed Jul 11, 2023
1 parent 10b1dbe commit dc8358b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 38 deletions.
39 changes: 32 additions & 7 deletions pkg/git_provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,33 +298,58 @@ func (c *GithubClientImpl) SetStatus(ctx *context.Context, repo *string, commit
return nil
}

func (c *GithubClientImpl) PingHooks(ctx *context.Context) error {

func (c *GithubClientImpl) PingHooks(ctx *context.Context) ([]*FailedHooks, error) {
var failedHooks []*FailedHooks
foundFailedHook := false
if c.cfg.OrgLevelWebhook {
for _, hook := range c.hooks {
resp, err := c.client.Organizations.PingHook(*ctx, c.cfg.OrgName, *hook.ID)
if err != nil {
return err
failedHook := &FailedHooks{
Hook: hook,
Err: err,
}
failedHooks = append(failedHooks, failedHook)
foundFailedHook = true
}

if resp.StatusCode == http.StatusNotFound {
return err
failedHook := &FailedHooks{
Hook: hook,
Err: fmt.Errorf("unable to find organization webhook for hook %s", hook.Config["url"]),
}
failedHooks = append(failedHooks, failedHook)
foundFailedHook = true
}
}
} else {
for _, repo := range strings.Split(c.cfg.GitProviderConfig.RepoList, ",") {
for _, hook := range c.hooks {
resp, err := c.client.Repositories.PingHook(*ctx, c.cfg.GitProviderConfig.OrgName, repo, *hook.ID)
if err != nil {
return err
failedHook := &FailedHooks{
Hook: hook,
Err: err,
}
failedHooks = append(failedHooks, failedHook)
foundFailedHook = true
}

if resp.StatusCode == http.StatusNotFound {
return err
failedHook := &FailedHooks{
Hook: hook,
Err: fmt.Errorf("unable to find repo webhook for repo:%s hook: %s", repo, hook.Config["url"]),
}
failedHooks = append(failedHooks, failedHook)
foundFailedHook = true
}
}
}
}

return nil
if foundFailedHook {
return failedHooks, fmt.Errorf("found failed hook %s", failedHooks)

Check failure on line 351 in pkg/git_provider/github.go

View workflow job for this annotation

GitHub Actions / Go Lint

printf: fmt.Errorf format %s has arg failedHooks of wrong type []*github.com/rookout/piper/pkg/git_provider.FailedHooks (govet)

Check failure on line 351 in pkg/git_provider/github.go

View workflow job for this annotation

GitHub Actions / Unit Tests

fmt.Errorf format %s has arg failedHooks of wrong type []*github.com/rookout/piper/pkg/git_provider.FailedHooks
}

return failedHooks, nil
}
7 changes: 6 additions & 1 deletion pkg/git_provider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"net/http"
)

type FailedHooks struct {
Hook *github.Hook
Err error
}

type CommitFile struct {
Path *string `json:"path"`
Content *string `json:"content"`
Expand Down Expand Up @@ -33,5 +38,5 @@ type Client interface {
UnsetWebhook(ctx *context.Context) error
HandlePayload(request *http.Request, secret []byte) (*WebhookPayload, error)
SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error
PingHooks(ctx *context.Context) error
PingHooks(ctx *context.Context) ([]*FailedHooks, error)
}
42 changes: 23 additions & 19 deletions pkg/server/health.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
package server

type Health interface {
check(msg healthCheck) error
recovery(msg healthCheck) error
failure(msg healthCheck) error
}
import (
"github.com/rookout/piper/pkg/clients"
"golang.org/x/net/context"
)

type healthCheck struct {
id string
id int64

Check failure on line 9 in pkg/server/health.go

View workflow job for this annotation

GitHub Actions / Go Lint

field `id` is unused (unused)
ctx *context.Context

Check failure on line 10 in pkg/server/health.go

View workflow job for this annotation

GitHub Actions / Go Lint

field `ctx` is unused (unused)
}

type HealthChecker struct {
Check chan *healthCheck
Recovery chan *healthCheck
Failure chan *healthCheck
clients *clients.Clients
Check chan *healthCheck
Failure chan *healthCheck
}

func NewHealthChecker() *HealthChecker {
func NewHealthChecker(clients *clients.Clients) *HealthChecker {
return &HealthChecker{
Check: make(chan *healthCheck),
Recovery: make(chan *healthCheck),
Failure: make(chan *healthCheck),
clients: clients,
Check: make(chan *healthCheck),
Failure: make(chan *healthCheck),
}
}

func (h *HealthChecker) check(msg *healthCheck) error {
func (h *HealthChecker) New(msg *healthCheck) error {
h.Check <- msg
return nil
}

func (h *HealthChecker) recovery(msg *healthCheck) error {
h.Recovery <- msg
func (h *HealthChecker) Fail(msg *healthCheck) error {
h.Failure <- msg
return nil
}

func (h *HealthChecker) failure(msg *healthCheck) error {
h.Failure <- msg
return nil
func (h *HealthChecker) Handle(healthCheck *healthCheck) {
//log.Printf("Health check started of: %d\n", healthCheck.id)
//failedHooks, err := h.clients.GitProvider.PingHooks(healthCheck.ctx)
//if err != nil {
// log.Printf("error in pinging hooks %s", err)
//}

}
11 changes: 0 additions & 11 deletions pkg/server/routes/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package routes
import (
"github.com/rookout/piper/pkg/clients"
"github.com/rookout/piper/pkg/conf"
"log"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -13,16 +12,6 @@ func AddHealthRoutes(cfg *conf.GlobalConfig, clients *clients.Clients, rg *gin.R
health := rg.Group("/healthz")

health.GET("", func(c *gin.Context) {
ctx := c.Copy().Request.Context()
err := clients.GitProvider.PingHooks(&ctx)
if err != nil {
log.Printf("error in pinging hooks %s", err)
//err = clients.GitProvider.SetWebhook()
}
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, "healthy")
})
}
7 changes: 7 additions & 0 deletions pkg/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ type Interface interface {
getRoutes()
ListenAndServe() *http.Server
}

type Health interface {
Check(msg healthCheck) error
Recover(msg healthCheck) error
Fail(msg healthCheck) error
handler() error
}

0 comments on commit dc8358b

Please sign in to comment.