2
0

app_events.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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) (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. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  66. if err != nil {
  67. return "", telemetry.Error(ctx, span, err, "could not create predeploy event")
  68. }
  69. return event.ID, nil
  70. }
  71. func updateExistingEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, eventID string, status types.PorterAppEventStatus, metadata map[string]interface{}) error {
  72. ctx, span := telemetry.NewSpan(ctx, "update-existing-event")
  73. defer span.End()
  74. req := &types.CreateOrUpdatePorterAppEventRequest{
  75. ID: eventID,
  76. Status: status,
  77. Metadata: metadata,
  78. DeploymentTargetID: deploymentTargetID,
  79. }
  80. _, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  81. if err != nil {
  82. return telemetry.Error(ctx, span, err, "could not update existing app event")
  83. }
  84. return nil
  85. }