]> source.dussan.org Git - gitea.git/commitdiff
Fix panic when getting notes by ref (#23372)
authorZettat123 <zettat123@gmail.com>
Wed, 8 Mar 2023 12:21:23 +0000 (20:21 +0800)
committerGitHub <noreply@github.com>
Wed, 8 Mar 2023 12:21:23 +0000 (20:21 +0800)
Fix #23357 .

Now the `/repos/{owner}/{repo}/git/notes/{sha}` API supports getting
notes by a ref or sha
(https://try.gitea.io/api/swagger#/repository/repoGetNote). But the
`GetNote` func can only accept commit ID.

https://github.com/go-gitea/gitea/blob/a12f5757372f751d25f9e5ca1f168f6920ded894/modules/git/notes_nogogit.go#L18

So we need to convert the query parameter to commit ID before calling
`GetNote`.

routers/api/v1/repo/notes.go

index 2d1f3291f8b9ca9962f1e7afb9a5d0a1c96719c4..74969f2cad7c881c3d57ca8974d36e49cfbf467f 100644 (file)
@@ -58,8 +58,18 @@ func getNote(ctx *context.APIContext, identifier string) {
                return
        }
 
+       commitSHA, err := ctx.Repo.GitRepo.ConvertToSHA1(identifier)
+       if err != nil {
+               if git.IsErrNotExist(err) {
+                       ctx.NotFound(err)
+               } else {
+                       ctx.Error(http.StatusInternalServerError, "ConvertToSHA1", err)
+               }
+               return
+       }
+
        var note git.Note
-       if err := git.GetNote(ctx, ctx.Repo.GitRepo, identifier, &note); err != nil {
+       if err := git.GetNote(ctx, ctx.Repo.GitRepo, commitSHA.String(), &note); err != nil {
                if git.IsErrNotExist(err) {
                        ctx.NotFound(identifier)
                        return