2020-03-16 09:56:34 +01:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
2020-12-14 06:34:35 +01:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-03-24 13:45:45 +01:00
|
|
|
event2 "github.com/goharbor/harbor/src/controller/event"
|
2020-12-14 06:34:35 +01:00
|
|
|
"github.com/goharbor/harbor/src/jobservice/job"
|
2020-03-16 09:56:34 +01:00
|
|
|
"github.com/goharbor/harbor/src/pkg/notifier/event"
|
|
|
|
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
autoTriggeredOperator = "auto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ScanImageMetaData defines meta data of image scanning event
|
|
|
|
type ScanImageMetaData struct {
|
|
|
|
Artifact *v1.Artifact
|
|
|
|
Status string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve image scanning metadata into common chart event
|
|
|
|
func (si *ScanImageMetaData) Resolve(evt *event.Event) error {
|
|
|
|
var eventType string
|
|
|
|
var topic string
|
|
|
|
|
2020-12-14 06:34:35 +01:00
|
|
|
switch job.Status(si.Status) {
|
|
|
|
case job.SuccessStatus:
|
2020-03-16 09:56:34 +01:00
|
|
|
eventType = event2.TopicScanningCompleted
|
|
|
|
topic = event2.TopicScanningCompleted
|
2020-12-14 06:34:35 +01:00
|
|
|
case job.ErrorStatus, job.StoppedStatus:
|
2020-03-16 09:56:34 +01:00
|
|
|
eventType = event2.TopicScanningFailed
|
|
|
|
topic = event2.TopicScanningFailed
|
|
|
|
default:
|
2020-12-14 06:34:35 +01:00
|
|
|
return fmt.Errorf("not supported scan hook status %s", si.Status)
|
2020-03-16 09:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data := &event2.ScanImageEvent{
|
|
|
|
EventType: eventType,
|
|
|
|
Artifact: si.Artifact,
|
|
|
|
OccurAt: time.Now(),
|
|
|
|
Operator: autoTriggeredOperator,
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.Topic = topic
|
|
|
|
evt.Data = data
|
|
|
|
return nil
|
|
|
|
}
|