195 lines
5.1 KiB
Go
195 lines
5.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
|
||
|
"gopkg.in/ini.v1"
|
||
|
)
|
||
|
|
||
|
// GetConfig fetch configuration
|
||
|
func GetConfig(configfile string, config *Config, globalconfig *GlobalConfig, githubconfig *GitHubConfig, gogsconfig *GogsConfig) {
|
||
|
flag.Usage = Usage
|
||
|
flag.Parse()
|
||
|
|
||
|
cfg, err := ini.Load(configfile)
|
||
|
HandleError(err)
|
||
|
|
||
|
globalsection := cfg.Section("global")
|
||
|
githubsection := cfg.Section("github")
|
||
|
gogssection := cfg.Section("gogs")
|
||
|
|
||
|
globalconfig.UserAgent = globalsection.Key("user_agent").String()
|
||
|
globalconfig.RequestTimeout, err = globalsection.Key("request_timeout").Duration()
|
||
|
HandleError(err)
|
||
|
|
||
|
githubconfig.StarsPages = githubsection.Key("stars_pages").String()
|
||
|
githubconfig.MaxPagesNumber, err = githubsection.Key("max_pages_number").Int()
|
||
|
HandleError(err)
|
||
|
|
||
|
githubconfig.AuthUsername = githubsection.Key("auth_username").String()
|
||
|
githubconfig.AuthPassword = githubsection.Key("auth_password").String()
|
||
|
githubconfig.ContentType = githubsection.Key("content_type").String()
|
||
|
|
||
|
gogsconfig.UID = -1
|
||
|
gogsconfig.Username = gogssection.Key("username").String()
|
||
|
gogsconfig.DestUsername = gogssection.Key("dest_username").String()
|
||
|
gogsconfig.RepoURLTmpl = gogssection.Key("repo_url_tmpl").String()
|
||
|
gogsconfig.OrgsURLTmpl = gogssection.Key("orgs_url_tmpl").String()
|
||
|
gogsconfig.MigrateURL = gogssection.Key("migrate_url").String()
|
||
|
gogsconfig.AuthToken = gogssection.Key("auth_token").String()
|
||
|
gogsconfig.ContentType = gogssection.Key("content_type").String()
|
||
|
gogsconfig.Mirror, err = gogssection.Key("mirror").Bool()
|
||
|
HandleError(err)
|
||
|
|
||
|
*config = Config{globalconfig: *globalconfig,
|
||
|
githubconfig: *githubconfig,
|
||
|
gogsconfig: *gogsconfig}
|
||
|
}
|
||
|
|
||
|
// CheckGogsIsExistingRepo ...
|
||
|
func CheckGogsIsExistingRepo(repo GitHubRepo) bool {
|
||
|
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 repo []GitHubRepo
|
||
|
for num := 1; num <= githubconfig.MaxPagesNumber; num++ {
|
||
|
url := fmt.Sprintf(githubconfig.StarsPages, githubconfig.AuthUsername, num)
|
||
|
|
||
|
InvokeGitHub(url)
|
||
|
|
||
|
err = json.Unmarshal(*GetResponseBody(resp), &repo)
|
||
|
HandleError(err)
|
||
|
|
||
|
for _, elem := range repo {
|
||
|
repolist = append(repolist, elem)
|
||
|
}
|
||
|
}
|
||
|
fmt.Println("Repositories from Github fetched !")
|
||
|
return repolist
|
||
|
}
|
||
|
|
||
|
// InvokeGitHub ...
|
||
|
func InvokeGitHub(url string) {
|
||
|
client = &http.Client{}
|
||
|
resp = &http.Response{}
|
||
|
|
||
|
req, err := http.NewRequest("GET", url, nil)
|
||
|
HandleError(err)
|
||
|
|
||
|
req.Header.Add("Content-Type", githubconfig.ContentType)
|
||
|
req.Header.Set("User-Agent", globalconfig.UserAgent)
|
||
|
req.SetBasicAuth(githubconfig.AuthUsername, githubconfig.AuthPassword)
|
||
|
|
||
|
resp, err = client.Do(req)
|
||
|
HandleError(err)
|
||
|
}
|
||
|
|
||
|
// GetResponseBody ...
|
||
|
func GetResponseBody(resp *http.Response) *[]byte {
|
||
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||
|
HandleError(err)
|
||
|
return &bodyBytes
|
||
|
}
|
||
|
|
||
|
// HandleError handles errors to return err
|
||
|
func HandleError(err error) error {
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// HandleFatalError fatal errors
|
||
|
func HandleFatalError(err error) {
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
os.Exit(2)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Usage displays possible arguments
|
||
|
func Usage() {
|
||
|
flag.PrintDefaults()
|
||
|
os.Exit(1)
|
||
|
}
|