| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package project_integration
- import (
- "errors"
- "fmt"
- "net/http"
- "net/url"
- "strings"
- "sync"
- "github.com/porter-dev/porter/api/server/handlers"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/apierrors"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/integrations/buildpacks"
- "github.com/porter-dev/porter/internal/models"
- ints "github.com/porter-dev/porter/internal/models/integrations"
- "github.com/xanzy/go-gitlab"
- )
- type GetGitlabRepoBuildpackHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewGetGitlabRepoBuildpackHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *GetGitlabRepoBuildpackHandler {
- return &GetGitlabRepoBuildpackHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (p *GetGitlabRepoBuildpackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- user, _ := r.Context().Value(types.UserScope).(*models.User)
- gi, _ := r.Context().Value(types.GitlabIntegrationScope).(*ints.GitlabIntegration)
- request := &types.GetGitlabBuildpackRequest{}
- ok := p.DecodeAndValidate(w, r, request)
- if !ok {
- return
- }
- client, err := getGitlabClient(p.Repo(), user.ID, project.ID, gi, p.Config())
- if err != nil {
- if errors.Is(err, errUnauthorizedGitlabUser) {
- p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(errUnauthorizedGitlabUser, http.StatusUnauthorized))
- }
- p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- dir, err := url.QueryUnescape(request.Dir)
- if err != nil {
- p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("malformed query param dir")))
- return
- }
- dir = strings.TrimPrefix(dir, "./")
- if len(dir) == 0 {
- dir = "."
- }
- tree, resp, err := client.Repositories.ListTree(request.RepoPath, &gitlab.ListTreeOptions{
- Path: gitlab.String(dir),
- Ref: gitlab.String(request.Branch),
- })
- if resp.StatusCode == http.StatusUnauthorized {
- p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unauthorized gitlab user"), http.StatusUnauthorized))
- return
- } else if resp.StatusCode == http.StatusNotFound {
- p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("no such gitlab project found"), http.StatusNotFound))
- return
- }
- if err != nil {
- p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- builderInfoMap := initBuilderInfo()
- var wg sync.WaitGroup
- wg.Add(len(buildpacks.Runtimes))
- for i := range buildpacks.Runtimes {
- go func(idx int) {
- defer func() {
- if rec := recover(); rec != nil {
- p.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("panic detected in runtime detection")))
- return
- }
- }()
- buildpacks.Runtimes[idx].DetectGitlab(
- client, tree, request.RepoPath, dir, request.Branch,
- builderInfoMap[buildpacks.PaketoBuilder], builderInfoMap[buildpacks.HerokuBuilder],
- )
- wg.Done()
- }(i)
- }
- wg.Wait()
- // FIXME: add Java buildpacks
- builderInfoMap[buildpacks.PaketoBuilder].Others = append(builderInfoMap[buildpacks.PaketoBuilder].Others,
- buildpacks.BuildpackInfo{
- Name: "Java",
- Buildpack: "gcr.io/paketo-buildpacks/java",
- })
- builderInfoMap[buildpacks.HerokuBuilder].Others = append(builderInfoMap[buildpacks.HerokuBuilder].Others,
- buildpacks.BuildpackInfo{
- Name: "Java",
- Buildpack: "heroku/java",
- })
- var builders []*buildpacks.BuilderInfo
- for _, v := range builderInfoMap {
- builders = append(builders, v)
- }
- p.WriteResult(w, r, builders)
- }
- func initBuilderInfo() map[string]*buildpacks.BuilderInfo {
- builders := make(map[string]*buildpacks.BuilderInfo)
- builders[buildpacks.PaketoBuilder] = &buildpacks.BuilderInfo{
- Name: "Paketo",
- Builders: []string{
- "paketobuildpacks/builder:full",
- },
- }
- builders[buildpacks.HerokuBuilder] = &buildpacks.BuilderInfo{
- Name: "Heroku",
- Builders: []string{
- "heroku/buildpacks:20",
- "heroku/buildpacks:18",
- },
- }
- return builders
- }
|