forked from forgejo/forgejo
merge upstream & fix conflicts
This commit is contained in:
commit
57744b54c4
62 changed files with 4142 additions and 1933 deletions
126
models/action.go
126
models/action.go
|
@ -13,10 +13,8 @@ import (
|
|||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/unknwon/com"
|
||||
|
@ -284,130 +282,6 @@ func (a *Action) GetIssueContent() string {
|
|||
return issue.Content
|
||||
}
|
||||
|
||||
// PushCommit represents a commit in a push operation.
|
||||
type PushCommit struct {
|
||||
Sha1 string
|
||||
Message string
|
||||
AuthorEmail string
|
||||
AuthorName string
|
||||
CommitterEmail string
|
||||
CommitterName string
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
// PushCommits represents list of commits in a push operation.
|
||||
type PushCommits struct {
|
||||
Len int
|
||||
Commits []*PushCommit
|
||||
CompareURL string
|
||||
|
||||
avatars map[string]string
|
||||
emailUsers map[string]*User
|
||||
}
|
||||
|
||||
// NewPushCommits creates a new PushCommits object.
|
||||
func NewPushCommits() *PushCommits {
|
||||
return &PushCommits{
|
||||
avatars: make(map[string]string),
|
||||
emailUsers: make(map[string]*User),
|
||||
}
|
||||
}
|
||||
|
||||
// ToAPIPayloadCommits converts a PushCommits object to
|
||||
// api.PayloadCommit format.
|
||||
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) {
|
||||
commits := make([]*api.PayloadCommit, len(pc.Commits))
|
||||
|
||||
if pc.emailUsers == nil {
|
||||
pc.emailUsers = make(map[string]*User)
|
||||
}
|
||||
var err error
|
||||
for i, commit := range pc.Commits {
|
||||
authorUsername := ""
|
||||
author, ok := pc.emailUsers[commit.AuthorEmail]
|
||||
if !ok {
|
||||
author, err = GetUserByEmail(commit.AuthorEmail)
|
||||
if err == nil {
|
||||
authorUsername = author.Name
|
||||
pc.emailUsers[commit.AuthorEmail] = author
|
||||
}
|
||||
} else {
|
||||
authorUsername = author.Name
|
||||
}
|
||||
|
||||
committerUsername := ""
|
||||
committer, ok := pc.emailUsers[commit.CommitterEmail]
|
||||
if !ok {
|
||||
committer, err = GetUserByEmail(commit.CommitterEmail)
|
||||
if err == nil {
|
||||
// TODO: check errors other than email not found.
|
||||
committerUsername = committer.Name
|
||||
pc.emailUsers[commit.CommitterEmail] = committer
|
||||
}
|
||||
} else {
|
||||
committerUsername = committer.Name
|
||||
}
|
||||
|
||||
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
|
||||
}
|
||||
|
||||
commits[i] = &api.PayloadCommit{
|
||||
ID: commit.Sha1,
|
||||
Message: commit.Message,
|
||||
URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
|
||||
Author: &api.PayloadUser{
|
||||
Name: commit.AuthorName,
|
||||
Email: commit.AuthorEmail,
|
||||
UserName: authorUsername,
|
||||
},
|
||||
Committer: &api.PayloadUser{
|
||||
Name: commit.CommitterName,
|
||||
Email: commit.CommitterEmail,
|
||||
UserName: committerUsername,
|
||||
},
|
||||
Added: fileStatus.Added,
|
||||
Removed: fileStatus.Removed,
|
||||
Modified: fileStatus.Modified,
|
||||
Timestamp: commit.Timestamp,
|
||||
}
|
||||
}
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
// AvatarLink tries to match user in database with e-mail
|
||||
// in order to show custom avatar, and falls back to general avatar link.
|
||||
func (pc *PushCommits) AvatarLink(email string) string {
|
||||
if pc.avatars == nil {
|
||||
pc.avatars = make(map[string]string)
|
||||
}
|
||||
avatar, ok := pc.avatars[email]
|
||||
if ok {
|
||||
return avatar
|
||||
}
|
||||
|
||||
u, ok := pc.emailUsers[email]
|
||||
if !ok {
|
||||
var err error
|
||||
u, err = GetUserByEmail(email)
|
||||
if err != nil {
|
||||
pc.avatars[email] = base.AvatarLink(email)
|
||||
if !IsErrUserNotExist(err) {
|
||||
log.Error("GetUserByEmail: %v", err)
|
||||
return ""
|
||||
}
|
||||
} else {
|
||||
pc.emailUsers[email] = u
|
||||
}
|
||||
}
|
||||
if u != nil {
|
||||
pc.avatars[email] = u.RelAvatarLink()
|
||||
}
|
||||
|
||||
return pc.avatars[email]
|
||||
}
|
||||
|
||||
// GetFeedsOptions options for retrieving feeds
|
||||
type GetFeedsOptions struct {
|
||||
RequestedUser *User // the user we want activity for
|
||||
|
|
|
@ -27,106 +27,6 @@ func TestAction_GetRepoLink(t *testing.T) {
|
|||
assert.Equal(t, expected, action.GetRepoLink())
|
||||
}
|
||||
|
||||
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
||||
pushCommits := NewPushCommits()
|
||||
pushCommits.Commits = []*PushCommit{
|
||||
{
|
||||
Sha1: "69554a6",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User2",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User2",
|
||||
Message: "not signed commit",
|
||||
},
|
||||
{
|
||||
Sha1: "27566bd",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User2",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User2",
|
||||
Message: "good signed commit (with not yet validated email)",
|
||||
},
|
||||
{
|
||||
Sha1: "5099b81",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User2",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User2",
|
||||
Message: "good signed commit",
|
||||
},
|
||||
}
|
||||
pushCommits.Len = len(pushCommits.Commits)
|
||||
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 16}).(*Repository)
|
||||
payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 3, len(payloadCommits))
|
||||
|
||||
assert.Equal(t, "69554a6", payloadCommits[0].ID)
|
||||
assert.Equal(t, "not signed commit", payloadCommits[0].Message)
|
||||
assert.Equal(t, "/user2/repo16/commit/69554a6", payloadCommits[0].URL)
|
||||
assert.Equal(t, "User2", payloadCommits[0].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
|
||||
assert.Equal(t, "User2", payloadCommits[0].Author.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[0].Author.UserName)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[0].Added)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[0].Removed)
|
||||
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[0].Modified)
|
||||
|
||||
assert.Equal(t, "27566bd", payloadCommits[1].ID)
|
||||
assert.Equal(t, "good signed commit (with not yet validated email)", payloadCommits[1].Message)
|
||||
assert.Equal(t, "/user2/repo16/commit/27566bd", payloadCommits[1].URL)
|
||||
assert.Equal(t, "User2", payloadCommits[1].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
|
||||
assert.Equal(t, "User2", payloadCommits[1].Author.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[1].Added)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[1].Removed)
|
||||
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[1].Modified)
|
||||
|
||||
assert.Equal(t, "5099b81", payloadCommits[2].ID)
|
||||
assert.Equal(t, "good signed commit", payloadCommits[2].Message)
|
||||
assert.Equal(t, "/user2/repo16/commit/5099b81", payloadCommits[2].URL)
|
||||
assert.Equal(t, "User2", payloadCommits[2].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[2].Committer.UserName)
|
||||
assert.Equal(t, "User2", payloadCommits[2].Author.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[2].Author.UserName)
|
||||
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
|
||||
}
|
||||
|
||||
func TestPushCommits_AvatarLink(t *testing.T) {
|
||||
pushCommits := NewPushCommits()
|
||||
pushCommits.Commits = []*PushCommit{
|
||||
{
|
||||
Sha1: "abcdef1",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User Two",
|
||||
AuthorEmail: "user4@example.com",
|
||||
AuthorName: "User Four",
|
||||
Message: "message1",
|
||||
},
|
||||
{
|
||||
Sha1: "abcdef2",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User Two",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User Two",
|
||||
Message: "message2",
|
||||
},
|
||||
}
|
||||
pushCommits.Len = len(pushCommits.Commits)
|
||||
|
||||
assert.Equal(t,
|
||||
"/suburl/user/avatar/user2/-1",
|
||||
pushCommits.AvatarLink("user2@example.com"))
|
||||
|
||||
assert.Equal(t,
|
||||
"https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
|
||||
pushCommits.AvatarLink("nonexistent@example.com"))
|
||||
}
|
||||
|
||||
func TestGetFeeds(t *testing.T) {
|
||||
// test with an individual user
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
updated_by: 2
|
||||
issue_id: 1
|
||||
created_unix: 946684800
|
||||
updated_unix: 946684800
|
||||
updated_unix: 946684820
|
||||
|
||||
-
|
||||
id: 2
|
||||
|
@ -17,8 +17,8 @@
|
|||
source: 1 # issue
|
||||
updated_by: 1
|
||||
issue_id: 2
|
||||
created_unix: 946684800
|
||||
updated_unix: 946684800
|
||||
created_unix: 946685800
|
||||
updated_unix: 946685820
|
||||
|
||||
-
|
||||
id: 3
|
||||
|
@ -27,9 +27,9 @@
|
|||
status: 3 # pinned
|
||||
source: 1 # issue
|
||||
updated_by: 1
|
||||
issue_id: 2
|
||||
created_unix: 946684800
|
||||
updated_unix: 946684800
|
||||
issue_id: 3
|
||||
created_unix: 946686800
|
||||
updated_unix: 946686800
|
||||
|
||||
-
|
||||
id: 4
|
||||
|
@ -38,6 +38,17 @@
|
|||
status: 1 # unread
|
||||
source: 1 # issue
|
||||
updated_by: 1
|
||||
issue_id: 2
|
||||
created_unix: 946684800
|
||||
updated_unix: 946684800
|
||||
issue_id: 5
|
||||
created_unix: 946687800
|
||||
updated_unix: 946687800
|
||||
|
||||
-
|
||||
id: 5
|
||||
user_id: 2
|
||||
repo_id: 2
|
||||
status: 1 # unread
|
||||
source: 1 # issue
|
||||
updated_by: 5
|
||||
issue_id: 4
|
||||
created_unix: 946688800
|
||||
updated_unix: 946688820
|
||||
|
|
|
@ -843,6 +843,20 @@ func (issue *Issue) GetLastEventLabel() string {
|
|||
return "repo.issues.opened_by"
|
||||
}
|
||||
|
||||
// GetLastComment return last comment for the current issue.
|
||||
func (issue *Issue) GetLastComment() (*Comment, error) {
|
||||
var c Comment
|
||||
exist, err := x.Where("type = ?", CommentTypeComment).
|
||||
And("issue_id = ?", issue.ID).Desc("id").Get(&c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exist {
|
||||
return nil, nil
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// GetLastEventLabelFake returns the localization label for the current issue without providing a link in the username.
|
||||
func (issue *Issue) GetLastEventLabelFake() string {
|
||||
if issue.IsClosed {
|
||||
|
|
|
@ -8,6 +8,7 @@ package models
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
|
@ -235,6 +236,22 @@ func (c *Comment) HTMLURL() string {
|
|||
return fmt.Sprintf("%s#%s", c.Issue.HTMLURL(), c.HashTag())
|
||||
}
|
||||
|
||||
// APIURL formats a API-string to the issue-comment
|
||||
func (c *Comment) APIURL() string {
|
||||
err := c.LoadIssue()
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.loadRepo(x)
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
}
|
||||
|
||||
return c.Issue.Repo.APIURL() + "/" + path.Join("issues/comments", fmt.Sprint(c.ID))
|
||||
}
|
||||
|
||||
// IssueURL formats a URL-string to the issue
|
||||
func (c *Comment) IssueURL() string {
|
||||
err := c.LoadIssue()
|
||||
|
|
|
@ -293,6 +293,8 @@ var migrations = []Migration{
|
|||
// v118 -> v119
|
||||
NewMigration("Add commit id and stale to reviews", addReviewCommitAndStale),
|
||||
// v119 -> v120
|
||||
NewMigration("Fix migrated repositories' git service type", fixMigratedRepositoryServiceType),
|
||||
// v120 -> v121
|
||||
NewMigration("add is_restricted column for users table", addIsRestricted),
|
||||
}
|
||||
|
||||
|
|
|
@ -4,14 +4,13 @@
|
|||
|
||||
package migrations
|
||||
|
||||
import "xorm.io/xorm"
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
func addIsRestricted(x *xorm.Engine) error {
|
||||
// User see models/user.go
|
||||
type User struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
IsRestricted bool `xorm:"NOT NULL DEFAULT false"`
|
||||
}
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
return x.Sync2(new(User))
|
||||
func fixMigratedRepositoryServiceType(x *xorm.Engine) error {
|
||||
_, err := x.Exec("UPDATE repository SET original_service_type = ? WHERE original_url LIKE 'https://github.com/%'", structs.GithubService)
|
||||
return err
|
||||
}
|
||||
|
|
17
models/migrations/v120.go
Normal file
17
models/migrations/v120.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2020 The Gitea 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 migrations
|
||||
|
||||
import "xorm.io/xorm"
|
||||
|
||||
func addIsRestricted(x *xorm.Engine) error {
|
||||
// User see models/user.go
|
||||
type User struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
IsRestricted bool `xorm:"NOT NULL DEFAULT false"`
|
||||
}
|
||||
|
||||
return x.Sync2(new(User))
|
||||
}
|
|
@ -6,8 +6,14 @@ package models
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -47,17 +53,67 @@ type Notification struct {
|
|||
IssueID int64 `xorm:"INDEX NOT NULL"`
|
||||
CommitID string `xorm:"INDEX"`
|
||||
CommentID int64
|
||||
Comment *Comment `xorm:"-"`
|
||||
|
||||
UpdatedBy int64 `xorm:"INDEX NOT NULL"`
|
||||
|
||||
Issue *Issue `xorm:"-"`
|
||||
Repository *Repository `xorm:"-"`
|
||||
Comment *Comment `xorm:"-"`
|
||||
User *User `xorm:"-"`
|
||||
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created INDEX NOT NULL"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
|
||||
}
|
||||
|
||||
// FindNotificationOptions represent the filters for notifications. If an ID is 0 it will be ignored.
|
||||
type FindNotificationOptions struct {
|
||||
UserID int64
|
||||
RepoID int64
|
||||
IssueID int64
|
||||
Status NotificationStatus
|
||||
UpdatedAfterUnix int64
|
||||
UpdatedBeforeUnix int64
|
||||
}
|
||||
|
||||
// ToCond will convert each condition into a xorm-Cond
|
||||
func (opts *FindNotificationOptions) ToCond() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.UserID != 0 {
|
||||
cond = cond.And(builder.Eq{"notification.user_id": opts.UserID})
|
||||
}
|
||||
if opts.RepoID != 0 {
|
||||
cond = cond.And(builder.Eq{"notification.repo_id": opts.RepoID})
|
||||
}
|
||||
if opts.IssueID != 0 {
|
||||
cond = cond.And(builder.Eq{"notification.issue_id": opts.IssueID})
|
||||
}
|
||||
if opts.Status != 0 {
|
||||
cond = cond.And(builder.Eq{"notification.status": opts.Status})
|
||||
}
|
||||
if opts.UpdatedAfterUnix != 0 {
|
||||
cond = cond.And(builder.Gte{"notification.updated_unix": opts.UpdatedAfterUnix})
|
||||
}
|
||||
if opts.UpdatedBeforeUnix != 0 {
|
||||
cond = cond.And(builder.Lte{"notification.updated_unix": opts.UpdatedBeforeUnix})
|
||||
}
|
||||
return cond
|
||||
}
|
||||
|
||||
// ToSession will convert the given options to a xorm Session by using the conditions from ToCond and joining with issue table if required
|
||||
func (opts *FindNotificationOptions) ToSession(e Engine) *xorm.Session {
|
||||
return e.Where(opts.ToCond())
|
||||
}
|
||||
|
||||
func getNotifications(e Engine, options FindNotificationOptions) (nl NotificationList, err error) {
|
||||
err = options.ToSession(e).OrderBy("notification.updated_unix DESC").Find(&nl)
|
||||
return
|
||||
}
|
||||
|
||||
// GetNotifications returns all notifications that fit to the given options.
|
||||
func GetNotifications(opts FindNotificationOptions) (NotificationList, error) {
|
||||
return getNotifications(x, opts)
|
||||
}
|
||||
|
||||
// CreateOrUpdateIssueNotifications creates an issue notification
|
||||
// for each watcher, or updates it if already exists
|
||||
func CreateOrUpdateIssueNotifications(issueID, commentID int64, notificationAuthorID int64) error {
|
||||
|
@ -238,22 +294,124 @@ func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, p
|
|||
return
|
||||
}
|
||||
|
||||
// APIFormat converts a Notification to api.NotificationThread
|
||||
func (n *Notification) APIFormat() *api.NotificationThread {
|
||||
result := &api.NotificationThread{
|
||||
ID: n.ID,
|
||||
Unread: !(n.Status == NotificationStatusRead || n.Status == NotificationStatusPinned),
|
||||
Pinned: n.Status == NotificationStatusPinned,
|
||||
UpdatedAt: n.UpdatedUnix.AsTime(),
|
||||
URL: n.APIURL(),
|
||||
}
|
||||
|
||||
//since user only get notifications when he has access to use minimal access mode
|
||||
if n.Repository != nil {
|
||||
result.Repository = n.Repository.APIFormat(AccessModeRead)
|
||||
}
|
||||
|
||||
//handle Subject
|
||||
switch n.Source {
|
||||
case NotificationSourceIssue:
|
||||
result.Subject = &api.NotificationSubject{Type: "Issue"}
|
||||
if n.Issue != nil {
|
||||
result.Subject.Title = n.Issue.Title
|
||||
result.Subject.URL = n.Issue.APIURL()
|
||||
comment, err := n.Issue.GetLastComment()
|
||||
if err == nil && comment != nil {
|
||||
result.Subject.LatestCommentURL = comment.APIURL()
|
||||
}
|
||||
}
|
||||
case NotificationSourcePullRequest:
|
||||
result.Subject = &api.NotificationSubject{Type: "Pull"}
|
||||
if n.Issue != nil {
|
||||
result.Subject.Title = n.Issue.Title
|
||||
result.Subject.URL = n.Issue.APIURL()
|
||||
comment, err := n.Issue.GetLastComment()
|
||||
if err == nil && comment != nil {
|
||||
result.Subject.LatestCommentURL = comment.APIURL()
|
||||
}
|
||||
}
|
||||
case NotificationSourceCommit:
|
||||
result.Subject = &api.NotificationSubject{
|
||||
Type: "Commit",
|
||||
Title: n.CommitID,
|
||||
}
|
||||
//unused until now
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// LoadAttributes load Repo Issue User and Comment if not loaded
|
||||
func (n *Notification) LoadAttributes() (err error) {
|
||||
return n.loadAttributes(x)
|
||||
}
|
||||
|
||||
func (n *Notification) loadAttributes(e Engine) (err error) {
|
||||
if err = n.loadRepo(e); err != nil {
|
||||
return
|
||||
}
|
||||
if err = n.loadIssue(e); err != nil {
|
||||
return
|
||||
}
|
||||
if err = n.loadUser(e); err != nil {
|
||||
return
|
||||
}
|
||||
if err = n.loadComment(e); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (n *Notification) loadRepo(e Engine) (err error) {
|
||||
if n.Repository == nil {
|
||||
n.Repository, err = getRepositoryByID(e, n.RepoID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getRepositoryByID [%d]: %v", n.RepoID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Notification) loadIssue(e Engine) (err error) {
|
||||
if n.Issue == nil {
|
||||
n.Issue, err = getIssueByID(e, n.IssueID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getIssueByID [%d]: %v", n.IssueID, err)
|
||||
}
|
||||
return n.Issue.loadAttributes(e)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Notification) loadComment(e Engine) (err error) {
|
||||
if n.Comment == nil && n.CommentID > 0 {
|
||||
n.Comment, err = GetCommentByID(n.CommentID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetCommentByID [%d]: %v", n.CommentID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Notification) loadUser(e Engine) (err error) {
|
||||
if n.User == nil {
|
||||
n.User, err = getUserByID(e, n.UserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getUserByID [%d]: %v", n.UserID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRepo returns the repo of the notification
|
||||
func (n *Notification) GetRepo() (*Repository, error) {
|
||||
n.Repository = new(Repository)
|
||||
_, err := x.
|
||||
Where("id = ?", n.RepoID).
|
||||
Get(n.Repository)
|
||||
return n.Repository, err
|
||||
return n.Repository, n.loadRepo(x)
|
||||
}
|
||||
|
||||
// GetIssue returns the issue of the notification
|
||||
func (n *Notification) GetIssue() (*Issue, error) {
|
||||
n.Issue = new(Issue)
|
||||
_, err := x.
|
||||
Where("id = ?", n.IssueID).
|
||||
Get(n.Issue)
|
||||
return n.Issue, err
|
||||
return n.Issue, n.loadIssue(x)
|
||||
}
|
||||
|
||||
// HTMLURL formats a URL-string to the notification
|
||||
|
@ -264,9 +422,34 @@ func (n *Notification) HTMLURL() string {
|
|||
return n.Issue.HTMLURL()
|
||||
}
|
||||
|
||||
// APIURL formats a URL-string to the notification
|
||||
func (n *Notification) APIURL() string {
|
||||
return setting.AppURL + path.Join("api/v1/notifications/threads", fmt.Sprintf("%d", n.ID))
|
||||
}
|
||||
|
||||
// NotificationList contains a list of notifications
|
||||
type NotificationList []*Notification
|
||||
|
||||
// APIFormat converts a NotificationList to api.NotificationThread list
|
||||
func (nl NotificationList) APIFormat() []*api.NotificationThread {
|
||||
var result = make([]*api.NotificationThread, 0, len(nl))
|
||||
for _, n := range nl {
|
||||
result = append(result, n.APIFormat())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// LoadAttributes load Repo Issue User and Comment if not loaded
|
||||
func (nl NotificationList) LoadAttributes() (err error) {
|
||||
for i := 0; i < len(nl); i++ {
|
||||
err = nl[i].LoadAttributes()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (nl NotificationList) getPendingRepoIDs() []int64 {
|
||||
var ids = make(map[int64]struct{}, len(nl))
|
||||
for _, notification := range nl {
|
||||
|
@ -486,7 +669,7 @@ func setNotificationStatusReadIfUnread(e Engine, userID, issueID int64) error {
|
|||
|
||||
// SetNotificationStatus change the notification status
|
||||
func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
|
||||
notification, err := getNotificationByID(notificationID)
|
||||
notification, err := getNotificationByID(x, notificationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -501,9 +684,14 @@ func SetNotificationStatus(notificationID int64, user *User, status Notification
|
|||
return err
|
||||
}
|
||||
|
||||
func getNotificationByID(notificationID int64) (*Notification, error) {
|
||||
// GetNotificationByID return notification by ID
|
||||
func GetNotificationByID(notificationID int64) (*Notification, error) {
|
||||
return getNotificationByID(x, notificationID)
|
||||
}
|
||||
|
||||
func getNotificationByID(e Engine, notificationID int64) (*Notification, error) {
|
||||
notification := new(Notification)
|
||||
ok, err := x.
|
||||
ok, err := e.
|
||||
Where("id = ?", notificationID).
|
||||
Get(notification)
|
||||
|
||||
|
@ -512,7 +700,7 @@ func getNotificationByID(notificationID int64) (*Notification, error) {
|
|||
}
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Notification %d does not exists", notificationID)
|
||||
return nil, ErrNotExist{ID: notificationID}
|
||||
}
|
||||
|
||||
return notification, nil
|
||||
|
|
|
@ -31,11 +31,13 @@ func TestNotificationsForUser(t *testing.T) {
|
|||
statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread}
|
||||
notfs, err := NotificationsForUser(user, statuses, 1, 10)
|
||||
assert.NoError(t, err)
|
||||
if assert.Len(t, notfs, 2) {
|
||||
assert.EqualValues(t, 2, notfs[0].ID)
|
||||
if assert.Len(t, notfs, 3) {
|
||||
assert.EqualValues(t, 5, notfs[0].ID)
|
||||
assert.EqualValues(t, user.ID, notfs[0].UserID)
|
||||
assert.EqualValues(t, 4, notfs[1].ID)
|
||||
assert.EqualValues(t, user.ID, notfs[1].UserID)
|
||||
assert.EqualValues(t, 2, notfs[2].ID)
|
||||
assert.EqualValues(t, user.ID, notfs[2].UserID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -590,7 +590,8 @@ func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
|
|||
return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
|
||||
}
|
||||
|
||||
if _, err = sess.ID(t.ID).AllCols().Update(t); err != nil {
|
||||
if _, err = sess.ID(t.ID).Cols("name", "lower_name", "description",
|
||||
"can_create_org_repo", "authorize", "includes_all_repositories").Update(t); err != nil {
|
||||
return fmt.Errorf("update: %v", err)
|
||||
}
|
||||
|
||||
|
@ -605,8 +606,7 @@ func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
|
|||
Delete(new(TeamUnit)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = sess.Insert(&t.Units); err != nil {
|
||||
if _, err = sess.Cols("org_id", "team_id", "type").Insert(&t.Units); err != nil {
|
||||
errRollback := sess.Rollback()
|
||||
if errRollback != nil {
|
||||
log.Error("UpdateTeam sess.Rollback: %v", errRollback)
|
||||
|
|
135
models/pull.go
135
models/pull.go
|
@ -13,7 +13,6 @@ import (
|
|||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
|
@ -35,6 +34,7 @@ const (
|
|||
PullRequestStatusChecking
|
||||
PullRequestStatusMergeable
|
||||
PullRequestStatusManuallyMerged
|
||||
PullRequestStatusError
|
||||
)
|
||||
|
||||
// PullRequest represents relation between pull request and repositories.
|
||||
|
@ -396,139 +396,6 @@ func (pr *PullRequest) GetGitRefName() string {
|
|||
return fmt.Sprintf("refs/pull/%d/head", pr.Index)
|
||||
}
|
||||
|
||||
// APIFormat assumes following fields have been assigned with valid values:
|
||||
// Required - Issue
|
||||
// Optional - Merger
|
||||
func (pr *PullRequest) APIFormat() *api.PullRequest {
|
||||
return pr.apiFormat(x)
|
||||
}
|
||||
|
||||
func (pr *PullRequest) apiFormat(e Engine) *api.PullRequest {
|
||||
var (
|
||||
baseBranch *git.Branch
|
||||
headBranch *git.Branch
|
||||
baseCommit *git.Commit
|
||||
headCommit *git.Commit
|
||||
err error
|
||||
)
|
||||
if err = pr.Issue.loadRepo(e); err != nil {
|
||||
log.Error("loadRepo[%d]: %v", pr.ID, err)
|
||||
return nil
|
||||
}
|
||||
apiIssue := pr.Issue.apiFormat(e)
|
||||
if pr.BaseRepo == nil {
|
||||
pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
|
||||
if err != nil {
|
||||
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if pr.HeadRepo == nil {
|
||||
pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
|
||||
if err != nil {
|
||||
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if err = pr.Issue.loadRepo(e); err != nil {
|
||||
log.Error("pr.Issue.loadRepo[%d]: %v", pr.ID, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
apiPullRequest := &api.PullRequest{
|
||||
ID: pr.ID,
|
||||
URL: pr.Issue.HTMLURL(),
|
||||
Index: pr.Index,
|
||||
Poster: apiIssue.Poster,
|
||||
Title: apiIssue.Title,
|
||||
Body: apiIssue.Body,
|
||||
Labels: apiIssue.Labels,
|
||||
Milestone: apiIssue.Milestone,
|
||||
Assignee: apiIssue.Assignee,
|
||||
Assignees: apiIssue.Assignees,
|
||||
State: apiIssue.State,
|
||||
Comments: apiIssue.Comments,
|
||||
HTMLURL: pr.Issue.HTMLURL(),
|
||||
DiffURL: pr.Issue.DiffURL(),
|
||||
PatchURL: pr.Issue.PatchURL(),
|
||||
HasMerged: pr.HasMerged,
|
||||
MergeBase: pr.MergeBase,
|
||||
Deadline: apiIssue.Deadline,
|
||||
Created: pr.Issue.CreatedUnix.AsTimePtr(),
|
||||
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
|
||||
}
|
||||
baseBranch, err = pr.BaseRepo.GetBranch(pr.BaseBranch)
|
||||
if err != nil {
|
||||
if git.IsErrBranchNotExist(err) {
|
||||
apiPullRequest.Base = nil
|
||||
} else {
|
||||
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
apiBaseBranchInfo := &api.PRBranchInfo{
|
||||
Name: pr.BaseBranch,
|
||||
Ref: pr.BaseBranch,
|
||||
RepoID: pr.BaseRepoID,
|
||||
Repository: pr.BaseRepo.innerAPIFormat(e, AccessModeNone, false),
|
||||
}
|
||||
baseCommit, err = baseBranch.GetCommit()
|
||||
if err != nil {
|
||||
if git.IsErrNotExist(err) {
|
||||
apiBaseBranchInfo.Sha = ""
|
||||
} else {
|
||||
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
apiBaseBranchInfo.Sha = baseCommit.ID.String()
|
||||
}
|
||||
apiPullRequest.Base = apiBaseBranchInfo
|
||||
}
|
||||
|
||||
headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch)
|
||||
if err != nil {
|
||||
if git.IsErrBranchNotExist(err) {
|
||||
apiPullRequest.Head = nil
|
||||
} else {
|
||||
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
apiHeadBranchInfo := &api.PRBranchInfo{
|
||||
Name: pr.HeadBranch,
|
||||
Ref: pr.HeadBranch,
|
||||
RepoID: pr.HeadRepoID,
|
||||
Repository: pr.HeadRepo.innerAPIFormat(e, AccessModeNone, false),
|
||||
}
|
||||
headCommit, err = headBranch.GetCommit()
|
||||
if err != nil {
|
||||
if git.IsErrNotExist(err) {
|
||||
apiHeadBranchInfo.Sha = ""
|
||||
} else {
|
||||
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
apiHeadBranchInfo.Sha = headCommit.ID.String()
|
||||
}
|
||||
apiPullRequest.Head = apiHeadBranchInfo
|
||||
}
|
||||
|
||||
if pr.Status != PullRequestStatusChecking {
|
||||
mergeable := pr.Status != PullRequestStatusConflict && !pr.IsWorkInProgress()
|
||||
apiPullRequest.Mergeable = mergeable
|
||||
}
|
||||
if pr.HasMerged {
|
||||
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
|
||||
apiPullRequest.MergedCommitID = &pr.MergedCommitID
|
||||
apiPullRequest.MergedBy = pr.Merger.APIFormat()
|
||||
}
|
||||
|
||||
return apiPullRequest
|
||||
}
|
||||
|
||||
func (pr *PullRequest) getHeadRepo(e Engine) (err error) {
|
||||
pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
|
||||
if err != nil && !IsErrRepoNotExist(err) {
|
||||
|
|
|
@ -29,16 +29,6 @@ func TestPullRequest_LoadIssue(t *testing.T) {
|
|||
assert.Equal(t, int64(2), pr.Issue.ID)
|
||||
}
|
||||
|
||||
func TestPullRequest_APIFormat(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
||||
assert.NoError(t, pr.LoadAttributes())
|
||||
assert.NoError(t, pr.LoadIssue())
|
||||
apiPullRequest := pr.APIFormat()
|
||||
assert.NotNil(t, apiPullRequest)
|
||||
assert.Nil(t, apiPullRequest.Head)
|
||||
}
|
||||
|
||||
func TestPullRequest_GetBaseRepo(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
||||
|
@ -182,7 +172,7 @@ func TestPullRequest_UpdateCols(t *testing.T) {
|
|||
BaseBranch: "baseBranch",
|
||||
HeadBranch: "headBranch",
|
||||
}
|
||||
pr.UpdateCols("head_branch")
|
||||
assert.NoError(t, pr.UpdateCols("head_branch"))
|
||||
|
||||
pr = AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
||||
assert.Equal(t, "master", pr.BaseBranch)
|
||||
|
|
|
@ -1071,17 +1071,18 @@ func initRepoCommit(tmpPath string, repo *Repository, u *User) (err error) {
|
|||
|
||||
// CreateRepoOptions contains the create repository options
|
||||
type CreateRepoOptions struct {
|
||||
Name string
|
||||
Description string
|
||||
OriginalURL string
|
||||
Gitignores string
|
||||
IssueLabels string
|
||||
License string
|
||||
Readme string
|
||||
IsPrivate bool
|
||||
IsMirror bool
|
||||
AutoInit bool
|
||||
Status RepositoryStatus
|
||||
Name string
|
||||
Description string
|
||||
OriginalURL string
|
||||
GitServiceType structs.GitServiceType
|
||||
Gitignores string
|
||||
IssueLabels string
|
||||
License string
|
||||
Readme string
|
||||
IsPrivate bool
|
||||
IsMirror bool
|
||||
AutoInit bool
|
||||
Status RepositoryStatus
|
||||
}
|
||||
|
||||
func getRepoInitFile(tp, name string) ([]byte, error) {
|
||||
|
@ -1369,6 +1370,7 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
|
|||
LowerName: strings.ToLower(opts.Name),
|
||||
Description: opts.Description,
|
||||
OriginalURL: opts.OriginalURL,
|
||||
OriginalServiceType: opts.GitServiceType,
|
||||
IsPrivate: opts.IsPrivate,
|
||||
IsFsckEnabled: !opts.IsMirror,
|
||||
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
|
||||
|
|
|
@ -194,12 +194,13 @@ func CreateMigrateTask(doer, u *User, opts base.MigrateOptions) (*Task, error) {
|
|||
}
|
||||
|
||||
repo, err := CreateRepository(doer, u, CreateRepoOptions{
|
||||
Name: opts.RepoName,
|
||||
Description: opts.Description,
|
||||
OriginalURL: opts.OriginalURL,
|
||||
IsPrivate: opts.Private,
|
||||
IsMirror: opts.Mirror,
|
||||
Status: RepositoryBeingMigrated,
|
||||
Name: opts.RepoName,
|
||||
Description: opts.Description,
|
||||
OriginalURL: opts.OriginalURL,
|
||||
GitServiceType: opts.GitServiceType,
|
||||
IsPrivate: opts.Private,
|
||||
IsMirror: opts.Mirror,
|
||||
Status: RepositoryBeingMigrated,
|
||||
})
|
||||
if err != nil {
|
||||
task.EndTime = timeutil.TimeStampNow()
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -27,33 +26,6 @@ const (
|
|||
EnvIsInternal = "GITEA_INTERNAL_PUSH"
|
||||
)
|
||||
|
||||
// CommitToPushCommit transforms a git.Commit to PushCommit type.
|
||||
func CommitToPushCommit(commit *git.Commit) *PushCommit {
|
||||
return &PushCommit{
|
||||
Sha1: commit.ID.String(),
|
||||
Message: commit.Message(),
|
||||
AuthorEmail: commit.Author.Email,
|
||||
AuthorName: commit.Author.Name,
|
||||
CommitterEmail: commit.Committer.Email,
|
||||
CommitterName: commit.Committer.Name,
|
||||
Timestamp: commit.Author.When,
|
||||
}
|
||||
}
|
||||
|
||||
// ListToPushCommits transforms a list.List to PushCommits type.
|
||||
func ListToPushCommits(l *list.List) *PushCommits {
|
||||
var commits []*PushCommit
|
||||
var actEmail string
|
||||
for e := l.Front(); e != nil; e = e.Next() {
|
||||
commit := e.Value.(*git.Commit)
|
||||
if actEmail == "" {
|
||||
actEmail = commit.Committer.Email
|
||||
}
|
||||
commits = append(commits, CommitToPushCommit(commit))
|
||||
}
|
||||
return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*User)}
|
||||
}
|
||||
|
||||
// PushUpdateAddDeleteTags updates a number of added and delete tags
|
||||
func PushUpdateAddDeleteTags(repo *Repository, gitRepo *git.Repository, addTags, delTags []string) error {
|
||||
sess := x.NewSession()
|
||||
|
@ -258,75 +230,25 @@ func pushUpdateAddTags(e Engine, repo *Repository, gitRepo *git.Repository, tags
|
|||
return nil
|
||||
}
|
||||
|
||||
// PushUpdateAddTag must be called for any push actions to add tag
|
||||
func PushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) error {
|
||||
rel, err := GetRelease(repo.ID, tagName)
|
||||
// SaveOrUpdateTag must be called for any push actions to add tag
|
||||
func SaveOrUpdateTag(repo *Repository, newRel *Release) error {
|
||||
rel, err := GetRelease(repo.ID, newRel.TagName)
|
||||
if err != nil && !IsErrReleaseNotExist(err) {
|
||||
return fmt.Errorf("GetRelease: %v", err)
|
||||
}
|
||||
|
||||
tag, err := gitRepo.GetTag(tagName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetTag: %v", err)
|
||||
}
|
||||
commit, err := tag.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
|
||||
sig := tag.Tagger
|
||||
if sig == nil {
|
||||
sig = commit.Author
|
||||
}
|
||||
if sig == nil {
|
||||
sig = commit.Committer
|
||||
}
|
||||
|
||||
var author *User
|
||||
var createdAt = time.Unix(1, 0)
|
||||
|
||||
if sig != nil {
|
||||
author, err = GetUserByEmail(sig.Email)
|
||||
if err != nil && !IsErrUserNotExist(err) {
|
||||
return fmt.Errorf("GetUserByEmail: %v", err)
|
||||
}
|
||||
createdAt = sig.When
|
||||
}
|
||||
|
||||
commitsCount, err := commit.CommitsCount()
|
||||
if err != nil {
|
||||
return fmt.Errorf("CommitsCount: %v", err)
|
||||
}
|
||||
|
||||
if rel == nil {
|
||||
rel = &Release{
|
||||
RepoID: repo.ID,
|
||||
Title: "",
|
||||
TagName: tagName,
|
||||
LowerTagName: strings.ToLower(tagName),
|
||||
Target: "",
|
||||
Sha1: commit.ID.String(),
|
||||
NumCommits: commitsCount,
|
||||
Note: "",
|
||||
IsDraft: false,
|
||||
IsPrerelease: false,
|
||||
IsTag: true,
|
||||
CreatedUnix: timeutil.TimeStamp(createdAt.Unix()),
|
||||
}
|
||||
if author != nil {
|
||||
rel.PublisherID = author.ID
|
||||
}
|
||||
|
||||
if _, err = x.InsertOne(rel); err != nil {
|
||||
rel = newRel
|
||||
if _, err = x.Insert(rel); err != nil {
|
||||
return fmt.Errorf("InsertOne: %v", err)
|
||||
}
|
||||
} else {
|
||||
rel.Sha1 = commit.ID.String()
|
||||
rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
|
||||
rel.NumCommits = commitsCount
|
||||
rel.Sha1 = newRel.Sha1
|
||||
rel.CreatedUnix = newRel.CreatedUnix
|
||||
rel.NumCommits = newRel.NumCommits
|
||||
rel.IsDraft = false
|
||||
if rel.IsTag && author != nil {
|
||||
rel.PublisherID = author.ID
|
||||
if rel.IsTag && newRel.PublisherID > 0 {
|
||||
rel.PublisherID = newRel.PublisherID
|
||||
}
|
||||
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
||||
return fmt.Errorf("Update: %v", err)
|
||||
|
|
|
@ -1,86 +0,0 @@
|
|||
// Copyright 2016 The Gitea 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 (
|
||||
"container/list"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCommitToPushCommit(t *testing.T) {
|
||||
now := time.Now()
|
||||
sig := &git.Signature{
|
||||
Email: "example@example.com",
|
||||
Name: "John Doe",
|
||||
When: now,
|
||||
}
|
||||
const hexString = "0123456789abcdef0123456789abcdef01234567"
|
||||
sha1, err := git.NewIDFromString(hexString)
|
||||
assert.NoError(t, err)
|
||||
pushCommit := CommitToPushCommit(&git.Commit{
|
||||
ID: sha1,
|
||||
Author: sig,
|
||||
Committer: sig,
|
||||
CommitMessage: "Commit Message",
|
||||
})
|
||||
assert.Equal(t, hexString, pushCommit.Sha1)
|
||||
assert.Equal(t, "Commit Message", pushCommit.Message)
|
||||
assert.Equal(t, "example@example.com", pushCommit.AuthorEmail)
|
||||
assert.Equal(t, "John Doe", pushCommit.AuthorName)
|
||||
assert.Equal(t, "example@example.com", pushCommit.CommitterEmail)
|
||||
assert.Equal(t, "John Doe", pushCommit.CommitterName)
|
||||
assert.Equal(t, now, pushCommit.Timestamp)
|
||||
}
|
||||
|
||||
func TestListToPushCommits(t *testing.T) {
|
||||
now := time.Now()
|
||||
sig := &git.Signature{
|
||||
Email: "example@example.com",
|
||||
Name: "John Doe",
|
||||
When: now,
|
||||
}
|
||||
|
||||
const hexString1 = "0123456789abcdef0123456789abcdef01234567"
|
||||
hash1, err := git.NewIDFromString(hexString1)
|
||||
assert.NoError(t, err)
|
||||
const hexString2 = "fedcba9876543210fedcba9876543210fedcba98"
|
||||
hash2, err := git.NewIDFromString(hexString2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
l := list.New()
|
||||
l.PushBack(&git.Commit{
|
||||
ID: hash1,
|
||||
Author: sig,
|
||||
Committer: sig,
|
||||
CommitMessage: "Message1",
|
||||
})
|
||||
l.PushBack(&git.Commit{
|
||||
ID: hash2,
|
||||
Author: sig,
|
||||
Committer: sig,
|
||||
CommitMessage: "Message2",
|
||||
})
|
||||
|
||||
pushCommits := ListToPushCommits(l)
|
||||
assert.Equal(t, 2, pushCommits.Len)
|
||||
if assert.Len(t, pushCommits.Commits, 2) {
|
||||
assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
|
||||
assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
|
||||
assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
|
||||
assert.Equal(t, now, pushCommits.Commits[0].Timestamp)
|
||||
|
||||
assert.Equal(t, "Message2", pushCommits.Commits[1].Message)
|
||||
assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1)
|
||||
assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
|
||||
assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO TestPushUpdate
|
Loading…
Add table
Add a link
Reference in a new issue