get_buildpack.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package gitinstallation
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/google/go-github/github"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  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/integrations/buildpacks"
  13. )
  14. type GithubGetBuildpackHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. authz.KubernetesAgentGetter
  17. }
  18. func NewGithubGetBuildpackHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *GithubGetBuildpackHandler {
  23. return &GithubGetBuildpackHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (c *GithubGetBuildpackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. request := &types.GetBuildpackRequest{}
  29. ok := c.DecodeAndValidate(w, r, request)
  30. if !ok {
  31. return
  32. }
  33. owner, name, ok := GetOwnerAndNameParams(c, w, r)
  34. if !ok {
  35. return
  36. }
  37. branch, ok := GetBranch(c, w, r)
  38. if !ok {
  39. return
  40. }
  41. client, err := GetGithubAppClientFromRequest(c.Config(), r)
  42. if err != nil {
  43. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  44. return
  45. }
  46. repoContentOptions := github.RepositoryContentGetOptions{}
  47. repoContentOptions.Ref = branch
  48. _, directoryContents, _, err := client.Repositories.GetContents(
  49. context.Background(),
  50. owner,
  51. name,
  52. request.Dir,
  53. &repoContentOptions,
  54. )
  55. if err != nil {
  56. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. res := &types.GetBuildpackResponse{}
  60. nodeRuntime := buildpacks.NewAPINodeRuntime(client)
  61. config := nodeRuntime.Detect(directoryContents, owner, name, request.Dir, repoContentOptions)
  62. if config != nil {
  63. res.Name = "Node.js"
  64. res.Runtime = config["runtime"].(string)
  65. if res.Runtime != "node-standalone" {
  66. res.Config = map[string]interface{}{"scripts": config["scripts"], "node_engine": config["node_engine"]}
  67. }
  68. }
  69. c.WriteResult(w, r, res)
  70. }