create_gitlab.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package project_integration
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. ints "github.com/porter-dev/porter/internal/models/integrations"
  13. )
  14. type CreateGitlabIntegration struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. func NewCreateGitlabIntegration(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *CreateGitlabIntegration {
  22. return &CreateGitlabIntegration{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (p *CreateGitlabIntegration) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  28. request := &types.CreateGitlabRequest{}
  29. if ok := p.DecodeAndValidate(w, r, request); !ok {
  30. return
  31. }
  32. _, err := url.Parse(request.InstanceURL)
  33. if err != nil {
  34. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  35. fmt.Errorf("malformed gitlab instance URL"), http.StatusBadRequest,
  36. ))
  37. return
  38. }
  39. gitlabIntegration, err := p.Repo().GitlabIntegration().CreateGitlabIntegration(&ints.GitlabIntegration{
  40. ProjectID: project.ID,
  41. InstanceURL: request.InstanceURL,
  42. AppClientID: []byte(request.AppClientID),
  43. AppClientSecret: []byte(request.AppClientSecret),
  44. })
  45. if err != nil {
  46. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  47. return
  48. }
  49. res := types.CreateGitlabResponse{
  50. GitlabIntegration: gitlabIntegration.ToGitlabIntegrationType(),
  51. }
  52. p.WriteResult(w, r, res)
  53. }