list_git.go 3.0 KB

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