<commit_before>package v1

import (
	"net/http"

	"github.com/gitpods/gitpods/internal/api/v1/models"
	"github.com/gitpods/gitpods/internal/api/v1/restapi"
	"github.com/gitpods/gitpods/internal/api/v1/restapi/operations"
	"github.com/gitpods/gitpods/internal/api/v1/restapi/operations/repositories"
	"github.com/gitpods/gitpods/internal/api/v1/restapi/operations/users"
	"github.com/gitpods/gitpods/repository"
	"github.com/gitpods/gitpods/session"
	"github.com/gitpods/gitpods/user"
	"github.com/go-openapi/loads"
	"github.com/go-openapi/runtime/middleware"
	"github.com/go-openapi/strfmt"
)

// API has the http.Handler for the OpenAPI implementation
type API struct {
	Handler http.Handler
}

// New creates a new API that adds our own Handler implementations
func New(rs repository.Service, us user.Service) (*API, error) {
	swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
	if err != nil {
		return nil, err
	}

	gitpodsAPI := operations.NewGitpodsAPI(swaggerSpec)

	gitpodsAPI.RepositoriesGetOwnerRepositoriesHandler = GetOwnerRepositoriesHandler(rs)
	gitpodsAPI.RepositoriesGetRepositoryHandler = GetRepositoryHandler(rs)
	gitpodsAPI.UsersListUsersHandler = ListUsersHandler(us)
	gitpodsAPI.UsersGetUserMeHandler = GetUserMeHandler(us)
	gitpodsAPI.UsersGetUserHandler = GetUserHandler(us)
	gitpodsAPI.UsersUpdateUserHandler = UpdateUserHandler(us)

	return &API{
		Handler: gitpodsAPI.Serve(nil),
	}, nil
}

func convertRepository(r *repository.Repository) *models.Repository {
	return &models.Repository{
		ID:            strfmt.UUID(r.ID),
		Name:          &r.Name,
		Description:   r.Description,
		DefaultBranch: r.DefaultBranch,
		Website:       r.Website,
		CreatedAt:     strfmt.DateTime(r.Created),
		UpdatedAt:     strfmt.DateTime(r.Updated),
		//Owner: nil,
	}
}<commit_msg>Create CreateRepositoryHandler<commit_after>package v1

import (
	"net/http"

	"github.com/gitpods/gitpods/internal/api/v1/models"
	"github.com/gitpods/gitpods/internal/api/v1/restapi"
	"github.com/gitpods/gitpods/internal/api/v1/restapi/operations"
	"github.com/gitpods/gitpods/internal/api/v1/restapi/operations/repositories"
	"github.com/gitpods/gitpods/internal/api/v1/restapi/operations/users"
	"github.com/gitpods/gitpods/repository"
	"github.com/gitpods/gitpods/session"
	"github.com/gitpods/gitpods/user"
	"github.com/go-openapi/loads"
	"github.com/go-openapi/runtime/middleware"
	"github.com/go-openapi/strfmt"
)

// API has the http.Handler for the OpenAPI implementation
type API struct {
	Handler http.Handler
}

// New creates a new API that adds our own Handler implementations
func New(rs repository.Service, us user.Service) (*API, error) {
	swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
	if err != nil {
		return nil, err
	}

	gitpodsAPI := operations.NewGitpodsAPI(swaggerSpec)

	gitpodsAPI.RepositoriesCreateRepositoryHandler = CreateRepositoryHandler(rs)
	gitpodsAPI.RepositoriesGetOwnerRepositoriesHandler = GetOwnerRepositoriesHandler(rs)
	gitpodsAPI.RepositoriesGetRepositoryHandler = GetRepositoryHandler(rs)
	gitpodsAPI.UsersGetUserHandler = GetUserHandler(us)
	gitpodsAPI.UsersGetUserMeHandler = GetUserMeHandler(us)
	gitpodsAPI.UsersListUsersHandler = ListUsersHandler(us)
	gitpodsAPI.UsersUpdateUserHandler = UpdateUserHandler(us)

	return &API{
		Handler: gitpodsAPI.Serve(nil),
	}, nil
}

func convertRepository(r *repository.Repository) *models.Repository {
	return &models.Repository{
		ID:            strfmt.UUID(r.ID),
		Name:          &r.Name,
		Description:   r.Description,
		DefaultBranch: r.DefaultBranch,
		Website:       r.Website,
		CreatedAt:     strfmt.DateTime(r.Created),
		UpdatedAt:     strfmt.DateTime(r.Updated),

		//Owner: nil, // TODO: Include via query parameter if wanted
	}
}

func CreateRepositoryHandler(rs repository.Service) repositories.CreateRepositoryHandlerFunc {
	return func(params repositories.CreateRepositoryParams) middleware.Responder {
		ctx := params.HTTPRequest.Context()
		owner := session.GetSessionUser(ctx)

		r, err := rs.Create(ctx, owner.Username, &repository.Repository{
			Name:        *params.NewRepository.Name,
			Description: params.NewRepository.Description,
			Website:     params.NewRepository.Website,
		})
		if err != nil {
			if v, ok := err.(repository.ValidationErrors); ok {
				message := "The given repository input is invalid"
				payload := &models.ValidationError{
					Message: &message,
				}
				for _, verr := range v.Errors {
					payload.Errors = append(payload.Errors, &models.ValidationErrorErrorsItems0{
						Field:   verr.Field,
						Message: verr.Error.Error(),
					})
				}
				return repositories.NewCreateRepositoryUnprocessableEntity().WithPayload(payload)
			}

			return repositories.NewCreateRepositoryDefault(http.StatusInternalServerError)
		}

		return repositories.NewCreateRepositoryOK().WithPayload(convertRepository(r))
	}
}
{'fraction_non_alphanumeric': 0.11378106387348531, 'fraction_numerical': 0.0026699527623742043, 'mean_word_length': 4.715962441314554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1741627', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1614, 'n_words': 261}