fixed process logic and adapted test case

pull/483/head
Marc Herren 2020-03-02 10:35:21 +01:00
parent 2e09f0c8e3
commit a4a5829931
2 changed files with 49 additions and 41 deletions

View File

@ -4,8 +4,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
"regexp"
"time"
"github.com/keel-hq/keel/types"
"github.com/prometheus/client_golang/prometheus"
@ -46,7 +46,6 @@ func init() {
// "repo_type": "private"
// }
// }
}
type harborWebhook struct {
Type string `json:"type"`
@ -69,51 +68,60 @@ type harborWebhook struct {
}
func (s *TriggerServer) harborHandler(resp http.ResponseWriter, req *http.Request) {
qw := harborWebhook{}
if err := json.NewDecoder(req.Body).Decode(&qw); err != nil {
hn := harborWebhook{}
if err := json.NewDecoder(req.Body).Decode(&hn); err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("trigger.harborHandler: failed to decode request")
resp.WriteHeader(http.StatusBadRequest)
return
}
log.WithFields(log.Fields{
"event": hn,
}).Debug("harborHandler: received event, looking for a pushImage tag")
if qw.type == "pushImage" {
if qw.ResourceURL == "" {
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(resp, "ResourceURL cannot be empty")
if hn.Type == "pushImage" {
//go trough all the ressource arrays
for _, e := range hn.EventData.Resources {
fmt.Println("Push found!")
//Split the combined <URL>:<tag> into seperate fields
splitRegexp := regexp.MustCompile("(.*):(.*)")
splitString := splitRegexp.FindAllStringSubmatch(e.ResourceURL, -1)
DockerURL := splitString[0][1]
tag := splitString[0][2]
if len(DockerURL) == 0 {
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(resp, "DockerURL cannot be empty")
return
}
if len(tag) == 0 {
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(resp, "tags cannot be empty")
return
}
//create event
event := types.Event{}
event.CreatedAt = time.Now()
event.TriggerName = "harbor"
event.Repository.Name = DockerURL
event.Repository.Tag = tag
log.WithFields(log.Fields{
"action": hn.Type,
"tag": tag,
"repository": DockerURL,
"digest": e.Digest,
}).Debug("harborHandler: got registry notification, processing")
s.trigger(event)
newHarborWebhooksCounter.With(prometheus.Labels{"image": event.Repository.Name}).Inc()
resp.WriteHeader(http.StatusOK)
return
}
//Split the combined <URL>:<tag> into seperate fields
split_regexp := regexp.MustCompile("(.*):(.*)")
split_string := split_regexp.FindAllStringSubmatch(qw.ResourceURL,-1)
DockerURL := split_string[0][1]
tag := split_string[0][2]
if len(DockerURL) == 0 {
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(resp, "DockerURL cannot be empty")
return
}
if len(tag) == 0 {
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(resp, "tags cannot be empty")
return
}
//create event
event := types.Event{}
event.CreatedAt = time.Now()
event.TriggerName = "harbor"
event.Repository.Name = DockerURL
event.Repository.Tag = tag
s.trigger(event)
newHarborWebhooksCounter.With(prometheus.Labels{"image": event.Repository.Name}).Inc()
}
resp.WriteHeader(http.StatusOK)
return
}

View File

@ -16,7 +16,7 @@ var fakeHarborWebhook = ` {
"resources": [
{
"digest": "sha256:b4758aaed11c155a476b9857e1178f157759c99cb04c907a04993f5481eff848",
"tag": "2.1.6",
"tag": "1.2.3",
"resource_url": "quay.io/mynamespace/repository:1.2.3"
}
],
@ -31,7 +31,7 @@ var fakeHarborWebhook = ` {
}
`
func TestQuayWebhookHandler(t *testing.T) {
func TestHarborWebhookHandler(t *testing.T) {
fp := &fakeProvider{}
srv, teardown := NewTestingServer(fp)