2023-04-25 05:18:42 +02:00
|
|
|
// Copyright Project Harbor Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
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
|
2021-08-15 11:21:39 +02:00
|
|
|
case job.StoppedStatus:
|
|
|
|
eventType = event2.TopicScanningStopped
|
|
|
|
topic = event2.TopicScanningStopped
|
|
|
|
case job.ErrorStatus:
|
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
|
|
|
|
}
|