2
0

list_git.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  56. p.WriteResult(w, r, res)
  57. return
  58. }
  59. accountIDs = append(accountIDs, ghAuthUser.GetID())
  60. accountIDMap[ghAuthUser.GetID()] = ghAuthUser.GetLogin()
  61. opts := &github.ListOptions{
  62. PerPage: 100,
  63. Page: 1,
  64. }
  65. for {
  66. orgs, pages, err := client.Organizations.List(context.Background(), "", opts)
  67. if err != nil {
  68. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  69. p.WriteResult(w, r, res)
  70. return
  71. }
  72. for _, org := range orgs {
  73. accountIDs = append(accountIDs, org.GetID())
  74. accountIDMap[org.GetID()] = org.GetLogin()
  75. }
  76. if pages.NextPage == 0 {
  77. break
  78. }
  79. }
  80. installationData, err := p.Repo().GithubAppInstallation().ReadGithubAppInstallationByAccountIDs(accountIDs)
  81. if err != nil {
  82. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  83. p.WriteResult(w, r, res)
  84. }
  85. for _, data := range installationData {
  86. res = append(res, &types.GitIntegration{
  87. Provider: "github",
  88. Name: accountIDMap[data.AccountID],
  89. InstallationID: data.InstallationID,
  90. })
  91. }
  92. p.WriteResult(w, r, res)
  93. }