Skip to content

Commit

Permalink
Import topics during migration (#7851)
Browse files Browse the repository at this point in the history
* add GetTopics interface

* CreateTopics

* remove un-needed comment
  • Loading branch information
sapk authored and lunny committed Aug 14, 2019
1 parent 99a004c commit 46d6b92
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/content/doc/advanced/migrations.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ create a Downloader.
```Go
type Downloader interface {
GetRepoInfo() (*Repository, error)
GetTopics() ([]string, error)
GetMilestones() ([]*Milestone, error)
GetReleases() ([]*Release, error)
GetLabels() ([]*Label, error)
Expand Down
1 change: 1 addition & 0 deletions modules/migrations/base/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package base
// Downloader downloads the site repo informations
type Downloader interface {
GetRepoInfo() (*Repository, error)
GetTopics() ([]string, error)
GetMilestones() ([]*Milestone, error)
GetReleases() ([]*Release, error)
GetLabels() ([]*Label, error)
Expand Down
1 change: 1 addition & 0 deletions modules/migrations/base/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package base
type Uploader interface {
MaxBatchInsertSize(tp string) int
CreateRepo(repo *Repository, opts MigrateOptions) error
CreateTopics(topic ...string) error
CreateMilestones(milestones ...*Milestone) error
CreateReleases(releases ...*Release) error
CreateLabels(labels ...*Label) error
Expand Down
5 changes: 5 additions & 0 deletions modules/migrations/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (g *PlainGitDownloader) GetRepoInfo() (*base.Repository, error) {
}, nil
}

// GetTopics returns empty list for plain git repo
func (g *PlainGitDownloader) GetTopics() ([]string, error) {
return []string{}, nil
}

// GetMilestones returns milestones
func (g *PlainGitDownloader) GetMilestones() ([]*base.Milestone, error) {
return nil, ErrNotSupported
Expand Down
5 changes: 5 additions & 0 deletions modules/migrations/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
return err
}

// CreateTopics creates topics
func (g *GiteaLocalUploader) CreateTopics(topics ...string) error {
return models.SaveTopics(g.repo.ID, topics...)
}

// CreateMilestones creates milestones
func (g *GiteaLocalUploader) CreateMilestones(milestones ...*base.Milestone) error {
var mss = make([]*models.Milestone, 0, len(milestones))
Expand Down
6 changes: 6 additions & 0 deletions modules/migrations/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ func (g *GithubDownloaderV3) GetRepoInfo() (*base.Repository, error) {
}, nil
}

// GetTopics return github topics
func (g *GithubDownloaderV3) GetTopics() ([]string, error) {
r, _, err := g.client.Repositories.Get(g.ctx, g.repoOwner, g.repoName)
return r.Topics, err
}

// GetMilestones returns milestones
func (g *GithubDownloaderV3) GetMilestones() ([]*base.Milestone, error) {
var perPage = 100
Expand Down
4 changes: 4 additions & 0 deletions modules/migrations/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func TestGitHubDownloadRepo(t *testing.T) {
OriginalURL: "https://github.com/go-gitea/gitea",
}, repo)

topics, err := downloader.GetTopics()
assert.NoError(t, err)
assert.Contains(t, topics, "gitea")

milestones, err := downloader.GetMilestones()
assert.NoError(t, err)
// before this tool release, we have 39 milestones on github.com/go-gitea/gitea
Expand Down
11 changes: 11 additions & 0 deletions modules/migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts
return err
}

log.Trace("migrating topics")
topics, err := downloader.GetTopics()
if err != nil {
return err
}
if len(topics) > 0 {
if err := uploader.CreateTopics(topics...); err != nil {
return err
}
}

if opts.Milestones {
log.Trace("migrating milestones")
milestones, err := downloader.GetMilestones()
Expand Down

0 comments on commit 46d6b92

Please sign in to comment.