2
0

app_events.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, clusterId uint) (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. }
  22. actionRunID := os.Getenv("GITHUB_RUN_ID")
  23. if actionRunID != "" {
  24. arid, err := strconv.Atoi(actionRunID)
  25. if err != nil {
  26. fmt.Println("could not parse action run id")
  27. return "", telemetry.Error(ctx, span, err, "could not parse action run id")
  28. }
  29. req.Metadata["action_run_id"] = arid
  30. repoName := os.Getenv("GITHUB_REPOSITORY")
  31. parsedRepoName := strings.Split(repoName, "/")
  32. if len(parsedRepoName) != 2 {
  33. fmt.Println("repo name is not in the format owner/name")
  34. return "", telemetry.Error(ctx, span, nil, "repo name is not in the format owner/name")
  35. }
  36. req.Metadata["repo"] = parsedRepoName[1]
  37. repoOwnerAccountID := os.Getenv("GITHUB_REPOSITORY_OWNER_ID")
  38. if repoOwnerAccountID != "" {
  39. arid, err := strconv.Atoi(repoOwnerAccountID)
  40. if err != nil {
  41. fmt.Println("could not parse repo owner account id")
  42. return "", telemetry.Error(ctx, span, err, "could not parse repo owner account id")
  43. }
  44. req.Metadata["github_account_id"] = arid
  45. }
  46. }
  47. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  48. if err != nil {
  49. fmt.Println("could not create build event")
  50. return "", telemetry.Error(ctx, span, err, "could not create build event")
  51. }
  52. return event.ID, nil
  53. }
  54. func createPredeployEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, createdAt time.Time) (string, error) {
  55. ctx, span := telemetry.NewSpan(ctx, "create-predeploy-event")
  56. defer span.End()
  57. req := &types.CreateOrUpdatePorterAppEventRequest{
  58. Status: types.PorterAppEventStatus_Progressing,
  59. Type: types.PorterAppEventType_PreDeploy,
  60. Metadata: make(map[string]interface{}),
  61. }
  62. req.Metadata["start_time"] = createdAt
  63. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  64. if err != nil {
  65. return "", telemetry.Error(ctx, span, err, "could not create predeploy event")
  66. }
  67. return event.ID, nil
  68. }
  69. func updateExistingEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, eventID string, status types.PorterAppEventStatus, metadata map[string]interface{}) error {
  70. ctx, span := telemetry.NewSpan(ctx, "update-existing-event")
  71. defer span.End()
  72. req := &types.CreateOrUpdatePorterAppEventRequest{
  73. ID: eventID,
  74. Status: status,
  75. Metadata: metadata,
  76. }
  77. _, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  78. if err != nil {
  79. return telemetry.Error(ctx, span, err, "could not update existing app event")
  80. }
  81. return nil
  82. }