Skip to content

Instantly share code, notes, and snippets.

@techknowlogick
Last active March 17, 2024 08:12
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save techknowlogick/c2367e03baff9f16b3c5cc9d9a5d13ca to your computer and use it in GitHub Desktop.
Save techknowlogick/c2367e03baff9f16b3c5cc9d9a5d13ca to your computer and use it in GitHub Desktop.
package main
import (
// TODO RUN A GO GET ON THE LIBRARIES BELOW
"code.gitea.io/sdk/gitea"
"context"
"time"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func main() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "GETTOKENFROMGITHUB"}, // TODO: GET TOKEN FROM GITHUB
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
giteaclient := gitea.NewClient("https://YOURGITEAHOST/", "GETTOKENFROMGITEA") // TODO: CONFIGURE WITH URL of your GITEA INSTANCE AND A TOKEN FROM YOUR GITEA INSTANCE
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{Type: "public", PerPage: 100},
}
// get all pages of results
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg(ctx, "PUTGITHUBORGYOUWANTCLONEDHERE", opt) // TODO: SET WITH NAME OF GITHUB ORG YOU WANT CLONED
if err != nil {
fmt.Println(err)
return
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
for i := 0; i < len(allRepos); i++ {
description := ""
if allRepos[i].Description != nil { // will throw a nil pointer error if description is passed directly to the below struct
description = *allRepos[i].Description
}
giteaclient.MigrateRepo(gitea.MigrateRepoOption{
CloneAddr: *allRepos[i].CloneURL,
UID: 4, // TODO: SET WITH THE ID OF YOUR USER IN GITEA (IN MY CASE 4 is the user id of an org on my gitea instance)
RepoName: *allRepos[i].Name,
// Mirror: true, // TODO: uncomment this if you want gitea to periodically check for changes
// Private: true, // TODO: uncomment this if you want the repo to be private on gitea
Description: description,
})
time.Sleep(100 * time.Millisecond) // THIS IS HERE SO THE GITEA SERVER DOESNT GET HAMMERED WITH REQUESTS
}
}
@Leopere
Copy link

Leopere commented Dec 11, 2018

Also for future visitors this is the fix for the redirects issue with the import for the gitea sdk

techknowlogick Today at 10:41 PM
mkdir -p $GOPATH/src/code.gitea.io/
cd $GOPATH/src/code.gitea.io/
git clone https://github.com/go-gitea/go-sdk.git sdk
then dep should work
there is a weird issue with the redirects right now

@Leopere
Copy link

Leopere commented Dec 11, 2018

Just added the strings to the code and tried running this in GoLand

GOROOT=/usr/local/go #gosetup
GOPATH=/Users/chamunks/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/t2/86w7z3xd13n82919txs_2v1w0000gn/T/___go_build_main_go -gcflags "all=-N -l" /Users/chamunks/go/src/local/techknowlogick/git-batch-cloner/main.go #gosetup
# command-line-arguments
git-batch-cloner/main.go:24:35: unknown field 'Type' in struct literal of type github.ListOptions

Compilation finished with exit code 2```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment