Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Update Branch" button to Pull Requests #9784

Merged
merged 39 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
38a27df
add Divergence
6543 Jan 15, 2020
2c7fc31
add Update Button
6543 Jan 15, 2020
26d686f
first working version
6543 Jan 16, 2020
d8522ac
re-use code
6543 Jan 16, 2020
efe6982
split raw merge commands and db-change functions (notify, cache, ...)
6543 Jan 16, 2020
328ecc3
use rawMerge (remove redundant code)
6543 Jan 16, 2020
9249d47
own function to get Diverging of PRs
6543 Jan 16, 2020
454ee44
use FlashError
6543 Jan 16, 2020
5d5d919
correct Error Msg
6543 Jan 16, 2020
3c6157b
hook is triggerd ... so remove comment
6543 Jan 16, 2020
88fa4c5
add "branch2" to "user2/repo1" because it unit-test "TestPullView_Rev…
6543 Jan 16, 2020
7776738
move GetPerm to IsUserAllowedToUpdate
6543 Jan 16, 2020
e3adbd2
add Flash Success MSG
6543 Jan 16, 2020
05c0c1b
imprufe code
6543 Jan 16, 2020
f7d924a
fix-lint
6543 Jan 16, 2020
e2f96d9
TEST: add PullRequest ID:5
6543 Jan 16, 2020
8bad5d2
correct comments
6543 Jan 16, 2020
7442991
Merge branch 'master' into update-pr
6543 Jan 16, 2020
143d8e8
make PR5 outdated
6543 Jan 16, 2020
fb33b4b
fix Tests
6543 Jan 16, 2020
069a393
WIP: add pull update test
6543 Jan 16, 2020
720fbf1
update revs
6543 Jan 16, 2020
892ddd9
update locales
6543 Jan 16, 2020
7aa9e84
working TEST
6543 Jan 16, 2020
7502ff5
Merge branch 'master' into update-pr
6543 Jan 16, 2020
36a34cd
update UI
6543 Jan 16, 2020
18fadef
misspell
6543 Jan 16, 2020
97e51ec
change style
6543 Jan 16, 2020
18eb495
add 1s delay so rev exist
6543 Jan 16, 2020
2b58961
move row up (before merge row)
6543 Jan 16, 2020
688a53a
fix lint nit
6543 Jan 16, 2020
97a6f83
UI remove divider
6543 Jan 16, 2020
32c5eee
Merge branch 'master' into update-pr
6543 Jan 16, 2020
6400bcc
Update style
lafriks Jan 17, 2020
fa4779b
nits
6543 Jan 17, 2020
fbbc7f4
do it right
6543 Jan 17, 2020
717406e
introduce IsSameRepo
6543 Jan 17, 2020
f26c8b3
remove useless check
6543 Jan 17, 2020
246748f
Merge branch 'master' into update-pr
6543 Jan 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions integrations/api_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ func TestAPISearchIssue(t *testing.T) {
var apiIssues []*api.Issue
DecodeJSON(t, resp, &apiIssues)

assert.Len(t, apiIssues, 8)
assert.Len(t, apiIssues, 9)

query := url.Values{}
query.Add("token", token)
link.RawQuery = query.Encode()
req = NewRequest(t, "GET", link.String())
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiIssues)
assert.Len(t, apiIssues, 8)
assert.Len(t, apiIssues, 9)

query.Add("state", "closed")
link.RawQuery = query.Encode()
Expand All @@ -163,5 +163,5 @@ func TestAPISearchIssue(t *testing.T) {
req = NewRequest(t, "GET", link.String())
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiIssues)
assert.Len(t, apiIssues, 0)
assert.Len(t, apiIssues, 1)
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
985f0301dba5e7b34be866819cd15ad3d8f508ee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
62fb502a7172d4453f0322a2cc85bddffa57f07a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4a357436d925b5c974181ff12a994538ddc5a269
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
62fb502a7172d4453f0322a2cc85bddffa57f07a
41 changes: 41 additions & 0 deletions integrations/pull_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 integrations

import (
"fmt"
"net/url"
"testing"

"code.gitea.io/gitea/models"
pull_service "code.gitea.io/gitea/services/pull"

"github.com/stretchr/testify/assert"
)

func TestPullUpdate(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {

pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 5}).(*models.PullRequest)
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)

//Test GetDiverging
diffCount, err := pull_service.GetDiverging(pr)
assert.NoError(t, err)
assert.EqualValues(t, 1, diffCount.Behind)
assert.EqualValues(t, 1, diffCount.Ahead)

message := fmt.Sprintf("Merge branch '%s' into %s", pr.BaseBranch, pr.HeadBranch)
err = pull_service.Update(pr,user, message)
assert.NoError(t, err)

