Implemented the remaining trim artifact cleanup
All checks were successful
ci/woodpecker/tag/release Pipeline was successful
All checks were successful
ci/woodpecker/tag/release Pipeline was successful
This commit is contained in:
@@ -73,20 +73,23 @@ func ParseArtifactJSON(data []byte) (Artifact, error) {
|
||||
func ValidateArtifact(artifact Artifact) error {
|
||||
switch artifact.Schema {
|
||||
case SchemaFull:
|
||||
if artifact.Full == nil {
|
||||
return fmt.Errorf("full artifact payload is missing")
|
||||
payload, err := artifact.fullPayload()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return schema.ValidateTranscript(*artifact.Full)
|
||||
return schema.ValidateTranscript(*payload)
|
||||
case SchemaIntermediate:
|
||||
if artifact.Intermediate == nil {
|
||||
return fmt.Errorf("intermediate artifact payload is missing")
|
||||
payload, err := artifact.intermediatePayload()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return schema.ValidateIntermediateTranscript(*artifact.Intermediate)
|
||||
return schema.ValidateIntermediateTranscript(*payload)
|
||||
case SchemaMinimal:
|
||||
if artifact.Minimal == nil {
|
||||
return fmt.Errorf("minimal artifact payload is missing")
|
||||
payload, err := artifact.minimalPayload()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return schema.ValidateMinimalTranscript(*artifact.Minimal)
|
||||
return schema.ValidateMinimalTranscript(*payload)
|
||||
default:
|
||||
return fmt.Errorf("unsupported artifact schema %q", artifact.Schema)
|
||||
}
|
||||
@@ -188,10 +191,11 @@ func (artifact Artifact) Version() string {
|
||||
func ApplyArtifact(input Artifact, opts Options) (ApplyArtifactResult, error) {
|
||||
switch input.Schema {
|
||||
case SchemaFull:
|
||||
if input.Full == nil {
|
||||
return ApplyArtifactResult{}, fmt.Errorf("full artifact payload is missing")
|
||||
payload, err := input.fullPayload()
|
||||
if err != nil {
|
||||
return ApplyArtifactResult{}, err
|
||||
}
|
||||
result, err := Apply(*input.Full, opts)
|
||||
result, err := Apply(*payload, opts)
|
||||
if err != nil {
|
||||
return ApplyArtifactResult{}, err
|
||||
}
|
||||
@@ -206,10 +210,11 @@ func ApplyArtifact(input Artifact, opts Options) (ApplyArtifactResult, error) {
|
||||
OverlapGroupsRecomputed: true,
|
||||
}, nil
|
||||
case SchemaIntermediate:
|
||||
if input.Intermediate == nil {
|
||||
return ApplyArtifactResult{}, fmt.Errorf("intermediate artifact payload is missing")
|
||||
payload, err := input.intermediatePayload()
|
||||
if err != nil {
|
||||
return ApplyArtifactResult{}, err
|
||||
}
|
||||
result, err := ApplyIntermediate(*input.Intermediate, opts)
|
||||
result, err := ApplyIntermediate(*payload, opts)
|
||||
if err != nil {
|
||||
return ApplyArtifactResult{}, err
|
||||
}
|
||||
@@ -224,10 +229,11 @@ func ApplyArtifact(input Artifact, opts Options) (ApplyArtifactResult, error) {
|
||||
OverlapGroupsRecomputed: false,
|
||||
}, nil
|
||||
case SchemaMinimal:
|
||||
if input.Minimal == nil {
|
||||
return ApplyArtifactResult{}, fmt.Errorf("minimal artifact payload is missing")
|
||||
payload, err := input.minimalPayload()
|
||||
if err != nil {
|
||||
return ApplyArtifactResult{}, err
|
||||
}
|
||||
result, err := ApplyMinimal(*input.Minimal, opts)
|
||||
result, err := ApplyMinimal(*payload, opts)
|
||||
if err != nil {
|
||||
return ApplyArtifactResult{}, err
|
||||
}
|
||||
@@ -254,18 +260,19 @@ func ConvertArtifact(input Artifact, outputSchema string) (Artifact, error) {
|
||||
|
||||
switch input.Schema {
|
||||
case SchemaFull:
|
||||
if input.Full == nil {
|
||||
return Artifact{}, fmt.Errorf("full artifact payload is missing")
|
||||
payload, err := input.fullPayload()
|
||||
if err != nil {
|
||||
return Artifact{}, err
|
||||
}
|
||||
switch outputSchema {
|
||||
case SchemaIntermediate:
|
||||
out := intermediateFromFull(*input.Full)
|
||||
out := intermediateFromFull(*payload)
|
||||
return Artifact{
|
||||
Schema: SchemaIntermediate,
|
||||
Intermediate: &out,
|
||||
}, nil
|
||||
case SchemaMinimal:
|
||||
out := minimalFromFull(*input.Full)
|
||||
out := minimalFromFull(*payload)
|
||||
return Artifact{
|
||||
Schema: SchemaMinimal,
|
||||
Minimal: &out,
|
||||
@@ -274,12 +281,13 @@ func ConvertArtifact(input Artifact, outputSchema string) (Artifact, error) {
|
||||
return Artifact{}, fmt.Errorf("unsupported output schema %q", outputSchema)
|
||||
}
|
||||
case SchemaIntermediate:
|
||||
if input.Intermediate == nil {
|
||||
return Artifact{}, fmt.Errorf("intermediate artifact payload is missing")
|
||||
payload, err := input.intermediatePayload()
|
||||
if err != nil {
|
||||
return Artifact{}, err
|
||||
}
|
||||
switch outputSchema {
|
||||
case SchemaMinimal:
|
||||
out := minimalFromIntermediate(*input.Intermediate)
|
||||
out := minimalFromIntermediate(*payload)
|
||||
return Artifact{
|
||||
Schema: SchemaMinimal,
|
||||
Minimal: &out,
|
||||
@@ -290,12 +298,13 @@ func ConvertArtifact(input Artifact, outputSchema string) (Artifact, error) {
|
||||
return Artifact{}, fmt.Errorf("unsupported output schema %q", outputSchema)
|
||||
}
|
||||
case SchemaMinimal:
|
||||
if input.Minimal == nil {
|
||||
return Artifact{}, fmt.Errorf("minimal artifact payload is missing")
|
||||
payload, err := input.minimalPayload()
|
||||
if err != nil {
|
||||
return Artifact{}, err
|
||||
}
|
||||
switch outputSchema {
|
||||
case SchemaIntermediate:
|
||||
out := intermediateFromMinimal(*input.Minimal)
|
||||
out := intermediateFromMinimal(*payload)
|
||||
return Artifact{
|
||||
Schema: SchemaIntermediate,
|
||||
Intermediate: &out,
|
||||
@@ -310,6 +319,27 @@ func ConvertArtifact(input Artifact, outputSchema string) (Artifact, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (artifact Artifact) fullPayload() (*schema.Transcript, error) {
|
||||
if artifact.Full == nil {
|
||||
return nil, fmt.Errorf("full artifact payload is missing")
|
||||
}
|
||||
return artifact.Full, nil
|
||||
}
|
||||
|
||||
func (artifact Artifact) intermediatePayload() (*schema.IntermediateTranscript, error) {
|
||||
if artifact.Intermediate == nil {
|
||||
return nil, fmt.Errorf("intermediate artifact payload is missing")
|
||||
}
|
||||
return artifact.Intermediate, nil
|
||||
}
|
||||
|
||||
func (artifact Artifact) minimalPayload() (*schema.MinimalTranscript, error) {
|
||||
if artifact.Minimal == nil {
|
||||
return nil, fmt.Errorf("minimal artifact payload is missing")
|
||||
}
|
||||
return artifact.Minimal, nil
|
||||
}
|
||||
|
||||
func intermediateFromFull(input schema.Transcript) schema.IntermediateTranscript {
|
||||
segments := make([]schema.IntermediateSegment, len(input.Segments))
|
||||
for index, segment := range input.Segments {
|
||||
|
||||
@@ -128,6 +128,61 @@ func TestConvertArtifactMinimalToFullFails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateArtifactRejectsMissingPayloads(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
artifact Artifact
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "full",
|
||||
artifact: Artifact{Schema: SchemaFull},
|
||||
want: "full artifact payload is missing",
|
||||
},
|
||||
{
|
||||
name: "intermediate",
|
||||
artifact: Artifact{Schema: SchemaIntermediate},
|
||||
want: "intermediate artifact payload is missing",
|
||||
},
|
||||
{
|
||||
name: "minimal",
|
||||
artifact: Artifact{Schema: SchemaMinimal},
|
||||
want: "minimal artifact payload is missing",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := ValidateArtifact(test.artifact)
|
||||
assertErrorContains(t, err, test.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyArtifactRejectsMissingPayload(t *testing.T) {
|
||||
_, err := ApplyArtifact(Artifact{Schema: SchemaFull}, Options{})
|
||||
assertErrorContains(t, err, "full artifact payload is missing")
|
||||
}
|
||||
|
||||
func TestConvertArtifactRejectsMissingPayloadWhenConversionRequested(t *testing.T) {
|
||||
_, err := ConvertArtifact(Artifact{Schema: SchemaFull}, SchemaMinimal)
|
||||
assertErrorContains(t, err, "full artifact payload is missing")
|
||||
}
|
||||
|
||||
func TestConvertArtifactSameSchemaDoesNotRequirePayload(t *testing.T) {
|
||||
artifact := Artifact{Schema: SchemaFull}
|
||||
converted, err := ConvertArtifact(artifact, SchemaFull)
|
||||
if err != nil {
|
||||
t.Fatalf("convert failed: %v", err)
|
||||
}
|
||||
if converted.Schema != SchemaFull {
|
||||
t.Fatalf("schema = %q, want %q", converted.Schema, SchemaFull)
|
||||
}
|
||||
if converted.Full != nil {
|
||||
t.Fatalf("full payload = %#v, want nil", converted.Full)
|
||||
}
|
||||
}
|
||||
|
||||
func mustMarshalJSON(t *testing.T, value any) []byte {
|
||||
t.Helper()
|
||||
data, err := json.Marshal(value)
|
||||
|
||||
Reference in New Issue
Block a user