Skip to content

Commit f849273

Browse files
committed
Merge branch 'release/v1.10' of git://github.com/go-gitea/gitea into wild/v1.10
2 parents 1d389ce + c63a801 commit f849273

File tree

13 files changed

+137
-27
lines changed

13 files changed

+137
-27
lines changed

models/branches.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ func (deletedBranch *DeletedBranch) LoadUser() {
480480
deletedBranch.DeletedBy = user
481481
}
482482

483+
// RemoveDeletedBranch removes all deleted branches
484+
func RemoveDeletedBranch(repoID int64, branch string) error {
485+
_, err := x.Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
486+
return err
487+
}
488+
483489
// RemoveOldDeletedBranches removes old deleted branches
484490
func RemoveOldDeletedBranches() {
485491
log.Trace("Doing: DeletedBranchesCleanup")

models/error.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,21 @@ func (err ErrNewIssueInsert) Error() string {
11041104
return err.OriginalError.Error()
11051105
}
11061106

1107+
// ErrReactionAlreadyExist is used when a existing reaction was try to created
1108+
type ErrReactionAlreadyExist struct {
1109+
Reaction string
1110+
}
1111+
1112+
// IsErrReactionAlreadyExist checks if an error is a ErrReactionAlreadyExist.
1113+
func IsErrReactionAlreadyExist(err error) bool {
1114+
_, ok := err.(ErrReactionAlreadyExist)
1115+
return ok
1116+
}
1117+
1118+
func (err ErrReactionAlreadyExist) Error() string {
1119+
return fmt.Sprintf("reaction '%s' already exists", err.Reaction)
1120+
}
1121+
11071122
// __________ .__ .__ __________ __
11081123
// \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
11091124
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\

models/issue_reaction.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type Reaction struct {
3030
type FindReactionsOptions struct {
3131
IssueID int64
3232
CommentID int64
33+
UserID int64
34+
Reaction string
3335
}
3436

3537
func (opts *FindReactionsOptions) toConds() builder.Cond {
@@ -40,6 +42,13 @@ func (opts *FindReactionsOptions) toConds() builder.Cond {
4042
if opts.CommentID > 0 {
4143
cond = cond.And(builder.Eq{"reaction.comment_id": opts.CommentID})
4244
}
45+
if opts.UserID > 0 {
46+
cond = cond.And(builder.Eq{"reaction.user_id": opts.UserID})
47+
}
48+
if opts.Reaction != "" {
49+
cond = cond.And(builder.Eq{"reaction.type": opts.Reaction})
50+
}
51+
4352
return cond
4453
}
4554

@@ -57,9 +66,25 @@ func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) {
5766
UserID: opts.Doer.ID,
5867
IssueID: opts.Issue.ID,
5968
}
69+
findOpts := FindReactionsOptions{
70+
IssueID: opts.Issue.ID,
71+
CommentID: -1, // reaction to issue only
72+
Reaction: opts.Type,
73+
UserID: opts.Doer.ID,
74+
}
6075
if opts.Comment != nil {
6176
reaction.CommentID = opts.Comment.ID
77+
findOpts.CommentID = opts.Comment.ID
78+
}
79+
80+
existingR, err := findReactions(e, findOpts)
81+
if err != nil {
82+
return nil, err
83+
}
84+
if len(existingR) > 0 {
85+
return existingR[0], ErrReactionAlreadyExist{Reaction: opts.Type}
6286
}
87+
6388
if _, err := e.Insert(reaction); err != nil {
6489
return nil, err
6590
}
@@ -76,19 +101,19 @@ type ReactionOptions struct {
76101
}
77102

78103
// CreateReaction creates reaction for issue or comment.
79-
func CreateReaction(opts *ReactionOptions) (reaction *Reaction, err error) {
104+
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
80105
sess := x.NewSession()
81106
defer sess.Close()
82-
if err = sess.Begin(); err != nil {
107+
if err := sess.Begin(); err != nil {
83108
return nil, err
84109
}
85110

86-
reaction, err = createReaction(sess, opts)
111+
reaction, err := createReaction(sess, opts)
87112
if err != nil {
88-
return nil, err
113+
return reaction, err
89114
}
90115

91-
if err = sess.Commit(); err != nil {
116+
if err := sess.Commit(); err != nil {
92117
return nil, err
93118
}
94119
return reaction, nil

models/issue_reaction_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ func TestIssueAddDuplicateReaction(t *testing.T) {
5050
Type: "heart",
5151
})
5252
assert.Error(t, err)
53-
assert.Nil(t, reaction)
53+
assert.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err)
5454

55-
AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
55+
existingR := AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction)
56+
assert.Equal(t, existingR.ID, reaction.ID)
5657
}
5758

5859
func TestIssueDeleteReaction(t *testing.T) {
@@ -129,7 +130,6 @@ func TestIssueCommentDeleteReaction(t *testing.T) {
129130
user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
130131
user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
131132
user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
132-
ghost := NewGhostUser()
133133

134134
issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
135135

@@ -139,14 +139,13 @@ func TestIssueCommentDeleteReaction(t *testing.T) {
139139
addReaction(t, user2, issue1, comment1, "heart")
140140
addReaction(t, user3, issue1, comment1, "heart")
141141
addReaction(t, user4, issue1, comment1, "+1")
142-
addReaction(t, ghost, issue1, comment1, "heart")
143142

144143
err := comment1.LoadReactions()
145144
assert.NoError(t, err)
146-
assert.Len(t, comment1.Reactions, 5)
145+
assert.Len(t, comment1.Reactions, 4)
147146

148147
reactions := comment1.Reactions.GroupByType()
149-
assert.Len(t, reactions["heart"], 4)
148+
assert.Len(t, reactions["heart"], 3)
150149
assert.Len(t, reactions["+1"], 1)
151150
}
152151

@@ -160,7 +159,7 @@ func TestIssueCommentReactionCount(t *testing.T) {
160159
comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
161160

162161
addReaction(t, user1, issue1, comment1, "heart")
163-
DeleteCommentReaction(user1, issue1, comment1, "heart")
162+
assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart"))
164163

165164
AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
166165
}

models/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,12 @@ func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
17811781
return sess.Commit()
17821782
}
17831783

1784+
// UpdateRepositoryStatus updates a repository's status
1785+
func UpdateRepositoryStatus(repoID int64, status RepositoryStatus) error {
1786+
_, err := x.Exec("UPDATE repository SET status = ? WHERE id = ?", status, repoID)
1787+
return err
1788+
}
1789+
17841790
// UpdateRepositoryUpdatedTime updates a repository's updated time
17851791
func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
17861792
_, err := x.Exec("UPDATE repository SET updated_unix = ? WHERE id = ?", updateTime.Unix(), repoID)

modules/repofiles/update.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,15 @@ func PushUpdate(repo *models.Repository, branch string, opts models.PushUpdateOp
453453
}
454454
}
455455
} else if !isDelRef {
456+
branchName := opts.RefFullName[len(git.BranchPrefix):]
457+
if err = models.RemoveDeletedBranch(repo.ID, branchName); err != nil {
458+
log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branchName, err)
459+
}
460+
456461
// If is branch reference
457462

