mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
89 lines
3.0 KiB
Go
89 lines
3.0 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
api "github.com/hanzoai/git/modules/structs"
|
|
"github.com/hanzoai/git/modules/util"
|
|
files_service "github.com/hanzoai/git/services/repository/files"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type createFileInBranchOptions struct {
|
|
OldBranch, NewBranch string
|
|
CommitMessage string
|
|
CommitterName string
|
|
CommitterEmail 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{
|
|
OldBranch: createOpts.OldBranch,
|
|
NewBranch: createOpts.NewBranch,
|
|
Message: createOpts.CommitMessage,
|
|
}
|
|
if createOpts.CommitterName != "" || createOpts.CommitterEmail != "" {
|
|
opts.Committer = &files_service.IdentityOptions{
|
|
GitUserName: createOpts.CommitterName,
|
|
GitUserEmail: createOpts.CommitterEmail,
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
|
|
func deleteFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName string) (*api.FilesResponse, error) {
|
|
ctx := context.TODO()
|
|
opts := &files_service.ChangeRepoFilesOptions{
|
|
Files: []*files_service.ChangeRepoFile{
|
|
{
|
|
Operation: "delete",
|
|
TreePath: treePath,
|
|
},
|
|
},
|
|
OldBranch: branchName,
|
|
Author: nil,
|
|
Committer: nil,
|
|
}
|
|
return files_service.ChangeRepoFiles(ctx, repo, user, opts)
|
|
}
|
|
|
|
func createOrReplaceFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) error {
|
|
_, err := deleteFileInBranch(user, repo, treePath, branchName)
|
|
|
|
if err != nil && !errors.Is(err, util.ErrNotExist) {
|
|
return err
|
|
}
|
|
|
|
_, err = createFileInBranch(user, repo, createFileInBranchOptions{OldBranch: branchName}, map[string]string{treePath: content})
|
|
return err
|
|
}
|
|
|
|
// 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})
|
|
}
|