-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a02734
commit ed7b4ed
Showing
6 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
auth_model "code.gitea.io/gitea/models/auth" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/util" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRepoMergeUpstream(t *testing.T) { | ||
onGiteaRun(t, func(*testing.T, *url.URL) { | ||
forkUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) | ||
|
||
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) | ||
baseUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: baseRepo.OwnerID}) | ||
|
||
checkFileContent := func(exp string) { | ||
req := NewRequest(t, "GET", fmt.Sprintf("/%s/test-repo-fork/raw/branch/master/new-file.txt", forkUser.Name)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
require.Equal(t, exp, resp.Body.String()) | ||
} | ||
|
||
session := loginUser(t, forkUser.Name) | ||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||
|
||
// create a fork | ||
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/forks", baseUser.Name, baseRepo.Name), &api.CreateForkOption{ | ||
Name: util.ToPointer("test-repo-fork"), | ||
}).AddTokenAuth(token) | ||
MakeRequest(t, req, http.StatusAccepted) | ||
forkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: forkUser.ID, Name: "test-repo-fork"}) | ||
|
||
// add a file in base repo | ||
require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-1")) | ||
|
||
// the repo shows a prompt to "sync fork" | ||
var mergeUpstreamLink string | ||
require.Eventually(t, func() bool { | ||
resp := session.MakeRequest(t, NewRequestf(t, "GET", "/%s/test-repo-fork", forkUser.Name), http.StatusOK) | ||
htmlDoc := NewHTMLParser(t, resp.Body) | ||
respMsg, _ := htmlDoc.Find(".ui.message").Html() | ||
if !strings.Contains(respMsg, `This branch is 1 commit behind <a href="/user2/repo1/src/branch/master">user2/repo1:master</a>`) { | ||
return false | ||
} | ||
mergeUpstreamLink = htmlDoc.Find("button[data-url*='merge-upstream']").AttrOr("data-url", "") | ||
require.NotEmpty(t, mergeUpstreamLink) | ||
return true | ||
}, 5*time.Second, 100*time.Millisecond) | ||
|
||
// click the "sync fork" button | ||
req = NewRequestWithValues(t, "POST", mergeUpstreamLink, map[string]string{"_csrf": GetUserCSRFToken(t, session)}) | ||
session.MakeRequest(t, req, http.StatusOK) | ||
checkFileContent("test-content-1") | ||
|
||
// update the files | ||
require.NoError(t, createOrReplaceFileInBranch(forkUser, forkRepo, "new-file-other.txt", "master", "test-content-other")) | ||
require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-2")) | ||
require.Eventually(t, func() bool { | ||
resp := session.MakeRequest(t, NewRequestf(t, "GET", "/%s/test-repo-fork", forkUser.Name), http.StatusOK) | ||
htmlDoc := NewHTMLParser(t, resp.Body) | ||
respMsg, _ := htmlDoc.Find(".ui.message:not(.positive)").Html() | ||
return strings.Contains(respMsg, `The base branch <a href="/user2/repo1/src/branch/master">user2/repo1:master</a> has new changes`) | ||
}, 5*time.Second, 100*time.Millisecond) | ||
|
||
// and do the merge-upstream by API | ||
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/test-repo-fork/merge-upstream", forkUser.Name), &api.MergeUpstreamRequest{ | ||
Branch: "master", | ||
}).AddTokenAuth(token) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
checkFileContent("test-content-2") | ||
|
||
var mergeResp api.MergeUpstreamResponse | ||
DecodeJSON(t, resp, &mergeResp) | ||
assert.Equal(t, "merge", mergeResp.MergeStyle) | ||
}) | ||
} |