reworked project structure
This commit is contained in:
parent
2d2ba028db
commit
6af4866f2d
196
functions.go
196
functions.go
@ -14,12 +14,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GetConfig fetch configuration
|
// GetConfig fetch configuration
|
||||||
func GetConfig(configfile string, config *Config, globalconfig *GlobalConfig, githubconfig *GitHubConfig, gogsconfig *GogsConfig) {
|
func GetConfig(config *Config) {
|
||||||
|
var configfile string
|
||||||
|
|
||||||
|
var globalconfig GlobalConfig
|
||||||
|
var githubconfig GitHubConfig
|
||||||
|
var gogsconfig GogsConfig
|
||||||
|
|
||||||
flag.Usage = Usage
|
flag.Usage = Usage
|
||||||
|
flag.StringVar(&configfile, "configfile", "github-to-gogs.ini", "config file to use with github-to-gogs section")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
cfg, err := ini.Load(configfile)
|
cfg, err := ini.Load(configfile)
|
||||||
HandleError(err)
|
HandleFatalError(err)
|
||||||
|
|
||||||
globalsection := cfg.Section("global")
|
globalsection := cfg.Section("global")
|
||||||
githubsection := cfg.Section("github")
|
githubsection := cfg.Section("github")
|
||||||
@ -48,96 +55,22 @@ func GetConfig(configfile string, config *Config, globalconfig *GlobalConfig, gi
|
|||||||
gogsconfig.Mirror, err = gogssection.Key("mirror").Bool()
|
gogsconfig.Mirror, err = gogssection.Key("mirror").Bool()
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
*config = Config{globalconfig: *globalconfig,
|
*config = Config{globalconfig: globalconfig,
|
||||||
githubconfig: *githubconfig,
|
githubconfig: githubconfig,
|
||||||
gogsconfig: *gogsconfig}
|
gogsconfig: gogsconfig}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckGogsIsExistingRepo ...
|
// GetReposFromGitHub get starred repositories from github
|
||||||
func CheckGogsIsExistingRepo(repo GitHubRepo) bool {
|
func GetReposFromGitHub(config *Config) []GitHubRepo {
|
||||||
var isExists bool
|
|
||||||
|
|
||||||
client = &http.Client{}
|
|
||||||
resp = &http.Response{}
|
|
||||||
|
|
||||||
gogsrepourl := fmt.Sprintf(gogsconfig.RepoURLTmpl, gogsconfig.DestUsername, repo.Name)
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", gogsrepourl, nil)
|
|
||||||
req.Header.Set("Authorization", gogsconfig.AuthToken)
|
|
||||||
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
|
||||||
HandleError(err)
|
|
||||||
|
|
||||||
if resp.StatusCode == 200 {
|
|
||||||
isExists = true
|
|
||||||
} else {
|
|
||||||
isExists = false
|
|
||||||
}
|
|
||||||
|
|
||||||
return isExists
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetGogsUserUID ...
|
|
||||||
func GetGogsUserUID(config *Config) {
|
|
||||||
var gogsorg GogsOrg
|
|
||||||
client = &http.Client{}
|
|
||||||
resp = &http.Response{}
|
|
||||||
|
|
||||||
gogsrepourl := fmt.Sprintf(config.gogsconfig.OrgsURLTmpl, config.gogsconfig.DestUsername)
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", gogsrepourl, nil)
|
|
||||||
req.Header.Set("Authorization", config.gogsconfig.AuthToken)
|
|
||||||
req.Header.Set("User-Agent", config.globalconfig.UserAgent)
|
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
|
||||||
HandleError(err)
|
|
||||||
|
|
||||||
err = json.Unmarshal(*GetResponseBody(resp), &gogsorg)
|
|
||||||
HandleError(err)
|
|
||||||
|
|
||||||
gogsconfig.UID = gogsorg.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// MigrateReposToGogs ...
|
|
||||||
func MigrateReposToGogs(repolist []GitHubRepo) {
|
|
||||||
fmt.Println("Migrate repos to Gogs...")
|
|
||||||
|
|
||||||
client = &http.Client{}
|
|
||||||
resp = &http.Response{}
|
|
||||||
|
|
||||||
for _, elem := range repolist {
|
|
||||||
if !CheckGogsIsExistingRepo(elem) {
|
|
||||||
jsondata := fmt.Sprintf(`{"uid" : %d, "repo_name" : "%s" , "mirror" : %v, "clone_addr" : "%s"}`, gogsconfig.UID, elem.Name, true, elem.CloneURL)
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetReposFromGitHub ...
|
|
||||||
func GetReposFromGitHub() []GitHubRepo {
|
|
||||||
var repolist []GitHubRepo
|
var repolist []GitHubRepo
|
||||||
var repo []GitHubRepo
|
var repo []GitHubRepo
|
||||||
for num := 1; num <= githubconfig.MaxPagesNumber; num++ {
|
|
||||||
url := fmt.Sprintf(githubconfig.StarsPages, githubconfig.AuthUsername, num)
|
|
||||||
|
|
||||||
InvokeGitHub(url)
|
for num := 1; num <= config.githubconfig.MaxPagesNumber; num++ {
|
||||||
|
url := fmt.Sprintf(config.githubconfig.StarsPages, config.githubconfig.AuthUsername, num)
|
||||||
|
|
||||||
err = json.Unmarshal(*GetResponseBody(resp), &repo)
|
resp := InvokeGitHub(config, url)
|
||||||
|
|
||||||
|
err := json.Unmarshal(*GetResponseBody(resp), &repo)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
for _, elem := range repo {
|
for _, elem := range repo {
|
||||||
@ -149,28 +82,101 @@ func GetReposFromGitHub() []GitHubRepo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// InvokeGitHub ...
|
// InvokeGitHub ...
|
||||||
func InvokeGitHub(url string) {
|
func InvokeGitHub(config *Config, url string) *http.Response {
|
||||||
client = &http.Client{}
|
|
||||||
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", config.githubconfig.ContentType)
|
||||||
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
req.Header.Set("User-Agent", config.globalconfig.UserAgent)
|
||||||
req.SetBasicAuth(githubconfig.AuthUsername, githubconfig.AuthPassword)
|
req.SetBasicAuth(config.githubconfig.AuthUsername, config.githubconfig.AuthPassword)
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
HandleError(err)
|
HandleError(err)
|
||||||
|
|
||||||
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetResponseBody ...
|
// GetResponseBody converts http responses bodies as bytes slice
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckGogsExistingRepo checks if there's an existing repository in gogs
|
||||||
|
func CheckGogsExistingRepo(config *Config, repo GitHubRepo) bool {
|
||||||
|
var isExists bool
|
||||||
|
|
||||||
|
gogsrepourl := fmt.Sprintf(config.gogsconfig.RepoURLTmpl, config.gogsconfig.DestUsername, repo.Name)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", gogsrepourl, nil)
|
||||||
|
req.Header.Set("Authorization", config.gogsconfig.AuthToken)
|
||||||
|
req.Header.Set("User-Agent", config.globalconfig.UserAgent)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
HandleError(err)
|
||||||
|
|
||||||
|
if resp.StatusCode == 200 {
|
||||||
|
isExists = true
|
||||||
|
} else {
|
||||||
|
isExists = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return isExists
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGogsUserUID get the logued user identifier
|
||||||
|
func GetGogsUserUID(config *Config) {
|
||||||
|
var gogsorg GogsOrg
|
||||||
|
|
||||||
|
gogsrepourl := fmt.Sprintf(config.gogsconfig.OrgsURLTmpl, config.gogsconfig.DestUsername)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", gogsrepourl, nil)
|
||||||
|
req.Header.Set("Authorization", config.gogsconfig.AuthToken)
|
||||||
|
req.Header.Set("User-Agent", config.globalconfig.UserAgent)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
HandleError(err)
|
||||||
|
|
||||||
|
err = json.Unmarshal(*GetResponseBody(resp), &gogsorg)
|
||||||
|
HandleError(err)
|
||||||
|
|
||||||
|
config.gogsconfig.UID = gogsorg.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// MigrateReposToGogs migrates input repositories to gogs
|
||||||
|
func MigrateReposToGogs(config *Config, repolist []GitHubRepo) {
|
||||||
|
fmt.Println("Migrate repos to Gogs...")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
for _, elem := range repolist {
|
||||||
|
if !CheckGogsExistingRepo(config, elem) {
|
||||||
|
jsondata := fmt.Sprintf(`{"uid" : %d, "repo_name" : "%s" , "mirror" : %v, "clone_addr" : "%s"}`, config.gogsconfig.UID, elem.Name, true, elem.CloneURL)
|
||||||
|
fmt.Println(fmt.Sprintf("Migrating repo %s to gogs rest api", elem.Name))
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", config.gogsconfig.MigrateURL, bytes.NewBufferString(jsondata))
|
||||||
|
HandleError(err)
|
||||||
|
|
||||||
|
req.Header.Add("Content-Type", config.gogsconfig.ContentType)
|
||||||
|
req.Header.Set("User-Agent", config.globalconfig.UserAgent)
|
||||||
|
req.Header.Set("Authorization", config.gogsconfig.AuthToken)
|
||||||
|
|
||||||
|
client.Timeout = config.globalconfig.RequestTimeout
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
HandleError(err)
|
||||||
|
|
||||||
|
fmt.Println(resp.StatusCode)
|
||||||
|
} else {
|
||||||
|
fmt.Println(fmt.Sprintf("Not migrating, %s exists in gogs", elem.Name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// HandleError handles errors to return err
|
// HandleError handles errors to return err
|
||||||
func HandleError(err error) error {
|
func HandleError(err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,25 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
var config Config
|
|
||||||
var globalconfig GlobalConfig
|
|
||||||
var githubconfig GitHubConfig
|
|
||||||
var gogsconfig GogsConfig
|
|
||||||
|
|
||||||
var err error
|
|
||||||
var client *http.Client
|
|
||||||
var resp *http.Response
|
|
||||||
var configpath string
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.StringVar(&configpath, "configfile", "github_to_gogs.ini", "config file to use with github_to_gogs section")
|
var config Config
|
||||||
flag.Parse()
|
GetConfig(&config)
|
||||||
GetConfig(configpath, &config, &globalconfig, &githubconfig, &gogsconfig)
|
|
||||||
GetGogsUserUID(&config)
|
GetGogsUserUID(&config)
|
||||||
repolist := GetReposFromGitHub()
|
repolist := GetReposFromGitHub(&config)
|
||||||
MigrateReposToGogs(repolist)
|
MigrateReposToGogs(&config, repolist)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user