added project

This commit is contained in:
Ishan Jain 2024-06-08 14:02:30 +05:30
parent e19c8d6269
commit 2fbf490d75
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
3 changed files with 208 additions and 2 deletions

17
go.mod
View File

@ -1,3 +1,20 @@
module gitea-github-mirror
go 1.22.3
require (
code.gitea.io/sdk/gitea v0.18.0
github.com/google/go-github v17.0.0+incompatible
github.com/sethvargo/go-envconfig v1.0.3
github.com/sirupsen/logrus v1.9.3
golang.org/x/oauth2 v0.21.0
)
require (
github.com/davidmz/go-pageant v1.0.2 // indirect
github.com/go-fed/httpsig v1.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sys v0.19.0 // indirect
)

52
go.sum Normal file
View File

@ -0,0 +1,52 @@
code.gitea.io/sdk/gitea v0.18.0 h1:+zZrwVmujIrgobt6wVBWCqITz6bn1aBjnCUHmpZrerI=
code.gitea.io/sdk/gitea v0.18.0/go.mod h1:IG9xZJoltDNeDSW0qiF2Vqx5orMWa7OhVWrjvrd5NpI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0=
github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE=
github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI=
github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sethvargo/go-envconfig v1.0.3 h1:ZDxFGT1M7RPX0wgDOCdZMidrEB+NrayYr6fL0/+pk4I=
github.com/sethvargo/go-envconfig v1.0.3/go.mod h1:JLd0KFWQYzyENqnEPWWZ49i4vzZo/6nRidxI8YvGiHw=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

141
main.go
View File

@ -1,7 +1,144 @@
package main
import "fmt"
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"code.gitea.io/sdk/gitea"
"github.com/google/go-github/github"
"github.com/sethvargo/go-envconfig"
"golang.org/x/oauth2"
)
const (
DefaultGiteaUrl = "https://git.ishanjain.me"
)
// Sync every 1 hour 01 minutes
const DefaultMirrorInterval = "1h01m0s"
type Config struct {
GithubUser string `env:"GITHUB_USER,required"`
GithubToken string `env:"GITHUB_TOKEN,required"`
GiteaToken string `env:"GITEA_TOKEN,required"`
GiteaUser string `env:"GITEA_USER,required"`
GiteaUrl string `env:"GITEA_URL"`
}
func readConfig() Config {
ctx := context.Background()
config := Config{}
err := envconfig.Process(ctx, &config)
if err != nil {
log.Panicf("error in reading env var: %v", err.Error())
}
if config.GiteaUrl == "" {
config.GiteaUrl = DefaultGiteaUrl
}
return config
}
func main() {
fmt.Println("vim-go")
config := readConfig()
// Github API
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: config.GithubToken})
oclient := oauth2.NewClient(ctx, ts)
ghClient := github.NewClient(oclient)
giteaClient, err := gitea.NewClient(config.GiteaUrl, gitea.SetBasicAuth(config.GiteaUser, config.GiteaToken))
if err != nil {
log.Errorf("error in creating client: %v", err.Error())
}
log.Infoln("fetching a list of repositories on Gitea")
giteaRepos := listGiteaRepositories(config, giteaClient)
log.WithFields(log.Fields{
"gitea_repo_count": len(giteaRepos),
}).Infoln("fetched a list of repositories on gitea")
// For each repo, Create a a repository on Github with the same visiblity settings
// Setup synchronisation operation with the github repo
for _, repo := range giteaRepos {
createRepo(config, repo, ghClient)
setupMirror(config, repo, giteaClient)
}
}
func setupMirror(config Config, repo *gitea.Repository, client *gitea.Client) {
client.PushMirrors(repo.Owner.UserName, repo.Name, gitea.CreatePushMirrorOption{
Interval: DefaultMirrorInterval,
RemoteAddress: fmt.Sprintf("https://github.com/%s/%s", config.GithubUser, repo.Name),
RemotePassword: config.GithubToken,
RemoteUsername: config.GithubUser,
SyncONCommit: true,
})
}
func createRepo(config Config, repo *gitea.Repository, client *github.Client) {
log.WithFields(log.Fields{
"name": repo.Name,
"org": repo.Owner.UserName,
"private": repo.Private,
}).Infoln("configuring mirror")
if repo.OriginalURL != "" {
return
}
_, _, err := client.Repositories.Create(context.Background(), "", &github.Repository{
Name: &repo.Name,
Private: &repo.Private,
Description: &repo.Description,
HasWiki: &repo.HasWiki,
HasProjects: &repo.HasProjects,
})
if err != nil {
log.WithFields(log.Fields{
"name": repo.Name,
"org": repo.Owner.UserName,
"private": repo.Private,
"error": err.Error(),
}).Errorln("error in creating repository")
}
}
func listGiteaRepositories(config Config, client *gitea.Client) []*gitea.Repository {
page := 1
output := []*gitea.Repository{}
for {
repos, _, err := client.ListUserRepos(config.GiteaUser, gitea.ListReposOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 20,
},
})
if err != nil {
log.Errorf("error in listing repositories: %v", err.Error())
break
}
if len(repos) == 0 {
break
}
output = append(output, repos...)
page += 1
}
return output
}