// staletea // Copyright (C) 2020 Jonas Franz // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . package jobs import ( "fmt" "gitea.com/jonasfranz/staletea/config" "gitea.com/jonasfranz/staletea/loader" "gitea.com/jonasfranz/staletea/models" ) func syncRepositories() error { savedRepos, err := models.FindAllRepositories() if err != nil { return err } savedRepoIDs := make(map[int64]*models.Repository) for _, savedRepo := range savedRepos { savedRepoIDs[savedRepo.ID] = savedRepo } l := loader.NewGiteaLoader(config.GiteaURL.Get().(string), config.BotAccessToken.Get().(string)) loadedRepos, err := l.LoadRepositories() if err != nil { return err } newRepos := make([]*models.Repository, 0) for _, loadedRepo := range loadedRepos { if _, ok := savedRepoIDs[loadedRepo.ID]; !ok { repoConfig, err := l.DownloadConfig(loadedRepo) if err != nil { fmt.Printf("cannot download config for repo %s/%s: %v\n", loadedRepo.Owner, loadedRepo.Name, err) continue } newRepos = append(newRepos, &models.Repository{ ID: loadedRepo.ID, Config: repoConfig, Activated: true, }) } } return models.CreateRepositories(newRepos) }