Add batch distributor notification state artifacts
This commit is contained in:
@@ -141,6 +141,33 @@ func (s *FilesystemStore) SaveDistributorNotification(_ context.Context, resolve
|
||||
}, artifact)
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) BatchDistributorNotificationPath(ref BatchDistributorNotificationRef) (string, error) {
|
||||
if s == nil {
|
||||
return "", fmt.Errorf("state store is required")
|
||||
}
|
||||
if err := validateBatchNotificationRef(ref); err != nil {
|
||||
return "", err
|
||||
}
|
||||
localDate := ref.StartedAt.In(ref.Location).Format("2006-01-02")
|
||||
return s.join(s.notificationsDir, "batches", ref.Batch, localDate, ref.BatchRunID+".distributor.json"), nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) SaveBatchDistributorNotification(_ context.Context, ref BatchDistributorNotificationRef, artifact BatchDistributorNotificationArtifact) (string, error) {
|
||||
path, err := s.BatchDistributorNotificationPath(ref)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if artifact.SchemaVersion == "" {
|
||||
artifact.SchemaVersion = BatchDistributorNotificationSchemaVersion
|
||||
}
|
||||
artifact.Batch = ref.Batch
|
||||
artifact.BatchRunID = ref.BatchRunID
|
||||
if err := fileutil.WriteJSONAtomic(path, artifact); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) SaveGeneratedTextRaw(_ context.Context, resolved report.Resolved, data []byte) (string, error) {
|
||||
return s.saveResolvedBytes(resolved, func(paths ArtifactPaths) string {
|
||||
return paths.GeneratedTextRaw
|
||||
@@ -461,6 +488,35 @@ func validateRelativeDir(name string, value string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBatchNotificationRef(ref BatchDistributorNotificationRef) error {
|
||||
if err := validatePathSegment("batch kind", ref.Batch); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validatePathSegment("batch run id", ref.BatchRunID); err != nil {
|
||||
return err
|
||||
}
|
||||
if ref.StartedAt.IsZero() {
|
||||
return fmt.Errorf("batch started time is required")
|
||||
}
|
||||
if ref.Location == nil {
|
||||
return fmt.Errorf("batch location is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePathSegment(name string, value string) error {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("%s is required", name)
|
||||
}
|
||||
if strings.ContainsAny(value, `/\`) {
|
||||
return fmt.Errorf("%s must not contain path separators", name)
|
||||
}
|
||||
if value == "." || value == ".." {
|
||||
return fmt.Errorf("%s must be a safe path segment", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readJSON(path string, target any) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user