code.gitea.io/gitea@v1.21.7/routers/api/v1/repo/notes.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	"code.gitea.io/gitea/modules/context"
    11  	"code.gitea.io/gitea/modules/git"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/services/convert"
    14  )
    15  
    16  // GetNote Get a note corresponding to a single commit from a repository
    17  func GetNote(ctx *context.APIContext) {
    18  	// swagger:operation GET /repos/{owner}/{repo}/git/notes/{sha} repository repoGetNote
    19  	// ---
    20  	// summary: Get a note corresponding to a single commit from a repository
    21  	// produces:
    22  	// - application/json
    23  	// parameters:
    24  	// - name: owner
    25  	//   in: path
    26  	//   description: owner of the repo
    27  	//   type: string
    28  	//   required: true
    29  	// - name: repo
    30  	//   in: path
    31  	//   description: name of the repo
    32  	//   type: string
    33  	//   required: true
    34  	// - name: sha
    35  	//   in: path
    36  	//   description: a git ref or commit sha
    37  	//   type: string
    38  	//   required: true
    39  	// - name: verification
    40  	//   in: query
    41  	//   description: include verification for every commit (disable for speedup, default 'true')
    42  	//   type: boolean
    43  	// - name: files
    44  	//   in: query
    45  	//   description: include a list of affected files for every commit (disable for speedup, default 'true')
    46  	//   type: boolean
    47  	// responses:
    48  	//   "200":
    49  	//     "$ref": "#/responses/Note"
    50  	//   "422":
    51  	//     "$ref": "#/responses/validationError"
    52  	//   "404":
    53  	//     "$ref": "#/responses/notFound"
    54  
    55  	sha := ctx.Params(":sha")
    56  	if !git.IsValidRefPattern(sha) {
    57  		ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
    58  		return
    59  	}
    60  	getNote(ctx, sha)
    61  }
    62  
    63  func getNote(ctx *context.APIContext, identifier string) {
    64  	if ctx.Repo.GitRepo == nil {
    65  		ctx.InternalServerError(fmt.Errorf("no open git repo"))
    66  		return
    67  	}
    68  
    69  	commitSHA, err := ctx.Repo.GitRepo.ConvertToSHA1(identifier)
    70  	if err != nil {
    71  		if git.IsErrNotExist(err) {
    72  			ctx.NotFound(err)
    73  		} else {
    74  			ctx.Error(http.StatusInternalServerError, "ConvertToSHA1", err)
    75  		}
    76  		return
    77  	}
    78  
    79  	var note git.Note
    80  	if err := git.GetNote(ctx, ctx.Repo.GitRepo, commitSHA.String(), &note); err != nil {
    81  		if git.IsErrNotExist(err) {
    82  			ctx.NotFound(identifier)
    83  			return
    84  		}
    85  		ctx.Error(http.StatusInternalServerError, "GetNote", err)
    86  		return
    87  	}
    88  
    89  	verification := ctx.FormString("verification") == "" || ctx.FormBool("verification")
    90  	files := ctx.FormString("files") == "" || ctx.FormBool("files")
    91  
    92  	cmt, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, note.Commit, nil,
    93  		convert.ToCommitOptions{
    94  			Stat:         true,
    95  			Verification: verification,
    96  			Files:        files,
    97  		})
    98  	if err != nil {
    99  		ctx.Error(http.StatusInternalServerError, "ToCommit", err)
   100  		return
   101  	}
   102  	apiNote := api.Note{Message: string(note.Message), Commit: cmt}
   103  	ctx.JSON(http.StatusOK, apiNote)
   104  }