rerun_workflow.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package gitinstallation
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/commonutils"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/server/shared/requestutils"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/telemetry"
  15. )
  16. type RerunWorkflowHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. func NewRerunWorkflowHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *RerunWorkflowHandler {
  24. return &RerunWorkflowHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. func (c *RerunWorkflowHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. ctx, span := telemetry.NewSpan(r.Context(), "serve-rerun-github-workflow")
  30. defer span.End()
  31. owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
  32. if reqErr != nil {
  33. err := telemetry.Error(ctx, span, nil, "repo owner not found in request")
  34. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  35. return
  36. }
  37. name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
  38. if reqErr != nil {
  39. err := telemetry.Error(ctx, span, nil, "repo name not found in request")
  40. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  41. return
  42. }
  43. filename := r.URL.Query().Get("filename")
  44. // if branch is empty then the latest workflow run is rerun, meaning that if
  45. // there were multiple workflow runs for the same file but for different branches
  46. // only the very latest of the workflow runs will be rerun
  47. branch := r.URL.Query().Get("branch")
  48. releaseName := r.URL.Query().Get("release_name")
  49. telemetry.WithAttributes(
  50. span,
  51. telemetry.AttributeKV{Key: "repo-owner", Value: owner},
  52. telemetry.AttributeKV{Key: "repo-name", Value: name},
  53. telemetry.AttributeKV{Key: "branch", Value: branch},
  54. telemetry.AttributeKV{Key: "release-name", Value: releaseName},
  55. )
  56. if filename == "" && releaseName == "" {
  57. err := telemetry.Error(ctx, span, nil, "filename and release name are both empty")
  58. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  59. return
  60. }
  61. if filename == "" {
  62. if c.Config().ServerConf.InstanceName != "" {
  63. filename = fmt.Sprintf("porter_%s_%s.yml", strings.Replace(
  64. strings.ToLower(releaseName), "-", "_", -1),
  65. strings.ToLower(c.Config().ServerConf.InstanceName),
  66. )
  67. } else {
  68. filename = fmt.Sprintf("porter_%s.yml", strings.Replace(
  69. strings.ToLower(releaseName), "-", "_", -1),
  70. )
  71. }
  72. }
  73. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "filename", Value: filename})
  74. client, err := GetGithubAppClientFromRequest(c.Config(), r)
  75. if err != nil {
  76. err := telemetry.Error(ctx, span, err, "error getting github app client from request")
  77. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  78. return
  79. }
  80. latestWorkflowRun, err := commonutils.GetLatestWorkflowRun(client, owner, name, filename, branch)
  81. if err != nil && errors.Is(err, commonutils.ErrNoWorkflowRuns) {
  82. err = telemetry.Error(ctx, span, err, "no workflow runs found")
  83. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  84. return
  85. } else if err != nil && errors.Is(err, commonutils.ErrWorkflowNotFound) {
  86. err = telemetry.Error(ctx, span, err, "workflow not found")
  87. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusNotFound))
  88. return
  89. } else if err != nil {
  90. err = telemetry.Error(ctx, span, err, "error getting latest workflow run")
  91. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  92. return
  93. }
  94. if latestWorkflowRun.GetStatus() == "in_progress" || latestWorkflowRun.GetStatus() == "queued" {
  95. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "existing-pending-workflow-status", Value: latestWorkflowRun.GetStatus()})
  96. w.WriteHeader(http.StatusConflict)
  97. c.WriteResult(w, r, latestWorkflowRun.GetHTMLURL())
  98. return
  99. }
  100. _, err = client.Actions.RerunWorkflowByID(r.Context(), owner, name, latestWorkflowRun.GetID())
  101. if err != nil {
  102. err = telemetry.Error(ctx, span, err, "error rerunning workflow")
  103. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  104. return
  105. }
  106. latestWorkflowRun, err = commonutils.GetLatestWorkflowRun(client, owner, name, filename, branch)
  107. if err != nil {
  108. err = telemetry.Error(ctx, span, err, "error getting latest workflow run after rerunning workflow")
  109. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  110. return
  111. }
  112. newWorkflowRunUrl := latestWorkflowRun.GetHTMLURL()
  113. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "new-workflow-run-url", Value: newWorkflowRunUrl})
  114. c.WriteResult(w, r, newWorkflowRunUrl)
  115. }