Skip to content

Commit 9d78c9c

Browse files
committed
Merge branch 'main' into feature/api_user_org_perm
2 parents bb2752d + 758c8c8 commit 9d78c9c

35 files changed

+1223
-44
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ This changelog goes through all the changes that have been made in each release
44
without substantial changes to our git log; to see the highlights of what has
55
been added to each release, please refer to the [blog](https://blog.gitea.io).
66

7+
## [1.15.4](https://github.com/go-gitea/gitea/releases/tag/v1.15.4) - 2021-10-08
8+
* BUGFIXES
9+
* Raw file API: don't try to interpret 40char filenames as commit SHA (#17185) (#17272)
10+
* Don't allow merged PRs to be reopened (#17192) (#17271)
11+
* Fix incorrect repository count on organization tab of dashboard (#17256) (#17266)
12+
* Fix unwanted team review request deletion (#17257) (#17264)
13+
* Fix broken Activities link in team dashboard (#17255) (#17258)
14+
* API pull's head/base have correct permission(#17214) (#17245)
15+
* Fix stange behavior of DownloadPullDiffOrPatch in incorect index (#17223) (#17227)
16+
* Upgrade xorm to v1.2.5 (#17177) (#17188)
17+
* Fix missing repo link in issue/pull assigned emails (#17183) (#17184)
18+
* Fix bug of get context user (#17169) (#17172)
19+
* Nicely handle missing user in collaborations (#17049) (#17166)
20+
* Add Horizontal scrollbar to inner menu on Chrome (#17086) (#17164)
21+
* Fix wrong i18n keys (#17150) (#17153)
22+
* Fix Archive Creation: correct transaction ending (#17151)
23+
* Prevent panic in Org mode HighlightCodeBlock (#17140) (#17141)
24+
* Create doctor command to fix repo_units broken by dumps from 1.14.3-1.14.6 (#17136) (#17137)
25+
* ENHANCEMENT
26+
* Check user instead of organization when creating a repo from a template via API (#16346) (#17195)
27+
* TRANSLATION
28+
* v1.15 fix Sprintf format 'verbs' in locale files (#17187)
29+
730
## [1.15.3](https://github.com/go-gitea/gitea/releases/tag/v1.15.3) - 2021-09-19
831

932
* ENHANCEMENTS

docs/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ params:
1818
description: Git with a cup of tea
1919
author: The Gitea Authors
2020
website: https://docs.gitea.io
21-
version: 1.15.3
21+
version: 1.15.4
2222
minGoVersion: 1.16
2323
goVersion: 1.17
2424
minNodeVersion: 12.17

integrations/rename_branch_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/models/db"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestRenameBranch(t *testing.T) {
17+
// get branch setting page
18+
session := loginUser(t, "user2")
19+
req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
20+
resp := session.MakeRequest(t, req, http.StatusOK)
21+
htmlDoc := NewHTMLParser(t, resp.Body)
22+
23+
postData := map[string]string{
24+
"_csrf": htmlDoc.GetCSRF(),
25+
"from": "master",
26+
"to": "main",
27+
}
28+
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", postData)
29+
session.MakeRequest(t, req, http.StatusFound)
30+
31+
// check new branch link
32+
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", postData)
33+
session.MakeRequest(t, req, http.StatusOK)
34+
35+
// check old branch link
36+
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", postData)
37+
resp = session.MakeRequest(t, req, http.StatusFound)
38+
location := resp.HeaderMap.Get("Location")
39+
assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location)
40+
41+
// check db
42+
repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
43+
assert.Equal(t, "main", repo1.DefaultBranch)
44+
}

models/branches.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type ProtectedBranch struct {
5353
func init() {
5454
db.RegisterModel(new(ProtectedBranch))
5555
db.RegisterModel(new(DeletedBranch))
56+
db.RegisterModel(new(RenamedBranch))
5657
}
5758

5859
// IsProtected returns if the branch is protected
@@ -588,3 +589,83 @@ func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
588589
log.Error("DeletedBranchesCleanup: %v", err)
589590
}
590591
}
592+
593+
// RenamedBranch provide renamed branch log
594+
// will check it when a branch can't be found
595+
type RenamedBranch struct {
596+
ID int64 `xorm:"pk autoincr"`
597+
RepoID int64 `xorm:"INDEX NOT NULL"`
598+
From string
599+
To string
600+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
601+
}
602+
603+
// FindRenamedBranch check if a branch was renamed
604+
func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist bool, err error) {
605+
branch = &RenamedBranch{
606+
RepoID: repoID,
607+
From: from,
608+
}
609+
exist, err = db.GetEngine(db.DefaultContext).Get(branch)
610+
611+
return
612+
}
613+
614+
// RenameBranch rename a branch
615+
func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault bool) error) (err error) {
616+
sess := db.NewSession(db.DefaultContext)
617+
defer sess.Close()
618+
if err := sess.Begin(); err != nil {
619+
return err
620+
}
621+
622+
// 1. update default branch if needed
623+
isDefault := repo.DefaultBranch == from
624+
if isDefault {
625+
repo.DefaultBranch = to
626+
_, err = sess.ID(repo.ID).Cols("default_branch").Update(repo)
627+
if err != nil {
628+
return err
629+
}
630+
}
631+
632+
// 2. Update protected branch if needed
633+
protectedBranch, err := getProtectedBranchBy(sess, repo.ID, from)
634+
if err != nil {
635+
return err
636+
}
637+
638+
if protectedBranch != nil {
639+
protectedBranch.BranchName = to
640+
_, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch)
641+
if err != nil {
642+
return err
643+
}
644+
}
645+
646+
// 3. Update all not merged pull request base branch name
647+
_, err = sess.Table(new(PullRequest)).Where("base_repo_id=? AND base_branch=? AND has_merged=?",
648+
repo.ID, from, false).
649+
Update(map[string]interface{}{"base_branch": to})
650+
if err != nil {
651+
return err
652+
}
653+
654+
// 4. do git action
655+
if err = gitAction(isDefault); err != nil {
656+
return err
657+
}
658+
659+
// 5. insert renamed branch record
660+
renamedBranch := &RenamedBranch{
661+
RepoID: repo.ID,
662+
From: from,
663+
To: to,
664+
}
665+
_, err = sess.Insert(renamedBranch)
666+
if err != nil {
667+
return err
668+
}
669+
670+
return sess.Commit()
671+
}

models/branches_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,52 @@ func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch {
7979

8080
return deletedBranch
8181
}
82+
83+
func TestFindRenamedBranch(t *testing.T) {
84+
assert.NoError(t, db.PrepareTestDatabase())
85+
branch, exist, err := FindRenamedBranch(1, "dev")
86+
assert.NoError(t, err)
87+
assert.Equal(t, true, exist)
88+
assert.Equal(t, "master", branch.To)
89+
90+
_, exist, err = FindRenamedBranch(1, "unknow")
91+
assert.NoError(t, err)
92+
assert.Equal(t, false, exist)
93+
}
94+
95+
func TestRenameBranch(t *testing.T) {
96+
assert.NoError(t, db.PrepareTestDatabase())
97+
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
98+
_isDefault := false
99+
100+
err := UpdateProtectBranch(repo1, &ProtectedBranch{
101+
RepoID: repo1.ID,
102+
BranchName: "master",
103+
}, WhitelistOptions{})
104+
assert.NoError(t, err)
105+
106+
assert.NoError(t, repo1.RenameBranch("master", "main", func(isDefault bool) error {
107+
_isDefault = isDefault
108+
return nil
109+
}))
110+
111+
assert.Equal(t, true, _isDefault)
112+
repo1 = db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
113+
assert.Equal(t, "main", repo1.DefaultBranch)
114+
115+
pull := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged
116+
assert.Equal(t, "master", pull.BaseBranch)
117+
118+
pull = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open
119+
assert.Equal(t, "main", pull.BaseBranch)
120+
121+
renamedBranch := db.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch)
122+
assert.Equal(t, "master", renamedBranch.From)
123+
assert.Equal(t, "main", renamedBranch.To)
124+
assert.Equal(t, int64(1), renamedBranch.RepoID)
125+
126+
db.AssertExistsAndLoadBean(t, &ProtectedBranch{
127+
RepoID: repo1.ID,
128+
BranchName: "main",
129+
})
130+
}

models/db/unit_tests.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
5454
opts.Dir = fixturesDir
5555
} else {
5656
for _, f := range fixtureFiles {
57-
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
57+
if len(f) != 0 {
58+
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
59+
}
5860
}
5961
}
6062

models/fixtures/renamed_branch.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-
2+
id: 1
3+
repo_id: 1
4+
from: dev
5+
to: master

models/issue.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"code.gitea.io/gitea/models/db"
17+
"code.gitea.io/gitea/models/issues"
1718
"code.gitea.io/gitea/modules/base"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/references"
@@ -803,8 +804,13 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
803804
return fmt.Errorf("UpdateIssueCols: %v", err)
804805
}
805806

806-
if err = issue.addCrossReferences(db.GetEngine(ctx), doer, true); err != nil {
807-
return err
807+
if err = issues.SaveIssueContentHistory(db.GetEngine(ctx), issue.PosterID, issue.ID, 0,
808+
timeutil.TimeStampNow(), issue.Content, false); err != nil {
809+
return fmt.Errorf("SaveIssueContentHistory: %v", err)
810+
}
811+
812+
if err = issue.addCrossReferences(ctx.Engine(), doer, true); err != nil {
813+
return fmt.Errorf("addCrossReferences: %v", err)
808814
}
809815

810816
return committer.Commit()
@@ -972,6 +978,12 @@ func newIssue(e db.Engine, doer *User, opts NewIssueOptions) (err error) {
972978
if err = opts.Issue.loadAttributes(e); err != nil {
973979
return err
974980
}
981+
982+
if err = issues.SaveIssueContentHistory(e, opts.Issue.PosterID, opts.Issue.ID, 0,
983+
timeutil.TimeStampNow(), opts.Issue.Content, true); err != nil {
984+
return err
985+
}
986+
975987
return opts.Issue.addCrossReferences(e, doer, false)
976988
}
977989

@@ -2132,6 +2144,12 @@ func UpdateReactionsMigrationsByType(gitServiceType structs.GitServiceType, orig
21322144
func deleteIssuesByRepoID(sess db.Engine, repoID int64) (attachmentPaths []string, err error) {
21332145
deleteCond := builder.Select("id").From("issue").Where(builder.Eq{"issue.repo_id": repoID})
21342146

2147+
// Delete content histories
2148+
if _, err = sess.In("issue_id", deleteCond).
2149+
Delete(&issues.ContentHistory{}); err != nil {
2150+
return
2151+
}
2152+
21352153
// Delete comments and attachments
21362154
if _, err = sess.In("issue_id", deleteCond).
21372155
Delete(&Comment{}); err != nil {

models/issue_comment.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"unicode/utf8"
1515

1616
"code.gitea.io/gitea/models/db"
17+
"code.gitea.io/gitea/models/issues"
1718
"code.gitea.io/gitea/modules/git"
1819
"code.gitea.io/gitea/modules/json"
1920
"code.gitea.io/gitea/modules/log"
@@ -1083,6 +1084,12 @@ func deleteComment(e db.Engine, comment *Comment) error {
10831084
return err
10841085
}
10851086

1087+
if _, err := e.Delete(&issues.ContentHistory{
1088+
CommentID: comment.ID,
1089+
}); err != nil {
1090+
return err
1091+
}
1092+
10861093
if comment.Type == CommentTypeComment {
10871094
if _, err := e.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
10881095
return err

0 commit comments

Comments
 (0)