drone/internal/api/controller/space/export.go
2023-09-12 16:18:23 -07:00

76 lines
2.1 KiB
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package space
import (
"context"
"fmt"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/services/exporter"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
"github.com/rs/zerolog/log"
)
type ExportInput struct {
RepoRef string `json:"repo_ref"`
AccountId string `json:"accountId"`
OrgIdentifier string `json:"orgIdentifier"`
ProjectIdentifier string `json:"projectIdentifier"`
Token string `json:"token"`
}
// Export creates a new empty repository in harness code and does git push to it.
func (c *Controller) Export(ctx context.Context, session *auth.Session, in *ExportInput) (*types.Repository, error) {
space, err := c.spaceStore.FindByRef(ctx, in.RepoRef)
if err = apiauth.CheckSpace(ctx, c.authorizer, session, space, enum.PermissionSpaceEdit, false); err != nil {
return nil, err
}
if err != nil {
return nil, err
}
err = c.sanitizeExportInput(in)
if err != nil {
return nil, fmt.Errorf("failed to sanitize input: %w", err)
}
providerInfo := exporter.HarnessCodeInfo{
AccountId: in.AccountId,
ProjectIdentifier: in.ProjectIdentifier,
OrgIdentifier: in.OrgIdentifier,
Token: in.Token,
}
// todo(abhinav): add pagination
repos, err := c.repoStore.List(ctx, space.ID, &types.RepoFilter{})
if err != nil {
return nil, err
}
log.Ctx(ctx).Info().Msgf("Add migration for repos ", providerInfo, repos)
return nil, nil
}
func (c *Controller) sanitizeExportInput(in *ExportInput) error {
if in.AccountId == "" {
return usererror.BadRequest("account id must be provided")
}
if in.OrgIdentifier == "" {
return usererror.BadRequest("organization identifier must be provided")
}
if in.ProjectIdentifier == "" {
return usererror.BadRequest("project identifier must be provided")
}
return nil
}