Skip to content

Commit

Permalink
Add "Update Branch" button to Pull Requests (#9784)
Browse files Browse the repository at this point in the history
* add Divergence

* add Update Button

* first working version

* re-use code

* split raw merge commands and db-change functions (notify, cache, ...)

* use rawMerge (remove redundant code)

* own function to get Diverging of PRs

* use FlashError

* correct Error Msg

* hook is triggerd ... so remove comment

* add "branch2" to "user2/repo1" because it unit-test "TestPullView_ReviewerMissed" use it but dont exist jet :/

* move GetPerm to IsUserAllowedToUpdate

* add Flash Success MSG

* imprufe code
- remove useless js chage

* fix-lint

* TEST: add PullRequest ID:5
Repo: user2/repo1
Base: branch1
Head: pr-to-update

* correct comments

* make PR5 outdated

* fix Tests

* WIP: add pull update test

* update revs

* update locales

* working TEST

* update UI

* misspell

* change style

* add 1s delay so rev exist

* move row up (before merge row)

* fix lint nit

* UI remove divider

* Update style

* nits

* do it right

* introduce IsSameRepo

* remove useless check

Co-authored-by: Lauris BH <[email protected]>
  • Loading branch information
6543 and lafriks committed Jan 17, 2020
1 parent 9f40bb0 commit 36943e5
Show file tree
Hide file tree
Showing 32 changed files with 489 additions and 68 deletions.
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)
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
65f1bf27bc3bf70f64657658635e66094edbcb4d refs/heads/master
985f0301dba5e7b34be866819cd15ad3d8f508ee refs/heads/branch2
62fb502a7172d4453f0322a2cc85bddffa57f07a refs/heads/pr-to-update
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
136 changes: 136 additions & 0 deletions integrations/pull_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/repofiles"
repo_module "code.gitea.io/gitea/modules/repository"
pull_service "code.gitea.io/gitea/services/pull"
repo_service "code.gitea.io/gitea/services/repository"

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

func TestPullUpdate(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
//Create PR to test
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
org26 := models.AssertExistsAndLoadBean(t, &models.User{ID: 26}).(*models.User)
pr := createOutdatedPR(t, user, org26)

//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)

})
}

func createOutdatedPR(t *testing.T, actor, forkOrg *models.User) *models.PullRequest {
baseRepo, err := repo_service.CreateRepository(actor, actor, models.CreateRepoOptions{
Name: "repo-pr-update",
Description: "repo-tmp-pr-update description",
AutoInit: true,
Gitignores: "C,C++",
License: "MIT",
Readme: "Default",
IsPrivate: false,
})
assert.NoError(t, err)
assert.NotEmpty(t, baseRepo)

headRepo, err := repo_module.ForkRepository(actor, forkOrg, baseRepo, "repo-pr-update", "desc")
assert.NoError(t, err)
assert.NotEmpty(t, headRepo)

//create a commit on base Repo
_, err = repofiles.CreateOrUpdateRepoFile(baseRepo, actor, &repofiles.UpdateRepoFileOptions{
TreePath: "File_A",
Message: "Add File A",
Content: "File A",
IsNewFile: true,
OldBranch: "master",
NewBranch: "master",
Author: &repofiles.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Committer: &repofiles.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Dates: &repofiles.CommitDateOptions{
Author: time.Now(),
Committer: time.Now(),
},
})
assert.NoError(t, err)

//create a commit on head Repo
_, err = repofiles.CreateOrUpdateRepoFile(headRepo, actor, &repofiles.UpdateRepoFileOptions{
TreePath: "File_B",
Message: "Add File on PR branch",
Content: "File B",
IsNewFile: true,
OldBranch: "master",
NewBranch: "newBranch",
Author: &repofiles.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Committer: &repofiles.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Dates: &repofiles.CommitDateOptions{
Author: time.Now(),
Committer: time.Now(),
},
})
assert.NoError(t, err)

//create Pull
pullIssue := &models.Issue{
RepoID: baseRepo.ID,
Title: "Test Pull -to-update-",
PosterID: actor.ID,
Poster: actor,
IsPull: true,
}
pullRequest := &models.PullRequest{
HeadRepoID: headRepo.ID,
BaseRepoID: baseRepo.ID,
HeadBranch: "newBranch",
BaseBranch: "master",
HeadRepo: headRepo,
BaseRepo: baseRepo,
Type: models.PullRequestGitea,
}
err = pull_service.NewPullRequest(baseRepo, pullIssue, nil, nil, pullRequest, nil)
assert.NoError(t, err)

issue := models.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue)
pr, err := models.GetPullRequestByIssueID(issue.ID)
assert.NoError(t, err)

return pr
}
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
5 changes: 5 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,8 @@ func (pr *PullRequest) IsHeadEqualWithBranch(branchName string) (bool, error) {
}
return baseCommit.HasPreviousCommit(headCommit.ID)
}

// IsSameRepo returns true if base repo and head repo is the same
func (pr *PullRequest) IsSameRepo() bool {
return pr.BaseRepoID == pr.HeadRepoID
}
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
4 changes: 4 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@ 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 = Branch update was successful
pulls.update_not_allowed = You are not allowed to update branch
pulls.outdated_with_base_branch = This branch is out-of-date with the base branch
milestones.new = New Milestone
milestones.open_tab = %d Open
Expand Down
Loading

0 comments on commit 36943e5

Please sign in to comment.