mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	merge
This commit is contained in:
		@@ -7,6 +7,8 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/gogits/gogs/modules/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Operation types of user action.
 | 
			
		||||
@@ -28,7 +30,7 @@ type Action struct {
 | 
			
		||||
	ActUserName string // Action user name.
 | 
			
		||||
	RepoId      int64
 | 
			
		||||
	RepoName    string
 | 
			
		||||
	Content     string    `xorm:"varchar(1000)"`
 | 
			
		||||
	Content     string    `xorm:"TEXT"`
 | 
			
		||||
	Created     time.Time `xorm:"created"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -79,6 +81,18 @@ func CommitRepoAction(userId int64, userName string,
 | 
			
		||||
		})
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Update repository last update time.
 | 
			
		||||
	repo, err := GetRepositoryByName(userId, repoName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	repo.IsBare = false
 | 
			
		||||
	if err = UpdateRepository(repo); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Trace("action.CommitRepoAction: %d/%s", userId, repo.LowerName)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -92,6 +106,8 @@ func NewRepoAction(user *User, repo *Repository) error {
 | 
			
		||||
		RepoId:      repo.Id,
 | 
			
		||||
		RepoName:    repo.Name,
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										151
									
								
								models/issue.go
									
									
									
									
									
								
							
							
						
						
									
										151
									
								
								models/issue.go
									
									
									
									
									
								
							@@ -4,16 +4,155 @@
 | 
			
		||||
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/gogits/gogs/modules/base"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	ErrIssueNotExist = errors.New("Issue does not exist")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Issue represents an issue or pull request of repository.
 | 
			
		||||
type Issue struct {
 | 
			
		||||
	Id       int64
 | 
			
		||||
	RepoId   int64 `xorm:"index"`
 | 
			
		||||
	PosterId int64
 | 
			
		||||
	Id          int64
 | 
			
		||||
	Index       int64 // Index in one repository.
 | 
			
		||||
	Name        string
 | 
			
		||||
	RepoId      int64 `xorm:"index"`
 | 
			
		||||
	PosterId    int64
 | 
			
		||||
	MilestoneId int64
 | 
			
		||||
	AssigneeId  int64
 | 
			
		||||
	IsPull      bool // Indicates whether is a pull request or not.
 | 
			
		||||
	IsClosed    bool
 | 
			
		||||
	Labels      string `xorm:"TEXT"`
 | 
			
		||||
	Mentions    string `xorm:"TEXT"`
 | 
			
		||||
	Content     string `xorm:"TEXT"`
 | 
			
		||||
	NumComments int
 | 
			
		||||
	Created     time.Time `xorm:"created"`
 | 
			
		||||
	Updated     time.Time `xorm:"updated"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type PullRequest struct {
 | 
			
		||||
	Id int64
 | 
			
		||||
// CreateIssue creates new issue for repository.
 | 
			
		||||
func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, content string, isPull bool) (*Issue, error) {
 | 
			
		||||
	count, err := GetIssueCount(repoId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO: find out mentions
 | 
			
		||||
	mentions := ""
 | 
			
		||||
 | 
			
		||||
	issue := &Issue{
 | 
			
		||||
		Index:       count + 1,
 | 
			
		||||
		Name:        name,
 | 
			
		||||
		RepoId:      repoId,
 | 
			
		||||
		PosterId:    userId,
 | 
			
		||||
		MilestoneId: milestoneId,
 | 
			
		||||
		AssigneeId:  assigneeId,
 | 
			
		||||
		IsPull:      isPull,
 | 
			
		||||
		Labels:      labels,
 | 
			
		||||
		Mentions:    mentions,
 | 
			
		||||
		Content:     content,
 | 
			
		||||
	}
 | 
			
		||||
	_, err = orm.Insert(issue)
 | 
			
		||||
	return issue, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetIssueCount returns count of issues in the repository.
 | 
			
		||||
func GetIssueCount(repoId int64) (int64, error) {
 | 
			
		||||
	return orm.Count(&Issue{RepoId: repoId})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetIssueById returns issue object by given id.
 | 
			
		||||
func GetIssueById(id int64) (*Issue, error) {
 | 
			
		||||
	issue := new(Issue)
 | 
			
		||||
	has, err := orm.Id(id).Get(issue)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
		return nil, ErrIssueNotExist
 | 
			
		||||
	}
 | 
			
		||||
	return issue, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetIssues returns a list of issues by given conditions.
 | 
			
		||||
func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, isMention bool, labels, sortType string) ([]Issue, error) {
 | 
			
		||||
	sess := orm.Limit(20, (page-1)*20)
 | 
			
		||||
 | 
			
		||||
	if repoId > 0 {
 | 
			
		||||
		sess = sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
 | 
			
		||||
	} else {
 | 
			
		||||
		sess = sess.Where("is_closed=?", isClosed)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if userId > 0 {
 | 
			
		||||
		sess = sess.And("assignee_id=?", userId)
 | 
			
		||||
	} else if posterId > 0 {
 | 
			
		||||
		sess = sess.And("poster_id=?", posterId)
 | 
			
		||||
	} else if isMention {
 | 
			
		||||
		sess = sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if milestoneId > 0 {
 | 
			
		||||
		sess = sess.And("milestone_id=?", milestoneId)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(labels) > 0 {
 | 
			
		||||
		for _, label := range strings.Split(labels, ",") {
 | 
			
		||||
			sess = sess.And("mentions like '%$" + label + "|%'")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch sortType {
 | 
			
		||||
	case "oldest":
 | 
			
		||||
		sess = sess.Asc("created")
 | 
			
		||||
	case "recentupdate":
 | 
			
		||||
		sess = sess.Desc("updated")
 | 
			
		||||
	case "leastupdate":
 | 
			
		||||
		sess = sess.Asc("updated")
 | 
			
		||||
	case "mostcomment":
 | 
			
		||||
		sess = sess.Desc("num_comments")
 | 
			
		||||
	case "leastcomment":
 | 
			
		||||
		sess = sess.Asc("num_comments")
 | 
			
		||||
	default:
 | 
			
		||||
		sess = sess.Desc("created")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var issues []Issue
 | 
			
		||||
	err := sess.Find(&issues)
 | 
			
		||||
	return issues, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Label represents a list of labels of repository for issues.
 | 
			
		||||
type Label struct {
 | 
			
		||||
	Id     int64
 | 
			
		||||
	RepoId int64 `xorm:"index"`
 | 
			
		||||
	Names  string
 | 
			
		||||
	Colors string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Milestone represents a milestone of repository.
 | 
			
		||||
type Milestone struct {
 | 
			
		||||
	Id        int64
 | 
			
		||||
	Name      string
 | 
			
		||||
	RepoId    int64 `xorm:"index"`
 | 
			
		||||
	IsClosed  bool
 | 
			
		||||
	Content   string
 | 
			
		||||
	NumIssues int
 | 
			
		||||
	DueDate   time.Time
 | 
			
		||||
	Created   time.Time `xorm:"created"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Comment represents a comment in commit and issue page.
 | 
			
		||||
type Comment struct {
 | 
			
		||||
	Id int64
 | 
			
		||||
	Id       int64
 | 
			
		||||
	PosterId int64
 | 
			
		||||
	IssueId  int64
 | 
			
		||||
	CommitId int64
 | 
			
		||||
	Line     int
 | 
			
		||||
	Content  string
 | 
			
		||||
	Created  time.Time `xorm:"created"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -72,7 +72,7 @@ func setEngine() {
 | 
			
		||||
func NewEngine() {
 | 
			
		||||
	setEngine()
 | 
			
		||||
	if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
 | 
			
		||||
		new(Action), new(Access)); err != nil {
 | 
			
		||||
		new(Action), new(Access), new(Issue)); err != nil {
 | 
			
		||||
		fmt.Printf("sync database struct error: %v\n", err)
 | 
			
		||||
		os.Exit(2)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,8 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/Unknwon/com"
 | 
			
		||||
 | 
			
		||||
	"github.com/gogits/gogs/modules/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -78,7 +80,7 @@ type PublicKey struct {
 | 
			
		||||
	OwnerId     int64  `xorm:"index"`
 | 
			
		||||
	Name        string `xorm:"unique not null"`
 | 
			
		||||
	Fingerprint string
 | 
			
		||||
	Content     string    `xorm:"text not null"`
 | 
			
		||||
	Content     string    `xorm:"TEXT not null"`
 | 
			
		||||
	Created     time.Time `xorm:"created"`
 | 
			
		||||
	Updated     time.Time `xorm:"updated"`
 | 
			
		||||
}
 | 
			
		||||
@@ -99,8 +101,8 @@ func AddPublicKey(key *PublicKey) (err error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Calculate fingerprint.
 | 
			
		||||
	tmpPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
 | 
			
		||||
		"id_rsa.pub")
 | 
			
		||||
	tmpPath := strings.Replace(filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
 | 
			
		||||
		"id_rsa.pub"), "\\", "/", -1)
 | 
			
		||||
	os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
 | 
			
		||||
	if err = ioutil.WriteFile(tmpPath, []byte(key.Content), os.ModePerm); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -127,25 +129,11 @@ func AddPublicKey(key *PublicKey) (err error) {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
 | 
			
		||||
func DeletePublicKey(key *PublicKey) (err error) {
 | 
			
		||||
	// Delete SSH key in database.
 | 
			
		||||
	has, err := orm.Id(key.Id).Get(key)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
		return errors.New("Public key does not exist")
 | 
			
		||||
	}
 | 
			
		||||
	if _, err = orm.Delete(key); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
func rewriteAuthorizedKeys(key *PublicKey, p, tmpP string) error {
 | 
			
		||||
	// Delete SSH key in SSH key file.
 | 
			
		||||
	sshOpLocker.Lock()
 | 
			
		||||
	defer sshOpLocker.Unlock()
 | 
			
		||||
 | 
			
		||||
	p := filepath.Join(sshPath, "authorized_keys")
 | 
			
		||||
	tmpP := filepath.Join(sshPath, "authorized_keys.tmp")
 | 
			
		||||
	fr, err := os.Open(p)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -188,8 +176,29 @@ func DeletePublicKey(key *PublicKey) (err error) {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	if err = os.Remove(p); err != nil {
 | 
			
		||||
// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
 | 
			
		||||
func DeletePublicKey(key *PublicKey) (err error) {
 | 
			
		||||
	// Delete SSH key in database.
 | 
			
		||||
	has, err := orm.Id(key.Id).Get(key)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if !has {
 | 
			
		||||
		return errors.New("Public key does not exist")
 | 
			
		||||
	}
 | 
			
		||||
	if _, err = orm.Delete(key); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	p := filepath.Join(sshPath, "authorized_keys")
 | 
			
		||||
	tmpP := filepath.Join(sshPath, "authorized_keys.tmp")
 | 
			
		||||
	log.Trace("ssh.DeletePublicKey(authorized_keys): %s", p)
 | 
			
		||||
 | 
			
		||||
	if err = rewriteAuthorizedKeys(key, p, tmpP); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	} else if err = os.Remove(p); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return os.Rename(tmpP, p)
 | 
			
		||||
 
 | 
			
		||||
@@ -83,10 +83,11 @@ type Repository struct {
 | 
			
		||||
	Name        string `xorm:"index not null"`
 | 
			
		||||
	Description string
 | 
			
		||||
	Website     string
 | 
			
		||||
	Private     bool
 | 
			
		||||
	NumWatches  int
 | 
			
		||||
	NumStars    int
 | 
			
		||||
	NumForks    int
 | 
			
		||||
	IsPrivate   bool
 | 
			
		||||
	IsBare      bool
 | 
			
		||||
	Created     time.Time `xorm:"created"`
 | 
			
		||||
	Updated     time.Time `xorm:"updated"`
 | 
			
		||||
}
 | 
			
		||||
@@ -139,7 +140,8 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv
 | 
			
		||||
		Name:        repoName,
 | 
			
		||||
		LowerName:   strings.ToLower(repoName),
 | 
			
		||||
		Description: desc,
 | 
			
		||||
		Private:     private,
 | 
			
		||||
		IsPrivate:   private,
 | 
			
		||||
		IsBare:      repoLang == "" && license == "" && !initReadme,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	repoPath := RepoPath(user.Name, repoName)
 | 
			
		||||
@@ -369,6 +371,18 @@ func RepoPath(userName, repoName string) string {
 | 
			
		||||
	return filepath.Join(UserPath(userName), repoName+".git")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func UpdateRepository(repo *Repository) error {
 | 
			
		||||
	if len(repo.Description) > 255 {
 | 
			
		||||
		repo.Description = repo.Description[:255]
 | 
			
		||||
	}
 | 
			
		||||
	if len(repo.Website) > 255 {
 | 
			
		||||
		repo.Website = repo.Website[:255]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err := orm.Id(repo.Id).UseBool().Cols("description", "website").Update(repo)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeleteRepository deletes a repository for a user or orgnaztion.
 | 
			
		||||
func DeleteRepository(userId, repoId int64, userName string) (err error) {
 | 
			
		||||
	repo := &Repository{Id: repoId, OwnerId: userId}
 | 
			
		||||
@@ -413,9 +427,9 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetRepositoryByName returns the repository by given name under user if exists.
 | 
			
		||||
func GetRepositoryByName(user *User, repoName string) (*Repository, error) {
 | 
			
		||||
func GetRepositoryByName(userId int64, repoName string) (*Repository, error) {
 | 
			
		||||
	repo := &Repository{
 | 
			
		||||
		OwnerId:   user.Id,
 | 
			
		||||
		OwnerId:   userId,
 | 
			
		||||
		LowerName: strings.ToLower(repoName),
 | 
			
		||||
	}
 | 
			
		||||
	has, err := orm.Get(repo)
 | 
			
		||||
 
 | 
			
		||||
@@ -201,7 +201,14 @@ func VerifyUserActiveCode(code string) (user *User) {
 | 
			
		||||
 | 
			
		||||
// UpdateUser updates user's information.
 | 
			
		||||
func UpdateUser(user *User) (err error) {
 | 
			
		||||
	_, err = orm.Id(user.Id).UseBool().Update(user)
 | 
			
		||||
	if len(user.Location) > 255 {
 | 
			
		||||
		user.Location = user.Location[:255]
 | 
			
		||||
	}
 | 
			
		||||
	if len(user.Website) > 255 {
 | 
			
		||||
		user.Website = user.Website[:255]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = orm.Id(user.Id).UseBool().Cols("website", "location").Update(user)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -279,9 +286,7 @@ func GetUserByName(name string) (*User, error) {
 | 
			
		||||
	if len(name) == 0 {
 | 
			
		||||
		return nil, ErrUserNotExist
 | 
			
		||||
	}
 | 
			
		||||
	user := &User{
 | 
			
		||||
		LowerName: strings.ToLower(name),
 | 
			
		||||
	}
 | 
			
		||||
	user := &User{LowerName: strings.ToLower(name)}
 | 
			
		||||
	has, err := orm.Get(user)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user