|
|
@ -16,7 +16,11 @@ |
|
|
|
|
|
|
|
|
|
|
|
package models |
|
|
|
package models |
|
|
|
|
|
|
|
|
|
|
|
import "github.com/go-xorm/xorm" |
|
|
|
import ( |
|
|
|
|
|
|
|
"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 { |
|
|
@ -25,7 +29,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 |
|
|
|
Activated bool `xorm:"-"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FindRepositoriesByUserID returns all repos of an user |
|
|
|
// FindRepositoriesByUserID returns all repos of an user |
|
|
@ -35,5 +39,96 @@ 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) |
|
|
|
return repos, e.Where("user_id = ?", userID).Find(&repos) |
|
|
|
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 |
|
|
|
} |
|
|
|
} |
|
|
|