/
GitHubFetcher.go
153 lines (136 loc) · 3.32 KB
/
GitHubFetcher.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
package main
import (
"context"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
var debug bool
// SetDebug turns on debugging output on this library
func SetDebug(d bool) {
debug = d
}
// Sync pull down all repos for an orgnaization
func Sync(orgname string, path string, clone bool, update bool, remove bool, useHTTPS bool) {
allRepos := getAllRepos(orgname)
allReposMap := make(map[string]bool)
for _, repo := range allRepos {
if !repoExistsLocally(repo, path) {
if clone {
_, err := doClone(repo, path, useHTTPS)
check(err)
}
} else if update {
_, err := doUpdate(repo, path)
check(err)
}
allReposMap[*repo.Name] = true
}
if remove {
cleanup(path, allReposMap)
}
}
func cleanup(path string, repos map[string]bool) {
files := getDirectories(path)
for _, directory := range files {
if !directory.IsDir() {
if debug {
log.Printf("Skipping %s as it is not a directory", directory.Name())
}
continue
}
// check if the directory exists in the map
if _, b := repos[directory.Name()]; !b {
if debug {
log.Printf("Removing %s", directory.Name())
}
os.RemoveAll(filepath.Join(path, directory.Name()))
} else if debug {
log.Printf("Skipping %s as it is found in the organization", directory.Name())
}
}
}
func getDirectories(path string) []os.FileInfo {
// Get a list of directories off this
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal("Error reading the directory: " + path)
}
return files
}
func getAllRepos(orgname string) []github.Repository {
client := getClient()
ctx := context.TODO()
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
// get all pages of results
var allRepos []github.Repository
for {
var repos []*github.Repository
var resp *github.Response
var err error
operation := func() error {
repos, resp, err = client.Repositories.ListByOrg(ctx, orgname, opt)
return err
}
err = makeGitHubCall(operation)
check(err)
for _, repo := range repos {
allRepos = append(allRepos, *repo)
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
if debug {
log.Printf("Found %d repo(s) for the organization %s", len(allRepos), orgname)
}
return allRepos
}
func getClient() *github.Client {
var tc *http.Client
envToken := os.Getenv("GITHUB_TOKEN")
if len(envToken) > 0 {
token := oauth2.Token{AccessToken: envToken}
ts := oauth2.StaticTokenSource(&token)
tc = oauth2.NewClient(oauth2.NoContext, ts)
}
client := github.NewClient(tc)
return client
}
func repoExistsLocally(repo github.Repository, path string) bool {
fullPath := filepath.Join(path, *repo.Name)
_, err := os.Stat(fullPath)
return err == nil
}
func doUpdate(repo github.Repository, path string) (int, error) {
if debug {
log.Printf("Updating %s", *repo.Name)
}
directory := filepath.Join(path, *repo.Name)
return runWithRetries(directory, "git", "pull")
}
func doClone(repo github.Repository, path string, useHTTPS bool) (int, error) {
var url string
if useHTTPS {
url = *repo.CloneURL
} else {
url = *repo.SSHURL
}
if debug {
log.Printf("Cloning %s (%s)", *repo.Name, url)
}
return runWithRetries(path, "git", "clone", url)
}
func check(e error) {
if e != nil {
log.Println(e)
panic(e)
}
}