// staletea // Copyright (C) 2019 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 models import ( "code.gitea.io/gitea/modules/structs" "github.com/go-xorm/xorm" "time" ) // Repository represents a Gitea repository indexed at the local database type Repository struct { ID int64 `xorm:"pk"` UserID int64 `xorm:"index"` Owner string `xorm:"unique(owner_name)"` Name string `xorm:"unique(owner_name)"` Activated bool `xorm:"-"` } // FindRepositoriesByUserID returns all repos of an user func FindRepositoriesByUserID(userID int64) ([]*Repository, error) { return findRepositoriesByUserID(x, userID) } func findRepositoriesByUserID(e *xorm.Engine, userID int64) ([]*Repository, error) { repos := make([]*Repository, 0) if err := e.Where("user_id = ?", userID).Find(&repos); err != nil { return nil, err } for _, repo := range repos { repo.Activated = true } return repos, nil } // SetActivatedRepositories sets the given repositories as active for the owner. It will sync all issues in addition. func SetActivatedRepositories(repos []*Repository, owner *User) error { return setActivatedRepositories(x, repos, owner) } func setActivatedRepositories(e *xorm.Engine, repos []*Repository, owner *User) error { if err := deleteAllReposByUserID(e, owner.ID); err != nil { return err } if err := insertRepos(e, repos); err != nil { return err } for _, repo := range repos { go repo.SyncIssues(owner) } return nil } // DeleteAllReposByUserID deletes all repositories owned by userID func DeleteAllReposByUserID(userID int64) error { return deleteAllReposByUserID(x, userID) } func deleteAllReposByUserID(e *xorm.Engine, userID int64) error { if _, err := e.Where("user_id = ?", userID).Delete(new(Repository)); err != nil { return err } return nil } // InsertRepos inserts repos into database func InsertRepos(repos []*Repository) error { return insertRepos(x, repos) } func insertRepos(e *xorm.Engine, repos []*Repository) error { _, err := e.Insert(repos) return err } // SyncIssues ... func (r *Repository) SyncIssues(user *User) error { return r.syncIssues(x, user) } func (r *Repository) syncIssues(e *xorm.Engine, user *User) error { page := 0 issues, err := user.GiteaClient().ListRepoIssues(r.Owner, r.Name, structs.ListIssueOption{ Page: page, State: "open", }) if err != nil { return err } currentIssues := issues for len(currentIssues) != 0 { page++ currentIssues, err = user.GiteaClient().ListRepoIssues(r.Owner, r.Name, structs.ListIssueOption{ Page: page, State: "open", }) if err != nil { return err } issues = append(issues, currentIssues...) } parsedIssues := make([]*Issue, len(issues)) now := time.Now() for index, issue := range issues { parsedIssues[index] = &Issue{ ID: issue.ID, Closed: issue.State == "closed", Excluded: false, // TODO check labels, IndexedAt: &now, MarkedAt: nil, Stale: false, UpdatedAt: &issue.Updated, RepoID: r.ID, } } e.In("id") _, err = e.Insert(parsedIssues) return err }