Backup Github Repos

Menu:

Requirement

  • Github Token
  • curl (this is Command)

GitHub GraphQL API

GitHub GraphQL API

My Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Type queries into this side of the screen, and you will 
# see intelligent typeaheads aware of the current GraphQL type schema,
# live syntax, and validation errors highlighted within the text.

# We'll get you started with a simple query showing your username!
query {
viewer {
repositories(isFork:false ,last:50){
totalCount
nodes{
name
}
}
}
}

Test Example

Change token for yourself token.

1
curl curl -H "Authorization: token your_token" -H  "Content-Type:application/json" -d '{"query": "{ viewer { repositories(isFork:false last: 50) { totalCount nodes { name }}}}"}' https://api.github.com/graphql

Output:

1
{"data":{"viewer":{"repositories":{"totalCount":1,"nodes":[{"name":"ka1i.github.io"}]}}}}

Startup Coding

Global setting

1
2
3
4
5
6
var (
user = "<Github Username>"
token = "<Github Token>" //Setting -> Developer settings -> Personal access tokens add token
bareurl = "https://github.com/"
barepath = "./GitRepos/"
)

GitHub GraphQL API Use github.com/shurcooL/githubv4

1
2
3
4
5
6
7
8
9
10
11
// QueryRepositories .
var QueryRepositories struct {
Viewer struct {
Repositorys struct {
TotalCount githubv4.Int
Nodes []struct {
Name githubv4.String
}
} `graphql:"repositories(isFork: false, last: 50)"`
}
}

Get repos list , use OAuth2

1
2
3
4
5
6
7
8
9
10
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: your_token},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
err := client.Query(context.Background(), &QueryRepositories, nil)
if err != nil {
log.Fatal(err)
}
log.Printf("Repositories TotalCount: %d", QueryRepositories.Viewer.Repositorys.TotalCount)

Traverse deal with repos

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
for items := 0; items < int(QueryRepositories.Viewer.Repositorys.TotalCount); items++ {
Repositories := QueryRepositories.Viewer.Repositorys.Nodes[items].Name
ReposURL := QueryRepositories.Viewer.Repositorys.Nodes[items].URL
log.Printf("Check: %s ===> %s", Repositories, ReposURL)
ReposPath := string(ReposURL[len(bareurl):])
// Create Repositories
if !FileExist(path.Join(Getpath(), ReposPath)) {
_, err := git.PlainClone(barepath+ReposPath, false, &git.CloneOptions{
URL: string(ReposURL),
Progress: os.Stdout,
Auth: &githttp.BasicAuth{
Username: user,
Password: token,
},
})
if err != nil {
log.Fatal(err)
// Warning use this function
//os.RemoveAll(barepath+ReposPath)
}
} else {
r, _ := git.PlainOpen(ReposPath)
w, _ := r.Worktree()
w.Pull(&git.PullOptions{
RemoteName: "origin",
Auth: &githttp.BasicAuth{
Username: user,
Password: token,
},
})
ref, _ := r.Head()
commit, _ := r.CommitObject(ref.Hash())
log.Printf("Pulling Repository: %s\n%s", ReposURL, commit)
}
}

3rd part code

1
2
3
4
5
6
7
8
9
10
11
12
13
func Getpath() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Println("error")
}
return strings.Replace(dir, "\\", "/", -1)
}

// FileExist func .
func FileExist(path string) bool {
_, err := os.Lstat(path)
return !os.IsNotExist(err)
}