|
|
@ -16,11 +16,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
package models |
|
|
|
package models |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import "github.com/go-xorm/xorm" |
|
|
|
"code.gitea.io/gitea/modules/structs" |
|
|
|
|
|
|
|
"github.com/go-xorm/xorm" |
|
|
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Repository represents a Gitea repository indexed at the local database |
|
|
|
// Repository represents a Gitea repository indexed at the local database |
|
|
|
type Repository struct { |
|
|
|
type Repository struct { |
|
|
@ -29,7 +25,7 @@ type Repository struct { |
|
|
|
Owner string `xorm:"unique(owner_name)"` |
|
|
|
Owner string `xorm:"unique(owner_name)"` |
|
|
|
Name string `xorm:"unique(owner_name)"` |
|
|
|
Name string `xorm:"unique(owner_name)"` |
|
|
|
|
|
|
|
|
|
|
|
Activated bool `xorm:"-"` |
|
|
|
Activated bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FindRepositoriesByUserID returns all repos of an user |
|
|
|
// FindRepositoriesByUserID returns all repos of an user |
|
|
@ -39,96 +35,5 @@ func FindRepositoriesByUserID(userID int64) ([]*Repository, error) { |
|
|
|
|
|
|
|
|
|
|
|
func findRepositoriesByUserID(e *xorm.Engine, userID int64) ([]*Repository, error) { |
|
|
|
func findRepositoriesByUserID(e *xorm.Engine, userID int64) ([]*Repository, error) { |
|
|
|
repos := make([]*Repository, 0) |
|
|
|
repos := make([]*Repository, 0) |
|
|
|
if err := e.Where("user_id = ?", userID).Find(&repos); err != nil { |
|
|
|
return repos, e.Where("user_id = ?", userID).Find(&repos) |
|
|
|
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 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|