list_git.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package project_integration
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/google/go-github/v39/github"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  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/config"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/models"
  13. "gorm.io/gorm"
  14. )
  15. type ListGitIntegrationHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewListGitIntegrationHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *ListGitIntegrationHandler {
  23. return &ListGitIntegrationHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (p *ListGitIntegrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  29. gitlabInts, err := p.Repo().GitlabIntegration().ListGitlabIntegrationsByProjectID(project.ID)
  30. var res types.ListGitIntegrationResponse
  31. if err == nil {
  32. for _, gitlabInt := range gitlabInts {
  33. res = append(res, &types.GitIntegration{
  34. Provider: "gitlab",
  35. InstanceURL: gitlabInt.InstanceURL,
  36. IntegrationID: gitlabInt.ID,
  37. })
  38. }
  39. }
  40. tok, err := gitinstallation.GetGithubAppOauthTokenFromRequest(p.Config(), r)
  41. if err != nil {
  42. if err == gorm.ErrRecordNotFound {
  43. // return empty array, this is not an error
  44. p.WriteResult(w, r, res)
  45. } else {
  46. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  47. }
  48. return
  49. }
  50. client := github.NewClient(p.Config().GithubAppConf.Client(context.Background(), tok))
  51. var accountIDs []int64
  52. accountIDMap := make(map[int64]string)
  53. ghAuthUser, _, err := client.Users.Get(context.Background(), "")
  54. if err != nil {
  55. p.WriteResult(w, r, res)
  56. return
  57. }
  58. accountIDs = append(accountIDs, ghAuthUser.GetID())
  59. accountIDMap[ghAuthUser.GetID()] = ghAuthUser.GetLogin()
  60. opts := &github.ListOptions{
  61. PerPage: 100,
  62. Page: 1,
  63. }
  64. for {
  65. orgs, pages, err := client.Organizations.List(context.Background(), "", opts)
  66. if err != nil {
  67. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  68. p.WriteResult(w, r, res)
  69. return
  70. }
  71. for _, org := range orgs {
  72. accountIDs = append(accountIDs, org.GetID())
  73. accountIDMap[org.GetID()] = org.GetLogin()
  74. }
  75. if pages.NextPage == 0 {
  76. break
  77. }
  78. }
  79. installationData, err := p.Repo().GithubAppInstallation().ReadGithubAppInstallationByAccountIDs(accountIDs)
  80. if err != nil {
  81. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  82. p.WriteResult(w, r, res)
  83. }
  84. for _, data := range installationData {
  85. res = append(res, &types.GitIntegration{
  86. Provider: "github",
  87. Name: accountIDMap[data.AccountID],
  88. InstallationID: data.InstallationID,
  89. })
  90. }
  91. p.WriteResult(w, r, res)
  92. }