summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/access.go2
-rw-r--r--models/commit_status.go2
-rw-r--r--models/gpg_key.go5
-rw-r--r--models/issue.go4
-rw-r--r--models/issue_comment.go13
-rw-r--r--models/issue_label.go10
-rw-r--r--models/issue_milestone.go24
-rw-r--r--models/issue_milestone_test.go8
-rw-r--r--models/issue_stopwatch.go5
-rw-r--r--models/issue_tracked_time.go31
-rw-r--r--models/issue_tracked_time_test.go22
-rw-r--r--models/notification.go5
-rw-r--r--models/oauth2_application.go8
-rw-r--r--models/org.go14
-rw-r--r--models/org_team.go15
-rw-r--r--models/org_team_test.go2
-rw-r--r--models/org_test.go2
-rw-r--r--models/repo.go8
-rw-r--r--models/repo_collaboration.go5
-rw-r--r--models/repo_generate.go2
-rw-r--r--models/repo_transfer.go4
-rw-r--r--models/review.go5
-rw-r--r--models/ssh_key.go6
-rw-r--r--models/ssh_key_deploy.go56
-rw-r--r--models/token.go9
-rw-r--r--models/topic.go17
-rw-r--r--models/topic_test.go15
-rw-r--r--models/user.go8
-rw-r--r--models/webhook.go74
-rw-r--r--models/webhook_test.go9
30 files changed, 239 insertions, 151 deletions
diff --git a/models/access.go b/models/access.go
index d35b900cfd..5d0b0b06cf 100644
--- a/models/access.go
+++ b/models/access.go
@@ -246,7 +246,7 @@ func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err
return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
}
- if err = repo.Owner.getTeams(e); err != nil {
+ if err = repo.Owner.loadTeams(e); err != nil {
return err
}
diff --git a/models/commit_status.go b/models/commit_status.go
index c275820242..1ee3ead217 100644
--- a/models/commit_status.go
+++ b/models/commit_status.go
@@ -159,7 +159,7 @@ func getLatestCommitStatus(e Engine, repoID int64, sha string, listOptions ListO
if len(ids) == 0 {
return statuses, nil
}
- return statuses, x.In("id", ids).Find(&statuses)
+ return statuses, e.In("id", ids).Find(&statuses)
}
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
diff --git a/models/gpg_key.go b/models/gpg_key.go
index 74ffb82a54..1072813b1b 100644
--- a/models/gpg_key.go
+++ b/models/gpg_key.go
@@ -71,6 +71,11 @@ func listGPGKeys(e Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error
return keys, sess.Find(&keys)
}
+// CountUserGPGKeys return number of gpg keys a user own
+func CountUserGPGKeys(userID int64) (int64, error) {
+ return x.Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
+}
+
// GetGPGKeyByID returns public key by given ID.
func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
key := new(GPGKey)
diff --git a/models/issue.go b/models/issue.go
index 225dfee20f..8b83612c2a 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -89,7 +89,7 @@ func init() {
func (issue *Issue) loadTotalTimes(e Engine) (err error) {
opts := FindTrackedTimesOptions{IssueID: issue.ID}
- issue.TotalTrackedTime, err = opts.ToSession(e).SumInt(&TrackedTime{}, "time")
+ issue.TotalTrackedTime, err = opts.toSession(e).SumInt(&TrackedTime{}, "time")
if err != nil {
return err
}
@@ -214,7 +214,7 @@ func (issue *Issue) loadCommentsByType(e Engine, tp CommentType) (err error) {
if issue.Comments != nil {
return nil
}
- issue.Comments, err = findComments(e, FindCommentsOptions{
+ issue.Comments, err = findComments(e, &FindCommentsOptions{
IssueID: issue.ID,
Type: tp,
})
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 755041efd7..06b74dff85 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -999,7 +999,7 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
return cond
}
-func findComments(e Engine, opts FindCommentsOptions) ([]*Comment, error) {
+func findComments(e Engine, opts *FindCommentsOptions) ([]*Comment, error) {
comments := make([]*Comment, 0, 10)
sess := e.Where(opts.toConds())
if opts.RepoID > 0 {
@@ -1019,10 +1019,19 @@ func findComments(e Engine, opts FindCommentsOptions) ([]*Comment, error) {
}
// FindComments returns all comments according options
-func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
+func FindComments(opts *FindCommentsOptions) ([]*Comment, error) {
return findComments(x, opts)
}
+// CountComments count all comments according options by ignoring pagination
+func CountComments(opts *FindCommentsOptions) (int64, error) {
+ sess := x.Where(opts.toConds())
+ if opts.RepoID > 0 {
+ sess.Join("INNER", "issue", "issue.id = comment.issue_id")
+ }
+ return sess.Count(&Comment{})
+}
+
// UpdateComment updates information of comment.
func UpdateComment(c *Comment, doer *User) error {
sess := x.NewSession()
diff --git a/models/issue_label.go b/models/issue_label.go
index d1ff469236..5d50203b5a 100644
--- a/models/issue_label.go
+++ b/models/issue_label.go
@@ -444,6 +444,11 @@ func GetLabelsByRepoID(repoID int64, sortType string, listOptions ListOptions) (
return getLabelsByRepoID(x, repoID, sortType, listOptions)
}
+// CountLabelsByRepoID count number of all labels that belong to given repository by ID.
+func CountLabelsByRepoID(repoID int64) (int64, error) {
+ return x.Where("repo_id = ?", repoID).Count(&Label{})
+}
+
// ________
// \_____ \_______ ____
// / | \_ __ \/ ___\
@@ -556,6 +561,11 @@ func GetLabelsByOrgID(orgID int64, sortType string, listOptions ListOptions) ([]
return getLabelsByOrgID(x, orgID, sortType, listOptions)
}
+// CountLabelsByOrgID count all labels that belong to given organization by ID.
+func CountLabelsByOrgID(orgID int64) (int64, error) {
+ return x.Where("org_id = ?", orgID).Count(&Label{})
+}
+
// .___
// | | ______ ________ __ ____
// | |/ ___// ___/ | \_/ __ \
diff --git a/models/issue_milestone.go b/models/issue_milestone.go
index 5e934cde0a..e6976a46c7 100644
--- a/models/issue_milestone.go
+++ b/models/issue_milestone.go
@@ -380,24 +380,33 @@ type GetMilestonesOption struct {
SortType string
}
-// GetMilestones returns milestones filtered by GetMilestonesOption's
-func GetMilestones(opts GetMilestonesOption) (MilestoneList, error) {
- sess := x.Where("repo_id = ?", opts.RepoID)
+func (opts GetMilestonesOption) toCond() builder.Cond {
+ cond := builder.NewCond()
+ if opts.RepoID != 0 {
+ cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
+ }
switch opts.State {
case api.StateClosed:
- sess = sess.And("is_closed = ?", true)
+ cond = cond.And(builder.Eq{"is_closed": true})
case api.StateAll:
break
// api.StateOpen:
default:
- sess = sess.And("is_closed = ?", false)
+ cond = cond.And(builder.Eq{"is_closed": false})
}
if len(opts.Name) != 0 {
- sess = sess.And(builder.Like{"name", opts.Name})
+ cond = cond.And(builder.Like{"name", opts.Name})
}
+ return cond
+}
+
+// GetMilestones returns milestones filtered by GetMilestonesOption's
+func GetMilestones(opts GetMilestonesOption) (MilestoneList, int64, error) {
+ sess := x.Where(opts.toCond())
+
if opts.Page != 0 {
sess = opts.setSessionPagination(sess)
}
@@ -420,7 +429,8 @@ func GetMilestones(opts GetMilestonesOption) (MilestoneList, error) {
}
miles := make([]*Milestone, 0, opts.PageSize)
- return miles, sess.Find(&miles)
+ total, err := sess.FindAndCount(&miles)
+ return miles, total, err
}
// SearchMilestones search milestones
diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go
index 5406129884..1472c951eb 100644
--- a/models/issue_milestone_test.go
+++ b/models/issue_milestone_test.go
@@ -50,7 +50,7 @@ func TestGetMilestonesByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
test := func(repoID int64, state api.StateType) {
repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
- milestones, err := GetMilestones(GetMilestonesOption{
+ milestones, _, err := GetMilestones(GetMilestonesOption{
RepoID: repo.ID,
State: state,
})
@@ -87,7 +87,7 @@ func TestGetMilestonesByRepoID(t *testing.T) {
test(3, api.StateClosed)
test(3, api.StateAll)
- milestones, err := GetMilestones(GetMilestonesOption{
+ milestones, _, err := GetMilestones(GetMilestonesOption{
RepoID: NonexistentID,
State: api.StateOpen,
})
@@ -100,7 +100,7 @@ func TestGetMilestones(t *testing.T) {
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
test := func(sortType string, sortCond func(*Milestone) int) {
for _, page := range []int{0, 1} {
- milestones, err := GetMilestones(GetMilestonesOption{
+ milestones, _, err := GetMilestones(GetMilestonesOption{
ListOptions: ListOptions{
Page: page,
PageSize: setting.UI.IssuePagingNum,
@@ -117,7 +117,7 @@ func TestGetMilestones(t *testing.T) {
}
assert.True(t, sort.IntsAreSorted(values))
- milestones, err = GetMilestones(GetMilestonesOption{
+ milestones, _, err = GetMilestones(GetMilestonesOption{
ListOptions: ListOptions{
Page: page,
PageSize: setting.UI.IssuePagingNum,
diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go
index 8cdad94fd4..9322e26bf2 100644
--- a/models/issue_stopwatch.go
+++ b/models/issue_stopwatch.go
@@ -55,6 +55,11 @@ func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, er
return sws, nil
}
+// CountUserStopwatches return count of all stopwatches of a user
+func CountUserStopwatches(userID int64) (int64, error) {
+ return x.Where("user_id = ?", userID).Count(&Stopwatch{})
+}
+
// StopwatchExists returns true if the stopwatch exists
func StopwatchExists(userID, issueID int64) bool {
_, exists, _ := getStopwatch(x, userID, issueID)
diff --git a/models/issue_tracked_time.go b/models/issue_tracked_time.go
index 43f2e13784..e7769b41dd 100644
--- a/models/issue_tracked_time.go
+++ b/models/issue_tracked_time.go
@@ -79,8 +79,8 @@ type FindTrackedTimesOptions struct {
CreatedBeforeUnix int64
}
-// ToCond will convert each condition into a xorm-Cond
-func (opts *FindTrackedTimesOptions) ToCond() builder.Cond {
+// toCond will convert each condition into a xorm-Cond
+func (opts *FindTrackedTimesOptions) toCond() builder.Cond {
cond := builder.NewCond().And(builder.Eq{"tracked_time.deleted": false})
if opts.IssueID != 0 {
cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
@@ -103,14 +103,14 @@ func (opts *FindTrackedTimesOptions) ToCond() builder.Cond {
return cond
}
-// ToSession will convert the given options to a xorm Session by using the conditions from ToCond and joining with issue table if required
-func (opts *FindTrackedTimesOptions) ToSession(e Engine) Engine {
+// toSession will convert the given options to a xorm Session by using the conditions from toCond and joining with issue table if required
+func (opts *FindTrackedTimesOptions) toSession(e Engine) Engine {
sess := e
if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
sess = e.Join("INNER", "issue", "issue.id = tracked_time.issue_id")
}
- sess = sess.Where(opts.ToCond())
+ sess = sess.Where(opts.toCond())
if opts.Page != 0 {
sess = opts.setEnginePagination(sess)
@@ -119,18 +119,27 @@ func (opts *FindTrackedTimesOptions) ToSession(e Engine) Engine {
return sess
}
-func getTrackedTimes(e Engine, options FindTrackedTimesOptions) (trackedTimes TrackedTimeList, err error) {
- err = options.ToSession(e).Find(&trackedTimes)
+func getTrackedTimes(e Engine, options *FindTrackedTimesOptions) (trackedTimes TrackedTimeList, err error) {
+ err = options.toSession(e).Find(&trackedTimes)
return
}
// GetTrackedTimes returns all tracked times that fit to the given options.
-func GetTrackedTimes(opts FindTrackedTimesOptions) (TrackedTimeList, error) {
+func GetTrackedTimes(opts *FindTrackedTimesOptions) (TrackedTimeList, error) {
return getTrackedTimes(x, opts)
}
+// CountTrackedTimes returns count of tracked times that fit to the given options.
+func CountTrackedTimes(opts *FindTrackedTimesOptions) (int64, error) {
+ sess := x.Where(opts.toCond())
+ if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
+ sess = sess.Join("INNER", "issue", "issue.id = tracked_time.issue_id")
+ }
+ return sess.Count(&TrackedTime{})
+}
+
func getTrackedSeconds(e Engine, opts FindTrackedTimesOptions) (trackedSeconds int64, err error) {
- return opts.ToSession(e).SumInt(&TrackedTime{}, "time")
+ return opts.toSession(e).SumInt(&TrackedTime{}, "time")
}
// GetTrackedSeconds return sum of seconds
@@ -188,7 +197,7 @@ func addTime(e Engine, user *User, issue *Issue, amount int64, created time.Time
}
// TotalTimes returns the spent time for each user by an issue
-func TotalTimes(options FindTrackedTimesOptions) (map[*User]string, error) {
+func TotalTimes(options *FindTrackedTimesOptions) (map[*User]string, error) {
trackedTimes, err := GetTrackedTimes(options)
if err != nil {
return nil, err
@@ -288,7 +297,7 @@ func deleteTimes(e Engine, opts FindTrackedTimesOptions) (removedTime int64, err
return
}
- _, err = opts.ToSession(e).Table("tracked_time").Cols("deleted").Update(&TrackedTime{Deleted: true})
+ _, err = opts.toSession(e).Table("tracked_time").Cols("deleted").Update(&TrackedTime{Deleted: true})
return
}
diff --git a/models/issue_tracked_time_test.go b/models/issue_tracked_time_test.go
index e30de6cb69..9de1dd699d 100644
--- a/models/issue_tracked_time_test.go
+++ b/models/issue_tracked_time_test.go
@@ -38,27 +38,27 @@ func TestGetTrackedTimes(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
// by Issue
- times, err := GetTrackedTimes(FindTrackedTimesOptions{IssueID: 1})
+ times, err := GetTrackedTimes(&FindTrackedTimesOptions{IssueID: 1})
assert.NoError(t, err)
assert.Len(t, times, 1)
assert.Equal(t, int64(400), times[0].Time)
- times, err = GetTrackedTimes(FindTrackedTimesOptions{IssueID: -1})
+ times, err = GetTrackedTimes(&FindTrackedTimesOptions{IssueID: -1})
assert.NoError(t, err)
assert.Len(t, times, 0)
// by User
- times, err = GetTrackedTimes(FindTrackedTimesOptions{UserID: 1})
+ times, err = GetTrackedTimes(&FindTrackedTimesOptions{UserID: 1})
assert.NoError(t, err)
assert.Len(t, times, 3)
assert.Equal(t, int64(400), times[0].Time)
- times, err = GetTrackedTimes(FindTrackedTimesOptions{UserID: 3})
+ times, err = GetTrackedTimes(&FindTrackedTimesOptions{UserID: 3})
assert.NoError(t, err)
assert.Len(t, times, 0)
// by Repo
- times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 2})
+ times, err = GetTrackedTimes(&FindTrackedTimesOptions{RepositoryID: 2})
assert.NoError(t, err)
assert.Len(t, times, 3)
assert.Equal(t, int64(1), times[0].Time)
@@ -66,11 +66,11 @@ func TestGetTrackedTimes(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, issue.RepoID, int64(2))
- times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 1})
+ times, err = GetTrackedTimes(&FindTrackedTimesOptions{RepositoryID: 1})
assert.NoError(t, err)
assert.Len(t, times, 5)
- times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 10})
+ times, err = GetTrackedTimes(&FindTrackedTimesOptions{RepositoryID: 10})
assert.NoError(t, err)
assert.Len(t, times, 0)
}
@@ -78,7 +78,7 @@ func TestGetTrackedTimes(t *testing.T) {
func TestTotalTimes(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- total, err := TotalTimes(FindTrackedTimesOptions{IssueID: 1})
+ total, err := TotalTimes(&FindTrackedTimesOptions{IssueID: 1})
assert.NoError(t, err)
assert.Len(t, total, 1)
for user, time := range total {
@@ -86,7 +86,7 @@ func TestTotalTimes(t *testing.T) {
assert.Equal(t, "6min 40s", time)
}
- total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 2})
+ total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 2})
assert.NoError(t, err)
assert.Len(t, total, 2)
for user, time := range total {
@@ -99,7 +99,7 @@ func TestTotalTimes(t *testing.T) {
}
}
- total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 5})
+ total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 5})
assert.NoError(t, err)
assert.Len(t, total, 1)
for user, time := range total {
@@ -107,7 +107,7 @@ func TestTotalTimes(t *testing.T) {
assert.Equal(t, "1s", time)
}
- total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 4})
+ total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 4})
assert.NoError(t, err)
assert.Len(t, total, 2)
}
diff --git a/models/notification.go b/models/notification.go
index c4c7728ad9..30bb7596a8 100644
--- a/models/notification.go
+++ b/models/notification.go
@@ -125,6 +125,11 @@ func GetNotifications(opts *FindNotificationOptions) (NotificationList, error) {
return getNotifications(x, opts)
}
+// CountNotifications count all notifications that fit to the given options and ignore pagination.
+func CountNotifications(opts *FindNotificationOptions) (int64, error) {
+ return x.Where(opts.ToCond()).Count(&Notification{})
+}
+
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
func CreateRepoTransferNotification(doer, newOwner *User, repo *Repository) error {
sess := x.NewSession()
diff --git a/models/oauth2_application.go b/models/oauth2_application.go
index 2aa9fbd3d9..9cf236f0cb 100644
--- a/models/oauth2_application.go
+++ b/models/oauth2_application.go
@@ -269,7 +269,7 @@ func DeleteOAuth2Application(id, userid int64) error {
}
// ListOAuth2Applications returns a list of oauth2 applications belongs to given user.
-func ListOAuth2Applications(uid int64, listOptions ListOptions) ([]*OAuth2Application, error) {
+func ListOAuth2Applications(uid int64, listOptions ListOptions) ([]*OAuth2Application, int64, error) {
sess := x.
Where("uid=?", uid).
Desc("id")
@@ -278,11 +278,13 @@ func ListOAuth2Applications(uid int64, listOptions ListOptions) ([]*OAuth2Applic
sess = listOptions.setSessionPagination(sess)
apps := make([]*OAuth2Application, 0, listOptions.PageSize)
- return apps, sess.Find(&apps)
+ total, err := sess.FindAndCount(&apps)
+ return apps, total, err
}
apps := make([]*OAuth2Application, 0, 5)
- return apps, sess.Find(&apps)
+ total, err := sess.FindAndCount(&apps)
+ return apps, total, err
}
//////////////////////////////////////////////////////
diff --git a/models/org.go b/models/org.go
index 58fb26b1bb..3de6bd14b2 100644
--- a/models/org.go
+++ b/models/org.go
@@ -52,7 +52,7 @@ func (org *User) GetOwnerTeam() (*Team, error) {
return org.getOwnerTeam(x)
}
-func (org *User) getTeams(e Engine) error {
+func (org *User) loadTeams(e Engine) error {
if org.Teams != nil {
return nil
}
@@ -62,13 +62,9 @@ func (org *User) getTeams(e Engine) error {
Find(&org.Teams)
}
-// GetTeams returns paginated teams that belong to organization.
-func (org *User) GetTeams(opts *SearchTeamOptions) error {
- if opts.Page != 0 {
- return org.getTeams(opts.getPaginatedSession())
- }
-
- return org.getTeams(x)
+// LoadTeams load teams if not loaded.
+func (org *User) LoadTeams() error {
+ return org.loadTeams(x)
}
// GetMembers returns all members of organization.
@@ -87,7 +83,7 @@ type FindOrgMembersOpts struct {
}
// CountOrgMembers counts the organization's members
-func CountOrgMembers(opts FindOrgMembersOpts) (int64, error) {
+func CountOrgMembers(opts *FindOrgMembersOpts) (int64, error) {
sess := x.Where("org_id=?", opts.OrgID)
if opts.PublicOnly {
sess.And("is_public = ?", true)
diff --git a/models/org_team.go b/models/org_team.go
index 6226772b9a..c380c8cd8e 100644
--- a/models/org_team.go
+++ b/models/org_team.go
@@ -790,16 +790,6 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
return getTeamMembers(x, teamID)
}
-func getUserTeams(e Engine, userID int64, listOptions ListOptions) (teams []*Team, err error) {
- sess := e.
- Join("INNER", "team_user", "team_user.team_id = team.id").
- Where("team_user.uid=?", userID)
- if listOptions.Page != 0 {
- sess = listOptions.setSessionPagination(sess)
- }
- return teams, sess.Find(&teams)
-}
-
func getUserOrgTeams(e Engine, orgID, userID int64) (teams []*Team, err error) {
return teams, e.
Join("INNER", "team_user", "team_user.team_id = team.id").
@@ -823,11 +813,6 @@ func GetUserOrgTeams(orgID, userID int64) ([]*Team, error) {
return getUserOrgTeams(x, orgID, userID)
}
-// GetUserTeams returns all teams that user belongs across all organizations.
-func GetUserTeams(userID int64, listOptions ListOptions) ([]*Team, error) {
- return getUserTeams(x, userID, listOptions)
-}
-
// AddTeamMember adds new membership of given team to given organization,
// the user will have membership to given organization automatically when needed.
func AddTeamMember(team *Team, userID int64) error {
diff --git a/models/org_team_test.go b/models/org_team_test.go
index 38e36cf82c..9dc26fd814 100644
--- a/models/org_team_test.go
+++ b/models/org_team_test.go
@@ -286,7 +286,7 @@ func TestGetTeamMembers(t *testing.T) {
func TestGetUserTeams(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
test := func(userID int64) {
- teams, err := GetUserTeams(userID, ListOptions{})
+ teams, _, err := SearchTeam(&SearchTeamOptions{UserID: userID})
assert.NoError(t, err)
for _, team := range teams {
AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
diff --git a/models/org_test.go b/models/org_test.go
index e494e502dd..7ba9d8ee88 100644
--- a/models/org_test.go
+++ b/models/org_test.go
@@ -86,7 +86,7 @@ func TestUser_GetOwnerTeam(t *testing.T) {
func TestUser_GetTeams(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
- assert.NoError(t, org.GetTeams(&SearchTeamOptions{}))
+ assert.NoError(t, org.LoadTeams())
if assert.Len(t, org.Teams, 4) {
assert.Equal(t, int64(1), org.Teams[0].ID)
assert.Equal(t, int64(2), org.Teams[1].ID)
diff --git a/models/repo.go b/models/repo.go
index 93827f6a88..f2e3f5b58b 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1125,8 +1125,8 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO
// Give access to all members in teams with access to all repositories.
if u.IsOrganization() {
- if err := u.getTeams(ctx.e); err != nil {
- return fmt.Errorf("GetTeams: %v", err)
+ if err := u.loadTeams(ctx.e); err != nil {
+ return fmt.Errorf("loadTeams: %v", err)
}
for _, t := range u.Teams {
if t.IncludesAllRepositories {
@@ -1439,7 +1439,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
return err
}
if org.IsOrganization() {
- if err = org.getTeams(sess); err != nil {
+ if err = org.loadTeams(sess); err != nil {
return err
}
}
@@ -1453,7 +1453,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
}
// Delete Deploy Keys
- deployKeys, err := listDeployKeys(sess, repo.ID, ListOptions{})
+ deployKeys, err := listDeployKeys(sess, &ListDeployKeysOptions{RepoID: repoID})
if err != nil {
return fmt.Errorf("listDeployKeys: %v", err)
}
diff --git a/models/repo_collaboration.go b/models/repo_collaboration.go
index b9488f5e2e..a8b715bbcf 100644
--- a/models/repo_collaboration.go
+++ b/models/repo_collaboration.go
@@ -102,6 +102,11 @@ func (repo *Repository) GetCollaborators(listOptions ListOptions) ([]*Collaborat
return repo.getCollaborators(x, listOptions)
}
+// CountCollaborators returns total number of collaborators for a repository
+func (repo *Repository) CountCollaborators() (int64, error) {
+ return x.Where("repo_id = ? ", repo.ID).Count(&Collaboration{})
+}
+
func (repo *Repository) getCollaboration(e Engine, uid int64) (*Collaboration, error) {
collaboration := &Collaboration{
RepoID: repo.ID,
diff --git a/models/repo_generate.go b/models/repo_generate.go
index 1cf73bc55e..66682903f1 100644
--- a/models/repo_generate.go
+++ b/models/repo_generate.go
@@ -111,7 +111,7 @@ func GenerateGitHooks(ctx DBContext, templateRepo, generateRepo *Repository) err
// GenerateWebhooks generates webhooks from a template repository
func GenerateWebhooks(ctx DBContext, templateRepo, generateRepo *Repository) error {
- templateWebhooks, err := GetWebhooksByRepoID(templateRepo.ID, ListOptions{})
+ templateWebhooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: templateRepo.ID})
if err != nil {
return err
}
diff --git a/models/repo_transfer.go b/models/repo_transfer.go
index d7ef0a8ca6..09b6290293 100644
--- a/models/repo_transfer.go
+++ b/models/repo_transfer.go
@@ -291,8 +291,8 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) (err e
}
if newOwner.IsOrganization() {
- if err := newOwner.getTeams(sess); err != nil {
- return fmt.Errorf("GetTeams: %v", err)
+ if err := newOwner.loadTeams(sess); err != nil {
+ return fmt.Errorf("LoadTeams: %v", err)
}
for _, t := range newOwner.Teams {
if t.IncludesAllRepositories {
diff --git a/models/review.go b/models/review.go
index acb54d970f..1ffff8feb6 100644
--- a/models/review.go
+++ b/models/review.go
@@ -208,6 +208,11 @@ func FindReviews(opts FindReviewOptions) ([]*Review, error) {
return findReviews(x, opts)
}
+// CountReviews returns count of reviews passing FindReviewOptions
+func CountReviews(opts FindReviewOptions) (int64, error) {
+ return x.Where(opts.toCond()).Count(&Review{})
+}
+
// CreateReviewOptions represent the options to create a review. Type, Issue and Reviewer are required.
type CreateReviewOptions struct {
Content string
diff --git a/models/ssh_key.go b/models/ssh_key.go
index 6cda4f1658..a2f4c610e4 100644
--- a/models/ssh_key.go
+++ b/models/ssh_key.go
@@ -205,6 +205,12 @@ func ListPublicKeys(uid int64, listOptions ListOptions) ([]*PublicKey, error) {
return keys, sess.Find(&keys)
}
+// CountPublicKeys count public keys a user has
+func CountPublicKeys(userID int64) (int64, error) {
+ sess := x.Where("owner_id = ? AND type != ?", userID, KeyTypePrincipal)
+ return sess.Count(&PublicKey{})
+}
+
// ListPublicKeysBySource returns a list of synchronized public keys for a given user and login source.
func ListPublicKeysBySource(uid, loginSourceID int64) ([]*PublicKey, error) {
keys := make([]*PublicKey, 0, 5)
diff --git a/models/ssh_key_deploy.go b/models/ssh_key_deploy.go
index 3189bcf456..e7d486b9f5 100644
--- a/models/ssh_key_deploy.go
+++ b/models/ssh_key_deploy.go
@@ -264,17 +264,40 @@ func deleteDeployKey(sess Engine, doer *User, id int64) error {
return nil
}
-// ListDeployKeys returns all deploy keys by given repository ID.
-func ListDeployKeys(repoID int64, listOptions ListOptions) ([]*DeployKey, error) {
- return listDeployKeys(x, repoID, listOptions)
+// ListDeployKeysOptions are options for ListDeployKeys
+type ListDeployKeysOptions struct {
+ ListOptions
+ RepoID int64
+ KeyID int64
+ Fingerprint string
+}
+
+func (opt ListDeployKeysOptions) toCond() builder.Cond {
+ cond := builder.NewCond()
+ if opt.RepoID != 0 {
+ cond = cond.And(builder.Eq{"repo_id": opt.RepoID})
+ }
+ if opt.KeyID != 0 {
+ cond = cond.And(builder.Eq{"key_id": opt.KeyID})
+ }
+ if opt.Fingerprint != "" {
+ cond = cond.And(builder.Eq{"fingerprint": opt.Fingerprint})
+ }
+ return cond
}
-func listDeployKeys(e Engine, repoID int64, listOptions ListOptions) ([]*DeployKey, error) {
- sess := e.Where("repo_id = ?", repoID)
- if listOptions.Page != 0 {
- sess = listOptions.setSessionPagination(sess)
+// ListDeployKeys returns a list of deploy keys matching the provided arguments.
+func ListDeployKeys(opts *ListDeployKeysOptions) ([]*DeployKey, error) {
+ return listDeployKeys(x, opts)
+}
- keys := make([]*DeployKey, 0, listOptions.PageSize)
+func listDeployKeys(e Engine, opts *ListDeployKeysOptions) ([]*DeployKey, error) {
+ sess := e.Where(opts.toCond())
+
+ if opts.Page != 0 {
+ sess = opts.setSessionPagination(sess)
+
+ keys := make([]*DeployKey, 0, opts.PageSize)
return keys, sess.Find(&keys)
}
@@ -282,18 +305,7 @@ func listDeployKeys(e Engine, repoID int64, listOptions ListOptions) ([]*DeployK
return keys, sess.Find(&keys)
}
-// SearchDeployKeys returns a list of deploy keys matching the provided arguments.
-func SearchDeployKeys(repoID, keyID int64, fingerprint string) ([]*DeployKey, error) {
- keys := make([]*DeployKey, 0, 5)
- cond := builder.NewCond()
- if repoID != 0 {
- cond = cond.And(builder.Eq{"repo_id": repoID})
- }
- if keyID != 0 {
- cond = cond.And(builder.Eq{"key_id": keyID})
- }
- if fingerprint != "" {
- cond = cond.And(builder.Eq{"fingerprint": fingerprint})
- }
- return keys, x.Where(cond).Find(&keys)
+// CountDeployKeys returns count deploy keys matching the provided arguments.
+func CountDeployKeys(opts *ListDeployKeysOptions) (int64, error) {
+ return x.Where(opts.toCond()).Count(&DeployKey{})
}
diff --git a/models/token.go b/models/token.go
index 357afe44a7..8e1f91d43f 100644
--- a/models/token.go
+++ b/models/token.go
@@ -122,6 +122,15 @@ func UpdateAccessToken(t *AccessToken) error {
return err
}
+// CountAccessTokens count access tokens belongs to given user by options
+func CountAccessTokens(opts ListAccessTokensOptions) (int64, error) {
+ sess := x.Where("uid=?", opts.UserID)
+ if len(opts.Name) != 0 {
+ sess = sess.Where("name=?", opts.Name)
+ }
+ return sess.Count(&AccessToken{})
+}
+
// DeleteAccessTokenByID deletes access token by given ID.
func DeleteAccessTokenByID(id, userID int64) error {
cnt, err := x.ID(id).Delete(&AccessToken{
diff --git a/models/topic.go b/models/topic.go
index 19c572fefe..7fc34f5bef 100644
--- a/models/topic.go
+++ b/models/topic.go
@@ -184,7 +184,7 @@ func (opts *FindTopicOptions) toConds() builder.Cond {
}
// FindTopics retrieves the topics via FindTopicOptions
-func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
+func FindTopics(opts *FindTopicOptions) ([]*Topic, int64, error) {
sess := x.Select("topic.*").Where(opts.toConds())
if opts.RepoID > 0 {
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
@@ -192,7 +192,18 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
if opts.PageSize != 0 && opts.Page != 0 {
sess = opts.setSessionPagination(sess)
}
- return topics, sess.Desc("topic.repo_count").Find(&topics)
+ topics := make([]*Topic, 0, 10)
+ total, err := sess.Desc("topic.repo_count").FindAndCount(&topics)
+ return topics, total, err
+}
+
+// CountTopics counts the number of topics matching the FindTopicOptions
+func CountTopics(opts *FindTopicOptions) (int64, error) {
+ sess := x.Where(opts.toConds())
+ if opts.RepoID > 0 {
+ sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
+ }
+ return sess.Count(new(Topic))
}
// GetRepoTopicByName retrieves topic from name for a repo if it exist
@@ -269,7 +280,7 @@ func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
// SaveTopics save topics to a repository
func SaveTopics(repoID int64, topicNames ...string) error {
- topics, err := FindTopics(&FindTopicOptions{
+ topics, _, err := FindTopics(&FindTopicOptions{
RepoID: repoID,
})
if err != nil {
diff --git a/models/topic_test.go b/models/topic_test.go
index 25232eb981..9386a71e35 100644
--- a/models/topic_test.go
+++ b/models/topic_test.go
@@ -17,17 +17,18 @@ func TestAddTopic(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- topics, err := FindTopics(&FindTopicOptions{})
+ topics, _, err := FindTopics(&FindTopicOptions{})
assert.NoError(t, err)
assert.Len(t, topics, totalNrOfTopics)
- topics, err = FindTopics(&FindTopicOptions{
+ topics, total, err := FindTopics(&FindTopicOptions{
ListOptions: ListOptions{Page: 1, PageSize: 2},
})
assert.NoError(t, err)
assert.Len(t, topics, 2)
+ assert.EqualValues(t, 6, total)
- topics, err = FindTopics(&FindTopicOptions{
+ topics, _, err = FindTopics(&FindTopicOptions{
RepoID: 1,
})
assert.NoError(t, err)
@@ -35,11 +36,11 @@ func TestAddTopic(t *testing.T) {
assert.NoError(t, SaveTopics(2, "golang"))
repo2NrOfTopics = 1
- topics, err = FindTopics(&FindTopicOptions{})
+ topics, _, err = FindTopics(&FindTopicOptions{})
assert.NoError(t, err)
assert.Len(t, topics, totalNrOfTopics)
- topics, err = FindTopics(&FindTopicOptions{
+ topics, _, err = FindTopics(&FindTopicOptions{
RepoID: 2,
})
assert.NoError(t, err)
@@ -52,11 +53,11 @@ func TestAddTopic(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 1, topic.RepoCount)
- topics, err = FindTopics(&FindTopicOptions{})
+ topics, _, err = FindTopics(&FindTopicOptions{})
assert.NoError(t, err)
assert.Len(t, topics, totalNrOfTopics)
- topics, err = FindTopics(&FindTopicOptions{
+ topics, _, err = FindTopics(&FindTopicOptions{
RepoID: 2,
})
assert.NoError(t, err)
diff --git a/models/user.go b/models/user.go
index c68417a2c3..1af9e545ad 100644
--- a/models/user.go
+++ b/models/user.go
@@ -1704,7 +1704,7 @@ func GetStarredRepos(userID int64, private bool, listOptions ListOptions) ([]*Re
}
// GetWatchedRepos returns the repos watched by a particular user
-func GetWatchedRepos(userID int64, private bool, listOptions ListOptions) ([]*Repository, error) {
+func GetWatchedRepos(userID int64, private bool, listOptions ListOptions) ([]*Repository, int64, error) {
sess := x.Where("watch.user_id=?", userID).
And("`watch`.mode<>?", RepoWatchModeDont).
Join("LEFT", "watch", "`repository`.id=`watch`.repo_id")
@@ -1716,11 +1716,13 @@ func GetWatchedRepos(userID int64, private bool, listOptions ListOptions) ([]*Re
sess = listOptions.setSessionPagination(sess)
repos := make([]*Repository, 0, listOptions.PageSize)
- return repos, sess.Find(&repos)
+ total, err := sess.FindAndCount(&repos)
+ return repos, total, err
}
repos := make([]*Repository, 0, 10)
- return repos, sess.Find(&repos)
+ total, err := sess.FindAndCount(&repos)
+ return repos, total, err
}
// IterateUser iterate users
diff --git a/models/webhook.go b/models/webhook.go
index 138ba26bde..79ce70a0de 100644
--- a/models/webhook.go
+++ b/models/webhook.go
@@ -16,8 +16,10 @@ import (
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
+ "code.gitea.io/gitea/modules/util"
gouuid "github.com/google/uuid"
+ "xorm.io/builder"
)
// HookContentType is the content type of a web hook
@@ -387,53 +389,51 @@ func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
})
}
-// GetActiveWebhooksByRepoID returns all active webhooks of repository.
-func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
- return getActiveWebhooksByRepoID(x, repoID)
+// ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts
+type ListWebhookOptions struct {
+ ListOptions
+ RepoID int64
+ OrgID int64
+ IsActive util.OptionalBool
}
-func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
- webhooks := make([]*Webhook, 0, 5)
- return webhooks, e.Where("is_active=?", true).
- Find(&webhooks, &Webhook{RepoID: repoID})
-}
-
-// GetWebhooksByRepoID returns all webhooks of a repository.
-func GetWebhooksByRepoID(repoID int64, listOptions ListOptions) ([]*Webhook, error) {
- if listOptions.Page == 0 {
- webhooks := make([]*Webhook, 0, 5)
- return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
+func (opts *ListWebhookOptions) toCond() builder.Cond {
+ cond := builder.NewCond()
+ if opts.RepoID != 0 {
+ cond = cond.And(builder.Eq{"webhook.repo_id": opts.RepoID})
+ }
+ if opts.OrgID != 0 {
+ cond = cond.And(builder.Eq{"webhook.org_id": opts.OrgID})
+ }
+ if !opts.IsActive.IsNone() {
+ cond = cond.And(builder.Eq{"webhook.is_active": opts.IsActive.IsTrue()})
}
+ return cond
+}
- sess := listOptions.getPaginatedSession()
- webhooks := make([]*Webhook, 0, listOptions.PageSize)
+func listWebhooksByOpts(e Engine, opts *ListWebhookOptions) ([]*Webhook, error) {
+ sess := e.Where(opts.toCond())
- return webhooks, sess.Find(&webhooks, &Webhook{RepoID: repoID})
-}
+ if opts.Page != 0 {
+ sess = opts.setSessionPagination(sess)
+ webhooks := make([]*Webhook, 0, opts.PageSize)
+ err := sess.Find(&webhooks)
+ return webhooks, err
+ }
-// GetActiveWebhooksByOrgID returns all active webhooks for an organization.
-func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
- return getActiveWebhooksByOrgID(x, orgID)
+ webhooks := make([]*Webhook, 0, 10)
+ err := sess.Find(&webhooks)
+ return webhooks, err
}
-func getActiveWebhooksByOrgID(e Engine, orgID int64) (ws []*Webhook, err error) {
- err = e.
- Where("org_id=?", orgID).
- And("is_active=?", true).
- Find(&ws)
- return ws, err
+// ListWebhooksByOpts return webhooks based on options
+func ListWebhooksByOpts(opts *ListWebhookOptions) ([]*Webhook, error) {
+ return listWebhooksByOpts(x, opts)
}
-// GetWebhooksByOrgID returns paginated webhooks for an organization.
-func GetWebhooksByOrgID(orgID int64, listOptions ListOptions) ([]*Webhook, error) {
- if listOptions.Page == 0 {
- ws := make([]*Webhook, 0, 5)
- return ws, x.Find(&ws, &Webhook{OrgID: orgID})
- }
-
- sess := listOptions.getPaginatedSession()
- ws := make([]*Webhook, 0, listOptions.PageSize)
- return ws, sess.Find(&ws, &Webhook{OrgID: orgID})
+// CountWebhooksByOpts count webhooks based on options and ignore pagination
+func CountWebhooksByOpts(opts *ListWebhookOptions) (int64, error) {
+ return x.Where(opts.toCond()).Count(&Webhook{})
}
// GetDefaultWebhooks returns all admin-default webhooks.
diff --git a/models/webhook_test.go b/models/webhook_test.go
index 84520666c0..625d643856 100644
--- a/models/webhook_test.go
+++ b/models/webhook_test.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
+ "code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert"
)
@@ -118,7 +119,7 @@ func TestGetWebhookByOrgID(t *testing.T) {
func TestGetActiveWebhooksByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- hooks, err := GetActiveWebhooksByRepoID(1)
+ hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1, IsActive: util.OptionalBoolTrue})
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(1), hooks[0].ID)
@@ -128,7 +129,7 @@ func TestGetActiveWebhooksByRepoID(t *testing.T) {
func TestGetWebhooksByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- hooks, err := GetWebhooksByRepoID(1, ListOptions{})
+ hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1})
assert.NoError(t, err)
if assert.Len(t, hooks, 2) {
assert.Equal(t, int64(1), hooks[0].ID)
@@ -138,7 +139,7 @@ func TestGetWebhooksByRepoID(t *testing.T) {
func TestGetActiveWebhooksByOrgID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- hooks, err := GetActiveWebhooksByOrgID(3)
+ hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3, IsActive: util.OptionalBoolTrue})
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(3), hooks[0].ID)
@@ -148,7 +149,7 @@ func TestGetActiveWebhooksByOrgID(t *testing.T) {
func TestGetWebhooksByOrgID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- hooks, err := GetWebhooksByOrgID(3, ListOptions{})
+ hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3})
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(3), hooks[0].ID)