// Copyright 2023 Harness, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package space import ( "context" "fmt" "github.com/harness/gitness/app/api/usererror" "github.com/harness/gitness/app/auth" "github.com/harness/gitness/app/services/importer" "github.com/harness/gitness/types" ) type ImportInput struct { CreateInput Provider importer.Provider `json:"provider"` ProviderSpace string `json:"provider_space"` } // Import creates new space and starts import of all repositories from the remote provider's space into it. func (c *Controller) Import(ctx context.Context, session *auth.Session, in *ImportInput) (*types.Space, error) { parentSpaceID, err := c.getSpaceCheckAuthSpaceCreation(ctx, session, in.ParentRef) if err != nil { return nil, err } if in.UID == "" { in.UID = in.ProviderSpace } err = c.sanitizeCreateInput(&in.CreateInput) if err != nil { return nil, fmt.Errorf("failed to sanitize input: %w", err) } remoteRepositories, err := importer.LoadRepositoriesFromProviderSpace(ctx, in.Provider, in.ProviderSpace) if err != nil { return nil, err } if len(remoteRepositories) == 0 { return nil, usererror.BadRequestf("found no repositories at %s", in.ProviderSpace) } repoIDs := make([]int64, len(remoteRepositories)) cloneURLs := make([]string, len(remoteRepositories)) var space *types.Space err = c.tx.WithTx(ctx, func(ctx context.Context) error { space, err = c.createSpaceInnerInTX(ctx, session, parentSpaceID, &in.CreateInput) if err != nil { return err } for i, remoteRepository := range remoteRepositories { repo := remoteRepository.ToRepo( space.ID, remoteRepository.UID, "", &session.Principal) err = c.repoStore.Create(ctx, repo) if err != nil { return fmt.Errorf("failed to create repository in storage: %w", err) } repoIDs[i] = repo.ID cloneURLs[i] = remoteRepository.CloneURL } jobGroupID := fmt.Sprintf("space-import-%d", space.ID) err = c.importer.RunMany(ctx, jobGroupID, in.Provider, repoIDs, cloneURLs) if err != nil { return fmt.Errorf("failed to start import repository jobs: %w", err) } return nil }) if err != nil { return nil, err } return space, nil }