-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtask.go
137 lines (121 loc) · 3.39 KB
/
task.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"github.com/TBXark/github-backup/config"
"github.com/TBXark/github-backup/provider/gitea"
"github.com/TBXark/github-backup/provider/github"
"github.com/TBXark/github-backup/provider/provider"
"github.com/TBXark/github-backup/utils/matcher"
"log"
)
func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.Provider, error) {
switch conf.Type {
case config.BackupProviderConfigTypeGitea:
c, err := config.ConvertToBackupProviderConfig[gitea.Config](conf.Config)
if err != nil {
return nil, err
}
return gitea.NewGitea(c), nil
}
return nil, fmt.Errorf("unknown backup provider type: %s", conf.Type)
}
type SyncTask struct {
conf *config.SyncConfig
counter map[string]int
}
func NewTask(conf *config.SyncConfig) *SyncTask {
return &SyncTask{
conf: conf,
counter: make(map[string]int, 100),
}
}
func (t *SyncTask) Run() {
for _, target := range t.conf.Targets {
t.execute(target)
}
}
func (t *SyncTask) execute(target *config.GithubConfig) {
// merge default config
target.MergeDefault(t.conf.DefaultConf)
// load all github repos
loader := github.NewGithub(target.Token)
repos, err := loader.LoadAllRepos(target.Owner, target.IsOwnerOrg)
if err != nil {
log.Panicf("load %s repos error: %s", target.RepoOwner, err.Error())
}
// build backup provider
backup, err := BuildBackupProvider(target.Backup)
if err != nil {
log.Panicf("build backup provider error: %s", err.Error())
}
// handle repos set
handledRepos := make(map[string]struct{})
from := &provider.Owner{
Name: target.Owner,
IsOrg: target.IsOwnerOrg,
}
to := &provider.Owner{
Name: target.RepoOwner,
IsOrg: target.IsRepoOwnerOrg,
}
log.Printf("found %d repos in %s", len(repos), target.Owner)
for _, repo := range repos {
// render repo identity
identity := matcher.Identity(target.Owner, repo.Name, repo.Private, repo.Fork, repo.Archived)
// check allow/deny rule
if target.Filter != nil {
if !matcher.IsMatch(identity, target.Filter.AllowRule...) {
if matcher.IsMatch(identity, target.Filter.DenyRule...) {
continue
}
}
}
githubToken := target.Token
// check specific GitHub token for this repo by regex
for k, v := range target.SpecificGithubToken {
if matcher.IsMatch(identity, k) {
githubToken = v
break
}
}
// migrate repo
delete(t.counter, repo.Name)
s, e := backup.MigrateRepo(from, to, &provider.Repo{
Name: repo.Name,
Description: repo.Description,
AuthToken: githubToken,
})
if e != nil {
log.Printf("migrate %s error: %s", repo.Name, e.Error())
} else {
log.Printf("migrate %s %s", repo.Name, s)
}
handledRepos[repo.Name] = struct{}{}
}
// delete unmatched repos if needed
if target.Filter.UnmatchedRepoAction == config.UnmatchedRepoActionDelete {
// load local repos
localRepos, lErr := backup.LoadRepos(to)
if lErr != nil {
log.Panicf("load %s repos error: %s", target.RepoOwner, lErr.Error())
}
// delete unmatched repos
for _, repo := range localRepos {
if _, ok := handledRepos[repo]; ok {
continue
}
if target.Filter.PreDeleteCheckCount > 0 {
if t.counter[repo] < target.Filter.PreDeleteCheckCount {
t.counter[repo]++
continue
}
}
s, e := backup.DeleteRepo(target.RepoOwner, repo)
if e != nil {
log.Printf("delete %s error: %s", repo, e.Error())
} else {
log.Printf("delete %s %s", repo, s)
}
}
}
}