mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-09 04:01:14 +01:00
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
/*
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
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 service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
"github.com/vmware/harbor/models"
|
|
svc_utils "github.com/vmware/harbor/service/utils"
|
|
|
|
"github.com/astaxie/beego"
|
|
)
|
|
|
|
type NotificationHandler struct {
|
|
beego.Controller
|
|
}
|
|
|
|
const MEDIA_TYPE_MANIFEST = "application/vnd.docker.distribution.manifest.v1+json"
|
|
|
|
func (n *NotificationHandler) Post() {
|
|
var notification models.Notification
|
|
// log.Printf("Notification Handler triggered!\n")
|
|
// log.Printf("request body in string: %s", string(n.Ctx.Input.CopyBody()))
|
|
err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), ¬ification)
|
|
|
|
if err != nil {
|
|
beego.Error("error while decoding json: ", err)
|
|
return
|
|
}
|
|
var username, action, repo, project string
|
|
for _, e := range notification.Events {
|
|
if e.Target.MediaType == MEDIA_TYPE_MANIFEST && strings.HasPrefix(e.Request.UserAgent, "docker") {
|
|
username = e.Actor.Name
|
|
action = e.Action
|
|
repo = e.Target.Repository
|
|
if strings.Contains(repo, "/") {
|
|
project = repo[0:strings.LastIndex(repo, "/")]
|
|
}
|
|
if username == "" {
|
|
username = "anonymous"
|
|
}
|
|
go dao.AccessLog(username, project, repo, action)
|
|
if action == "push" {
|
|
go func() {
|
|
err2 := svc_utils.RefreshCatalogCache()
|
|
if err2 != nil {
|
|
beego.Error("Error happens when refreshing cache:", err2)
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (n *NotificationHandler) Render() error {
|
|
return nil
|
|
}
|