helpers.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package gitinstallation
  2. import (
  3. "net/http"
  4. "net/url"
  5. "github.com/bradleyfalzon/ghinstallation"
  6. "github.com/google/go-github/github"
  7. "github.com/porter-dev/porter/api/server/handlers"
  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/server/shared/requestutils"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/models/integrations"
  14. "github.com/porter-dev/porter/internal/oauth"
  15. "golang.org/x/oauth2"
  16. )
  17. // GetGithubAppOauthTokenFromRequest gets the GH oauth token from the request based on the currently
  18. // logged in user
  19. func GetGithubAppOauthTokenFromRequest(config *config.Config, r *http.Request) (*oauth2.Token, error) {
  20. // read the user from context
  21. user, _ := r.Context().Value(types.UserScope).(*models.User)
  22. getOAuthInt := config.Repo.GithubAppOAuthIntegration().ReadGithubAppOauthIntegration
  23. oauthInt, err := getOAuthInt(user.GithubAppIntegrationID)
  24. if err != nil {
  25. return nil, err
  26. }
  27. _, _, err = oauth.GetAccessToken(oauthInt.SharedOAuthModel,
  28. &config.GithubAppConf.Config,
  29. oauth.MakeUpdateGithubAppOauthIntegrationFunction(oauthInt, config.Repo),
  30. )
  31. if err != nil {
  32. // try again, in case the token got updated
  33. oauthInt2, err := getOAuthInt(user.GithubAppIntegrationID)
  34. if err != nil || oauthInt2.Expiry == oauthInt.Expiry {
  35. return nil, err
  36. }
  37. oauthInt.AccessToken = oauthInt2.AccessToken
  38. oauthInt.RefreshToken = oauthInt2.RefreshToken
  39. oauthInt.Expiry = oauthInt2.Expiry
  40. }
  41. return &oauth2.Token{
  42. AccessToken: string(oauthInt.AccessToken),
  43. RefreshToken: string(oauthInt.RefreshToken),
  44. Expiry: oauthInt.Expiry,
  45. TokenType: "Bearer",
  46. }, nil
  47. }
  48. // GetGithubAppClientFromRequest gets the github app installation id from the request and authenticates
  49. // using it and a private key file
  50. func GetGithubAppClientFromRequest(config *config.Config, r *http.Request) (*github.Client, error) {
  51. // get installation id from context
  52. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  53. itr, err := ghinstallation.NewKeyFromFile(
  54. http.DefaultTransport,
  55. config.GithubAppConf.AppID,
  56. ga.InstallationID,
  57. config.GithubAppConf.SecretPath,
  58. )
  59. if err != nil {
  60. return nil, err
  61. }
  62. return github.NewClient(&http.Client{Transport: itr}), nil
  63. }
  64. // GetOwnerAndNameParams gets the owner and name ref for the Github repo
  65. func GetOwnerAndNameParams(c handlers.PorterHandler, w http.ResponseWriter, r *http.Request) (string, string, bool) {
  66. owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
  67. if reqErr != nil {
  68. c.HandleAPIError(w, r, reqErr)
  69. return "", "", false
  70. }
  71. name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
  72. if reqErr != nil {
  73. c.HandleAPIError(w, r, reqErr)
  74. return "", "", false
  75. }
  76. return owner, name, true
  77. }
  78. // GetBranch gets the unencoded branch
  79. func GetBranch(c handlers.PorterHandler, w http.ResponseWriter, r *http.Request) (string, bool) {
  80. branch, reqErr := requestutils.GetURLParamString(r, types.URLParamGitBranch)
  81. if reqErr != nil {
  82. c.HandleAPIError(w, r, reqErr)
  83. return "", false
  84. }
  85. branch, err := url.QueryUnescape(branch)
  86. if reqErr != nil {
  87. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  88. return "", false
  89. }
  90. return branch, true
  91. }