~hokiegeek/gitlab-to-srht

e65e62d6e3e76c734b56813769f8ab27f49e22ae — HokieGeek 2 months ago 3e1829f master
Dunno...
1 files changed, 75 insertions(+), 20 deletions(-)

M main.go
M main.go => main.go +75 -20
@@ 14,7 14,16 @@ import (
	"regexp"
	"strings"
	"time"
	// "gopkg.in/src-d/go-git.v4"

	"golang.org/x/crypto/ssh"

	"gopkg.in/src-d/go-git.v4"
	"gopkg.in/src-d/go-git.v4/config"

	// githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
	// gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
	githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
	"gopkg.in/src-d/go-git.v4/storage/memory"
)

type gitlabProjectResponse []gitlabProject


@@ 260,24 269,6 @@ func createSrhtRepo(token, name, description string) error {
}

func pushToSrht(glURL, srhtURL string) error {
	/*
		glURL := "https://gitlab.com/HokieGeek/goxbm.git"
		srhtURL := "https://git.sr.ht/~hokiegeek/goxbm"

		r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: glURL})

		// Add a new remote, with the default fetch refspec
		_, err := r.CreateRemote(&config.RemoteConfig{
			Name: "srht",
			URLs: []string{srhtURL},
		})

		err := r.Push(&git.PushOptions{
			RemoteName: "srht",
			RefSpecs:   []string{"master"},
		})
	*/

	dir, err := ioutil.TempFile("", "gl2srht")
	if err != nil {
		log.Fatal(err)


@@ 314,9 305,61 @@ func pushToSrht(glURL, srhtURL string) error {
	return nil
}

func pushToSrht2(signer ssh.Signer, user, token, glURL, srhtURL string) error {
	r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: glURL})

	// Add a new remote, with the default fetch refspec
	_, err = r.CreateRemote(&config.RemoteConfig{
		Name: "srht",
		URLs: []string{srhtURL},
	})
	if err != nil {
		return fmt.Errorf("could not create the remote: %v", err)
	}

	/*
		currentUser, err := usr.Current()
		if err != nil {
			panic(err)
		}

		auth, err := gitssh.NewPublicKeysFromFile("git", currentUser.HomeDir+"/.ssh/id_ed25519_npgit", "")
		if err != nil {
			panic(err)
		}
	*/

	fmt.Println("Pushing....")
	auth := &githttp.BasicAuth{"hokiegeek", "s26%sLm7^UUI6X9g#!u^e^Md&ZaAhDTm"}
	// auth := &githttp.BasicAuth{Username: "token", Password: token}
	// auth := &gitssh.PublicKeys{User: "git", Signer: signer}
	// auth := githttp.NewBasicAuth(u, p)
	/*
		auth, err := gitssh.NewSSHAgentAuth("git")
		if err != nil {
			panic(err)
		}
	*/
	fmt.Printf("key == %v\n", auth)
	fmt.Printf("token == %s\n", token)
	err = r.Push(&git.PushOptions{
		RemoteName: "srht",
		// Auth:       &githttp.BasicAuth{"hokiegeek", "s26%sLm7^UUI6X9g#!u^e^Md&ZaAhDTm"},
		// Auth: &githttp.BasicAuth{Username: "token", Password: token},
		Auth:     auth,
		RefSpecs: []config.RefSpec{config.DefaultPushRefSpec},
	})
	if err != nil {
		return fmt.Errorf("could not push the repo: %v", err)
	}

	return nil
}

func main() {
	gitlab := strings.Split(os.Args[1], ":")
	srht := strings.Split(os.Args[2], ":")
	pkeyfile := os.Args[3]

	fmt.Println(gitlab)
	fmt.Println(srht)


@@ 336,9 379,21 @@ func main() {
		panic(err)
	}

	pem, err := ioutil.ReadFile(pkeyfile)
	if err != nil {
		panic(err)
	}
	signer, err := ssh.ParsePrivateKey(pem)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", signer.PublicKey().Type())

	glURL := "https://gitlab.com/HokieGeek/goxbm.git"
	srhtURL := "https://git.sr.ht/~hokiegeek/goxbm"
	if err := pushToSrht(glURL, srhtURL); err != nil {
	// srhtURL := "https://hokiegeek@s26%sLm7^UUI6X9g#!u^e^Md&ZaAhDTm:git.sr.ht/~hokiegeek/goxbm"
	// srhtURL := fmt.Sprintf("https://hokiegeek@%s:git.sr.ht/~hokiegeek/goxbm", url.QueryEscape("s26%sLm7^UUI6X9g#!u^e^Md&ZaAhDTm"))
	if err := pushToSrht2(signer, srht[0], srht[1], glURL, srhtURL); err != nil {
		if _, er := srhtreq(http.MethodDelete, "/api/repos/goxbm", srht[1], nil); er != nil {
			fmt.Printf("%v", er)
		}