新增: 响应结构体

This commit is contained in:
yystopf 2022-07-14 14:23:29 +08:00
parent bfd5804dd0
commit a7eebc5c59
5 changed files with 33 additions and 7 deletions

View File

@ -15,6 +15,21 @@ import (
api "code.gitea.io/gitea/modules/structs"
)
func GetBatchFileResponseFromCommit(repo *models.Repository, commit *git.Commit, branch string, treeNames []string) (*api.BatchFileResponse, error) {
fileCommitResponse, _ := GetFileCommitResponse(repo, commit)
verification := GetPayloadCommitVerification(commit)
batchFileResponse := &api.BatchFileResponse{
Commit: fileCommitResponse,
Verification: verification,
}
for _, treeName := range treeNames {
fileContent, _ := GetContents(repo, treeName, branch, false)
batchFileResponse.Contents = append(batchFileResponse.Contents, fileContent)
}
return batchFileResponse, nil
}
// GetFileResponseFromCommit Constructs a FileResponse from a Commit object
func GetFileResponseFromCommit(repo *models.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) {
fileContents, _ := GetContents(repo, treeName, branch, false) // ok if fails, then will be nil

View File

@ -511,7 +511,7 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
return file, nil
}
func CreateOrUpdateOrDeleteRepofiles(repo *models.Repository, doer *models.User, opts *BatchUpdateFileOptions, exchange *ExchangeFileOption) (*structs.FileResponse, error) {
func CreateOrUpdateOrDeleteRepofiles(repo *models.Repository, doer *models.User, opts *BatchUpdateFileOptions, exchange *ExchangeFileOption) (*structs.BatchFileResponse, error) {
var protectedPatterns []glob.Glob
if opts.OldBranch == "" {
@ -594,6 +594,7 @@ func CreateOrUpdateOrDeleteRepofiles(repo *models.Repository, doer *models.User,
opts.LastCommitID = lastCommitID.String()
}
var commitHash string
var treeNames []string
for {
select {
@ -867,6 +868,8 @@ func CreateOrUpdateOrDeleteRepofiles(repo *models.Repository, doer *models.User,
}
}
}
opts.Files = append(opts.Files, file)
treeNames = append(treeNames, file.TreePath)
case err := <-exchange.ErrChan:
return nil, err
case _ = <-exchange.StopChan:
@ -900,7 +903,7 @@ end:
return nil, err
}
file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, "")
file, err := GetBatchFileResponseFromCommit(repo, commit, opts.NewBranch, treeNames)
if err != nil {
return nil, err
}

View File

@ -119,6 +119,12 @@ type FileResponse struct {
Verification *PayloadCommitVerification `json:"verification"`
}
type BatchFileResponse struct {
Contents []*ContentsResponse `json:"contents"`
Commit *FileCommitResponse `json:"commit"`
Verification *PayloadCommitVerification `json:"verification"`
}
// FileDeleteResponse contains information about a repo's file that was deleted
type FileDeleteResponse struct {
Content interface{} `json:"content"` // to be set to nil

View File

@ -280,6 +280,7 @@ func CreateFile(ctx *context.APIContext) {
if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
handleCreateOrUpdateFileError(ctx, err)
return
} else {
ctx.JSON(http.StatusCreated, fileResponse)
}
@ -312,7 +313,7 @@ func BatchChangeFile(ctx *context.APIContext) {
// "$ref": "#/definitions/BatchChangeFileOptions"
// responses:
// "201":
// "$ref": "#/responses/FileResponse"
// "$ref": "#/responses/BatchFileResponse"
// "403":
// "$ref": "#/responses/error"
// "404":
@ -342,10 +343,11 @@ func BatchChangeFile(ctx *context.APIContext) {
apiBatchOpts.Header.Dates.Committer = time.Now()
}
if _, err := createOrUpdateOrDeleteFiles(ctx, apiBatchOpts); err != nil {
if batchFileResponse, err := createOrUpdateOrDeleteFiles(ctx, apiBatchOpts); err != nil {
handleCreateOrUpdateFileError(ctx, err)
} else {
ctx.JSON(http.StatusOK, batchFileResponse)
}
ctx.JSON(http.StatusOK, map[string]string{})
}
// UpdateFile handles API call for updating a file
@ -455,7 +457,7 @@ func handleCreateOrUpdateFileError(ctx *context.APIContext, err error) {
ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
}
func createOrUpdateOrDeleteFiles(ctx *context.APIContext, apiBatchOpts *api.BatchChangeFileOptions) (*api.FileResponse, error) {
func createOrUpdateOrDeleteFiles(ctx *context.APIContext, apiBatchOpts *api.BatchChangeFileOptions) (*api.BatchFileResponse, error) {
if !canWriteFiles(ctx.Repo) {
return nil, models.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.User.ID,

View File

@ -3508,7 +3508,7 @@
],
"responses": {
"201": {
"$ref": "#/responses/FileResponse"
"$ref": "#/responses/BatchFileResponse"
},
"403": {
"$ref": "#/responses/error"