
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/7337 - Massive replacement of changing `code.gitea.io/gitea` to `forgejo.org`. - Resolves forgejo/discussions#258 Co-authored-by: Gusted <postmaster@gusted.xyz> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7354 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org> Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"forgejo.org/models"
|
|
repo_model "forgejo.org/models/repo"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/git"
|
|
api "forgejo.org/modules/structs"
|
|
files_service "forgejo.org/services/repository/files"
|
|
)
|
|
|
|
func createFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) (*api.FilesResponse, error) {
|
|
opts := &files_service.ChangeRepoFilesOptions{
|
|
Files: []*files_service.ChangeRepoFile{
|
|
{
|
|
Operation: "create",
|
|
TreePath: treePath,
|
|
ContentReader: strings.NewReader(content),
|
|
},
|
|
},
|
|
OldBranch: branchName,
|
|
Author: nil,
|
|
Committer: nil,
|
|
}
|
|
return files_service.ChangeRepoFiles(git.DefaultContext, repo, user, opts)
|
|
}
|
|
|
|
func deleteFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName string) (*api.FilesResponse, error) {
|
|
opts := &files_service.ChangeRepoFilesOptions{
|
|
Files: []*files_service.ChangeRepoFile{
|
|
{
|
|
Operation: "delete",
|
|
TreePath: treePath,
|
|
},
|
|
},
|
|
OldBranch: branchName,
|
|
Author: nil,
|
|
Committer: nil,
|
|
}
|
|
return files_service.ChangeRepoFiles(git.DefaultContext, 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 && !models.IsErrRepoFileDoesNotExist(err) {
|
|
return err
|
|
}
|
|
|
|
_, err = createFileInBranch(user, repo, treePath, branchName, 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")
|
|
}
|