-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-org.go
128 lines (118 loc) · 3.55 KB
/
github-org.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
package main
import (
"code.gitea.io/sdk/gitea"
"context"
"fmt"
"github.com/google/go-github/github"
"github.com/zenthangplus/goccm"
"net/http"
"os"
)
func migrateOrgGithubToGitea(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{}
githubOrgObj, _, err := githubClient.Organizations.Get(ctx, githubAccName)
if err != nil {
fmt.Println("Org not exists")
fmt.Println(err)
} else {
fmt.Println(githubOrgObj)
description := ""
if githubOrgObj.Description != nil { // will throw a nil pointer error if description is passed directly to the below struct
description = *githubOrgObj.Description
} else {
description = ""
}
orgOption = gitea.CreateOrgOption{
Name: *githubOrgObj.Login,
//FullName: *githubOrgObj.
Description: description,
Website: *githubOrgObj.HTMLURL,
//Location: *githubOrgObj.Location,
Visibility: gitea.VisibleTypeLimited,
}
}
// get all repositories from organization
var allRepos []*github.Repository
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
fmt.Println("read repo before", *githubOrgObj.Login)
for {
repos, resp, err := githubClient.Repositories.ListByOrg(ctx, *githubOrgObj.Login, opt)
//fmt.Println(repos)
if err != nil {
fmt.Println(err)
fmt.Println(resp)
os.Exit(1)
}
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, resp, err := giteaClient.GetOrg(*githubOrgObj.Login)
fmt.Println(giteaOrgObj, resp, err)
if err != nil {
if err.Error() == "404 Not Found" {
fmt.Println("organization not exists in gitea")
fmt.Println("create org : " + *githubOrgObj.Login)
giteaOrgObj, resp, err = giteaClient.CreateOrg(orgOption)
if err != nil {
fmt.Println("exit. org id is ", giteaOrgObj.ID)
os.Exit(1)
}
}
}
//if giteaOrgObj.ID == 1 {
// fmt.Println("exit. org id is ", giteaOrgObj.ID)
// os.Exit(1)
// return
//}
c := goccm.New(10)
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
}
//giteaClient.TransferRepo("archmagece", *allRepos[i].Name, gitea.TransferRepoOption{
// NewOwner: giteaOrgObj.UserName,
//})
//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()
}