-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-usr.go
112 lines (103 loc) · 3.09 KB
/
github-usr.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
package main
import (
"code.gitea.io/sdk/gitea"
"context"
"fmt"
"github.com/google/go-github/github"
"github.com/zenthangplus/goccm"
"net/http"
"os"
)
func migrateUsrGithubToGitea(githubAccName, githubToken, giteaHost, giteaToken string) {
ctx := context.Background()
//ts := oauth2.StaticTokenSource(
// &oauth2.Token{AccessToken: githubToken},
//)
//tc := oauth2.NewClient(ctx, ts)
//
//githubClient := github.NewClient(tc)
githubClient := github.NewClient(&http.Client{})
orgOption := gitea.CreateOrgOption{}
githubUsrObj, _, err := githubClient.Users.Get(ctx, githubAccName)
if err != nil {
fmt.Println("Org not exists")
fmt.Println(err)
} else {
fmt.Println(githubUsrObj)
orgOption = gitea.CreateOrgOption{
Name: *githubUsrObj.Login,
//FullName: *githubUsrObj.
//Description: ,
Website: *githubUsrObj.ReposURL,
//Location: *githubUsrObj.Location,
Visibility: gitea.VisibleTypeLimited,
}
}
// get all repositories from organization
var allRepos []*github.Repository
opt := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
for {
repos, resp, err := githubClient.Repositories.List(ctx, *githubUsrObj.Login, opt)
//fmt.Println(repos)
if err != nil {
fmt.Println(err)
//os.Exit(1)
return
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
fmt.Println("read repo success")
// avatar 다운로드 수정 할까했는데 gitea에 변경api가 없음
//err := downloadFile(*githubOrgObj.AvatarURL, "tmpavatar")
//if err != nil {
// log.Fatal(err)
//}
giteaClient, _ := gitea.NewClient("https://"+giteaHost+"/", gitea.SetToken(giteaToken))
// create org if not exists
giteaOrgObj, res, err := giteaClient.GetOrg(*githubUsrObj.Login)
fmt.Println(giteaOrgObj, res, err)
if err != nil && err.Error() == "404 Not Found" {
fmt.Println("organization not exists in gitea")
fmt.Println("create org : " + *githubUsrObj.Login)
giteaOrgObj, res, err = giteaClient.CreateOrg(orgOption)
if err != nil {
fmt.Println("exit. org id is ", giteaOrgObj.ID)
os.Exit(1)
return
}
}
c := goccm.New(10)
// https://api.github.com/orgs/wikibook !?!!?!? https://api.github.com/users/wikibook ?!?!?!?!
for i := 0; i < len(allRepos); i++ {
c.Wait()
fmt.Printf("repo name %d/%d id: %d %s\n", i, len(allRepos), giteaOrgObj.ID, *allRepos[i].Name)
description := ""
if allRepos[i].Description != nil { // will throw a nil pointer error if description is passed directly to the below struct
description = *allRepos[i].Description
}
//res, err := giteaClient.DeleteRepo("archmagece", *allRepos[i].Name)
//if err != nil {
// fmt.Println(res)
// fmt.Println("errorr")
//}
go func(i int, description string) {
repo, _, _ := giteaClient.MigrateRepo(gitea.MigrateRepoOption{
CloneAddr: *allRepos[i].CloneURL,
RepoOwnerID: giteaOrgObj.ID,
RepoName: *allRepos[i].Name,
Mirror: true,
Private: false,
Description: description,
})
fmt.Println("finish", repo.Name, repo.CloneURL)
c.Done()
}(i, description)
}
c.WaitAllDone()
}