Migration Api Changed #392

Merged
jolheiser merged 3 commits from 6543/go-sdk:migration-api-changed into master 2020-09-12 13:58:23 +00:00
3 changed files with 116 additions and 27 deletions

View File

@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
@ -345,32 +346,6 @@ func (c *Client) DeleteRepo(owner, repo string) error {
return err
}
// MigrateRepoOption options for migrating a repository from an external service
type MigrateRepoOption struct {
CloneAddr string `json:"clone_addr"`
AuthUsername string `json:"auth_username"`
AuthPassword string `json:"auth_password"`
UID int `json:"uid"`
RepoName string `json:"repo_name"`
Mirror bool `json:"mirror"`
Private bool `json:"private"`
Description string `json:"description"`
}
// MigrateRepo migrates a repository from other Git hosting sources for the
// authenticated user.
//
// To migrate a repository for a organization, the authenticated user must be a
// owner of the specified organization.
func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
}
// MirrorSync adds a mirrored repository to the mirror sync queue.
func (c *Client) MirrorSync(owner, repo string) error {
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/mirror-sync", owner, repo), nil, nil)

114
gitea/repo_migrate.go Normal file
View File

@ -0,0 +1,114 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
import (
"bytes"
"encoding/json"
"fmt"
)
// GitServiceType represents a git service
type GitServiceType string
const (
// GitServicePlain represents a plain git service
GitServicePlain GitServiceType = "git"
//GitServiceGithub represents github.com
GitServiceGithub GitServiceType = "github"
// GitServiceGitlab represents a gitlab service
GitServiceGitlab GitServiceType = "gitlab"
// Not supported jet
// // GitServiceGitea represents a gitea service
// GitServiceGitea GitServiceType = "gitea"
6543 marked this conversation as resolved Outdated

Sorry, I know that this is accepted even in the API, but imo we shouldn't be promoting a service that doesn't actually exist.
Currently this will just fall through as we don't have a Gogs factory, essentially making this a plain git migration.

Sorry, I know that this is accepted even in the API, but imo we shouldn't be promoting a service that doesn't actually exist. Currently this will just fall through as we don't have a Gogs factory, essentially making this a plain git migration.
Outdated
Review

I'll comment it out until we have one

I'll comment it out until we have one
// // GitServiceGogs represents a gogs service
// GitServiceGogs GitServiceType = "gogs"
)
// MigrateRepoOption options for migrating a repository from an external service
type MigrateRepoOption struct {
RepoName string `json:"repo_name"`
RepoOwner string `json:"repo_owner"`
// deprecated use RepoOwner
RepoOwnerID int64 `json:"uid"`
CloneAddr string `json:"clone_addr"`
Service GitServiceType `json:"service"`
AuthUsername string `json:"auth_username"`
AuthPassword string `json:"auth_password"`
AuthToken string `json:"auth_token"`
Mirror bool `json:"mirror"`
Private bool `json:"private"`
Description string `json:"description"`
Wiki bool `json:"wiki"`
Milestones bool `json:"milestones"`
Labels bool `json:"labels"`
Issues bool `json:"issues"`
PullRequests bool `json:"pull_requests"`
Releases bool `json:"releases"`
}
// Validate the MigrateRepoOption struct
func (opt *MigrateRepoOption) Validate() error {
// check user options
if len(opt.CloneAddr) == 0 {
return fmt.Errorf("CloneAddr required")
}
if len(opt.RepoName) == 0 {
return fmt.Errorf("RepoName required")
} else if len(opt.RepoName) > 100 {
return fmt.Errorf("RepoName to long")
}
if len(opt.Description) > 255 {
return fmt.Errorf("Description to long")
}
switch opt.Service {
case GitServiceGithub:
if len(opt.AuthToken) == 0 {
return fmt.Errorf("github require token authentication")
}
}
return nil
}
// MigrateRepo migrates a repository from other Git hosting sources for the authenticated user.
//
// To migrate a repository for a organization, the authenticated user must be a
// owner of the specified organization.
6543 marked this conversation as resolved Outdated

GitLab also has token support and so should probably prefer it.

GitLab also has token support and so should probably prefer it.
Outdated
Review

@jolheiser this will force it too and fail with basicAuth !!!

@jolheiser this will force it too and **fail** with basicAuth !!!
func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
if err := opt.Validate(); err != nil {
6543 marked this conversation as resolved Outdated

authentication

`authentication`
return nil, err
}
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
if len(opt.AuthToken) != 0 {
// gitea <= 1.12 dont understand AuthToken
opt.AuthUsername = opt.AuthToken
opt.AuthPassword, opt.AuthToken = "", ""
}
if len(opt.RepoOwner) != 0 {
// gitea <= 1.12 dont understand RepoOwner
u, err := c.GetUserInfo(opt.RepoOwner)
if err != nil {
return nil, err
}
opt.RepoOwnerID = u.ID
} else if opt.RepoOwnerID == 0 {
// gitea <= 1.12 require RepoOwnerID
u, err := c.GetMyUserInfo()
if err != nil {
return nil, err
}
opt.RepoOwnerID = u.ID
}
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
}

View File

@ -45,7 +45,7 @@ func TestRepoMigrateAndLanguages(t *testing.T) {
repoM, err := c.MigrateRepo(MigrateRepoOption{
CloneAddr: "https://gitea.com/gitea/go-sdk.git",
RepoName: "sdk-mirror",
UID: int(user.ID),
RepoOwner: user.UserName,
Mirror: true,
Private: false,
Description: "mirror sdk",