-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
216 lines (201 loc) · 6.29 KB
/
main.go
File metadata and controls
216 lines (201 loc) · 6.29 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
func mirrorGitRepo(repoDir, cloneUrl, mainBranch string) {
out, err := exec.Command("rm", "-rf", repoDir).CombinedOutput()
if err != nil {
fmt.Println(" Error during delete directory: " + string(out))
}
out, err = exec.Command("git", "clone", "--mirror", cloneUrl, repoDir).CombinedOutput()
if err != nil {
fmt.Printf(" Error mirroring %s:\n%s\n ", cloneUrl, string(out))
} else {
fmt.Printf(" Bare mirror success for %s\n", cloneUrl)
}
}
func syncGitRepo(repoDir, gitUrl, mainBranch string) {
mirror := os.Getenv("BITSYNC_MIRROR")
if mirror == "true" {
mirrorGitRepo(repoDir, gitUrl, mainBranch)
return
}
// fmt.Printf(" Processing git repo %s from %s\n", repoDir, gitUrl)
if _, err := os.Stat(repoDir); err != nil {
out, err := exec.Command("git", "clone", gitUrl, repoDir).CombinedOutput()
if err != nil {
fmt.Printf(" Error during clone of %s:\n%s\n", gitUrl, string(out))
return
} else {
fmt.Printf(" Clone successful for %s\n", gitUrl)
return
}
}
out, err := exec.Command("git", "-C", repoDir, "reset", "--hard").CombinedOutput()
if err != nil {
fmt.Printf(" Error during hard reset in %s:\n%s\n", repoDir, string(out))
}
out, err = exec.Command("git", "-C", repoDir, "fetch", "--all", "--prune").CombinedOutput()
if err != nil {
fmt.Printf(" Error during fetch %s:\n%s\n ", gitUrl, string(out))
} else {
fmt.Printf(" Fetch successful for %s\n", gitUrl)
}
out, err = exec.Command("git", "-C", repoDir, "checkout", mainBranch).CombinedOutput()
if err != nil {
fmt.Printf(" Error during checkout %s in %s:\n%s\n", mainBranch, repoDir, string(out))
}
out, err = exec.Command("git", "-C", repoDir, "pull").CombinedOutput()
if err != nil {
fmt.Printf(" Error during pull in %s:\n%s\n", repoDir, string(out))
}
}
func processGitHubRepo(repo GitHubRepository) {
homeDir, err := os.UserHomeDir()
checkErr(err)
var gitHubRepoPath, gitHubOrgPath string
mirror := os.Getenv("BITSYNC_MIRROR")
if mirror == "true" {
gitHubOrgPath = filepath.Join(homeDir, "mirrors", "github", repo.Owner.Login)
gitHubRepoPath = filepath.Join(homeDir, "mirrors", "github", repo.Owner.Login, repo.Name)
} else {
gitHubOrgPath = filepath.Join(homeDir, "repos", "github", repo.Owner.Login)
gitHubRepoPath = filepath.Join(homeDir, "repos", "github", repo.Owner.Login, repo.Name)
}
err = os.MkdirAll(gitHubOrgPath, 0750)
checkErr(err)
syncGitRepo(gitHubRepoPath, repo.SSHUrl, repo.DefaultBranch)
}
func getWorkerCount() int {
workersStr := os.Getenv("BITSYNC_WORKERS")
if workersStr == "" {
return 6
}
workers, err := strconv.Atoi(workersStr)
if err != nil || workers < 1 {
return 4
}
return workers
}
func processGitHubOrg(token, org string) {
fmt.Printf("Processing repos in GitHub Org %v\n", org)
repos := FetchGitHubRepos(token, org)
workerCount := getWorkerCount()
repoCh := make(chan GitHubRepository)
var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for repo := range repoCh {
processGitHubRepo(repo)
}
}()
}
for _, repo := range repos {
repoCh <- repo
}
close(repoCh)
wg.Wait()
}
func processGitHubOrgs() {
ghtoken := os.Getenv("GHTOKEN")
if ghtoken == "" {
fmt.Println("GHTOKEN is not set, skipping GitHub repositories")
} else {
fmt.Println("GHTOKEN is set, processing GitHub repositories")
var ghorgs []string
ghorg := os.Getenv("GHORG")
if ghorg != "" {
fmt.Printf("GHORG is set, processing selected GitHub orgs\n")
ghorgs = strings.Split(ghorg, ",")
} else {
fmt.Printf("GHORG is not set, processing all GitHub orgs\n")
ghorgs = FetchGitHubOrganisations(ghtoken)
}
for _, ghorg := range ghorgs {
processGitHubOrg(ghtoken, ghorg)
}
}
}
func processBitBucketRepo(workspace string, repo BitbucketRepository) {
homeDir, err := os.UserHomeDir()
checkErr(err)
var BitBucketRepoPath, BitBucketProjectPath string
mirror := os.Getenv("BITSYNC_MIRROR")
if mirror == "true" {
BitBucketProjectPath = filepath.Join(homeDir, "mirrors", "bitbucket", workspace, repo.Project.Key)
BitBucketRepoPath = filepath.Join(homeDir, "mirrors", "bitbucket", workspace, repo.Project.Key, repo.Slug)
} else {
BitBucketProjectPath = filepath.Join(homeDir, "repos", "bitbucket", workspace, repo.Project.Key)
BitBucketRepoPath = filepath.Join(homeDir, "repos", "bitbucket", workspace, repo.Project.Key, repo.Slug)
}
BitBucketCloneUrl := "git@bitbucket.org:" + workspace + "/" + repo.Slug
err = os.MkdirAll(BitBucketProjectPath, 0750)
checkErr(err)
syncGitRepo(BitBucketRepoPath, BitBucketCloneUrl, repo.Mainbranch.Name)
}
func processBitBucketWorkspace(bbUser, bbAppPass, bbWorkspace string) {
fmt.Printf("Processing BitBucket workspace %v\n", bbWorkspace)
repos := FetchBitbucketRepos(bbUser, bbAppPass, bbWorkspace)
workerCount := getWorkerCount()
repoCh := make(chan BitbucketRepository)
var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for repo := range repoCh {
processBitBucketRepo(bbWorkspace, repo)
}
}()
}
for _, repo := range repos {
repoCh <- repo
}
close(repoCh)
wg.Wait()
}
func processBitBucketWorkspaces() {
bbuser := os.Getenv("BBUSER")
bbapppass := os.Getenv("BBAPPPASS")
bborg := os.Getenv("BBORG")
if bbuser == "" || bbapppass == "" {
fmt.Println("BBUSER and/or BBAPPPASS not set, skipping BitBucket repositories")
} else {
fmt.Println("BBUSER and BBAPPPASS are set, processing BitBucket repositories")
var bbWorkspaces []string
if bborg == "" {
fmt.Println("BBORG is not set, processing all BitBucket Workspaces")
bbWorkspaces = FetchBitBucketOrganisations(bbuser, bbapppass)
} else {
fmt.Println("BBORG is set, processing selected BitBucket Workspaces")
bbWorkspaces = strings.Split(bborg, ",")
}
for _, bbWorkspace := range bbWorkspaces {
processBitBucketWorkspace(bbuser, bbapppass, bbWorkspace)
}
}
}
func checkErr(err error) {
if err != nil {
fmt.Printf("An error occured. %v\n", err)
os.Exit(1)
}
}
func main() {
startTime := time.Now()
fmt.Printf("Starting BitSync process at %v\n", startTime)
processGitHubOrgs()
processBitBucketWorkspaces()
endTime := time.Now()
fmt.Printf("Finished BitSync process at %v\n", endTime)
fmt.Printf("Elapsed time %v\n", endTime.Sub(startTime))
}