mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Rewrite XORM queries
This commit is contained in:
		@@ -124,14 +124,18 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
 | 
			
		||||
// GetAccessibleRepositories finds repositories which the user has access but does not own.
 | 
			
		||||
// If limit is smaller than 1 means returns all found results.
 | 
			
		||||
func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
 | 
			
		||||
	sess := x.Where("owner_id !=? ", user.ID).Desc("updated_unix")
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Where("owner_id !=? ", user.ID).
 | 
			
		||||
		Desc("updated_unix")
 | 
			
		||||
	if limit > 0 {
 | 
			
		||||
		sess.Limit(limit)
 | 
			
		||||
		repos = make([]*Repository, 0, limit)
 | 
			
		||||
	} else {
 | 
			
		||||
		repos = make([]*Repository, 0, 10)
 | 
			
		||||
	}
 | 
			
		||||
	return repos, sess.Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).Find(&repos)
 | 
			
		||||
	return repos, sess.
 | 
			
		||||
		Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).
 | 
			
		||||
		Find(&repos)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func maxAccessMode(modes ...AccessMode) AccessMode {
 | 
			
		||||
 
 | 
			
		||||
@@ -591,9 +591,14 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error
 | 
			
		||||
// actorID can be -1 when isProfile is true or to skip the permission check.
 | 
			
		||||
func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
 | 
			
		||||
	actions := make([]*Action, 0, 20)
 | 
			
		||||
	sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID)
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Limit(20, int(offset)).
 | 
			
		||||
		Desc("id").
 | 
			
		||||
		Where("user_id = ?", ctxUser.ID)
 | 
			
		||||
	if isProfile {
 | 
			
		||||
		sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
 | 
			
		||||
		sess.
 | 
			
		||||
			And("is_private = ?", false).
 | 
			
		||||
			And("act_user_id = ?", ctxUser.ID)
 | 
			
		||||
	} else if actorID != -1 && ctxUser.IsOrganization() {
 | 
			
		||||
		// FIXME: only need to get IDs here, not all fields of repository.
 | 
			
		||||
		repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
 | 
			
		||||
 
 | 
			
		||||
@@ -103,7 +103,10 @@ func CountNotices() int64 {
 | 
			
		||||
// Notices returns number of notices in given page.
 | 
			
		||||
func Notices(page, pageSize int) ([]*Notice, error) {
 | 
			
		||||
	notices := make([]*Notice, 0, pageSize)
 | 
			
		||||
	return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(¬ices)
 | 
			
		||||
	return notices, x.
 | 
			
		||||
		Limit(pageSize, (page-1)*pageSize).
 | 
			
		||||
		Desc("id").
 | 
			
		||||
		Find(¬ices)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeleteNotice deletes a system notice by given ID.
 | 
			
		||||
@@ -127,6 +130,8 @@ func DeleteNoticesByIDs(ids []int64) error {
 | 
			
		||||
	if len(ids) == 0 {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	_, err := x.Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
 | 
			
		||||
	_, err := x.
 | 
			
		||||
		Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").
 | 
			
		||||
		Delete(new(Notice))
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -17,12 +17,12 @@ import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/Unknwon/com"
 | 
			
		||||
	"github.com/go-gitea/git"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/base"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/log"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/process"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/setting"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/template/highlight"
 | 
			
		||||
	"github.com/go-gitea/git"
 | 
			
		||||
	"github.com/sergi/go-diff/diffmatchpatch"
 | 
			
		||||
	"golang.org/x/net/html/charset"
 | 
			
		||||
	"golang.org/x/text/transform"
 | 
			
		||||
 
 | 
			
		||||
@@ -15,8 +15,8 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/Unknwon/com"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
	api "github.com/go-gitea/go-sdk/gitea"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
	gouuid "github.com/satori/go.uuid"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/base"
 | 
			
		||||
@@ -820,13 +820,17 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
 | 
			
		||||
	sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
 | 
			
		||||
 | 
			
		||||
	if opts.RepoID > 0 {
 | 
			
		||||
		sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed)
 | 
			
		||||
		sess.
 | 
			
		||||
			Where("issue.repo_id=?", opts.RepoID).
 | 
			
		||||
			And("issue.is_closed=?", opts.IsClosed)
 | 
			
		||||
	} else if opts.RepoIDs != nil {
 | 
			
		||||
		// In case repository IDs are provided but actually no repository has issue.
 | 
			
		||||
		if len(opts.RepoIDs) == 0 {
 | 
			
		||||
			return make([]*Issue, 0), nil
 | 
			
		||||
		}
 | 
			
		||||
		sess.In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).And("issue.is_closed=?", opts.IsClosed)
 | 
			
		||||
		sess.
 | 
			
		||||
			In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).
 | 
			
		||||
			And("issue.is_closed=?", opts.IsClosed)
 | 
			
		||||
	} else {
 | 
			
		||||
		sess.Where("issue.is_closed=?", opts.IsClosed)
 | 
			
		||||
	}
 | 
			
		||||
@@ -863,12 +867,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
 | 
			
		||||
	if len(opts.Labels) > 0 && opts.Labels != "0" {
 | 
			
		||||
		labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
 | 
			
		||||
		if len(labelIDs) > 0 {
 | 
			
		||||
			sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").In("issue_label.label_id", labelIDs)
 | 
			
		||||
			sess.
 | 
			
		||||
				Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
 | 
			
		||||
				In("issue_label.label_id", labelIDs)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if opts.IsMention {
 | 
			
		||||
		sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").And("issue_user.is_mentioned = ?", true)
 | 
			
		||||
		sess.
 | 
			
		||||
			Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
 | 
			
		||||
			And("issue_user.is_mentioned = ?", true)
 | 
			
		||||
 | 
			
		||||
		if opts.UserID > 0 {
 | 
			
		||||
			sess.And("issue_user.uid = ?", opts.UserID)
 | 
			
		||||
@@ -991,7 +999,10 @@ func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*Issue
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ius := make([]*IssueUser, 0, 10)
 | 
			
		||||
	sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed).In("repo_id", rids)
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Limit(20, (page-1)*20).
 | 
			
		||||
		Where("is_closed=?", isClosed).
 | 
			
		||||
		In("repo_id", rids)
 | 
			
		||||
	err := sess.Find(&ius)
 | 
			
		||||
	return ius, err
 | 
			
		||||
}
 | 
			
		||||
@@ -999,7 +1010,10 @@ func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*Issue
 | 
			
		||||
// GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
 | 
			
		||||
func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
 | 
			
		||||
	ius := make([]*IssueUser, 0, 10)
 | 
			
		||||
	sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Limit(20, (page-1)*20).
 | 
			
		||||
		Where("uid=?", uid).
 | 
			
		||||
		And("is_closed=?", isClosed)
 | 
			
		||||
	if rid > 0 {
 | 
			
		||||
		sess.And("repo_id=?", rid)
 | 
			
		||||
	}
 | 
			
		||||
@@ -1101,12 +1115,16 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
 | 
			
		||||
	stats := &IssueStats{}
 | 
			
		||||
 | 
			
		||||
	countSession := func(opts *IssueStatsOptions) *xorm.Session {
 | 
			
		||||
		sess := x.Where("issue.repo_id = ?", opts.RepoID).And("is_pull = ?", opts.IsPull)
 | 
			
		||||
		sess := x.
 | 
			
		||||
			Where("issue.repo_id = ?", opts.RepoID).
 | 
			
		||||
			And("is_pull = ?", opts.IsPull)
 | 
			
		||||
 | 
			
		||||
		if len(opts.Labels) > 0 && opts.Labels != "0" {
 | 
			
		||||
			labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
 | 
			
		||||
			if len(labelIDs) > 0 {
 | 
			
		||||
				sess.Join("INNER", "issue_label", "issue.id = issue_id").In("label_id", labelIDs)
 | 
			
		||||
				sess.
 | 
			
		||||
					Join("INNER", "issue_label", "issue.id = issue_id").
 | 
			
		||||
					In("label_id", labelIDs)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -1163,7 +1181,9 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
 | 
			
		||||
	stats := &IssueStats{}
 | 
			
		||||
 | 
			
		||||
	countSession := func(isClosed, isPull bool, repoID int64, repoIDs []int64) *xorm.Session {
 | 
			
		||||
		sess := x.Where("issue.is_closed = ?", isClosed).And("issue.is_pull = ?", isPull)
 | 
			
		||||
		sess := x.
 | 
			
		||||
			Where("issue.is_closed = ?", isClosed).
 | 
			
		||||
			And("issue.is_pull = ?", isPull)
 | 
			
		||||
 | 
			
		||||
		if repoID > 0 || len(repoIDs) == 0 {
 | 
			
		||||
			sess.And("repo_id = ?", repoID)
 | 
			
		||||
@@ -1203,7 +1223,8 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
 | 
			
		||||
// GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
 | 
			
		||||
func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
 | 
			
		||||
	countSession := func(isClosed, isPull bool, repoID int64) *xorm.Session {
 | 
			
		||||
		sess := x.Where("issue.repo_id = ?", isClosed).
 | 
			
		||||
		sess := x.
 | 
			
		||||
			Where("issue.repo_id = ?", isClosed).
 | 
			
		||||
			And("is_pull = ?", isPull).
 | 
			
		||||
			And("repo_id = ?", repoID)
 | 
			
		||||
 | 
			
		||||
@@ -1463,7 +1484,9 @@ func UpdateMilestone(m *Milestone) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func countRepoMilestones(e Engine, repoID int64) int64 {
 | 
			
		||||
	count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
 | 
			
		||||
	count, _ := e.
 | 
			
		||||
		Where("repo_id=?", repoID).
 | 
			
		||||
		Count(new(Milestone))
 | 
			
		||||
	return count
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1473,7 +1496,9 @@ func CountRepoMilestones(repoID int64) int64 {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func countRepoClosedMilestones(e Engine, repoID int64) int64 {
 | 
			
		||||
	closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
 | 
			
		||||
	closed, _ := e.
 | 
			
		||||
		Where("repo_id=? AND is_closed=?", repoID, true).
 | 
			
		||||
		Count(new(Milestone))
 | 
			
		||||
	return closed
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1484,7 +1509,9 @@ func CountRepoClosedMilestones(repoID int64) int64 {
 | 
			
		||||
 | 
			
		||||
// MilestoneStats returns number of open and closed milestones of given repository.
 | 
			
		||||
func MilestoneStats(repoID int64) (open int64, closed int64) {
 | 
			
		||||
	open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
 | 
			
		||||
	open, _ = x.
 | 
			
		||||
		Where("repo_id=? AND is_closed=?", repoID, false).
 | 
			
		||||
		Count(new(Milestone))
 | 
			
		||||
	return open, CountRepoClosedMilestones(repoID)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -356,7 +356,9 @@ func GetCommentByID(id int64) (*Comment, error) {
 | 
			
		||||
 | 
			
		||||
func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
 | 
			
		||||
	comments := make([]*Comment, 0, 10)
 | 
			
		||||
	sess := e.Where("issue_id = ?", issueID).Asc("created_unix")
 | 
			
		||||
	sess := e.
 | 
			
		||||
		Where("issue_id = ?", issueID).
 | 
			
		||||
		Asc("created_unix")
 | 
			
		||||
	if since > 0 {
 | 
			
		||||
		sess.And("updated_unix >= ?", since)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -138,13 +138,20 @@ func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
 | 
			
		||||
// it silently ignores label IDs that are not belong to the repository.
 | 
			
		||||
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
 | 
			
		||||
	labels := make([]*Label, 0, len(labelIDs))
 | 
			
		||||
	return labels, x.Where("repo_id = ?", repoID).In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
 | 
			
		||||
	return labels, x.
 | 
			
		||||
		Where("repo_id = ?", repoID).
 | 
			
		||||
		In("id", base.Int64sToStrings(labelIDs)).
 | 
			
		||||
		Asc("name").
 | 
			
		||||
		Find(&labels)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetLabelsByRepoID returns all labels that belong to given repository by ID.
 | 
			
		||||
func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
 | 
			
		||||
	labels := make([]*Label, 0, 10)
 | 
			
		||||
	return labels, x.Where("repo_id = ?", repoID).Asc("name").Find(&labels)
 | 
			
		||||
	return labels, x.
 | 
			
		||||
		Where("repo_id = ?", repoID).
 | 
			
		||||
		Asc("name").
 | 
			
		||||
		Find(&labels)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
 | 
			
		||||
@@ -161,7 +168,11 @@ func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	labels := make([]*Label, 0, len(labelIDs))
 | 
			
		||||
	return labels, e.Where("id > 0").In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
 | 
			
		||||
	return labels, e.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		In("id", base.Int64sToStrings(labelIDs)).
 | 
			
		||||
		Asc("name").
 | 
			
		||||
		Find(&labels)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetLabelsByIssueID returns all labels that belong to given issue by ID.
 | 
			
		||||
@@ -197,7 +208,9 @@ func DeleteLabel(repoID, labelID int64) error {
 | 
			
		||||
 | 
			
		||||
	if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil {
 | 
			
		||||
	} else if _, err = sess.
 | 
			
		||||
		Where("label_id = ?", labelID).
 | 
			
		||||
		Delete(new(IssueLabel)); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -293,7 +306,10 @@ func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
 | 
			
		||||
 | 
			
		||||
func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
 | 
			
		||||
	issueLabels := make([]*IssueLabel, 0, 10)
 | 
			
		||||
	return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)
 | 
			
		||||
	return issueLabels, e.
 | 
			
		||||
		Where("issue_id=?", issueID).
 | 
			
		||||
		Asc("label_id").
 | 
			
		||||
		Find(&issueLabels)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetIssueLabels returns all issue-label relations of given issue by ID.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										108
									
								
								models/org.go
									
									
									
									
									
								
							
							
						
						
									
										108
									
								
								models/org.go
									
									
									
									
									
								
							@@ -13,7 +13,6 @@ import (
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/base"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
@@ -50,7 +49,10 @@ func (org *User) GetOwnerTeam() (*Team, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (org *User) getTeams(e Engine) error {
 | 
			
		||||
	return e.Where("org_id=?", org.ID).OrderBy("CASE WHEN name LIKE '" + OWNER_TEAM + "' THEN '' ELSE name END").Find(&org.Teams)
 | 
			
		||||
	return e.
 | 
			
		||||
		Where("org_id=?", org.ID).
 | 
			
		||||
		OrderBy("CASE WHEN name LIKE '" + OWNER_TEAM + "' THEN '' ELSE name END").
 | 
			
		||||
		Find(&org.Teams)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetTeams returns all teams that belong to organization.
 | 
			
		||||
@@ -181,14 +183,20 @@ func GetOrgByName(name string) (*User, error) {
 | 
			
		||||
 | 
			
		||||
// CountOrganizations returns number of organizations.
 | 
			
		||||
func CountOrganizations() int64 {
 | 
			
		||||
	count, _ := x.Where("type=1").Count(new(User))
 | 
			
		||||
	count, _ := x.
 | 
			
		||||
		Where("type=1").
 | 
			
		||||
		Count(new(User))
 | 
			
		||||
	return count
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Organizations returns number of organizations in given page.
 | 
			
		||||
func Organizations(page, pageSize int) ([]*User, error) {
 | 
			
		||||
	orgs := make([]*User, 0, pageSize)
 | 
			
		||||
	return orgs, x.Limit(pageSize, (page-1)*pageSize).Where("type=1").Asc("name").Find(&orgs)
 | 
			
		||||
	return orgs, x.
 | 
			
		||||
		Limit(pageSize, (page-1)*pageSize).
 | 
			
		||||
		Where("type=1").
 | 
			
		||||
		Asc("name").
 | 
			
		||||
		Find(&orgs)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeleteOrganization completely and permanently deletes everything of organization.
 | 
			
		||||
@@ -237,19 +245,30 @@ type OrgUser struct {
 | 
			
		||||
 | 
			
		||||
// IsOrganizationOwner returns true if given user is in the owner team.
 | 
			
		||||
func IsOrganizationOwner(orgId, uid int64) bool {
 | 
			
		||||
	has, _ := x.Where("is_owner=?", true).And("uid=?", uid).And("org_id=?", orgId).Get(new(OrgUser))
 | 
			
		||||
	has, _ := x.
 | 
			
		||||
		Where("is_owner=?", true).
 | 
			
		||||
		And("uid=?", uid).
 | 
			
		||||
		And("org_id=?", orgId).
 | 
			
		||||
		Get(new(OrgUser))
 | 
			
		||||
	return has
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsOrganizationMember returns true if given user is member of organization.
 | 
			
		||||
func IsOrganizationMember(orgId, uid int64) bool {
 | 
			
		||||
	has, _ := x.Where("uid=?", uid).And("org_id=?", orgId).Get(new(OrgUser))
 | 
			
		||||
	has, _ := x.
 | 
			
		||||
		Where("uid=?", uid).
 | 
			
		||||
		And("org_id=?", orgId).
 | 
			
		||||
		Get(new(OrgUser))
 | 
			
		||||
	return has
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsPublicMembership returns true if given user public his/her membership.
 | 
			
		||||
func IsPublicMembership(orgId, uid int64) bool {
 | 
			
		||||
	has, _ := x.Where("uid=?", uid).And("org_id=?", orgId).And("is_public=?", true).Get(new(OrgUser))
 | 
			
		||||
	has, _ := x.
 | 
			
		||||
		Where("uid=?", uid).
 | 
			
		||||
		And("org_id=?", orgId).
 | 
			
		||||
		And("is_public=?", true).
 | 
			
		||||
		Get(new(OrgUser))
 | 
			
		||||
	return has
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -319,14 +338,19 @@ func GetOrgUsersByUserID(uid int64, all bool) ([]*OrgUser, error) {
 | 
			
		||||
// GetOrgUsersByOrgID returns all organization-user relations by organization ID.
 | 
			
		||||
func GetOrgUsersByOrgID(orgID int64) ([]*OrgUser, error) {
 | 
			
		||||
	ous := make([]*OrgUser, 0, 10)
 | 
			
		||||
	err := x.Where("org_id=?", orgID).Find(&ous)
 | 
			
		||||
	err := x.
 | 
			
		||||
		Where("org_id=?", orgID).
 | 
			
		||||
		Find(&ous)
 | 
			
		||||
	return ous, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ChangeOrgUserStatus changes public or private membership status.
 | 
			
		||||
func ChangeOrgUserStatus(orgID, uid int64, public bool) error {
 | 
			
		||||
	ou := new(OrgUser)
 | 
			
		||||
	has, err := x.Where("uid=?", uid).And("org_id=?", orgID).Get(ou)
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Where("uid=?", uid).
 | 
			
		||||
		And("org_id=?", orgID).
 | 
			
		||||
		Get(ou)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
@@ -370,7 +394,10 @@ func AddOrgUser(orgID, uid int64) error {
 | 
			
		||||
func RemoveOrgUser(orgID, userID int64) error {
 | 
			
		||||
	ou := new(OrgUser)
 | 
			
		||||
 | 
			
		||||
	has, err := x.Where("uid=?", userID).And("org_id=?", orgID).Get(ou)
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Where("uid=?", userID).
 | 
			
		||||
		And("org_id=?", orgID).
 | 
			
		||||
		Get(ou)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("get org-user: %v", err)
 | 
			
		||||
	} else if !has {
 | 
			
		||||
@@ -425,7 +452,10 @@ func RemoveOrgUser(orgID, userID int64) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(repoIDs) > 0 {
 | 
			
		||||
		if _, err = sess.Where("user_id = ?", user.ID).In("repo_id", repoIDs).Delete(new(Access)); err != nil {
 | 
			
		||||
		if _, err = sess.
 | 
			
		||||
			Where("user_id = ?", user.ID).
 | 
			
		||||
			In("repo_id", repoIDs).
 | 
			
		||||
			Delete(new(Access)); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@@ -506,32 +536,29 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
 | 
			
		||||
		page = 1
 | 
			
		||||
	}
 | 
			
		||||
	repos := make([]*Repository, 0, pageSize)
 | 
			
		||||
	// FIXME: use XORM chain operations instead of raw SQL.
 | 
			
		||||
	if err = x.SQL(fmt.Sprintf(`SELECT repository.* FROM repository
 | 
			
		||||
	INNER JOIN team_repo
 | 
			
		||||
	ON team_repo.repo_id = repository.id
 | 
			
		||||
	WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
 | 
			
		||||
	GROUP BY repository.id
 | 
			
		||||
	ORDER BY updated_unix DESC
 | 
			
		||||
	LIMIT %d OFFSET %d`,
 | 
			
		||||
		strings.Join(base.Int64sToStrings(teamIDs), ","), pageSize, (page-1)*pageSize),
 | 
			
		||||
		org.ID, false).Find(&repos); err != nil {
 | 
			
		||||
	if err := x.
 | 
			
		||||
		Select("`repository`.*").
 | 
			
		||||
		Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
 | 
			
		||||
		Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
 | 
			
		||||
		Or("team_repo.team_id IN (?)", strings.Join(base.Int64sToStrings(teamIDs), ",")).
 | 
			
		||||
		GroupBy("`repository`.id").
 | 
			
		||||
		OrderBy("updated_unix DESC").
 | 
			
		||||
		Limit(pageSize, (page-1)*pageSize).
 | 
			
		||||
		Find(&repos); err != nil {
 | 
			
		||||
		return nil, 0, fmt.Errorf("get repositories: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	results, err := x.Query(fmt.Sprintf(`SELECT repository.id FROM repository
 | 
			
		||||
	INNER JOIN team_repo
 | 
			
		||||
	ON team_repo.repo_id = repository.id
 | 
			
		||||
	WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
 | 
			
		||||
	GROUP BY repository.id
 | 
			
		||||
	ORDER BY updated_unix DESC`,
 | 
			
		||||
		strings.Join(base.Int64sToStrings(teamIDs), ",")),
 | 
			
		||||
		org.ID, false)
 | 
			
		||||
	repoCount, err := x.
 | 
			
		||||
		Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
 | 
			
		||||
		Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
 | 
			
		||||
		Or("team_repo.team_id IN (?)", strings.Join(base.Int64sToStrings(teamIDs), ",")).
 | 
			
		||||
		GroupBy("`repository`.id").
 | 
			
		||||
		Count(&Repository{})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error(4, "count user repositories in organization: %v", err)
 | 
			
		||||
		return nil, 0, fmt.Errorf("count user repositories in organization: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return repos, int64(len(results)), nil
 | 
			
		||||
	return repos, repoCount, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetUserRepositories returns mirror repositories of the organization
 | 
			
		||||
@@ -546,15 +573,12 @@ func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	repos := make([]*Repository, 0, 10)
 | 
			
		||||
	if err = x.SQL(fmt.Sprintf(`SELECT repository.* FROM repository
 | 
			
		||||
	INNER JOIN team_repo
 | 
			
		||||
	ON team_repo.repo_id = repository.id AND repository.is_mirror = ?
 | 
			
		||||
	WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
 | 
			
		||||
	GROUP BY repository.id
 | 
			
		||||
	ORDER BY updated_unix DESC`,
 | 
			
		||||
		strings.Join(base.Int64sToStrings(teamIDs), ",")),
 | 
			
		||||
		true, org.ID, false).Find(&repos); err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("get repositories: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	return repos, nil
 | 
			
		||||
	return repos, x.
 | 
			
		||||
		Select("`repository`.*").
 | 
			
		||||
		Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
 | 
			
		||||
		Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
 | 
			
		||||
		Or("team_repo.team_id IN (?)", strings.Join(base.Int64sToStrings(teamIDs), ",")).
 | 
			
		||||
		GroupBy("`repository`.id").
 | 
			
		||||
		OrderBy("updated_unix DESC").
 | 
			
		||||
		Find(&repos)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,9 @@ func (t *Team) IsMember(uid int64) bool {
 | 
			
		||||
 | 
			
		||||
func (t *Team) getRepositories(e Engine) (err error) {
 | 
			
		||||
	teamRepos := make([]*TeamRepo, 0, t.NumRepos)
 | 
			
		||||
	if err = x.Where("team_id=?", t.ID).Find(&teamRepos); err != nil {
 | 
			
		||||
	if err = x.
 | 
			
		||||
		Where("team_id=?", t.ID).
 | 
			
		||||
		Find(&teamRepos); err != nil {
 | 
			
		||||
		return fmt.Errorf("get team-repos: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -225,7 +227,10 @@ func NewTeam(t *Team) (err error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t.LowerName = strings.ToLower(t.Name)
 | 
			
		||||
	has, err = x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).Get(new(Team))
 | 
			
		||||
	has, err = x.
 | 
			
		||||
		Where("org_id=?", t.OrgID).
 | 
			
		||||
		And("lower_name=?", t.LowerName).
 | 
			
		||||
		Get(new(Team))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if has {
 | 
			
		||||
@@ -303,7 +308,11 @@ func UpdateTeam(t *Team, authChanged bool) (err error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t.LowerName = strings.ToLower(t.Name)
 | 
			
		||||
	has, err := x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).And("id!=?", t.ID).Get(new(Team))
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Where("org_id=?", t.OrgID).
 | 
			
		||||
		And("lower_name=?", t.LowerName).
 | 
			
		||||
		And("id!=?", t.ID).
 | 
			
		||||
		Get(new(Team))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if has {
 | 
			
		||||
@@ -357,7 +366,10 @@ func DeleteTeam(t *Team) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Delete team-user.
 | 
			
		||||
	if _, err = sess.Where("org_id=?", org.ID).Where("team_id=?", t.ID).Delete(new(TeamUser)); err != nil {
 | 
			
		||||
	if _, err = sess.
 | 
			
		||||
		Where("org_id=?", org.ID).
 | 
			
		||||
		Where("team_id=?", t.ID).
 | 
			
		||||
		Delete(new(TeamUser)); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -389,7 +401,11 @@ type TeamUser struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func isTeamMember(e Engine, orgID, teamID, uid int64) bool {
 | 
			
		||||
	has, _ := e.Where("org_id=?", orgID).And("team_id=?", teamID).And("uid=?", uid).Get(new(TeamUser))
 | 
			
		||||
	has, _ := e.
 | 
			
		||||
		Where("org_id=?", orgID).
 | 
			
		||||
		And("team_id=?", teamID).
 | 
			
		||||
		And("uid=?", uid).
 | 
			
		||||
		Get(new(TeamUser))
 | 
			
		||||
	return has
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -400,7 +416,9 @@ func IsTeamMember(orgID, teamID, uid int64) bool {
 | 
			
		||||
 | 
			
		||||
func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
 | 
			
		||||
	teamUsers := make([]*TeamUser, 0, 10)
 | 
			
		||||
	if err = e.Where("team_id=?", teamID).Find(&teamUsers); err != nil {
 | 
			
		||||
	if err = e.
 | 
			
		||||
		Where("team_id=?", teamID).
 | 
			
		||||
		Find(&teamUsers); err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("get team-users: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	members := make([]*User, 0, len(teamUsers))
 | 
			
		||||
@@ -421,7 +439,10 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
 | 
			
		||||
 | 
			
		||||
func getUserTeams(e Engine, orgId, uid int64) ([]*Team, error) {
 | 
			
		||||
	tus := make([]*TeamUser, 0, 5)
 | 
			
		||||
	if err := e.Where("uid=?", uid).And("org_id=?", orgId).Find(&tus); err != nil {
 | 
			
		||||
	if err := e.
 | 
			
		||||
		Where("uid=?", uid).
 | 
			
		||||
		And("org_id=?", orgId).
 | 
			
		||||
		Find(&tus); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -492,7 +513,10 @@ func AddTeamMember(orgID, teamID, uid int64) error {
 | 
			
		||||
 | 
			
		||||
	// We make sure it exists before.
 | 
			
		||||
	ou := new(OrgUser)
 | 
			
		||||
	if _, err = sess.Where("uid = ?", uid).And("org_id = ?", orgID).Get(ou); err != nil {
 | 
			
		||||
	if _, err = sess.
 | 
			
		||||
		Where("uid = ?", uid).
 | 
			
		||||
		And("org_id = ?", orgID).
 | 
			
		||||
		Get(ou); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	ou.NumTeams++
 | 
			
		||||
@@ -541,7 +565,10 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
 | 
			
		||||
	}
 | 
			
		||||
	if _, err := e.Delete(tu); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if _, err = e.Id(t.ID).AllCols().Update(t); err != nil {
 | 
			
		||||
	} else if _, err = e.
 | 
			
		||||
		Id(t.ID).
 | 
			
		||||
		AllCols().
 | 
			
		||||
		Update(t); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -554,7 +581,10 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
 | 
			
		||||
 | 
			
		||||
	// This must exist.
 | 
			
		||||
	ou := new(OrgUser)
 | 
			
		||||
	_, err = e.Where("uid = ?", uid).And("org_id = ?", org.ID).Get(ou)
 | 
			
		||||
	_, err = e.
 | 
			
		||||
		Where("uid = ?", uid).
 | 
			
		||||
		And("org_id = ?", org.ID).
 | 
			
		||||
		Get(ou)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -562,7 +592,10 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
 | 
			
		||||
	if t.IsOwnerTeam() {
 | 
			
		||||
		ou.IsOwner = false
 | 
			
		||||
	}
 | 
			
		||||
	if _, err = e.Id(ou.ID).AllCols().Update(ou); err != nil {
 | 
			
		||||
	if _, err = e.
 | 
			
		||||
		Id(ou.ID).
 | 
			
		||||
		AllCols().
 | 
			
		||||
		Update(ou); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
@@ -597,7 +630,11 @@ type TeamRepo struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool {
 | 
			
		||||
	has, _ := e.Where("org_id=?", orgID).And("team_id=?", teamID).And("repo_id=?", repoID).Get(new(TeamRepo))
 | 
			
		||||
	has, _ := e.
 | 
			
		||||
		Where("org_id=?", orgID).
 | 
			
		||||
		And("team_id=?", teamID).
 | 
			
		||||
		And("repo_id=?", repoID).
 | 
			
		||||
		Get(new(TeamRepo))
 | 
			
		||||
	return has
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,13 +12,13 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/Unknwon/com"
 | 
			
		||||
	"github.com/go-gitea/git"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/log"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/process"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/setting"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/sync"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
	"github.com/go-gitea/git"
 | 
			
		||||
	api "github.com/go-gitea/go-sdk/gitea"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var PullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
 | 
			
		||||
@@ -462,9 +462,11 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
 | 
			
		||||
// by given head/base and repo/branch.
 | 
			
		||||
func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
 | 
			
		||||
	pr := new(PullRequest)
 | 
			
		||||
	has, err := x.Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
 | 
			
		||||
			headRepoID, headBranch, baseRepoID, baseBranch, false, false).
 | 
			
		||||
		Join("INNER", "issue", "issue.id=pull_request.issue_id").Get(pr)
 | 
			
		||||
		Join("INNER", "issue", "issue.id=pull_request.issue_id").
 | 
			
		||||
		Get(pr)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
@@ -478,18 +480,22 @@ func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch
 | 
			
		||||
// by given head information (repo and branch).
 | 
			
		||||
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
 | 
			
		||||
	prs := make([]*PullRequest, 0, 2)
 | 
			
		||||
	return prs, x.Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
 | 
			
		||||
	return prs, x.
 | 
			
		||||
		Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
 | 
			
		||||
			repoID, branch, false, false).
 | 
			
		||||
		Join("INNER", "issue", "issue.id = pull_request.issue_id").Find(&prs)
 | 
			
		||||
		Join("INNER", "issue", "issue.id = pull_request.issue_id").
 | 
			
		||||
		Find(&prs)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetUnmergedPullRequestsByBaseInfo returnss all pull requests that are open and has not been merged
 | 
			
		||||
// by given base information (repo and branch).
 | 
			
		||||
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
 | 
			
		||||
	prs := make([]*PullRequest, 0, 2)
 | 
			
		||||
	return prs, x.Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
 | 
			
		||||
	return prs, x.
 | 
			
		||||
		Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
 | 
			
		||||
			repoID, branch, false, false).
 | 
			
		||||
		Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs)
 | 
			
		||||
		Join("INNER", "issue", "issue.id=pull_request.issue_id").
 | 
			
		||||
		Find(&prs)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
 | 
			
		||||
@@ -638,7 +644,10 @@ func (prs PullRequestList) loadAttributes(e Engine) error {
 | 
			
		||||
		issueIDs = append(issueIDs, prs[i].IssueID)
 | 
			
		||||
	}
 | 
			
		||||
	issues := make([]*Issue, 0, len(issueIDs))
 | 
			
		||||
	if err := e.Where("id > 0").In("id", issueIDs).Find(&issues); err != nil {
 | 
			
		||||
	if err := e.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		In("id", issueIDs).
 | 
			
		||||
		Find(&issues); err != nil {
 | 
			
		||||
		return fmt.Errorf("find issues: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -725,7 +734,10 @@ func ChangeUsernameInPullRequests(oldUserName, newUserName string) error {
 | 
			
		||||
	pr := PullRequest{
 | 
			
		||||
		HeadUserName: strings.ToLower(newUserName),
 | 
			
		||||
	}
 | 
			
		||||
	_, err := x.Cols("head_user_name").Where("head_user_name = ?", strings.ToLower(oldUserName)).Update(pr)
 | 
			
		||||
	_, err := x.
 | 
			
		||||
		Cols("head_user_name").
 | 
			
		||||
		Where("head_user_name = ?", strings.ToLower(oldUserName)).
 | 
			
		||||
		Update(pr)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -127,7 +127,9 @@ func GetRelease(repoID int64, tagName string) (*Release, error) {
 | 
			
		||||
// GetReleaseByID returns release with given ID.
 | 
			
		||||
func GetReleaseByID(id int64) (*Release, error) {
 | 
			
		||||
	rel := new(Release)
 | 
			
		||||
	has, err := x.Id(id).Get(rel)
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Id(id).
 | 
			
		||||
		Get(rel)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
@@ -142,7 +144,10 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err
 | 
			
		||||
	if page <= 0 {
 | 
			
		||||
		page = 1
 | 
			
		||||
	}
 | 
			
		||||
	err = x.Desc("created_unix").Limit(pageSize, (page-1)*pageSize).Find(&rels, Release{RepoID: repoID})
 | 
			
		||||
	err = x.
 | 
			
		||||
		Desc("created_unix").
 | 
			
		||||
		Limit(pageSize, (page-1)*pageSize).
 | 
			
		||||
		Find(&rels, Release{RepoID: repoID})
 | 
			
		||||
	return rels, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -21,15 +21,15 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/Unknwon/cae/zip"
 | 
			
		||||
	"github.com/Unknwon/com"
 | 
			
		||||
	"github.com/go-gitea/git"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/bindata"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/log"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/markdown"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/process"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/setting"
 | 
			
		||||
	"github.com/go-gitea/gitea/modules/sync"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
	"github.com/go-gitea/git"
 | 
			
		||||
	api "github.com/go-gitea/go-sdk/gitea"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
	version "github.com/mcuadros/go-version"
 | 
			
		||||
	ini "gopkg.in/ini.v1"
 | 
			
		||||
)
 | 
			
		||||
@@ -334,7 +334,9 @@ func (repo *Repository) getAssignees(e Engine) (_ []*User, err error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	accesses := make([]*Access, 0, 10)
 | 
			
		||||
	if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, AccessModeWrite).Find(&accesses); err != nil {
 | 
			
		||||
	if err = e.
 | 
			
		||||
		Where("repo_id = ? AND mode >= ?", repo.ID, AccessModeWrite).
 | 
			
		||||
		Find(&accesses); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -1209,7 +1211,9 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
 | 
			
		||||
 | 
			
		||||
func getRepositoriesByForkID(e Engine, forkID int64) ([]*Repository, error) {
 | 
			
		||||
	repos := make([]*Repository, 0, 10)
 | 
			
		||||
	return repos, e.Where("fork_id=?", forkID).Find(&repos)
 | 
			
		||||
	return repos, e.
 | 
			
		||||
		Where("fork_id=?", forkID).
 | 
			
		||||
		Find(&repos)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetRepositoriesByForkID returns all repositories with given fork ID.
 | 
			
		||||
@@ -1341,7 +1345,9 @@ func DeleteRepository(uid, repoID int64) error {
 | 
			
		||||
	// Delete comments and attachments.
 | 
			
		||||
	issues := make([]*Issue, 0, 25)
 | 
			
		||||
	attachmentPaths := make([]string, 0, len(issues))
 | 
			
		||||
	if err = sess.Where("repo_id=?", repoID).Find(&issues); err != nil {
 | 
			
		||||
	if err = sess.
 | 
			
		||||
		Where("repo_id=?", repoID).
 | 
			
		||||
		Find(&issues); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	for i := range issues {
 | 
			
		||||
@@ -1350,7 +1356,9 @@ func DeleteRepository(uid, repoID int64) error {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		attachments := make([]*Attachment, 0, 5)
 | 
			
		||||
		if err = sess.Where("issue_id=?", issues[i].ID).Find(&attachments); err != nil {
 | 
			
		||||
		if err = sess.
 | 
			
		||||
			Where("issue_id=?", issues[i].ID).
 | 
			
		||||
			Find(&attachments); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		for j := range attachments {
 | 
			
		||||
@@ -1450,7 +1458,9 @@ func GetRepositoryByID(id int64) (*Repository, error) {
 | 
			
		||||
 | 
			
		||||
// GetUserRepositories returns a list of repositories of given user.
 | 
			
		||||
func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Repository, error) {
 | 
			
		||||
	sess := x.Where("owner_id = ?", userID).Desc("updated_unix")
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Where("owner_id = ?", userID).
 | 
			
		||||
		Desc("updated_unix")
 | 
			
		||||
	if !private {
 | 
			
		||||
		sess.And("is_private=?", false)
 | 
			
		||||
	}
 | 
			
		||||
@@ -1467,13 +1477,20 @@ func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Rep
 | 
			
		||||
// GetUserRepositories returns a list of mirror repositories of given user.
 | 
			
		||||
func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
 | 
			
		||||
	repos := make([]*Repository, 0, 10)
 | 
			
		||||
	return repos, x.Where("owner_id = ?", userID).And("is_mirror = ?", true).Find(&repos)
 | 
			
		||||
	return repos, x.
 | 
			
		||||
		Where("owner_id = ?", userID).
 | 
			
		||||
		And("is_mirror = ?", true).
 | 
			
		||||
		Find(&repos)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
 | 
			
		||||
func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err error) {
 | 
			
		||||
	return repos, x.Limit(pageSize, (page-1)*pageSize).
 | 
			
		||||
		Where("is_private=?", false).Limit(pageSize).Desc("updated_unix").Find(&repos)
 | 
			
		||||
	return repos, x.
 | 
			
		||||
		Limit(pageSize, (page-1)*pageSize).
 | 
			
		||||
		Where("is_private=?", false).
 | 
			
		||||
		Limit(pageSize).
 | 
			
		||||
		Desc("updated_unix").
 | 
			
		||||
		Find(&repos)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getRepositoryCount(e Engine, u *User) (int64, error) {
 | 
			
		||||
@@ -1532,7 +1549,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
 | 
			
		||||
 | 
			
		||||
// DeleteRepositoryArchives deletes all repositories' archives.
 | 
			
		||||
func DeleteRepositoryArchives() error {
 | 
			
		||||
	return x.Where("id > 0").Iterate(new(Repository),
 | 
			
		||||
	return x.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		Iterate(new(Repository),
 | 
			
		||||
			func(idx int, bean interface{}) error {
 | 
			
		||||
				repo := bean.(*Repository)
 | 
			
		||||
				return os.RemoveAll(filepath.Join(repo.RepoPath(), "archives"))
 | 
			
		||||
@@ -1541,7 +1560,9 @@ func DeleteRepositoryArchives() error {
 | 
			
		||||
 | 
			
		||||
func gatherMissingRepoRecords() ([]*Repository, error) {
 | 
			
		||||
	repos := make([]*Repository, 0, 10)
 | 
			
		||||
	if err := x.Where("id > 0").Iterate(new(Repository),
 | 
			
		||||
	if err := x.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		Iterate(new(Repository),
 | 
			
		||||
			func(idx int, bean interface{}) error {
 | 
			
		||||
				repo := bean.(*Repository)
 | 
			
		||||
				if !com.IsDir(repo.RepoPath()) {
 | 
			
		||||
@@ -1602,7 +1623,9 @@ func ReinitMissingRepositories() error {
 | 
			
		||||
 | 
			
		||||
// RewriteRepositoryUpdateHook rewrites all repositories' update hook.
 | 
			
		||||
func RewriteRepositoryUpdateHook() error {
 | 
			
		||||
	return x.Where("id > 0").Iterate(new(Repository),
 | 
			
		||||
	return x.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		Iterate(new(Repository),
 | 
			
		||||
			func(idx int, bean interface{}) error {
 | 
			
		||||
				repo := bean.(*Repository)
 | 
			
		||||
				return createUpdateHook(repo.RepoPath())
 | 
			
		||||
@@ -1628,7 +1651,9 @@ func GitFsck() {
 | 
			
		||||
 | 
			
		||||
	log.Trace("Doing: GitFsck")
 | 
			
		||||
 | 
			
		||||
	if err := x.Where("id>0").Iterate(new(Repository),
 | 
			
		||||
	if err := x.
 | 
			
		||||
		Where("id>0").
 | 
			
		||||
		Iterate(new(Repository),
 | 
			
		||||
			func(idx int, bean interface{}) error {
 | 
			
		||||
				repo := bean.(*Repository)
 | 
			
		||||
				repoPath := repo.RepoPath()
 | 
			
		||||
@@ -1647,7 +1672,9 @@ func GitFsck() {
 | 
			
		||||
 | 
			
		||||
func GitGcRepos() error {
 | 
			
		||||
	args := append([]string{"gc"}, setting.Git.GCArgs...)
 | 
			
		||||
	return x.Where("id > 0").Iterate(new(Repository),
 | 
			
		||||
	return x.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		Iterate(new(Repository),
 | 
			
		||||
			func(idx int, bean interface{}) error {
 | 
			
		||||
				repo := bean.(*Repository)
 | 
			
		||||
				if err := repo.GetOwner(); err != nil {
 | 
			
		||||
@@ -1796,7 +1823,10 @@ func (repos RepositoryList) loadAttributes(e Engine) error {
 | 
			
		||||
		userIDs = append(userIDs, userID)
 | 
			
		||||
	}
 | 
			
		||||
	users := make([]*User, 0, len(userIDs))
 | 
			
		||||
	if err := e.Where("id > 0").In("id", userIDs).Find(&users); err != nil {
 | 
			
		||||
	if err := e.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		In("id", userIDs).
 | 
			
		||||
		Find(&users); err != nil {
 | 
			
		||||
		return fmt.Errorf("find users: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	for i := range users {
 | 
			
		||||
@@ -1829,7 +1859,10 @@ func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
 | 
			
		||||
		repoIDs = append(repoIDs, repos[i].ID)
 | 
			
		||||
	}
 | 
			
		||||
	mirrors := make([]*Mirror, 0, len(repoIDs))
 | 
			
		||||
	if err := e.Where("id > 0").In("repo_id", repoIDs).Find(&mirrors); err != nil {
 | 
			
		||||
	if err := e.
 | 
			
		||||
		Where("id > 0").
 | 
			
		||||
		In("repo_id", repoIDs).
 | 
			
		||||
		Find(&mirrors); err != nil {
 | 
			
		||||
		return fmt.Errorf("find mirrors: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -1910,7 +1943,9 @@ func GetWatchers(repoID int64) ([]*Watch, error) {
 | 
			
		||||
// Repository.GetWatchers returns range of users watching given repository.
 | 
			
		||||
func (repo *Repository) GetWatchers(page int) ([]*User, error) {
 | 
			
		||||
	users := make([]*User, 0, ItemsPerPage)
 | 
			
		||||
	sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("watch.repo_id=?", repo.ID)
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Limit(ItemsPerPage, (page-1)*ItemsPerPage).
 | 
			
		||||
		Where("watch.repo_id=?", repo.ID)
 | 
			
		||||
	if setting.UsePostgreSQL {
 | 
			
		||||
		sess = sess.Join("LEFT", "watch", `"user".id=watch.user_id`)
 | 
			
		||||
	} else {
 | 
			
		||||
@@ -1998,7 +2033,9 @@ func IsStaring(userID, repoID int64) bool {
 | 
			
		||||
 | 
			
		||||
func (repo *Repository) GetStargazers(page int) ([]*User, error) {
 | 
			
		||||
	users := make([]*User, 0, ItemsPerPage)
 | 
			
		||||
	sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("star.repo_id=?", repo.ID)
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Limit(ItemsPerPage, (page-1)*ItemsPerPage).
 | 
			
		||||
		Where("star.repo_id=?", repo.ID)
 | 
			
		||||
	if setting.UsePostgreSQL {
 | 
			
		||||
		sess = sess.Join("LEFT", "star", `"user".id=star.uid`)
 | 
			
		||||
	} else {
 | 
			
		||||
@@ -2017,7 +2054,9 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) {
 | 
			
		||||
// HasForkedRepo checks if given user has already forked a repository with given ID.
 | 
			
		||||
func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
 | 
			
		||||
	repo := new(Repository)
 | 
			
		||||
	has, _ := x.Where("owner_id=? AND fork_id=?", ownerID, repoID).Get(repo)
 | 
			
		||||
	has, _ := x.
 | 
			
		||||
		Where("owner_id=? AND fork_id=?", ownerID, repoID).
 | 
			
		||||
		Get(repo)
 | 
			
		||||
	return repo, has
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -131,7 +131,10 @@ func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err = sess.Id(collaboration.ID).AllCols().Update(collaboration); err != nil {
 | 
			
		||||
	if _, err = sess.
 | 
			
		||||
		Id(collaboration.ID).
 | 
			
		||||
		AllCols().
 | 
			
		||||
		Update(collaboration); err != nil {
 | 
			
		||||
		return fmt.Errorf("update collaboration: %v", err)
 | 
			
		||||
	} else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
 | 
			
		||||
		return fmt.Errorf("update access table: %v", err)
 | 
			
		||||
 
 | 
			
		||||
@@ -387,7 +387,9 @@ func DeleteUploads(uploads ...*Upload) (err error) {
 | 
			
		||||
	for i := 0; i < len(uploads); i++ {
 | 
			
		||||
		ids[i] = uploads[i].ID
 | 
			
		||||
	}
 | 
			
		||||
	if _, err = sess.In("id", ids).Delete(new(Upload)); err != nil {
 | 
			
		||||
	if _, err = sess.
 | 
			
		||||
		In("id", ids).
 | 
			
		||||
		Delete(new(Upload)); err != nil {
 | 
			
		||||
		return fmt.Errorf("delete uploads: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -199,7 +199,9 @@ func MirrorUpdate() {
 | 
			
		||||
 | 
			
		||||
	log.Trace("Doing: MirrorUpdate")
 | 
			
		||||
 | 
			
		||||
	if err := x.Where("next_update_unix<=?", time.Now().Unix()).Iterate(new(Mirror), func(idx int, bean interface{}) error {
 | 
			
		||||
	if err := x.
 | 
			
		||||
		Where("next_update_unix<=?", time.Now().Unix()).
 | 
			
		||||
		Iterate(new(Mirror), func(idx int, bean interface{}) error {
 | 
			
		||||
			m := bean.(*Mirror)
 | 
			
		||||
			if m.Repo == nil {
 | 
			
		||||
				log.Error(4, "Disconnected mirror repository found: %d", m.ID)
 | 
			
		||||
 
 | 
			
		||||
@@ -398,7 +398,9 @@ func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Key name of same user cannot be duplicated.
 | 
			
		||||
	has, err := x.Where("owner_id = ? AND name = ?", ownerID, name).Get(new(PublicKey))
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Where("owner_id = ? AND name = ?", ownerID, name).
 | 
			
		||||
		Get(new(PublicKey))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if has {
 | 
			
		||||
@@ -428,7 +430,9 @@ func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) {
 | 
			
		||||
// GetPublicKeyByID returns public key by given ID.
 | 
			
		||||
func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
 | 
			
		||||
	key := new(PublicKey)
 | 
			
		||||
	has, err := x.Id(keyID).Get(key)
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Id(keyID).
 | 
			
		||||
		Get(key)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
@@ -441,7 +445,9 @@ func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
 | 
			
		||||
// and returns public key found.
 | 
			
		||||
func SearchPublicKeyByContent(content string) (*PublicKey, error) {
 | 
			
		||||
	key := new(PublicKey)
 | 
			
		||||
	has, err := x.Where("content like ?", content+"%").Get(key)
 | 
			
		||||
	has, err := x.
 | 
			
		||||
		Where("content like ?", content+"%").
 | 
			
		||||
		Get(key)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
@@ -453,7 +459,9 @@ func SearchPublicKeyByContent(content string) (*PublicKey, error) {
 | 
			
		||||
// ListPublicKeys returns a list of public keys belongs to given user.
 | 
			
		||||
func ListPublicKeys(uid int64) ([]*PublicKey, error) {
 | 
			
		||||
	keys := make([]*PublicKey, 0, 5)
 | 
			
		||||
	return keys, x.Where("owner_id = ?", uid).Find(&keys)
 | 
			
		||||
	return keys, x.
 | 
			
		||||
		Where("owner_id = ?", uid).
 | 
			
		||||
		Find(&keys)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdatePublicKey updates given public key.
 | 
			
		||||
@@ -595,14 +603,18 @@ func (k *DeployKey) GetContent() error {
 | 
			
		||||
 | 
			
		||||
func checkDeployKey(e Engine, keyID, repoID int64, name string) error {
 | 
			
		||||
	// Note: We want error detail, not just true or false here.
 | 
			
		||||
	has, err := e.Where("key_id = ? AND repo_id = ?", keyID, repoID).Get(new(DeployKey))
 | 
			
		||||
	has, err := e.
 | 
			
		||||
		Where("key_id = ? AND repo_id = ?", keyID, repoID).
 | 
			
		||||
		Get(new(DeployKey))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if has {
 | 
			
		||||
		return ErrDeployKeyAlreadyExist{keyID, repoID}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	has, err = e.Where("repo_id = ? AND name = ?", repoID, name).Get(new(DeployKey))
 | 
			
		||||
	has, err = e.
 | 
			
		||||
		Where("repo_id = ? AND name = ?", repoID, name).
 | 
			
		||||
		Get(new(DeployKey))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if has {
 | 
			
		||||
@@ -630,7 +642,9 @@ func addDeployKey(e *xorm.Session, keyID, repoID int64, name, fingerprint string
 | 
			
		||||
 | 
			
		||||
// HasDeployKey returns true if public key is a deploy key of given repository.
 | 
			
		||||
func HasDeployKey(keyID, repoID int64) bool {
 | 
			
		||||
	has, _ := x.Where("key_id = ? AND repo_id = ?", keyID, repoID).Get(new(DeployKey))
 | 
			
		||||
	has, _ := x.
 | 
			
		||||
		Where("key_id = ? AND repo_id = ?", keyID, repoID).
 | 
			
		||||
		Get(new(DeployKey))
 | 
			
		||||
	return has
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -739,7 +753,9 @@ func DeleteDeployKey(doer *User, id int64) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check if this is the last reference to same key content.
 | 
			
		||||
	has, err := sess.Where("key_id = ?", key.KeyID).Get(new(DeployKey))
 | 
			
		||||
	has, err := sess.
 | 
			
		||||
		Where("key_id = ?", key.KeyID).
 | 
			
		||||
		Get(new(DeployKey))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
@@ -754,5 +770,7 @@ func DeleteDeployKey(doer *User, id int64) error {
 | 
			
		||||
// ListDeployKeys returns all deploy keys by given repository ID.
 | 
			
		||||
func ListDeployKeys(repoID int64) ([]*DeployKey, error) {
 | 
			
		||||
	keys := make([]*DeployKey, 0, 5)
 | 
			
		||||
	return keys, x.Where("repo_id = ?", repoID).Find(&keys)
 | 
			
		||||
	return keys, x.
 | 
			
		||||
		Where("repo_id = ?", repoID).
 | 
			
		||||
		Find(&keys)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -72,7 +72,10 @@ func GetAccessTokenBySHA(sha string) (*AccessToken, error) {
 | 
			
		||||
// ListAccessTokens returns a list of access tokens belongs to given user.
 | 
			
		||||
func ListAccessTokens(uid int64) ([]*AccessToken, error) {
 | 
			
		||||
	tokens := make([]*AccessToken, 0, 5)
 | 
			
		||||
	return tokens, x.Where("uid=?", uid).Desc("id").Find(&tokens)
 | 
			
		||||
	return tokens, x.
 | 
			
		||||
		Where("uid=?", uid).
 | 
			
		||||
		Desc("id").
 | 
			
		||||
		Find(&tokens)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateAccessToken updates information of access token.
 | 
			
		||||
 
 | 
			
		||||
@@ -288,7 +288,9 @@ func (u *User) AvatarLink() string {
 | 
			
		||||
// User.GetFollwoers returns range of user's followers.
 | 
			
		||||
func (u *User) GetFollowers(page int) ([]*User, error) {
 | 
			
		||||
	users := make([]*User, 0, ItemsPerPage)
 | 
			
		||||
	sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("follow.follow_id=?", u.ID)
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Limit(ItemsPerPage, (page-1)*ItemsPerPage).
 | 
			
		||||
		Where("follow.follow_id=?", u.ID)
 | 
			
		||||
	if setting.UsePostgreSQL {
 | 
			
		||||
		sess = sess.Join("LEFT", "follow", `"user".id=follow.user_id`)
 | 
			
		||||
	} else {
 | 
			
		||||
@@ -304,7 +306,9 @@ func (u *User) IsFollowing(followID int64) bool {
 | 
			
		||||
// GetFollowing returns range of user's following.
 | 
			
		||||
func (u *User) GetFollowing(page int) ([]*User, error) {
 | 
			
		||||
	users := make([]*User, 0, ItemsPerPage)
 | 
			
		||||
	sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("follow.user_id=?", u.ID)
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Limit(ItemsPerPage, (page-1)*ItemsPerPage).
 | 
			
		||||
		Where("follow.user_id=?", u.ID)
 | 
			
		||||
	if setting.UsePostgreSQL {
 | 
			
		||||
		sess = sess.Join("LEFT", "follow", `"user".id=follow.follow_id`)
 | 
			
		||||
	} else {
 | 
			
		||||
@@ -416,7 +420,9 @@ func (u *User) IsPublicMember(orgId int64) bool {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (u *User) getOrganizationCount(e Engine) (int64, error) {
 | 
			
		||||
	return e.Where("uid=?", u.ID).Count(new(OrgUser))
 | 
			
		||||
	return e.
 | 
			
		||||
		Where("uid=?", u.ID).
 | 
			
		||||
		Count(new(OrgUser))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetOrganizationCount returns count of membership of organization of user.
 | 
			
		||||
@@ -479,7 +485,9 @@ func IsUserExist(uid int64, name string) (bool, error) {
 | 
			
		||||
	if len(name) == 0 {
 | 
			
		||||
		return false, nil
 | 
			
		||||
	}
 | 
			
		||||
	return x.Where("id!=?", uid).Get(&User{LowerName: strings.ToLower(name)})
 | 
			
		||||
	return x.
 | 
			
		||||
		Where("id!=?", uid).
 | 
			
		||||
		Get(&User{LowerName: strings.ToLower(name)})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetUserSalt returns a ramdom user salt token.
 | 
			
		||||
@@ -575,7 +583,9 @@ func CreateUser(u *User) (err error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func countUsers(e Engine) int64 {
 | 
			
		||||
	count, _ := e.Where("type=0").Count(new(User))
 | 
			
		||||
	count, _ := e.
 | 
			
		||||
		Where("type=0").
 | 
			
		||||
		Count(new(User))
 | 
			
		||||
	return count
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -587,7 +597,11 @@ func CountUsers() int64 {
 | 
			
		||||
// Users returns number of users in given page.
 | 
			
		||||
func Users(page, pageSize int) ([]*User, error) {
 | 
			
		||||
	users := make([]*User, 0, pageSize)
 | 
			
		||||
	return users, x.Limit(pageSize, (page-1)*pageSize).Where("type=0").Asc("name").Find(&users)
 | 
			
		||||
	return users, x.
 | 
			
		||||
		Limit(pageSize, (page-1)*pageSize).
 | 
			
		||||
		Where("type=0").
 | 
			
		||||
		Asc("name").
 | 
			
		||||
		Find(&users)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// get user by erify code
 | 
			
		||||
@@ -661,7 +675,9 @@ func ChangeUserName(u *User, newUserName string) (err error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Delete all local copies of repository wiki that user owns.
 | 
			
		||||
	if err = x.Where("owner_id=?", u.ID).Iterate(new(Repository), func(idx int, bean interface{}) error {
 | 
			
		||||
	if err = x.
 | 
			
		||||
		Where("owner_id=?", u.ID).
 | 
			
		||||
		Iterate(new(Repository), func(idx int, bean interface{}) error {
 | 
			
		||||
			repo := bean.(*Repository)
 | 
			
		||||
			RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath())
 | 
			
		||||
			return nil
 | 
			
		||||
@@ -676,7 +692,11 @@ func updateUser(e Engine, u *User) error {
 | 
			
		||||
	// Organization does not need email
 | 
			
		||||
	if !u.IsOrganization() {
 | 
			
		||||
		u.Email = strings.ToLower(u.Email)
 | 
			
		||||
		has, err := e.Where("id!=?", u.ID).And("type=?", u.Type).And("email=?", u.Email).Get(new(User))
 | 
			
		||||
		has, err := e.
 | 
			
		||||
			Where("id!=?", u.ID).
 | 
			
		||||
			And("type=?", u.Type).
 | 
			
		||||
			And("email=?", u.Email).
 | 
			
		||||
			Get(new(User))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		} else if has {
 | 
			
		||||
@@ -843,7 +863,9 @@ func DeleteUser(u *User) (err error) {
 | 
			
		||||
// DeleteInactivateUsers deletes all inactivate users and email addresses.
 | 
			
		||||
func DeleteInactivateUsers() (err error) {
 | 
			
		||||
	users := make([]*User, 0, 10)
 | 
			
		||||
	if err = x.Where("is_active = ?", false).Find(&users); err != nil {
 | 
			
		||||
	if err = x.
 | 
			
		||||
		Where("is_active = ?", false).
 | 
			
		||||
		Find(&users); err != nil {
 | 
			
		||||
		return fmt.Errorf("get all inactive users: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	// FIXME: should only update authorized_keys file once after all deletions.
 | 
			
		||||
@@ -857,7 +879,9 @@ func DeleteInactivateUsers() (err error) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = x.Where("is_activated = ?", false).Delete(new(EmailAddress))
 | 
			
		||||
	_, err = x.
 | 
			
		||||
		Where("is_activated = ?", false).
 | 
			
		||||
		Delete(new(EmailAddress))
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -868,13 +892,10 @@ func UserPath(userName string) string {
 | 
			
		||||
 | 
			
		||||
func GetUserByKeyID(keyID int64) (*User, error) {
 | 
			
		||||
	user := new(User)
 | 
			
		||||
	has, err := x.SQL("SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?", keyID).Get(user)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
		return nil, ErrUserNotKeyOwner
 | 
			
		||||
	}
 | 
			
		||||
	return user, nil
 | 
			
		||||
	return user, x.
 | 
			
		||||
		Join("INNER", "public_key", "`public_key`.owner_id = `user`.id").
 | 
			
		||||
		Where("`public_key`.id=?", keyID).
 | 
			
		||||
		Find(user)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getUserByID(e Engine, id int64) (*User, error) {
 | 
			
		||||
@@ -935,7 +956,10 @@ func GetUserEmailsByNames(names []string) []string {
 | 
			
		||||
// GetUsersByIDs returns all resolved users from a list of Ids.
 | 
			
		||||
func GetUsersByIDs(ids []int64) ([]*User, error) {
 | 
			
		||||
	ous := make([]*User, 0, len(ids))
 | 
			
		||||
	err := x.In("id", ids).Asc("name").Find(&ous)
 | 
			
		||||
	err := x.
 | 
			
		||||
		In("id", ids).
 | 
			
		||||
		Asc("name").
 | 
			
		||||
		Find(&ous)
 | 
			
		||||
	return ous, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1050,7 +1074,8 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
 | 
			
		||||
	searchQuery := "%" + opts.Keyword + "%"
 | 
			
		||||
	users = make([]*User, 0, opts.PageSize)
 | 
			
		||||
	// Append conditions
 | 
			
		||||
	sess := x.Where("LOWER(lower_name) LIKE ?", searchQuery).
 | 
			
		||||
	sess := x.
 | 
			
		||||
		Where("LOWER(lower_name) LIKE ?", searchQuery).
 | 
			
		||||
		Or("LOWER(full_name) LIKE ?", searchQuery).
 | 
			
		||||
		And("type = ?", opts.Type)
 | 
			
		||||
 | 
			
		||||
@@ -1064,7 +1089,9 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
 | 
			
		||||
	if len(opts.OrderBy) > 0 {
 | 
			
		||||
		sess.OrderBy(opts.OrderBy)
 | 
			
		||||
	}
 | 
			
		||||
	return users, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&users)
 | 
			
		||||
	return users, count, sess.
 | 
			
		||||
		Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
 | 
			
		||||
		Find(&users)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ___________    .__  .__
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,9 @@ type EmailAddress struct {
 | 
			
		||||
// GetEmailAddresses returns all email addresses belongs to given user.
 | 
			
		||||
func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
 | 
			
		||||
	emails := make([]*EmailAddress, 0, 5)
 | 
			
		||||
	if err := x.Where("uid=?", uid).Find(&emails); err != nil {
 | 
			
		||||
	if err := x.
 | 
			
		||||
		Where("uid=?", uid).
 | 
			
		||||
		Find(&emails); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -120,7 +122,10 @@ func (email *EmailAddress) Activate() error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	email.IsActivated = true
 | 
			
		||||
	if _, err := sess.Id(email.ID).AllCols().Update(email); err != nil {
 | 
			
		||||
	if _, err := sess.
 | 
			
		||||
		Id(email.ID).
 | 
			
		||||
		AllCols().
 | 
			
		||||
		Update(email); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if err = updateUser(sess, user); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -133,7 +138,9 @@ func DeleteEmailAddress(email *EmailAddress) (err error) {
 | 
			
		||||
	if email.ID > 0 {
 | 
			
		||||
		_, err = x.Id(email.ID).Delete(new(EmailAddress))
 | 
			
		||||
	} else {
 | 
			
		||||
		_, err = x.Where("email=?", email.Email).Delete(new(EmailAddress))
 | 
			
		||||
		_, err = x.
 | 
			
		||||
			Where("email=?", email.Email).
 | 
			
		||||
			Delete(new(EmailAddress))
 | 
			
		||||
	}
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -276,7 +276,10 @@ func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
 | 
			
		||||
 | 
			
		||||
// GetActiveWebhooksByOrgID returns all active webhooks for an organization.
 | 
			
		||||
func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
 | 
			
		||||
	err = x.Where("org_id=?", orgID).And("is_active=?", true).Find(&ws)
 | 
			
		||||
	err = x.
 | 
			
		||||
		Where("org_id=?", orgID).
 | 
			
		||||
		And("is_active=?", true).
 | 
			
		||||
		Find(&ws)
 | 
			
		||||
	return ws, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -413,7 +416,11 @@ func (t *HookTask) SimpleMarshalJSON(v interface{}) string {
 | 
			
		||||
// HookTasks returns a list of hook tasks by given conditions.
 | 
			
		||||
func HookTasks(hookID int64, page int) ([]*HookTask, error) {
 | 
			
		||||
	tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
 | 
			
		||||
	return tasks, x.Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).Where("hook_id=?", hookID).Desc("id").Find(&tasks)
 | 
			
		||||
	return tasks, x.
 | 
			
		||||
		Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
 | 
			
		||||
		Where("hook_id=?", hookID).
 | 
			
		||||
		Desc("id").
 | 
			
		||||
		Find(&tasks)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateHookTask creates a new hook task,
 | 
			
		||||
@@ -580,7 +587,9 @@ func (t *HookTask) deliver() {
 | 
			
		||||
// TODO: shoot more hooks at same time.
 | 
			
		||||
func DeliverHooks() {
 | 
			
		||||
	tasks := make([]*HookTask, 0, 10)
 | 
			
		||||
	x.Where("is_delivered=?", false).Iterate(new(HookTask),
 | 
			
		||||
	x.
 | 
			
		||||
		Where("is_delivered=?", false).
 | 
			
		||||
		Iterate(new(HookTask),
 | 
			
		||||
			func(idx int, bean interface{}) error {
 | 
			
		||||
				t := bean.(*HookTask)
 | 
			
		||||
				t.deliver()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user