create_gitlab.go 1.5 KB

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