copy updated settings from gitea on subsequent runs

This commit is contained in:
Ishan Jain 2024-06-09 17:04:59 +05:30
parent 2fbf490d75
commit 792ca95013
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

53
main.go
View File

@ -67,11 +67,19 @@ func main() {
// Setup synchronisation operation with the github repo // Setup synchronisation operation with the github repo
for _, repo := range giteaRepos { for _, repo := range giteaRepos {
createRepo(config, repo, ghClient) log.WithFields(log.Fields{
"name": repo.Name,
"org": repo.Owner.UserName,
"private": repo.Private,
}).Infoln("configuring repo")
repoCreated := setupRepo(config, repo, ghClient)
if repoCreated {
setupMirror(config, repo, giteaClient) setupMirror(config, repo, giteaClient)
} }
} }
}
func setupMirror(config Config, repo *gitea.Repository, client *gitea.Client) { func setupMirror(config Config, repo *gitea.Repository, client *gitea.Client) {
@ -85,17 +93,13 @@ func setupMirror(config Config, repo *gitea.Repository, client *gitea.Client) {
} }
func createRepo(config Config, repo *gitea.Repository, client *github.Client) { func setupRepo(config Config, repo *gitea.Repository, client *github.Client) bool {
log.WithFields(log.Fields{
"name": repo.Name,
"org": repo.Owner.UserName,
"private": repo.Private,
}).Infoln("configuring mirror")
if repo.OriginalURL != "" { if repo.OriginalURL != "" {
return return false
} }
if githubExists(client, config.GithubUser, repo.Name) {
// Create repository if it doesn't exist already
_, _, err := client.Repositories.Create(context.Background(), "", &github.Repository{ _, _, err := client.Repositories.Create(context.Background(), "", &github.Repository{
Name: &repo.Name, Name: &repo.Name,
Private: &repo.Private, Private: &repo.Private,
@ -103,7 +107,6 @@ func createRepo(config Config, repo *gitea.Repository, client *github.Client) {
HasWiki: &repo.HasWiki, HasWiki: &repo.HasWiki,
HasProjects: &repo.HasProjects, HasProjects: &repo.HasProjects,
}) })
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"name": repo.Name, "name": repo.Name,
@ -113,6 +116,36 @@ func createRepo(config Config, repo *gitea.Repository, client *github.Client) {
}).Errorln("error in creating repository") }).Errorln("error in creating repository")
} }
return true
}
// Copy description, private state and other parameters if it exists
_, _, err := client.Repositories.Edit(context.Background(), config.GithubUser, repo.Name, &github.Repository{
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 updating repository")
}
return false
}
func githubExists(client *github.Client, owner, repo string) bool {
_, _, err := client.Repositories.Get(context.Background(), owner, repo)
if err != nil {
return false
}
return true
} }
func listGiteaRepositories(config Config, client *gitea.Client) []*gitea.Repository { func listGiteaRepositories(config Config, client *gitea.Client) []*gitea.Repository {