-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepos.go
106 lines (94 loc) · 2.72 KB
/
repos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"flag"
"fmt"
"github.com/artur-sak13/gitmv/provider"
)
const reposHelp = `Migrate all repos from one Git provider to another.`
func (cmd *reposCommand) Name() string { return "repos" }
func (cmd *reposCommand) Args() string { return "[OPTIONS]" }
func (cmd *reposCommand) ShortHelp() string { return reposHelp }
func (cmd *reposCommand) LongHelp() string { return reposHelp }
func (cmd *reposCommand) Hidden() bool { return false }
func (cmd *reposCommand) Register(fs *flag.FlagSet) {}
type reposCommand struct{}
func (cmd *reposCommand) Run(ctx context.Context, args []string) error {
return runCommand(ctx, cmd.handleRepos)
}
// handleRepo will return
func (cmd *reposCommand) handleRepos(ctx context.Context, src, dest provider.GitProvider) error {
repos, err := src.GetRepositories()
if err != nil {
return err
}
github := dest.(*provider.GithubProvider)
err = github.LoadCache()
if err != nil {
return err
}
count := 0
for _, repo := range repos {
if repo.Fork || repo.Empty {
continue
}
cachedrepo, ok := github.Repocache[repo.Name]
if !ok {
count++
fmt.Printf("Missing repo: %s\n", repo.Name)
_, err := dest.CreateRepository(repo)
if err != nil {
return fmt.Errorf("error creating repository: %v\n%+v", err, repo)
}
_, err = dest.MigrateRepo(repo, src.GetAuth().Token)
if err != nil {
return fmt.Errorf("error migrating repository: %v", err)
}
}
labels, err := src.GetLabels(repo.PID, repo.Name)
if err != nil {
return err
}
for _, label := range labels {
_, ok := cachedrepo.Labels[label.Name]
if !ok {
fmt.Printf("Missing label: %s\n", label.Name)
_, err := dest.CreateLabel(label)
if err != nil {
return fmt.Errorf("error creating label: %v\n%+v", err, label)
}
}
}
issues, err := src.GetIssues(repo.PID, repo.Name)
if err != nil {
return err
}
for _, issue := range issues {
cachedissue, ok := cachedrepo.Issues[issue.Title]
if !ok {
fmt.Printf("Missing issue: %s\n", issue.Title)
newIssue, err := dest.CreateIssue(issue)
if err != nil {
return fmt.Errorf("error creating issue: %v\n%+v", err, issue)
}
cachedissue = github.NewCachedIssue(newIssue)
}
comments, err := src.GetComments(repo.PID, issue.Number, repo.Name)
if err != nil {
return err
}
for _, comment := range comments {
_, ok := cachedissue.Comments[comment.CreatedAt]
if !ok {
fmt.Printf("Missing comment: %s\n", comment.Body)
err := dest.CreateIssueComment(cachedissue.Issue.Number, comment)
if err != nil {
return fmt.Errorf("error creating comment: %v\n%+v", err, comment)
}
}
}
}
}
fmt.Printf("Repos missing: %d\n", count)
return nil
}