code.gitea.io/gitea@v1.21.7/tests/integration/api_repo_file_helpers.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"strings"
     8  
     9  	"code.gitea.io/gitea/models"
    10  	repo_model "code.gitea.io/gitea/models/repo"
    11  	user_model "code.gitea.io/gitea/models/user"
    12  	"code.gitea.io/gitea/modules/git"
    13  	api "code.gitea.io/gitea/modules/structs"
    14  	files_service "code.gitea.io/gitea/services/repository/files"
    15  )
    16  
    17  func createFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) (*api.FilesResponse, error) {
    18  	opts := &files_service.ChangeRepoFilesOptions{
    19  		Files: []*files_service.ChangeRepoFile{
    20  			{
    21  				Operation:     "create",
    22  				TreePath:      treePath,
    23  				ContentReader: strings.NewReader(content),
    24  			},
    25  		},
    26  		OldBranch: branchName,
    27  		Author:    nil,
    28  		Committer: nil,
    29  	}
    30  	return files_service.ChangeRepoFiles(git.DefaultContext, repo, user, opts)
    31  }
    32  
    33  func deleteFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName string) (*api.FilesResponse, error) {
    34  	opts := &files_service.ChangeRepoFilesOptions{
    35  		Files: []*files_service.ChangeRepoFile{
    36  			{
    37  				Operation: "delete",
    38  				TreePath:  treePath,
    39  			},
    40  		},
    41  		OldBranch: branchName,
    42  		Author:    nil,
    43  		Committer: nil,
    44  	}
    45  	return files_service.ChangeRepoFiles(git.DefaultContext, repo, user, opts)
    46  }
    47  
    48  func createOrReplaceFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) error {
    49  	_, err := deleteFileInBranch(user, repo, treePath, branchName)
    50  
    51  	if err != nil && !models.IsErrRepoFileDoesNotExist(err) {
    52  		return err
    53  	}
    54  
    55  	_, err = createFileInBranch(user, repo, treePath, branchName, content)
    56  	return err
    57  }
    58  
    59  func createFile(user *user_model.User, repo *repo_model.Repository, treePath string) (*api.FilesResponse, error) {
    60  	return createFileInBranch(user, repo, treePath, repo.DefaultBranch, "This is a NEW file")
    61  }