//Test GetDiverging after update
diffCount, err = pull_service.GetDiverging(pr)
assert.NoError(t, err)
assert.EqualValues(t, 0, diffCount.Behind)
assert.EqualValues(t, 2, diffCount.Ahead)

})
}
4 changes: 2 additions & 2 deletions integrations/repo_activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func TestRepoActivity(t *testing.T) {
list = htmlDoc.doc.Find("#merged-pull-requests").Next().Find("p.desc")
assert.Len(t, list.Nodes, 1)

// Should be 2 merged proposed pull requests
// Should be 3 merged proposed pull requests
list = htmlDoc.doc.Find("#proposed-pull-requests").Next().Find("p.desc")
assert.Len(t, list.Nodes, 2)
assert.Len(t, list.Nodes, 3)

// Should be 3 new issues
list = htmlDoc.doc.Find("#new-issues").Next().Find("p.desc")
Expand Down
12 changes: 12 additions & 0 deletions models/fixtures/issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,15 @@
created_unix: 946684830
updated_unix: 999307200
deadline_unix: 1019307200

-
id: 11
repo_id: 1
index: 5
poster_id: 1
name: pull5
content: content for the a pull request
is_closed: false
is_pull: true
created_unix: 1579194806
updated_unix: 1579194806
15 changes: 14 additions & 1 deletion models/fixtures/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@
head_branch: branch1
base_branch: master
merge_base: abcdef1234567890
has_merged: false
has_merged: false

-
id: 5 # this PR is outdated (one commit behind branch1 )
type: 0 # gitea pull request
status: 2 # mergable
issue_id: 11
index: 5
head_repo_id: 1
base_repo_id: 1
head_branch: pr-to-update
base_branch: branch1
merge_base: 1234567890abcdef
has_merged: false
2 changes: 1 addition & 1 deletion models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
is_private: false
num_issues: 2
num_closed_issues: 1
num_pulls: 2
num_pulls: 3
num_closed_pulls: 0
num_milestones: 3
num_closed_milestones: 1
Expand Down
8 changes: 4 additions & 4 deletions models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {

total, ids, err = SearchIssueIDsByKeyword("for", []int64{1}, 10, 0)
assert.NoError(t, err)
assert.EqualValues(t, 4, total)
assert.EqualValues(t, []int64{1, 2, 3, 5}, ids)
assert.EqualValues(t, 5, total)
assert.EqualValues(t, []int64{1, 2, 3, 5, 11}, ids)

// issue1's comment id 2
total, ids, err = SearchIssueIDsByKeyword("good", []int64{1}, 10, 0)
Expand Down Expand Up @@ -305,8 +305,8 @@ func testInsertIssue(t *testing.T, title, content string) {
assert.True(t, has)
assert.EqualValues(t, issue.Title, newIssue.Title)
assert.EqualValues(t, issue.Content, newIssue.Content)
// there are 4 issues and max index is 4 on repository 1, so this one should 5
assert.EqualValues(t, 5, newIssue.Index)
// there are 5 issues and max index is 5 on repository 1, so this one should 6
assert.EqualValues(t, 6, newIssue.Index)

_, err = x.ID(issue.ID).Delete(new(Issue))
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion models/issue_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Test_newIssueUsers(t *testing.T) {
newIssue := &Issue{
RepoID: repo.ID,
PosterID: 4,
Index: 5,
Index: 6,
Title: "newTestIssueTitle",
Content: "newTestIssueContent",
}
Expand Down
18 changes: 10 additions & 8 deletions models/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func TestPullRequestsNewest(t *testing.T) {
Labels: []string{},
})
assert.NoError(t, err)
assert.Equal(t, int64(2), count)
if assert.Len(t, prs, 2) {
assert.Equal(t, int64(2), prs[0].ID)
assert.Equal(t, int64(1), prs[1].ID)
assert.EqualValues(t, 3, count)
if assert.Len(t, prs, 3) {
assert.EqualValues(t, 5, prs[0].ID)
assert.EqualValues(t, 2, prs[1].ID)
assert.EqualValues(t, 1, prs[2].ID)
}
}

Expand All @@ -77,10 +78,11 @@ func TestPullRequestsOldest(t *testing.T) {
Labels: []string{},
})
assert.NoError(t, err)
assert.Equal(t, int64(2), count)
if assert.Len(t, prs, 2) {
assert.Equal(t, int64(1), prs[0].ID)
assert.Equal(t, int64(2), prs[1].ID)
assert.EqualValues(t, 3, count)
if assert.Len(t, prs, 3) {
assert.EqualValues(t, 1, prs[0].ID)
assert.EqualValues(t, 2, prs[1].ID)
assert.EqualValues(t, 5, prs[2].ID)
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/indexer/issues/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestBleveSearchIssues(t *testing.T) {

ids, err = SearchIssuesByKeyword([]int64{1}, "for")
assert.NoError(t, err)
assert.EqualValues(t, []int64{1, 2, 3, 5}, ids)
assert.EqualValues(t, []int64{1, 2, 3, 5, 11}, ids)

ids, err = SearchIssuesByKeyword([]int64{1}, "good")
assert.NoError(t, err)
Expand All @@ -89,7 +89,7 @@ func TestDBSearchIssues(t *testing.T) {

ids, err = SearchIssuesByKeyword([]int64{1}, "for")
assert.NoError(t, err)
assert.EqualValues(t, []int64{1, 2, 3, 5}, ids)
assert.EqualValues(t, []int64{1, 2, 3, 5, 11}, ids)

ids, err = SearchIssuesByKeyword([]int64{1}, "good")
assert.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,8 @@ pulls.open_unmerged_pull_exists = `You cannot perform a reopen operation because
pulls.status_checking = Some checks are pending
pulls.status_checks_success = All checks were successful
pulls.status_checks_error = Some checks failed
pulls.update_branch = Update Branch
pulls.update_branch_success = Update Branch was successful

milestones.new = New Milestone
milestones.open_tab = %d Open
Expand Down
74 changes: 73 additions & 1 deletion routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,15 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare

setMergeTarget(ctx, pull)

divergence, divergenceError := pull_service.GetDiverging(pull)
if divergenceError != nil {
ctx.ServerError("GetDiverging", divergenceError)
return nil
}
ctx.Data["Divergence"] = divergence

if err := pull.LoadProtectedBranch(); err != nil {
ctx.ServerError("GetLatestCommitStatus", err)
ctx.ServerError("LoadProtectedBranch", err)
return nil
}
ctx.Data["EnableStatusCheck"] = pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck
Expand Down Expand Up @@ -587,6 +594,71 @@ func ViewPullFiles(ctx *context.Context) {
ctx.HTML(200, tplPullFiles)
}

// UpdatePullRequest merge master into PR
func UpdatePullRequest(ctx *context.Context) {

6543 marked this conversation as resolved.
Show resolved Hide resolved
issue := checkPullInfo(ctx)
if ctx.Written() {
return
}
if issue.IsClosed {
ctx.NotFound("MergePullRequest", nil)
return
}

6543 marked this conversation as resolved.
Show resolved Hide resolved
if issue.PullRequest.HasMerged {
ctx.NotFound("MergePullRequest", nil)
return
}

if err := issue.PullRequest.LoadBaseRepo(); err != nil {
ctx.InternalServerError(err)
return
}
if err := issue.PullRequest.LoadHeadRepo(); err != nil {
ctx.InternalServerError(err)
return
}

allowedUpdate, err := pull_service.IsUserAllowedToUpdate(issue.PullRequest, ctx.User)
if err != nil {
ctx.ServerError("IsUserAllowedToMerge", err)
return
}

// ToDo: add check if maintainers are allowed to change branch ... (need migration & co)
if !allowedUpdate {
ctx.Flash.Error(ctx.Tr("repo.pulls.update_not_allowed"))
6543 marked this conversation as resolved.
Show resolved Hide resolved
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
return
}

// default merge commit message
message := fmt.Sprintf("Merge branch '%s' into %s", issue.PullRequest.BaseBranch, issue.PullRequest.HeadBranch)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be configurable via .ini or via Crowdin.

Copy link
Member

@lunny lunny Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for this one, sometimes, we use English commit messages even if I'm using a Chinese User Interface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use app.ini like we did with closing keywords and WIP...


if err = pull_service.Update(issue.PullRequest, ctx.User, message); err != nil {
sanitize := func(x string) string {
runes := []rune(x)

if len(runes) > 512 {
x = "..." + string(runes[len(runes)-512:])
}

return strings.Replace(html.EscapeString(x), "\n", "<br>", -1)
}
if models.IsErrMergeConflicts(err) {
conflictError := err.(models.ErrMergeConflicts)
ctx.Flash.Error(ctx.Tr("repo.pulls.merge_conflict", sanitize(conflictError.StdErr), sanitize(conflictError.StdOut)))
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
return
}
ctx.Flash.Error(err.Error())
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
}
ctx.Flash.Success(ctx.Tr("repo.pulls.update_branch_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
}

// MergePullRequest response for merging pull request
func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
issue := checkPullInfo(ctx)
Expand Down
1 change: 1 addition & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get(".patch", repo.DownloadPullPatch)
m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
m.Post("/merge", context.RepoMustNotBeArchived(), reqRepoPullsWriter, bindIgnErr(auth.MergePullRequestForm{}), repo.MergePullRequest)
m.Post("/update", ignSignInAndCsrf, repo.UpdatePullRequest)
6543 marked this conversation as resolved.
Show resolved Hide resolved
m.Post("/cleanup", context.RepoMustNotBeArchived(), context.RepoRef(), repo.CleanUpPullRequest)
m.Group("/files", func() {
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
Expand Down
Loading