2
0

app_events.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, commitSHA 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. req.Metadata["commit_sha"] = commitSHA
  49. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  50. if err != nil {
  51. fmt.Println("could not create build event")
  52. return "", telemetry.Error(ctx, span, err, "could not create build event")
  53. }
  54. return event.ID, nil
  55. }
  56. func createPredeployEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, createdAt time.Time, appRevisionID string, commitSHA string) (string, error) {
  57. ctx, span := telemetry.NewSpan(ctx, "create-predeploy-event")
  58. defer span.End()
  59. req := &types.CreateOrUpdatePorterAppEventRequest{
  60. Status: types.PorterAppEventStatus_Progressing,
  61. Type: types.PorterAppEventType_PreDeploy,
  62. Metadata: make(map[string]interface{}),
  63. DeploymentTargetID: deploymentTargetID,
  64. }
  65. req.Metadata["start_time"] = createdAt
  66. req.Metadata["app_revision_id"] = appRevisionID
  67. req.Metadata["commit_sha"] = commitSHA
  68. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  69. if err != nil {
  70. return "", telemetry.Error(ctx, span, err, "could not create predeploy event")
  71. }
  72. return event.ID, nil
  73. }
  74. 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 {
  75. ctx, span := telemetry.NewSpan(ctx, "update-existing-event")
  76. defer span.End()
  77. req := &types.CreateOrUpdatePorterAppEventRequest{
  78. ID: eventID,
  79. Status: status,
  80. Metadata: metadata,
  81. DeploymentTargetID: deploymentTargetID,
  82. Type: eventType,
  83. }
  84. _, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  85. if err != nil {
  86. return telemetry.Error(ctx, span, err, "could not update existing app event")
  87. }
  88. return nil
  89. }