summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2014-04-14 14:50:37 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2014-04-14 14:50:37 +0800
commit8283e16ef7af197ee77b1be412bf875ae97c789e (patch)
treec145517a3b885c49408f35e0cb9c9aabe6201a35 /models
parent6cee65db5a6e6ef7372f19d45c22a416e130afed (diff)
parentf644cefa865c04b440902695ba90114b224c640d (diff)
downloadgitea-8283e16ef7af197ee77b1be412bf875ae97c789e.tar.gz
gitea-8283e16ef7af197ee77b1be412bf875ae97c789e.zip
Merge branch 'dev' of github.com:gogits/gogs into dev
Diffstat (limited to 'models')
-rw-r--r--models/action.go17
-rw-r--r--models/models.go2
-rw-r--r--models/oauth2.go31
-rw-r--r--models/release.go83
-rw-r--r--models/repo.go11
-rw-r--r--models/update.go2
-rw-r--r--models/user.go2
7 files changed, 128 insertions, 20 deletions
diff --git a/models/action.go b/models/action.go
index a642a82c98..a9a41a9f4d 100644
--- a/models/action.go
+++ b/models/action.go
@@ -6,8 +6,11 @@ package models
import (
"encoding/json"
+ "strings"
"time"
+ "github.com/gogits/git"
+
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
)
@@ -22,6 +25,7 @@ const (
OP_CREATE_ISSUE
OP_PULL_REQUEST
OP_TRANSFER_REPO
+ OP_PUSH_TAG
)
// Action represents user operation type and other information to repository.,
@@ -67,7 +71,16 @@ func (a Action) GetContent() string {
// CommitRepoAction adds new action for committing repository.
func CommitRepoAction(userId int64, userName, actEmail string,
repoId int64, repoName string, refName string, commit *base.PushCommits) error {
- log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
+ // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
+
+ opType := OP_COMMIT_REPO
+ // Check it's tag push or branch.
+ if strings.HasPrefix(refName, "refs/tags/") {
+ opType = OP_PUSH_TAG
+ commit = &base.PushCommits{}
+ }
+
+ refName = git.RefEndName(refName)
bs, err := json.Marshal(commit)
if err != nil {
@@ -76,7 +89,7 @@ func CommitRepoAction(userId int64, userName, actEmail string,
}
if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
- OpType: OP_COMMIT_REPO, Content: string(bs), RepoId: repoId, RepoName: repoName, RefName: refName}); err != nil {
+ OpType: opType, Content: string(bs), RepoId: repoId, RepoName: repoName, RefName: refName}); err != nil {
log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
return err
}
diff --git a/models/models.go b/models/models.go
index ab13548ef7..b0a12a8dff 100644
--- a/models/models.go
+++ b/models/models.go
@@ -34,7 +34,7 @@ var (
func init() {
tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch),
new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow),
- new(Mirror))
+ new(Mirror), new(Release))
}
func LoadModelsConfig() {
diff --git a/models/oauth2.go b/models/oauth2.go
index f8780fe682..d1ae4611b8 100644
--- a/models/oauth2.go
+++ b/models/oauth2.go
@@ -14,11 +14,15 @@ const (
OT_GOOGLE
OT_TWITTER
OT_QQ
+ OT_WEIBO
+ OT_BITBUCKET
+ OT_OSCHINA
+ OT_FACEBOOK
)
var (
- ErrOauth2RecordNotExists = errors.New("not exists oauth2 record")
- ErrOauth2NotAssociatedWithUser = errors.New("not associated with user")
+ ErrOauth2RecordNotExist = errors.New("OAuth2 record does not exist")
+ ErrOauth2NotAssociated = errors.New("OAuth2 is not associated with user")
)
type Oauth2 struct {
@@ -35,11 +39,9 @@ func BindUserOauth2(userId, oauthId int64) error {
return err
}
-func AddOauth2(oa *Oauth2) (err error) {
- if _, err = orm.Insert(oa); err != nil {
- return err
- }
- return nil
+func AddOauth2(oa *Oauth2) error {
+ _, err := orm.Insert(oa)
+ return err
}
func GetOauth2(identity string) (oa *Oauth2, err error) {
@@ -48,9 +50,9 @@ func GetOauth2(identity string) (oa *Oauth2, err error) {
if err != nil {
return
} else if !isExist {
- return nil, ErrOauth2RecordNotExists
+ return nil, ErrOauth2RecordNotExist
} else if oa.Uid == -1 {
- return oa, ErrOauth2NotAssociatedWithUser
+ return oa, ErrOauth2NotAssociated
}
oa.User, err = GetUserById(oa.Uid)
return oa, err
@@ -61,9 +63,14 @@ func GetOauth2ById(id int64) (oa *Oauth2, err error) {
has, err := orm.Id(id).Get(oa)
if err != nil {
return nil, err
- }
- if !has {
- return nil, ErrOauth2RecordNotExists
+ } else if !has {
+ return nil, ErrOauth2RecordNotExist
}
return oa, nil
}
+
+// GetOauthByUserId returns list of oauthes that are releated to given user.
+func GetOauthByUserId(uid int64) (oas []*Oauth2, err error) {
+ err = orm.Find(&oas, Oauth2{Uid: uid})
+ return oas, err
+}
diff --git a/models/release.go b/models/release.go
new file mode 100644
index 0000000000..08d74d3f4f
--- /dev/null
+++ b/models/release.go
@@ -0,0 +1,83 @@
+// Copyright 2014 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package models
+
+import (
+ "errors"
+ "strings"
+ "time"
+
+ "github.com/Unknwon/com"
+ "github.com/gogits/git"
+)
+
+var (
+ ErrReleaseAlreadyExist = errors.New("Release already exist")
+)
+
+// Release represents a release of repository.
+type Release struct {
+ Id int64
+ RepoId int64
+ PublisherId int64
+ Publisher *User `xorm:"-"`
+ Title string
+ TagName string
+ LowerTagName string
+ SHA1 string
+ NumCommits int
+ NumCommitsBehind int `xorm:"-"`
+ Note string `xorm:"TEXT"`
+ IsPrerelease bool
+ Created time.Time `xorm:"created"`
+}
+
+// GetReleasesByRepoId returns a list of releases of repository.
+func GetReleasesByRepoId(repoId int64) (rels []*Release, err error) {
+ err = orm.Desc("created").Find(&rels, Release{RepoId: repoId})
+ return rels, err
+}
+
+// IsReleaseExist returns true if release with given tag name already exists.
+func IsReleaseExist(repoId int64, tagName string) (bool, error) {
+ if len(tagName) == 0 {
+ return false, nil
+ }
+
+ return orm.Get(&Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)})
+}
+
+// CreateRelease creates a new release of repository.
+func CreateRelease(repoPath string, rel *Release, gitRepo *git.Repository) error {
+ isExist, err := IsReleaseExist(rel.RepoId, rel.TagName)
+ if err != nil {
+ return err
+ } else if isExist {
+ return ErrReleaseAlreadyExist
+ }
+
+ if !git.IsTagExist(repoPath, rel.TagName) {
+ _, stderr, err := com.ExecCmdDir(repoPath, "git", "tag", rel.TagName, "-m", "\""+rel.Title+"\"")
+ if err != nil {
+ return err
+ } else if strings.Contains(stderr, "fatal:") {
+ return errors.New(stderr)
+ }
+ } else {
+ commit, err := gitRepo.GetCommitOfTag(rel.TagName)
+ if err != nil {
+ return err
+ }
+
+ rel.NumCommits, err = commit.CommitsCount()
+ if err != nil {
+ return err
+ }
+ }
+
+ rel.LowerTagName = strings.ToLower(rel.TagName)
+ _, err = orm.InsertOne(rel)
+ return err
+}
diff --git a/models/repo.go b/models/repo.go
index 01736b631d..bb0c164e24 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -75,9 +75,9 @@ type Repository struct {
NumStars int
NumForks int
NumIssues int
- NumReleases int `xorm:"NOT NULL"`
NumClosedIssues int
NumOpenIssues int `xorm:"-"`
+ NumTags int `xorm:"-"`
IsPrivate bool
IsMirror bool
IsBare bool
@@ -714,9 +714,14 @@ func GetRepositoryById(id int64) (*Repository, error) {
}
// GetRepositories returns the list of repositories of given user.
-func GetRepositories(user *User) ([]Repository, error) {
+func GetRepositories(user *User, private bool) ([]Repository, error) {
repos := make([]Repository, 0, 10)
- err := orm.Desc("updated").Find(&repos, &Repository{OwnerId: user.Id})
+ sess := orm.Desc("updated")
+ if !private {
+ sess.Where("is_private=?", false)
+ }
+
+ err := sess.Find(&repos, &Repository{OwnerId: user.Id})
return repos, err
}
diff --git a/models/update.go b/models/update.go
index ba0e979348..2f59547b72 100644
--- a/models/update.go
+++ b/models/update.go
@@ -78,7 +78,7 @@ func Update(refName, oldCommitId, newCommitId, userName, repoName string, userId
//commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
if err = CommitRepoAction(userId, userName, actEmail,
- repos.Id, repoName, git.RefEndName(refName), &base.PushCommits{l.Len(), commits}); err != nil {
+ repos.Id, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err)
}
}
diff --git a/models/user.go b/models/user.go
index ea2c79b771..ffee2dd9e5 100644
--- a/models/user.go
+++ b/models/user.go
@@ -234,7 +234,7 @@ func ChangeUserName(user *User, newUserName string) (err error) {
}
}
- repos, err := GetRepositories(user)
+ repos, err := GetRepositories(user, true)
if err != nil {
return err
}