revisions.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package porter_app
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "time"
  6. "github.com/porter-dev/api-contracts/generated/go/helpers"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. )
  10. // Revision represents the data for a single revision
  11. type Revision struct {
  12. // B64AppProto is the base64 encoded app proto definition
  13. B64AppProto string `json:"b64_app_proto"`
  14. // Status is the status of the revision
  15. Status string `json:"status"`
  16. // RevisionNumber is the revision number with respect to the app and deployment target
  17. RevisionNumber uint64 `json:"revision_number"`
  18. // CreatedAt is the time the revision was created
  19. CreatedAt time.Time `json:"created_at"`
  20. // UpdatedAt is the time the revision was updated
  21. UpdatedAt time.Time `json:"updated_at"`
  22. }
  23. // EncodedRevisionFromProto converts an AppRevision proto object into a Revision object
  24. func EncodedRevisionFromProto(ctx context.Context, appRevision *porterv1.AppRevision) (Revision, error) {
  25. ctx, span := telemetry.NewSpan(ctx, "encoded-revision-from-proto")
  26. defer span.End()
  27. var revision Revision
  28. if appRevision == nil {
  29. return revision, telemetry.Error(ctx, span, nil, "current app revision definition is nil")
  30. }
  31. appProto := appRevision.App
  32. if appProto == nil {
  33. return revision, telemetry.Error(ctx, span, nil, "app proto is nil")
  34. }
  35. encoded, err := helpers.MarshalContractObject(ctx, appProto)
  36. if err != nil {
  37. return revision, telemetry.Error(ctx, span, err, "error marshalling app proto back to json")
  38. }
  39. b64 := base64.StdEncoding.EncodeToString(encoded)
  40. revision = Revision{
  41. B64AppProto: b64,
  42. Status: appRevision.Status,
  43. RevisionNumber: appRevision.RevisionNumber,
  44. CreatedAt: appRevision.CreatedAt.AsTime(),
  45. UpdatedAt: appRevision.UpdatedAt.AsTime(),
  46. }
  47. return revision, nil
  48. }