summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/action.go27
-rw-r--r--models/issue.go151
-rw-r--r--models/models.go4
-rw-r--r--models/publickey.go47
-rw-r--r--models/repo.go30
-rw-r--r--models/user.go13
6 files changed, 235 insertions, 37 deletions
diff --git a/models/action.go b/models/action.go
index 107d4b1057..1174929354 100644
--- a/models/action.go
+++ b/models/action.go
@@ -7,6 +7,9 @@ package models
import (
"encoding/json"
"time"
+
+ "github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/log"
)
// Operation types of user action.
@@ -28,7 +31,8 @@ type Action struct {
ActUserName string // Action user name.
RepoId int64
RepoName string
- Content string
+ RefName string
+ Content string `xorm:"TEXT"`
Created time.Time `xorm:"created"`
}
@@ -44,13 +48,17 @@ func (a Action) GetRepoName() string {
return a.RepoName
}
+func (a Action) GetBranch() string {
+ return a.RefName
+}
+
func (a Action) GetContent() string {
return a.Content
}
// CommitRepoAction records action for commit repository.
func CommitRepoAction(userId int64, userName string,
- repoId int64, repoName string, commits [][]string) error {
+ repoId int64, repoName string, refName string, commits *base.PushCommits) error {
bs, err := json.Marshal(commits)
if err != nil {
return err
@@ -76,9 +84,22 @@ func CommitRepoAction(userId int64, userName string,
Content: string(bs),
RepoId: repoId,
RepoName: repoName,
+ RefName: refName,
})
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 +113,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
}
diff --git a/models/issue.go b/models/issue.go
index c669d201f6..929567b1b7 100644
--- a/models/issue.go
+++ b/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.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
+ } else {
+ sess.Where("is_closed=?", isClosed)
+ }
+
+ if userId > 0 {
+ sess.And("assignee_id=?", userId)
+ } else if posterId > 0 {
+ sess.And("poster_id=?", posterId)
+ } else if isMention {
+ sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
+ }
+
+ if milestoneId > 0 {
+ sess.And("milestone_id=?", milestoneId)
+ }
+
+ if len(labels) > 0 {
+ for _, label := range strings.Split(labels, ",") {
+ sess.And("mentions like '%$" + label + "|%'")
+ }
+ }
+
+ switch sortType {
+ case "oldest":
+ sess.Asc("created")
+ case "recentupdate":
+ sess.Desc("updated")
+ case "leastupdate":
+ sess.Asc("updated")
+ case "mostcomment":
+ sess.Desc("num_comments")
+ case "leastcomment":
+ sess.Asc("num_comments")
+ default:
+ 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"`
}
diff --git a/models/models.go b/models/models.go
index 8713ff2896..ad19a929fb 100644
--- a/models/models.go
+++ b/models/models.go
@@ -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)
}
@@ -91,5 +91,5 @@ func GetStatistic() (stats Statistic) {
stats.Counter.Watch, _ = orm.Count(new(Watch))
stats.Counter.Action, _ = orm.Count(new(Action))
stats.Counter.Access, _ = orm.Count(new(Access))
- return stats
+ return
}
diff --git a/models/publickey.go b/models/publickey.go
index c69bca681d..3f2fcabd3b 100644
--- a/models/publickey.go
+++ b/models/publickey.go
@@ -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)
diff --git a/models/repo.go b/models/repo.go
index 4972661cb3..e27e99b056 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -10,6 +10,7 @@ import (
"fmt"
"io/ioutil"
"os"
+ "os/exec"
"path"
"path/filepath"
"regexp"
@@ -83,10 +84,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 +141,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)
@@ -196,6 +199,13 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv
return nil, err
}
+ c := exec.Command("git", "update-server-info")
+ c.Dir = repoPath
+ err = c.Run()
+ if err != nil {
+ log.Error("repo.CreateRepository(exec update-server-info): %v", err)
+ }
+
return repo, NewRepoAction(user, repo)
}
@@ -369,6 +379,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 +435,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)
diff --git a/models/user.go b/models/user.go
index cedf342496..88bbabe6d5 100644
--- a/models/user.go
+++ b/models/user.go
@@ -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", "is_active", "is_admin").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