revisions.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // ID is the revision id
  13. ID string `json:"id"`
  14. // B64AppProto is the base64 encoded app proto definition
  15. B64AppProto string `json:"b64_app_proto"`
  16. // Status is the status of the revision
  17. Status string `json:"status"`
  18. // RevisionNumber is the revision number with respect to the app and deployment target
  19. RevisionNumber uint64 `json:"revision_number"`
  20. // CreatedAt is the time the revision was created
  21. CreatedAt time.Time `json:"created_at"`
  22. // UpdatedAt is the time the revision was updated
  23. UpdatedAt time.Time `json:"updated_at"`
  24. }
  25. // EncodedRevisionFromProto converts an AppRevision proto object into a Revision object
  26. func EncodedRevisionFromProto(ctx context.Context, appRevision *porterv1.AppRevision) (Revision, error) {
  27. ctx, span := telemetry.NewSpan(ctx, "encoded-revision-from-proto")
  28. defer span.End()
  29. var revision Revision
  30. if appRevision == nil {
  31. return revision, telemetry.Error(ctx, span, nil, "current app revision definition is nil")
  32. }
  33. appProto := appRevision.App
  34. if appProto == nil {
  35. return revision, telemetry.Error(ctx, span, nil, "app proto is nil")
  36. }
  37. encoded, err := helpers.MarshalContractObject(ctx, appProto)
  38. if err != nil {
  39. return revision, telemetry.Error(ctx, span, err, "error marshalling app proto back to json")
  40. }
  41. b64 := base64.StdEncoding.EncodeToString(encoded)
  42. revision = Revision{
  43. B64AppProto: b64,
  44. Status: appRevision.Status,
  45. ID: appRevision.Id,
  46. RevisionNumber: appRevision.RevisionNumber,
  47. CreatedAt: appRevision.CreatedAt.AsTime(),
  48. UpdatedAt: appRevision.UpdatedAt.AsTime(),
  49. }
  50. return revision, nil
  51. }