2020-01-17 07:03:40 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-17 07:03:40 +01:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
package integration
|
2020-01-17 07:03:40 +01:00
|
|
|
|
|
|
|
import (
|
2020-08-04 22:55:22 +02:00
|
|
|
"net/http"
|
2020-01-17 07:03:40 +01:00
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-17 22:46:03 +01:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-05-20 16:08:52 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 11:37:59 +02:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-16 09:53:21 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-01-20 00:26:57 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-08-25 04:31:57 +02:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-01-17 07:03:40 +01:00
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2021-11-24 08:56:24 +01:00
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
2020-01-17 07:03:40 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-08-04 22:55:22 +02:00
|
|
|
func TestAPIPullUpdate(t *testing.T) {
|
2020-01-17 07:03:40 +01:00
|
|
|
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
2022-01-20 18:46:10 +01:00
|
|
|
// Create PR to test
|
2022-08-16 04:22:25 +02:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26})
|
2020-01-17 07:03:40 +01:00
|
|
|
pr := createOutdatedPR(t, user, org26)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// Test GetDiverging
|
2022-01-20 00:26:57 +01:00
|
|
|
diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
|
2020-01-17 07:03:40 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, diffCount.Behind)
|
|
|
|
assert.EqualValues(t, 1, diffCount.Ahead)
|
2022-11-19 09:12:33 +01:00
|
|
|
assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
|
|
|
|
assert.NoError(t, pr.LoadIssue(db.DefaultContext))
|
2020-01-17 07:03:40 +01:00
|
|
|
|
2020-08-04 22:55:22 +02:00
|
|
|
session := loginUser(t, "user2")
|
2023-01-17 22:46:03 +01:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2020-08-04 22:55:22 +02:00
|
|
|
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)
|
2020-01-17 07:03:40 +01:00
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// Test GetDiverging after update
|
2022-01-20 00:26:57 +01:00
|
|
|
diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
|
2020-01-17 07:03:40 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 0, diffCount.Behind)
|
|
|
|
assert.EqualValues(t, 2, diffCount.Ahead)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:03:45 +02:00
|
|
|
func TestAPIPullUpdateByRebase(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
2022-01-20 18:46:10 +01:00
|
|
|
// Create PR to test
|
2022-08-16 04:22:25 +02:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26})
|
2021-08-31 16:03:45 +02:00
|
|
|
pr := createOutdatedPR(t, user, org26)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// Test GetDiverging
|
2022-01-20 00:26:57 +01:00
|
|
|
diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
|
2021-08-31 16:03:45 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, diffCount.Behind)
|
|
|
|
assert.EqualValues(t, 1, diffCount.Ahead)
|
2022-11-19 09:12:33 +01:00
|
|
|
assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
|
|
|
|
assert.NoError(t, pr.LoadIssue(db.DefaultContext))
|
2021-08-31 16:03:45 +02:00
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
2023-01-17 22:46:03 +01:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2021-08-31 16:03:45 +02:00
|
|
|
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)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// Test GetDiverging after update
|
2022-01-20 00:26:57 +01:00
|
|
|
diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
|
2021-08-31 16:03:45 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 0, diffCount.Behind)
|
|
|
|
assert.EqualValues(t, 1, diffCount.Ahead)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *issues_model.PullRequest {
|
2023-02-28 23:17:51 +01:00
|
|
|
baseRepo, err := repo_service.CreateRepository(db.DefaultContext, actor, actor, repo_module.CreateRepoOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
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)
|
|
|
|
|
2022-03-29 21:13:41 +02:00
|
|
|
headRepo, err := repo_service.ForkRepository(git.DefaultContext, actor, forkOrg, repo_service.ForkRepoOptions{
|
2021-08-28 10:37:14 +02:00
|
|
|
BaseRepo: baseRepo,
|
|
|
|
Name: "repo-pr-update",
|
|
|
|
Description: "desc",
|
|
|
|
})
|
2020-01-17 07:03:40 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, headRepo)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// create a commit on base Repo
|
2022-01-20 00:26:57 +01:00
|
|
|
_, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, baseRepo, actor, &files_service.UpdateRepoFileOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
TreePath: "File_A",
|
|
|
|
Message: "Add File A",
|
|
|
|
Content: "File A",
|
|
|
|
IsNewFile: true,
|
|
|
|
OldBranch: "master",
|
|
|
|
NewBranch: "master",
|
2021-11-24 08:56:24 +01:00
|
|
|
Author: &files_service.IdentityOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
Name: actor.Name,
|
|
|
|
Email: actor.Email,
|
|
|
|
},
|
2021-11-24 08:56:24 +01:00
|
|
|
Committer: &files_service.IdentityOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
Name: actor.Name,
|
|
|
|
Email: actor.Email,
|
|
|
|
},
|
2021-11-24 08:56:24 +01:00
|
|
|
Dates: &files_service.CommitDateOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
Author: time.Now(),
|
|
|
|
Committer: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// create a commit on head Repo
|
2022-01-20 00:26:57 +01:00
|
|
|
_, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, headRepo, actor, &files_service.UpdateRepoFileOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
TreePath: "File_B",
|
|
|
|
Message: "Add File on PR branch",
|
|
|
|
Content: "File B",
|
|
|
|
IsNewFile: true,
|
|
|
|
OldBranch: "master",
|
|
|
|
NewBranch: "newBranch",
|
2021-11-24 08:56:24 +01:00
|
|
|
Author: &files_service.IdentityOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
Name: actor.Name,
|
|
|
|
Email: actor.Email,
|
|
|
|
},
|
2021-11-24 08:56:24 +01:00
|
|
|
Committer: &files_service.IdentityOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
Name: actor.Name,
|
|
|
|
Email: actor.Email,
|
|
|
|
},
|
2021-11-24 08:56:24 +01:00
|
|
|
Dates: &files_service.CommitDateOptions{
|
2020-01-17 07:03:40 +01:00
|
|
|
Author: time.Now(),
|
|
|
|
Committer: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// create Pull
|
2022-06-13 11:37:59 +02:00
|
|
|
pullIssue := &issues_model.Issue{
|
2020-01-17 07:03:40 +01:00
|
|
|
RepoID: baseRepo.ID,
|
|
|
|
Title: "Test Pull -to-update-",
|
|
|
|
PosterID: actor.ID,
|
|
|
|
Poster: actor,
|
|
|
|
IsPull: true,
|
|
|
|
}
|
2022-06-13 11:37:59 +02:00
|
|
|
pullRequest := &issues_model.PullRequest{
|
2020-01-17 07:03:40 +01:00
|
|
|
HeadRepoID: headRepo.ID,
|
|
|
|
BaseRepoID: baseRepo.ID,
|
|
|
|
HeadBranch: "newBranch",
|
|
|
|
BaseBranch: "master",
|
|
|
|
HeadRepo: headRepo,
|
|
|
|
BaseRepo: baseRepo,
|
2022-06-13 11:37:59 +02:00
|
|
|
Type: issues_model.PullRequestGitea,
|
2020-01-17 07:03:40 +01:00
|
|
|
}
|
2022-01-20 00:26:57 +01:00
|
|
|
err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
|
2020-01-17 07:03:40 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{Title: "Test Pull -to-update-"})
|
2022-06-13 11:37:59 +02:00
|
|
|
pr, err := issues_model.GetPullRequestByIssueID(db.DefaultContext, issue.ID)
|
2020-01-17 07:03:40 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return pr
|
|
|
|
}
|