added features to change destination username via the name instead of user id

This commit is contained in:
Paul 2019-01-27 23:47:41 +01:00
parent d559356e57
commit 4386664cf5
3 changed files with 155 additions and 125 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.swp
github_to_gogs github_to_gogs
github_to_gogs.ini github_to_gogs.ini

View File

@ -20,14 +20,20 @@ var gogsconfig GogsConfig
var client *http.Client var client *http.Client
var resp *http.Response var resp *http.Response
var err error var err error
var repo []Githubrepo var repo []GitHubRepo
var repo_list []Githubrepo var repo_list []GitHubRepo
var gogsorg GogsOrg
type Githubrepo struct { type GitHubRepo struct {
Name string `json:"name"` Name string `json:"name"`
Clone_Url string `json:"clone_url"` Clone_Url string `json:"clone_url"`
} }
type GogsOrg struct {
Id int `json:"id"`
Username string `json:"username"`
}
type GogsMigrateRepo struct { type GogsMigrateRepo struct {
Name string Name string
Clone_Url string Clone_Url string
@ -51,7 +57,9 @@ type GitHubConfig struct {
type GogsConfig struct { type GogsConfig struct {
Uid int Uid int
Username string Username string
DestUsername string
RepoUrlTmpl string RepoUrlTmpl string
OrgsUrlTmpl string
MigrateUrl string MigrateUrl string
AuthToken string AuthToken string
ContentType string ContentType string
@ -60,6 +68,7 @@ type GogsConfig struct {
func main() { func main() {
GetConfig() GetConfig()
GetGogsUserUid()
GetReposFromGitHub() GetReposFromGitHub()
MigrateReposToGogs(repo_list) MigrateReposToGogs(repo_list)
} }
@ -103,11 +112,11 @@ func GetConfig() {
githubconfig.AuthPassword = github_section.Key("auth_password").String() githubconfig.AuthPassword = github_section.Key("auth_password").String()
githubconfig.ContentType = github_section.Key("content_type").String() githubconfig.ContentType = github_section.Key("content_type").String()
gogsconfig.Uid, err = gogs_section.Key("uid").Int() gogsconfig.Uid = -1
HandleError(err)
gogsconfig.Username = gogs_section.Key("username").String() gogsconfig.Username = gogs_section.Key("username").String()
gogsconfig.DestUsername = gogs_section.Key("dest_username").String()
gogsconfig.RepoUrlTmpl = gogs_section.Key("repo_url_tmpl").String() gogsconfig.RepoUrlTmpl = gogs_section.Key("repo_url_tmpl").String()
gogsconfig.OrgsUrlTmpl = gogs_section.Key("orgs_url_tmpl").String()
gogsconfig.MigrateUrl = gogs_section.Key("migrate_url").String() gogsconfig.MigrateUrl = gogs_section.Key("migrate_url").String()
gogsconfig.AuthToken = gogs_section.Key("auth_token").String() gogsconfig.AuthToken = gogs_section.Key("auth_token").String()
gogsconfig.ContentType = gogs_section.Key("content_type").String() gogsconfig.ContentType = gogs_section.Key("content_type").String()
@ -116,13 +125,13 @@ func GetConfig() {
} }
func CheckGogsIsExistingRepo(repo Githubrepo) bool { func CheckGogsIsExistingRepo(repo GitHubRepo) bool {
var isexists bool = false var isExists bool = false
client = &http.Client{} client = &http.Client{}
resp = &http.Response{} resp = &http.Response{}
var gogs_repo_url string = fmt.Sprintf(gogsconfig.RepoUrlTmpl,gogsconfig.Username,repo.Name) var gogs_repo_url string = fmt.Sprintf(gogsconfig.RepoUrlTmpl,gogsconfig.DestUsername,repo.Name)
req, err := http.NewRequest("GET", gogs_repo_url, nil) req, err := http.NewRequest("GET", gogs_repo_url, nil)
req.Header.Set("Authorization", gogsconfig.AuthToken) req.Header.Set("Authorization", gogsconfig.AuthToken)
@ -132,15 +141,34 @@ func CheckGogsIsExistingRepo(repo Githubrepo) bool {
HandleError(err) HandleError(err)
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
isexists = true isExists = true
} else { } else {
isexists = false isExists = false
} }
return isexists return isExists
} }
func MigrateReposToGogs(repo_list []Githubrepo) { func GetGogsUserUid() {
client = &http.Client{}
resp = &http.Response{}
var gogs_repo_url string = fmt.Sprintf(gogsconfig.OrgsUrlTmpl,gogsconfig.DestUsername)
req, err := http.NewRequest("GET", gogs_repo_url, nil)
req.Header.Set("Authorization", gogsconfig.AuthToken)
req.Header.Set("User-Agent", globalconfig.UserAgent)
resp, err = client.Do(req)
HandleError(err)
err = json.Unmarshal(*GetResponseBody(resp), &gogsorg)
HandleError(err)
gogsconfig.Uid = gogsorg.Id
}
func MigrateReposToGogs(repo_list []GitHubRepo) {
fmt.Println("Migrate repos to Gogs...") fmt.Println("Migrate repos to Gogs...")
client = &http.Client{} client = &http.Client{}

View File

@ -10,9 +10,10 @@ auth_password="pass"
content_type="application/x-www-form-urlencoded" content_type="application/x-www-form-urlencoded"
[gogs] [gogs]
uid=1
username="user" username="user"
dest_username="user_or_org"
repo_url_tmpl="https://gogs.example.com/api/v1/repos/%s/%s" repo_url_tmpl="https://gogs.example.com/api/v1/repos/%s/%s"
orgs_url_tmpl="https://git.paulbsd.com/api/v1/orgs/%s"
migrate_url="https://gogs.example.com/api/v1/repos/migrate" migrate_url="https://gogs.example.com/api/v1/repos/migrate"
auth_token="token xxxx" auth_token="token xxxx"
content_type="application/json" content_type="application/json"