added features to change destination username via the name instead of user id
This commit is contained in:
parent
d559356e57
commit
4386664cf5
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
|
*.swp
|
||||||
github_to_gogs
|
github_to_gogs
|
||||||
github_to_gogs.ini
|
github_to_gogs.ini
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
"time"
|
"time"
|
||||||
"os"
|
"os"
|
||||||
"flag"
|
"flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalconfig GlobalConfig
|
var globalconfig GlobalConfig
|
||||||
@ -20,193 +20,221 @@ 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
|
||||||
Uid int
|
Uid int
|
||||||
Mirror bool
|
Mirror bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type GlobalConfig struct {
|
type GlobalConfig struct {
|
||||||
RequestTimeout time.Duration
|
RequestTimeout time.Duration
|
||||||
UserAgent string
|
UserAgent string
|
||||||
}
|
}
|
||||||
|
|
||||||
type GitHubConfig struct {
|
type GitHubConfig struct {
|
||||||
StarsPages string
|
StarsPages string
|
||||||
MaxPagesNumber int
|
MaxPagesNumber int
|
||||||
AuthUsername string
|
AuthUsername string
|
||||||
AuthPassword string
|
AuthPassword string
|
||||||
ContentType string
|
ContentType string
|
||||||
}
|
}
|
||||||
|
|
||||||
type GogsConfig struct {
|
type GogsConfig struct {
|
||||||
Uid int
|
Uid int
|
||||||
Username string
|
Username string
|
||||||
RepoUrlTmpl string
|
DestUsername string
|
||||||
MigrateUrl string
|
RepoUrlTmpl string
|
||||||
AuthToken string
|
OrgsUrlTmpl string
|
||||||
ContentType string
|
MigrateUrl string
|
||||||
Mirror bool
|
AuthToken string
|
||||||
|
ContentType string
|
||||||
|
Mirror bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
GetConfig()
|
GetConfig()
|
||||||
GetReposFromGitHub()
|
GetGogsUserUid()
|
||||||
MigrateReposToGogs(repo_list)
|
GetReposFromGitHub()
|
||||||
|
MigrateReposToGogs(repo_list)
|
||||||
}
|
}
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
fmt.Fprintf(os.Stderr, "usage: github_to_gogs [inputfile]\n")
|
fmt.Fprintf(os.Stderr, "usage: github_to_gogs [inputfile]\n")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfig() {
|
func GetConfig() {
|
||||||
flag.Usage = usage
|
flag.Usage = usage
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
var configfile string
|
var configfile string
|
||||||
|
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
configfile = os.Args[1]
|
configfile = os.Args[1]
|
||||||
} else {
|
} else {
|
||||||
usage()
|
usage()
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
config, err := ini.Load(configfile)
|
config, err := ini.Load(configfile)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
global_section := config.Section("global")
|
global_section := config.Section("global")
|
||||||
github_section := config.Section("github")
|
github_section := config.Section("github")
|
||||||
gogs_section := config.Section("gogs")
|
gogs_section := config.Section("gogs")
|
||||||
|
|
||||||
globalconfig.UserAgent = global_section.Key("user_agent").String()
|
globalconfig.UserAgent = global_section.Key("user_agent").String()
|
||||||
globalconfig.RequestTimeout, err = global_section.Key("request_timeout").Duration()
|
globalconfig.RequestTimeout, err = global_section.Key("request_timeout").Duration()
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
githubconfig.StarsPages = github_section.Key("stars_pages").String()
|
githubconfig.StarsPages = github_section.Key("stars_pages").String()
|
||||||
githubconfig.MaxPagesNumber, err = github_section.Key("max_pages_number").Int()
|
githubconfig.MaxPagesNumber, err = github_section.Key("max_pages_number").Int()
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
githubconfig.AuthUsername = github_section.Key("auth_username").String()
|
githubconfig.AuthUsername = github_section.Key("auth_username").String()
|
||||||
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.DestUsername = gogs_section.Key("dest_username").String()
|
||||||
gogsconfig.Username = gogs_section.Key("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()
|
||||||
gogsconfig.Mirror, err = gogs_section.Key("mirror").Bool()
|
gogsconfig.Mirror, err = gogs_section.Key("mirror").Bool()
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
resp, err = client.Do(req)
|
||||||
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() {
|
||||||
fmt.Println("Migrate repos to Gogs...")
|
client = &http.Client{}
|
||||||
|
resp = &http.Response{}
|
||||||
|
|
||||||
client = &http.Client{}
|
var gogs_repo_url string = fmt.Sprintf(gogsconfig.OrgsUrlTmpl,gogsconfig.DestUsername)
|
||||||
resp = &http.Response{}
|
|
||||||
|
|
||||||
for _,elem := range(repo_list) {
|
req, err := http.NewRequest("GET", gogs_repo_url, nil)
|
||||||
if !CheckGogsIsExistingRepo(elem) {
|
req.Header.Set("Authorization", gogsconfig.AuthToken)
|
||||||
jsondata := fmt.Sprintf(`{"uid" : %d, "repo_name" : "%s" , "mirror" : %v, "clone_addr" : "%s"}`, gogsconfig.Uid, elem.Name, true, elem.Clone_Url)
|
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
||||||
fmt.Println(fmt.Sprintf("OK : Send migrating instruction for %s to gogs webservice",elem.Name))
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", gogsconfig.MigrateUrl, bytes.NewBufferString(jsondata))
|
resp, err = client.Do(req)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
req.Header.Add("Content-Type", gogsconfig.ContentType)
|
err = json.Unmarshal(*GetResponseBody(resp), &gogsorg)
|
||||||
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
HandleError(err)
|
||||||
req.Header.Set("Authorization", gogsconfig.AuthToken)
|
|
||||||
|
|
||||||
client.Timeout=globalconfig.RequestTimeout
|
gogsconfig.Uid = gogsorg.Id
|
||||||
|
}
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
func MigrateReposToGogs(repo_list []GitHubRepo) {
|
||||||
HandleError(err)
|
fmt.Println("Migrate repos to Gogs...")
|
||||||
} else {
|
|
||||||
fmt.Println(fmt.Sprintf("KO : Not migrating, %s exists ",elem.Name))
|
client = &http.Client{}
|
||||||
}
|
resp = &http.Response{}
|
||||||
}
|
|
||||||
|
for _,elem := range(repo_list) {
|
||||||
|
if !CheckGogsIsExistingRepo(elem) {
|
||||||
|
jsondata := fmt.Sprintf(`{"uid" : %d, "repo_name" : "%s" , "mirror" : %v, "clone_addr" : "%s"}`, gogsconfig.Uid, elem.Name, true, elem.Clone_Url)
|
||||||
|
fmt.Println(fmt.Sprintf("OK : Send migrating instruction for %s to gogs webservice",elem.Name))
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", gogsconfig.MigrateUrl, bytes.NewBufferString(jsondata))
|
||||||
|
HandleError(err)
|
||||||
|
|
||||||
|
req.Header.Add("Content-Type", gogsconfig.ContentType)
|
||||||
|
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
||||||
|
req.Header.Set("Authorization", gogsconfig.AuthToken)
|
||||||
|
|
||||||
|
client.Timeout = globalconfig.RequestTimeout
|
||||||
|
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
HandleError(err)
|
||||||
|
} else {
|
||||||
|
fmt.Println(fmt.Sprintf("KO : Not migrating, %s exists ",elem.Name))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetReposFromGitHub() {
|
func GetReposFromGitHub() {
|
||||||
for num := 1; num <= githubconfig.MaxPagesNumber; num++ {
|
for num := 1; num <= githubconfig.MaxPagesNumber; num++ {
|
||||||
url := fmt.Sprintf(githubconfig.StarsPages, githubconfig.AuthUsername, num)
|
url := fmt.Sprintf(githubconfig.StarsPages, githubconfig.AuthUsername, num)
|
||||||
|
|
||||||
InvokeGitHub(url)
|
InvokeGitHub(url)
|
||||||
|
|
||||||
err = json.Unmarshal(*GetResponseBody(resp), &repo)
|
err = json.Unmarshal(*GetResponseBody(resp), &repo)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
for _,elem := range repo {
|
for _,elem := range repo {
|
||||||
repo_list = append(repo_list, elem)
|
repo_list = append(repo_list, elem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("Repositories from Github fetched !")
|
fmt.Println("Repositories from Github fetched !")
|
||||||
}
|
}
|
||||||
|
|
||||||
func InvokeGitHub(url string) {
|
func InvokeGitHub(url string) {
|
||||||
client = &http.Client{}
|
client = &http.Client{}
|
||||||
resp = &http.Response{}
|
resp = &http.Response{}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
req.Header.Add("Content-Type", githubconfig.ContentType)
|
req.Header.Add("Content-Type", githubconfig.ContentType)
|
||||||
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
||||||
req.SetBasicAuth(githubconfig.AuthUsername,githubconfig.AuthPassword)
|
req.SetBasicAuth(githubconfig.AuthUsername,githubconfig.AuthPassword)
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
resp, err = client.Do(req)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetResponseBody(resp *http.Response) *[]byte {
|
func GetResponseBody(resp *http.Response) *[]byte {
|
||||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
return &bodyBytes
|
return &bodyBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleError(err error) {
|
func HandleError(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
|
Loading…
Reference in New Issue
Block a user