Share typed publication completion evidence
This commit is contained in:
125
internal/artifacts/publication_metadata.go
Normal file
125
internal/artifacts/publication_metadata.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package artifacts
|
||||
|
||||
// PublicationOutcomeStatus is the publication decision for one source.
|
||||
type PublicationOutcomeStatus string
|
||||
|
||||
const (
|
||||
PublicationPublished PublicationOutcomeStatus = "published"
|
||||
PublicationCarriedLocked PublicationOutcomeStatus = "carried_locked"
|
||||
PublicationLockedUnpublished PublicationOutcomeStatus = "locked_unpublished"
|
||||
PublicationOptionalMissing PublicationOutcomeStatus = "optional_missing"
|
||||
PublicationUnselected PublicationOutcomeStatus = "unselected"
|
||||
)
|
||||
|
||||
// PublicationOutcome describes a source's published or skipped outcome.
|
||||
type PublicationOutcome struct {
|
||||
Source string
|
||||
Status PublicationOutcomeStatus
|
||||
Destination string
|
||||
DeclaredKey string
|
||||
OriginRun string
|
||||
}
|
||||
|
||||
// PublicationBoolField preserves absence and invalid types independently.
|
||||
type PublicationBoolField struct {
|
||||
Value bool
|
||||
Present, Valid bool
|
||||
}
|
||||
|
||||
// PublicationStringField preserves absence and invalid types independently.
|
||||
type PublicationStringField struct {
|
||||
Value string
|
||||
Present, Valid bool
|
||||
}
|
||||
|
||||
// PublicationCompletion contains the fields used to authorize cleanup.
|
||||
type PublicationCompletion struct {
|
||||
Uploaded, Skipped PublicationBoolField
|
||||
RunID, CommitKey, PointerKey PublicationStringField
|
||||
}
|
||||
|
||||
const (
|
||||
PublicationUploadedKey = "uploaded"
|
||||
PublicationSkippedKey = "skipped"
|
||||
PublicationRunIDKey = "published_run_id"
|
||||
PublicationCommitKey = "remote_commit_key"
|
||||
PublicationPointerKey = "current_commit_pointer_key"
|
||||
PublicationOutcomesKey = "publish_output_outcomes"
|
||||
)
|
||||
|
||||
// DecodePublicationCompletion reads individual fields without deciding policy.
|
||||
func DecodePublicationCompletion(metadata map[string]any) PublicationCompletion {
|
||||
readBool := func(key string) PublicationBoolField {
|
||||
raw, present := metadata[key]
|
||||
value, valid := raw.(bool)
|
||||
return PublicationBoolField{Value: value, Present: present, Valid: valid}
|
||||
}
|
||||
readString := func(key string) PublicationStringField {
|
||||
raw, present := metadata[key]
|
||||
value, valid := raw.(string)
|
||||
return PublicationStringField{Value: value, Present: present, Valid: valid}
|
||||
}
|
||||
return PublicationCompletion{Uploaded: readBool(PublicationUploadedKey), Skipped: readBool(PublicationSkippedKey), RunID: readString(PublicationRunIDKey), CommitKey: readString(PublicationCommitKey), PointerKey: readString(PublicationPointerKey)}
|
||||
}
|
||||
|
||||
// PublicationIdentity identifies a successfully committed publication.
|
||||
type PublicationIdentity struct{ RunID, CommitKey, PointerKey string }
|
||||
|
||||
// PublicationCompletionMetadata encodes successful publication control evidence.
|
||||
func PublicationCompletionMetadata(identity PublicationIdentity) map[string]any {
|
||||
return map[string]any{PublicationUploadedKey: true, PublicationRunIDKey: identity.RunID, PublicationCommitKey: identity.CommitKey, PublicationPointerKey: identity.PointerKey}
|
||||
}
|
||||
|
||||
// PublicationOutcomeMetadata preserves the existing per-source map shape.
|
||||
func PublicationOutcomeMetadata(outcome PublicationOutcome) map[string]any {
|
||||
item := map[string]any{"source": outcome.Source, "status": string(outcome.Status)}
|
||||
if outcome.Destination != "" {
|
||||
item["destination"] = outcome.Destination
|
||||
}
|
||||
if outcome.DeclaredKey != "" {
|
||||
item["declared_key"] = outcome.DeclaredKey
|
||||
}
|
||||
if outcome.OriginRun != "" {
|
||||
item["origin_run"] = outcome.OriginRun
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
// DecodePublicationOutcomes skips malformed rows while retaining valid rows in order.
|
||||
func DecodePublicationOutcomes(metadata map[string]any) []PublicationOutcome {
|
||||
var result []PublicationOutcome
|
||||
appendRow := func(item map[string]any) {
|
||||
source, sourceOK := item["source"].(string)
|
||||
status, statusOK := item["status"].(string)
|
||||
if !sourceOK || !statusOK || !validPublicationOutcome(PublicationOutcomeStatus(status)) {
|
||||
return
|
||||
}
|
||||
outcome := PublicationOutcome{Source: source, Status: PublicationOutcomeStatus(status)}
|
||||
outcome.Destination, _ = item["destination"].(string)
|
||||
outcome.DeclaredKey, _ = item["declared_key"].(string)
|
||||
outcome.OriginRun, _ = item["origin_run"].(string)
|
||||
result = append(result, outcome)
|
||||
}
|
||||
switch rows := metadata[PublicationOutcomesKey].(type) {
|
||||
case []map[string]any:
|
||||
for _, row := range rows {
|
||||
appendRow(row)
|
||||
}
|
||||
case []any:
|
||||
for _, raw := range rows {
|
||||
if row, ok := raw.(map[string]any); ok {
|
||||
appendRow(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func validPublicationOutcome(status PublicationOutcomeStatus) bool {
|
||||
switch status {
|
||||
case PublicationPublished, PublicationCarriedLocked, PublicationLockedUnpublished, PublicationOptionalMissing, PublicationUnselected:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
39
internal/artifacts/publication_metadata_test.go
Normal file
39
internal/artifacts/publication_metadata_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package artifacts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublicationMetadataRoundTrip(t *testing.T) {
|
||||
metadata := PublicationCompletionMetadata(PublicationIdentity{RunID: "run", CommitKey: "commit", PointerKey: "pointer"})
|
||||
metadata[PublicationOutcomesKey] = []map[string]any{
|
||||
PublicationOutcomeMetadata(PublicationOutcome{Source: "one", Status: PublicationPublished, Destination: "a", DeclaredKey: "key", OriginRun: "run"}),
|
||||
{"source": "bad", "status": "invalid"},
|
||||
PublicationOutcomeMetadata(PublicationOutcome{Source: "two", Status: PublicationOptionalMissing}),
|
||||
}
|
||||
before := DecodePublicationCompletion(metadata)
|
||||
outcomes := DecodePublicationOutcomes(metadata)
|
||||
if !before.Uploaded.Valid || !before.Uploaded.Value || before.Skipped.Present || !before.RunID.Valid || len(outcomes) != 2 {
|
||||
t.Fatalf("completion = %+v, outcomes = %+v", before, outcomes)
|
||||
}
|
||||
body, err := json.Marshal(metadata)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal(body, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := DecodePublicationCompletion(decoded); !reflect.DeepEqual(got, before) {
|
||||
t.Fatalf("completion = %+v, want %+v", got, before)
|
||||
}
|
||||
if got := DecodePublicationOutcomes(decoded); !reflect.DeepEqual(got, outcomes) {
|
||||
t.Fatalf("outcomes = %+v, want %+v", got, outcomes)
|
||||
}
|
||||
decoded[PublicationSkippedKey] = "false"
|
||||
if field := DecodePublicationCompletion(decoded).Skipped; !field.Present || field.Valid {
|
||||
t.Fatalf("malformed skipped = %+v", field)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user