app_events.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package v2
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "time"
  9. api "github.com/porter-dev/porter/api/client"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/telemetry"
  12. )
  13. func createBuildEvent(ctx context.Context, client api.Client, applicationName string, projectId uint, clusterId uint, deploymentTargetID string) (string, error) {
  14. ctx, span := telemetry.NewSpan(ctx, "create-build-event")
  15. defer span.End()
  16. req := &types.CreateOrUpdatePorterAppEventRequest{
  17. Status: types.PorterAppEventStatus_Progressing,
  18. Type: types.PorterAppEventType_Build,
  19. TypeExternalSource: "GITHUB",
  20. Metadata: make(map[string]interface{}),
  21. DeploymentTargetID: deploymentTargetID,
  22. }
  23. actionRunID := os.Getenv("GITHUB_RUN_ID")
  24. if actionRunID != "" {
  25. arid, err := strconv.Atoi(actionRunID)
  26. if err != nil {
  27. fmt.Println("could not parse action run id")
  28. return "", telemetry.Error(ctx, span, err, "could not parse action run id")
  29. }
  30. req.Metadata["action_run_id"] = arid
  31. repoName := os.Getenv("GITHUB_REPOSITORY")
  32. parsedRepoName := strings.Split(repoName, "/")
  33. if len(parsedRepoName) != 2 {
  34. fmt.Println("repo name is not in the format owner/name")
  35. return "", telemetry.Error(ctx, span, nil, "repo name is not in the format owner/name")
  36. }
  37. req.Metadata["repo"] = parsedRepoName[1]
  38. repoOwnerAccountID := os.Getenv("GITHUB_REPOSITORY_OWNER_ID")
  39. if repoOwnerAccountID != "" {
  40. arid, err := strconv.Atoi(repoOwnerAccountID)
  41. if err != nil {
  42. fmt.Println("could not parse repo owner account id")
  43. return "", telemetry.Error(ctx, span, err, "could not parse repo owner account id")
  44. }
  45. req.Metadata["github_account_id"] = arid
  46. }
  47. }
  48. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  49. if err != nil {
  50. fmt.Println("could not create build event")
  51. return "", telemetry.Error(ctx, span, err, "could not create build event")
  52. }
  53. return event.ID, nil
  54. }
  55. func createPredeployEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, createdAt time.Time, appRevisionID string) (string, error) {
  56. ctx, span := telemetry.NewSpan(ctx, "create-predeploy-event")
  57. defer span.End()
  58. req := &types.CreateOrUpdatePorterAppEventRequest{
  59. Status: types.PorterAppEventStatus_Progressing,
  60. Type: types.PorterAppEventType_PreDeploy,
  61. Metadata: make(map[string]interface{}),
  62. DeploymentTargetID: deploymentTargetID,
  63. }
  64. req.Metadata["start_time"] = createdAt
  65. req.Metadata["app_revision_id"] = appRevisionID
  66. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  67. if err != nil {
  68. return "", telemetry.Error(ctx, span, err, "could not create predeploy event")
  69. }
  70. return event.ID, nil
  71. }
  72. func updateExistingEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, eventType types.PorterAppEventType, eventID string, status types.PorterAppEventStatus, metadata map[string]interface{}) error {
  73. ctx, span := telemetry.NewSpan(ctx, "update-existing-event")
  74. defer span.End()
  75. req := &types.CreateOrUpdatePorterAppEventRequest{
  76. ID: eventID,
  77. Status: status,
  78. Metadata: metadata,
  79. DeploymentTargetID: deploymentTargetID,
  80. Type: eventType,
  81. }
  82. _, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  83. if err != nil {
  84. return telemetry.Error(ctx, span, err, "could not update existing app event")
  85. }
  86. return nil
  87. }