241 lines
5.7 KiB
Go
241 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"log"
|
|
"bytes"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"gopkg.in/ini.v1"
|
|
"time"
|
|
"os"
|
|
"flag"
|
|
)
|
|
|
|
var globalconfig GlobalConfig
|
|
var githubconfig GitHubConfig
|
|
var gogsconfig GogsConfig
|
|
|
|
var client *http.Client
|
|
var resp *http.Response
|
|
var err error
|
|
var repo []GitHubRepo
|
|
var repo_list []GitHubRepo
|
|
var gogsorg GogsOrg
|
|
|
|
type GitHubRepo struct {
|
|
Name string `json:"name"`
|
|
Clone_Url string `json:"clone_url"`
|
|
}
|
|
|
|
type GogsOrg struct {
|
|
Id int `json:"id"`
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
type GogsMigrateRepo struct {
|
|
Name string
|
|
Clone_Url string
|
|
Uid int
|
|
Mirror bool
|
|
}
|
|
|
|
type GlobalConfig struct {
|
|
RequestTimeout time.Duration
|
|
UserAgent string
|
|
}
|
|
|
|
type GitHubConfig struct {
|
|
StarsPages string
|
|
MaxPagesNumber int
|
|
AuthUsername string
|
|
AuthPassword string
|
|
ContentType string
|
|
}
|
|
|
|
type GogsConfig struct {
|
|
Uid int
|
|
Username string
|
|
DestUsername string
|
|
RepoUrlTmpl string
|
|
OrgsUrlTmpl string
|
|
MigrateUrl string
|
|
AuthToken string
|
|
ContentType string
|
|
Mirror bool
|
|
}
|
|
|
|
func main() {
|
|
GetConfig()
|
|
GetGogsUserUid()
|
|
GetReposFromGitHub()
|
|
MigrateReposToGogs(repo_list)
|
|
}
|
|
|
|
func usage() {
|
|
fmt.Fprintf(os.Stderr, "usage: github_to_gogs [inputfile]\n")
|
|
flag.PrintDefaults()
|
|
os.Exit(2)
|
|
}
|
|
|
|
func GetConfig() {
|
|
flag.Usage = usage
|
|
flag.Parse()
|
|
|
|
var configfile string
|
|
|
|
if len(os.Args) > 1 {
|
|
configfile = os.Args[1]
|
|
} else {
|
|
usage()
|
|
os.Exit(2)
|
|
}
|
|
HandleError(err)
|
|
|
|
config, err := ini.Load(configfile)
|
|
HandleError(err)
|
|
|
|
global_section := config.Section("global")
|
|
github_section := config.Section("github")
|
|
gogs_section := config.Section("gogs")
|
|
|
|
globalconfig.UserAgent = global_section.Key("user_agent").String()
|
|
globalconfig.RequestTimeout, err = global_section.Key("request_timeout").Duration()
|
|
HandleError(err)
|
|
|
|
githubconfig.StarsPages = github_section.Key("stars_pages").String()
|
|
githubconfig.MaxPagesNumber, err = github_section.Key("max_pages_number").Int()
|
|
HandleError(err)
|
|
|
|
githubconfig.AuthUsername = github_section.Key("auth_username").String()
|
|
githubconfig.AuthPassword = github_section.Key("auth_password").String()
|
|
githubconfig.ContentType = github_section.Key("content_type").String()
|
|
|
|
gogsconfig.Uid = -1
|
|
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.OrgsUrlTmpl = gogs_section.Key("orgs_url_tmpl").String()
|
|
gogsconfig.MigrateUrl = gogs_section.Key("migrate_url").String()
|
|
gogsconfig.AuthToken = gogs_section.Key("auth_token").String()
|
|
gogsconfig.ContentType = gogs_section.Key("content_type").String()
|
|
gogsconfig.Mirror, err = gogs_section.Key("mirror").Bool()
|
|
HandleError(err)
|
|
|
|
}
|
|
|
|
func CheckGogsIsExistingRepo(repo GitHubRepo) bool {
|
|
var isExists bool = false
|
|
|
|
client = &http.Client{}
|
|
resp = &http.Response{}
|
|
|
|
var gogs_repo_url string = fmt.Sprintf(gogsconfig.RepoUrlTmpl,gogsconfig.DestUsername,repo.Name)
|
|
|
|
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)
|
|
|
|
if resp.StatusCode == 200 {
|
|
isExists = true
|
|
} else {
|
|
isExists = false
|
|
}
|
|
|
|
return isExists
|
|
}
|
|
|
|
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...")
|
|
|
|
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() {
|
|
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 {
|
|
repo_list = append(repo_list, elem)
|
|
}
|
|
}
|
|
fmt.Println("Repositories from Github fetched !")
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func GetResponseBody(resp *http.Response) *[]byte {
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
HandleError(err)
|
|
return &bodyBytes
|
|
}
|
|
|
|
func HandleError(err error) {
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|