mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-27 00:23:41 +09:00 
			
		
		
		
	Honor delete branch on merge repo setting when using merge API (#35488)
Fix #35463. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		| @@ -6,26 +6,36 @@ package integration | ||||
| import ( | ||||
| 	"context" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| 	repo_model "code.gitea.io/gitea/models/repo" | ||||
| 	user_model "code.gitea.io/gitea/models/user" | ||||
| 	api "code.gitea.io/gitea/modules/structs" | ||||
| 	"code.gitea.io/gitea/modules/util" | ||||
| 	files_service "code.gitea.io/gitea/services/repository/files" | ||||
|  | ||||
| 	"github.com/stretchr/testify/require" | ||||
| ) | ||||
|  | ||||
| func createFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) (*api.FilesResponse, error) { | ||||
| type createFileInBranchOptions struct { | ||||
| 	OldBranch, NewBranch string | ||||
| } | ||||
|  | ||||
| func testCreateFileInBranch(t *testing.T, user *user_model.User, repo *repo_model.Repository, createOpts createFileInBranchOptions, files map[string]string) *api.FilesResponse { | ||||
| 	resp, err := createFileInBranch(user, repo, createOpts, files) | ||||
| 	require.NoError(t, err) | ||||
| 	return resp | ||||
| } | ||||
|  | ||||
| func createFileInBranch(user *user_model.User, repo *repo_model.Repository, createOpts createFileInBranchOptions, files map[string]string) (*api.FilesResponse, error) { | ||||
| 	ctx := context.TODO() | ||||
| 	opts := &files_service.ChangeRepoFilesOptions{ | ||||
| 		Files: []*files_service.ChangeRepoFile{ | ||||
| 			{ | ||||
| 				Operation:     "create", | ||||
| 				TreePath:      treePath, | ||||
| 				ContentReader: strings.NewReader(content), | ||||
| 			}, | ||||
| 		}, | ||||
| 		OldBranch: branchName, | ||||
| 		Author:    nil, | ||||
| 		Committer: nil, | ||||
| 	opts := &files_service.ChangeRepoFilesOptions{OldBranch: createOpts.OldBranch, NewBranch: createOpts.NewBranch} | ||||
| 	for path, content := range files { | ||||
| 		opts.Files = append(opts.Files, &files_service.ChangeRepoFile{ | ||||
| 			Operation:     "create", | ||||
| 			TreePath:      path, | ||||
| 			ContentReader: strings.NewReader(content), | ||||
| 		}) | ||||
| 	} | ||||
| 	return files_service.ChangeRepoFiles(ctx, repo, user, opts) | ||||
| } | ||||
| @@ -53,10 +63,12 @@ func createOrReplaceFileInBranch(user *user_model.User, repo *repo_model.Reposit | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	_, err = createFileInBranch(user, repo, treePath, branchName, content) | ||||
| 	_, err = createFileInBranch(user, repo, createFileInBranchOptions{OldBranch: branchName}, map[string]string{treePath: content}) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func createFile(user *user_model.User, repo *repo_model.Repository, treePath string) (*api.FilesResponse, error) { | ||||
| 	return createFileInBranch(user, repo, treePath, repo.DefaultBranch, "This is a NEW file") | ||||
| // TODO: replace all usages of this function with testCreateFileInBranch or testCreateFile | ||||
| func createFile(user *user_model.User, repo *repo_model.Repository, treePath string, optContent ...string) (*api.FilesResponse, error) { | ||||
| 	content := util.OptionalArg(optContent, "This is a NEW file") // some tests need this default content because its SHA is hardcoded | ||||
| 	return createFileInBranch(user, repo, createFileInBranchOptions{}, map[string]string{treePath: content}) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user