458463
// Clear cache for branch commit count
459-
cache.Remove(repo.GetCommitsCountCacheKey(opts.RefFullName[len(git.BranchPrefix):], true))
464+
cache.Remove(repo.GetCommitsCountCacheKey(branchName, true))
460465

461466
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
462467
if err != nil {

modules/templates/helper.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ func NewFuncMap() []template.FuncMap {
239239
"MirrorFullAddress": mirror_service.AddressNoCredentials,
240240
"MirrorUserName": mirror_service.Username,
241241
"MirrorPassword": mirror_service.Password,
242+
"contain": func(s []int64, id int64) bool {
243+
for i := 0; i < len(s); i++ {
244+
if s[i] == id {
245+
return true
246+
}
247+
}
248+
return false
249+
},
242250
}}
243251
}
244252

routers/api/v1/repo/repo.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package repo
77

88
import (
9+
"bytes"
10+
"errors"
911
"fmt"
1012
"net/http"
1113
"net/url"
@@ -431,10 +433,31 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
431433
opts.Releases = false
432434
}
433435

434-
repo, err := migrations.MigrateRepository(ctx.User, ctxUser.Name, opts)
435-
if err == nil {
436-
notification.NotifyCreateRepository(ctx.User, ctxUser, repo)
436+
var repo *models.Repository
437+
defer func() {
438+
if e := recover(); e != nil {
439+
var buf bytes.Buffer
440+
fmt.Fprintf(&buf, "Handler crashed with error: %v", log.Stack(2))
441+
err = errors.New(buf.String())
442+
}
443+
444+
if err == nil {
445+
repo.Status = models.RepositoryReady
446+
if err := models.UpdateRepositoryStatus(repo.ID, repo.Status); err == nil {
447+
notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
448+
return
449+
}
450+
}
437451

452+
if repo != nil {
453+
if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
454+
log.Error("DeleteRepository: %v", errDelete)
455+
}
456+
}
457+
}()
458+
459+
repo, err = migrations.MigrateRepository(ctx.User, ctxUser.Name, opts)
460+
if err == nil {
438461
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
439462
ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin))
440463
return

routers/repo/issue.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
261261
}
262262

263263
ctx.Data["IssueStats"] = issueStats
264-
ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
264+
ctx.Data["SelLabelIDs"] = labelIDs
265+
ctx.Data["SelectLabels"] = selectLabels
265266
ctx.Data["ViewType"] = viewType
266267
ctx.Data["SortType"] = sortType
267268
ctx.Data["MilestoneID"] = milestoneID

routers/repo/pull.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"code.gitea.io/gitea/modules/git"
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/notification"
24+
"code.gitea.io/gitea/modules/repofiles"
2425
"code.gitea.io/gitea/modules/setting"
2526
"code.gitea.io/gitea/modules/util"
2627
"code.gitea.io/gitea/services/gitdiff"
@@ -928,6 +929,21 @@ func CleanUpPullRequest(ctx *context.Context) {
928929
return
929930
}
930931

932+
if err := repofiles.PushUpdate(
933+
pr.HeadRepo,
934+
pr.HeadBranch,
935+
models.PushUpdateOptions{
936+
RefFullName: git.BranchPrefix + pr.HeadBranch,
937+
OldCommitID: branchCommitID,
938+
NewCommitID: git.EmptySHA,
939+
PusherID: ctx.User.ID,
940+
PusherName: ctx.User.Name,
941+
RepoUserName: pr.HeadRepo.Owner.Name,
942+
RepoName: pr.HeadRepo.Name,
943+
}); err != nil {
944+
log.Error("Update: %v", err)
945+
}
946+
931947
if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
932948
// Do not fail here as branch has already been deleted
933949
log.Error("DeleteBranch: %v", err)

0 commit comments

Comments
 (0)