-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (153 loc) · 5.86 KB
/
main.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
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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
func mirrorGitRepo(repoDir, cloneUrl, mainBranch string) {
fmt.Printf(" Bare mirror %s from %s\n", repoDir, cloneUrl)
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.Println("Error during mirror: " + string(out))
}
}
func cloneOrSyncGitRepo(repoDir, cloneUrl, mainBranch string) {
mirror := os.Getenv("BITSYNC_MIRROR")
if mirror == "true" {
mirrorGitRepo(repoDir, cloneUrl, mainBranch)
return
}
fmt.Printf(" Processing git repo %s from %s\n", repoDir, cloneUrl)
if _, err := os.Stat(repoDir); err != nil {
out, err := exec.Command("git", "clone", cloneUrl, repoDir).CombinedOutput()
if err != nil {
fmt.Println("Error during clone: " + string(out))
}
}
out, err := exec.Command("git", "-C", repoDir, "reset", "--hard").CombinedOutput()
if err != nil {
fmt.Println("Error during hard reset: " + string(out))
}
out, err = exec.Command("git", "-C", repoDir, "fetch", "--all", "--prune").CombinedOutput()
if err != nil {
fmt.Println("Error during fetch all: " + string(out))
}
out, err = exec.Command("git", "-C", repoDir, "checkout", mainBranch).CombinedOutput()
if err != nil {
fmt.Println("Error during checkout: " + string(out))
}
out, err = exec.Command("git", "-C", repoDir, "pull").CombinedOutput()
if err != nil {
fmt.Println("Error during pull: " + 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)
cloneOrSyncGitRepo(gitHubRepoPath, repo.SSHUrl, repo.DefaultBranch)
}
func processGitHubOrg(token, org string) {
fmt.Printf("Processing repos in GitHub Org %v\n", org)
repos := FetchGitHubRepos(token, org)
for _, repo := range repos {
processGitHubRepo(repo)
}
}
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 := "[email protected]:" + workspace + "/" + repo.Slug
err = os.MkdirAll(BitBucketProjectPath, 0750)
checkErr(err)
cloneOrSyncGitRepo(BitBucketRepoPath, BitBucketCloneUrl, repo.Mainbranch.Name)
}
func processBitBucketWorkspace(bbUser, bbAppPass, bbWorkspace string) {
fmt.Printf("Processing BitBucket workspace %v\n", bbWorkspace)
repos := FetchBitbucketRepos(bbUser, bbAppPass, bbWorkspace)
for _, repo := range repos {
processBitBucketRepo(bbWorkspace, repo)
}
}
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))
}