Skip to content

Commit

Permalink
Merge pull request #160 from massdriver-cloud/fix-artifact-publish
Browse files Browse the repository at this point in the history
Fix artifact publish
  • Loading branch information
chrisghill authored Jul 19, 2024
2 parents 812ef7f + 6b81c06 commit 1e81002
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
10 changes: 8 additions & 2 deletions cmd/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io"
"os"
"xo/src/artifact"
"xo/src/bundle"
Expand Down Expand Up @@ -104,6 +105,11 @@ func runArtifactPublish(cmd *cobra.Command, args []string) error {
}
defer artFile.Close()
}
artifactBytes, err := io.ReadAll(artFile)
if err != nil {
log.Error().Err(err).Msg("unable to open artifact file")
return err
}

schemasFile, err := os.Open(schemasPath)
if err != nil {
Expand All @@ -112,7 +118,7 @@ func runArtifactPublish(cmd *cobra.Command, args []string) error {
}

log.Info().Msg("Validating artifact " + field + "...")
valid, err := artifact.Validate(field, artFile, schemasFile)
valid, err := artifact.Validate(field, artifactBytes, schemasFile)
if !valid || err != nil {
log.Error().Err(err).Msg("artifact is invalid")
return err
Expand All @@ -134,7 +140,7 @@ func runArtifactPublish(cmd *cobra.Command, args []string) error {
return err
}

err = artifact.Publish(ctx, mdClient, artFile, &bun, field, artName)
err = artifact.Publish(ctx, mdClient, artifactBytes, &bun, field, artName)
if err != nil {
log.Error().Err(err).Msg("an error occurred while publishing artifact")
return err
Expand Down
14 changes: 3 additions & 11 deletions src/artifact/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"xo/src/bundle"
"xo/src/massdriver"
"xo/src/telemetry"
Expand All @@ -14,7 +13,7 @@ import (
"go.opentelemetry.io/otel/codes"
)

func Publish(ctx context.Context, c *massdriver.MassdriverClient, artifact io.Reader, bun *bundle.Bundle, field, name string) error {
func Publish(ctx context.Context, c *massdriver.MassdriverClient, artifact []byte, bun *bundle.Bundle, field, name string) error {
_, span := otel.Tracer("xo").Start(ctx, "ArtifactPublish")
telemetry.SetSpanAttributes(span)
defer span.End()
Expand All @@ -38,16 +37,9 @@ func Publish(ctx context.Context, c *massdriver.MassdriverClient, artifact io.Re
// this here is a bit clunky. We're nesting the metadata object WITHIN the artifact. However, the schemas don't expect
// the metadata block. So after validation we need to unmarshal the artifact to a map so we can add the metadata in
var unmarshaledArtifact map[string]interface{}

artifactBytes, err := io.ReadAll(artifact)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return err
}

err = json.Unmarshal(artifactBytes, &unmarshaledArtifact)
err = json.Unmarshal(artifact, &unmarshaledArtifact)
if err != nil {
fmt.Println(string(artifact))
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return err
Expand Down
8 changes: 3 additions & 5 deletions src/artifact/publish_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package artifact_test

import (
"bytes"
"context"
"testing"
"xo/src/artifact"
Expand All @@ -18,7 +17,7 @@ func TestPublish(t *testing.T) {
bun bundle.Bundle
field string
artifactName string
artifact string
artifact []byte
want string
}
tests := []testData{
Expand All @@ -28,7 +27,7 @@ func TestPublish(t *testing.T) {
bun: bundle.Bundle{Artifacts: map[string]interface{}{"properties": map[string]interface{}{"foobar": map[string]interface{}{"$ref": "massdriver/artifact-type"}}}},
field: "foobar",
artifactName: "artName",
artifact: `{"foo":"bar"}`,
artifact: []byte(`{"foo":"bar"}`),
want: `{"metadata":{"timestamp":"2021-01-01 12:00:00.1234","provisioner":"testaform","event_type":"artifact_updated"},"payload":{"deployment_id":"depId","artifact":{"foo":"bar","metadata":{"field":"foobar","provider_resource_id":"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2","type":"massdriver/artifact-type","name":"artName"}}}}`,
},
}
Expand All @@ -39,8 +38,7 @@ func TestPublish(t *testing.T) {
massdriver.EventTimeString = func() string { return "2021-01-01 12:00:00.1234" }
testClient := testmass.NewMassdriverTestClient(tc.deploymentId)

input := bytes.NewBuffer([]byte(tc.artifact))
err := artifact.Publish(context.TODO(), &testClient.MassClient, input, &tc.bun, tc.field, tc.artifactName)
err := artifact.Publish(context.TODO(), &testClient.MassClient, tc.artifact, &tc.bun, tc.field, tc.artifactName)
if err != nil {
t.Fatalf("%d, unexpected error", err)
}
Expand Down
8 changes: 2 additions & 6 deletions src/artifact/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ type artifactSchema struct {
Properties map[string]interface{} `json:"properties"`
}

func Validate(field string, artifactIn, schemasIn io.Reader) (bool, error) {
func Validate(field string, artifact []byte, schemasIn io.Reader) (bool, error) {

artifactBytes, err := io.ReadAll(artifactIn)
if err != nil {
return false, err
}
schemaBytes, err := io.ReadAll(schemasIn)
if err != nil {
return false, err
Expand All @@ -35,7 +31,7 @@ func Validate(field string, artifactIn, schemasIn io.Reader) (bool, error) {
}

sl := gojsonschema.NewGoLoader(specificSchema.(map[string]interface{}))
dl := gojsonschema.NewBytesLoader(artifactBytes)
dl := gojsonschema.NewBytesLoader(artifact)

return jsonschema.Validate(sl, dl)
}
8 changes: 3 additions & 5 deletions src/artifact/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func TestValidate(t *testing.T) {
name string
field string
schemaPath string
artifact string
artifact []byte
want bool
}
tests := []testData{
{
name: "pass",
field: "one",
schemaPath: "testdata/schema-artifacts.json",
artifact: `{"data":{"foo":{"bar":"baz"}},"specs":{"hello":"world"}}`,
artifact: []byte(`{"data":{"foo":{"bar":"baz"}},"specs":{"hello":"world"}}`),
want: true,
},
}
Expand All @@ -33,9 +33,7 @@ func TestValidate(t *testing.T) {
}
schemasBuffer := bytes.NewBuffer(schemasBytes)

artifactBuffer := bytes.NewBufferString(tc.artifact)

got, err := artifact.Validate(tc.field, artifactBuffer, schemasBuffer)
got, err := artifact.Validate(tc.field, tc.artifact, schemasBuffer)
if err != nil {
t.Fatalf("%d, unexpected error", err)
}
Expand Down

0 comments on commit 1e81002

Please sign in to comment.