CreateMirrorRepo often called by the crawler server to create new repo which will then be mirrored from other sources
(ctx context.Context, req types.CreateMirrorRepoReq)
| 126 | |
| 127 | // CreateMirrorRepo often called by the crawler server to create new repo which will then be mirrored from other sources |
| 128 | func (c *MirrorComponent) CreateMirrorRepo(ctx context.Context, req types.CreateMirrorRepoReq) (*database.Mirror, error) { |
| 129 | var username string |
| 130 | namespace := c.mapNamespaceAndName(req.SourceNamespace) |
| 131 | name := req.SourceName |
| 132 | user, err := c.userStore.FindByUsername(ctx, req.CurrentUser) |
| 133 | if err != nil { |
| 134 | return nil, errors.New("user does not exist") |
| 135 | } |
| 136 | if !user.CanAdmin() { |
| 137 | return nil, errors.New("user does not have admin permission") |
| 138 | } |
| 139 | repo, err := c.repoStore.FindByPath(ctx, req.RepoType, namespace, name) |
| 140 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 141 | return nil, fmt.Errorf("failed to check repo existance, error: %w", err) |
| 142 | } |
| 143 | if repo != nil { |
| 144 | name = fmt.Sprintf("%s_%s", req.SourceNamespace, req.SourceName) |
| 145 | repo, err = c.repoStore.FindByPath(ctx, req.RepoType, namespace, name) |
| 146 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 147 | return nil, fmt.Errorf("failed to check repo existance, error: %w", err) |
| 148 | } |
| 149 | if repo != nil { |
| 150 | return nil, fmt.Errorf("repo already exists,repo type:%s, source namespace: %s, source name: %s, target namespace: %s, target name: %s", |
| 151 | req.RepoType, req.SourceNamespace, req.SourceName, namespace, name) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | dbNamespace, err := c.namespaceStore.FindByPath(ctx, namespace) |
| 156 | if err != nil { |
| 157 | return nil, fmt.Errorf("namespace does not exist, namespace: %s", namespace) |
| 158 | } |
| 159 | username = dbNamespace.User.Username |
| 160 | |
| 161 | // create repo, create mirror repo |
| 162 | gitRepo, repo, err := c.repoComp.CreateRepo(ctx, types.CreateRepoReq{ |
| 163 | Username: username, |
| 164 | Namespace: namespace, |
| 165 | Name: name, |
| 166 | Nickname: name, |
| 167 | //TODO: tranlate description automatically |
| 168 | Description: req.Description, |
| 169 | //only mirror public repository |
| 170 | Private: true, |
| 171 | License: req.License, |
| 172 | DefaultBranch: req.DefaultBranch, |
| 173 | RepoType: req.RepoType, |
| 174 | }) |
| 175 | |
| 176 | if err != nil { |
| 177 | return nil, fmt.Errorf("failed to create OpenCSG repo, error: %w", err) |
| 178 | } |
| 179 | |
| 180 | if req.RepoType == types.ModelRepo { |
| 181 | dbModel := database.Model{ |
| 182 | Repository: repo, |
| 183 | RepositoryID: repo.ID, |
| 184 | } |
| 185 |
nothing calls this directly
no test coverage detected