You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
5.4 KiB
Go
175 lines
5.4 KiB
Go
5 years ago
|
// 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.
|
||
|
|
||
2 years ago
|
package integration
|
||
5 years ago
|
|
||
|
import (
|
||
4 years ago
|
"net/http"
|
||
5 years ago
|
"net/url"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
2 years ago
|
"code.gitea.io/gitea/models/db"
|
||
2 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/git"
|
||
2 years ago
|
repo_module "code.gitea.io/gitea/modules/repository"
|
||
5 years ago
|
pull_service "code.gitea.io/gitea/services/pull"
|
||
|
repo_service "code.gitea.io/gitea/services/repository"
|
||
3 years ago
|
files_service "code.gitea.io/gitea/services/repository/files"
|
||
5 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
4 years ago
|
func TestAPIPullUpdate(t *testing.T) {
|
||
5 years ago
|
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
||
3 years ago
|
// Create PR to test
|
||
2 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||
|
org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26})
|
||
5 years ago
|
pr := createOutdatedPR(t, user, org26)
|
||
|
|
||
3 years ago
|
// Test GetDiverging
|
||
3 years ago
|
diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 1, diffCount.Behind)
|
||
|
assert.EqualValues(t, 1, diffCount.Ahead)
|
||
4 years ago
|
assert.NoError(t, pr.LoadBaseRepo())
|
||
|
assert.NoError(t, pr.LoadIssue())
|
||
5 years ago
|
|
||
4 years ago
|
session := loginUser(t, "user2")
|
||
|
token := getTokenForLoggedInUser(t, session)
|
||
|
req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
|
||
|
session.MakeRequest(t, req, http.StatusOK)
|
||
5 years ago
|
|
||
3 years ago
|
// Test GetDiverging after update
|
||
3 years ago
|
diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 0, diffCount.Behind)
|
||
|
assert.EqualValues(t, 2, diffCount.Ahead)
|
||
|
})
|
||
|
}
|
||
|
|
||
3 years ago
|
func TestAPIPullUpdateByRebase(t *testing.T) {
|
||
|
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
||
3 years ago
|
// Create PR to test
|
||
2 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||
|
org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26})
|
||
3 years ago
|
pr := createOutdatedPR(t, user, org26)
|
||
|
|
||
3 years ago
|
// Test GetDiverging
|
||
3 years ago
|
diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 1, diffCount.Behind)
|
||
|
assert.EqualValues(t, 1, diffCount.Ahead)
|
||
|
assert.NoError(t, pr.LoadBaseRepo())
|
||
|
assert.NoError(t, pr.LoadIssue())
|
||
|
|
||
|
session := loginUser(t, "user2")
|
||
|
token := getTokenForLoggedInUser(t, session)
|
||
|
req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?style=rebase&token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
|
||
|
session.MakeRequest(t, req, http.StatusOK)
|
||
|
|
||
3 years ago
|
// Test GetDiverging after update
|
||
3 years ago
|
diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 0, diffCount.Behind)
|
||
|
assert.EqualValues(t, 1, diffCount.Ahead)
|
||
|
})
|
||
|
}
|
||
|
|
||
2 years ago
|
func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *issues_model.PullRequest {
|
||
2 years ago
|
baseRepo, err := repo_service.CreateRepository(actor, actor, repo_module.CreateRepoOptions{
|
||
5 years ago
|
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)
|
||
|
|
||
2 years ago
|
headRepo, err := repo_service.ForkRepository(git.DefaultContext, actor, forkOrg, repo_service.ForkRepoOptions{
|
||
3 years ago
|
BaseRepo: baseRepo,
|
||
|
Name: "repo-pr-update",
|
||
|
Description: "desc",
|
||
|
})
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.NotEmpty(t, headRepo)
|
||
|
|
||
3 years ago
|
// create a commit on base Repo
|
||
3 years ago
|
_, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, baseRepo, actor, &files_service.UpdateRepoFileOptions{
|
||
5 years ago
|
TreePath: "File_A",
|
||
|
Message: "Add File A",
|
||
|
Content: "File A",
|
||
|
IsNewFile: true,
|
||
|
OldBranch: "master",
|
||
|
NewBranch: "master",
|
||
3 years ago
|
Author: &files_service.IdentityOptions{
|
||
5 years ago
|
Name: actor.Name,
|
||
|
Email: actor.Email,
|
||
|
},
|
||
3 years ago
|
Committer: &files_service.IdentityOptions{
|
||
5 years ago
|
Name: actor.Name,
|
||
|
Email: actor.Email,
|
||
|
},
|
||
3 years ago
|
Dates: &files_service.CommitDateOptions{
|
||
5 years ago
|
Author: time.Now(),
|
||
|
Committer: time.Now(),
|
||
|
},
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
|
|
||
3 years ago
|
// create a commit on head Repo
|
||
3 years ago
|
_, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, headRepo, actor, &files_service.UpdateRepoFileOptions{
|
||
5 years ago
|
TreePath: "File_B",
|
||
|
Message: "Add File on PR branch",
|
||
|
Content: "File B",
|
||
|
IsNewFile: true,
|
||
|
OldBranch: "master",
|
||
|
NewBranch: "newBranch",
|
||
3 years ago
|
Author: &files_service.IdentityOptions{
|
||
5 years ago
|
Name: actor.Name,
|
||
|
Email: actor.Email,
|
||
|
},
|
||
3 years ago
|
Committer: &files_service.IdentityOptions{
|
||
5 years ago
|
Name: actor.Name,
|
||
|
Email: actor.Email,
|
||
|
},
|
||
3 years ago
|
Dates: &files_service.CommitDateOptions{
|
||
5 years ago
|
Author: time.Now(),
|
||
|
Committer: time.Now(),
|
||
|
},
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
|
|
||
3 years ago
|
// create Pull
|
||
2 years ago
|
pullIssue := &issues_model.Issue{
|
||
5 years ago
|
RepoID: baseRepo.ID,
|
||
|
Title: "Test Pull -to-update-",
|
||
|
PosterID: actor.ID,
|
||
|
Poster: actor,
|
||
|
IsPull: true,
|
||
|
}
|
||
2 years ago
|
pullRequest := &issues_model.PullRequest{
|
||
5 years ago
|
HeadRepoID: headRepo.ID,
|
||
|
BaseRepoID: baseRepo.ID,
|
||
|
HeadBranch: "newBranch",
|
||
|
BaseBranch: "master",
|
||
|
HeadRepo: headRepo,
|
||
|
BaseRepo: baseRepo,
|
||
2 years ago
|
Type: issues_model.PullRequestGitea,
|
||
5 years ago
|
}
|
||
3 years ago
|
err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{Title: "Test Pull -to-update-"})
|
||
2 years ago
|
pr, err := issues_model.GetPullRequestByIssueID(db.DefaultContext, issue.ID)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
|
||
|
return pr
|
||
|
}
|