]> source.dussan.org Git - gitea.git/commitdiff
Handle invalid target when creating releases using API (#31841)
authorKemal Zebari <60799661+kemzeb@users.noreply.github.com>
Thu, 12 Sep 2024 07:47:31 +0000 (00:47 -0700)
committerGitHub <noreply@github.com>
Thu, 12 Sep 2024 07:47:31 +0000 (07:47 +0000)
A 500 status code was thrown when passing a non-existent target to the
create release API. This snapshot handles this error and instead throws
a 404 status code.

Discovered while working on #31840.

routers/api/v1/repo/release.go
services/release/release.go
tests/integration/api_releases_test.go

index ef587f6274b91d8400ed1b74e7ff62308c1ab05e..478bdbd797a8e1dc7002a565674334b73e990266 100644 (file)
@@ -12,6 +12,7 @@ import (
        "code.gitea.io/gitea/models/perm"
        repo_model "code.gitea.io/gitea/models/repo"
        "code.gitea.io/gitea/models/unit"
+       "code.gitea.io/gitea/modules/git"
        api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/web"
        "code.gitea.io/gitea/routers/api/v1/utils"
@@ -251,6 +252,8 @@ func CreateRelease(ctx *context.APIContext) {
                                ctx.Error(http.StatusConflict, "ReleaseAlreadyExist", err)
                        } else if models.IsErrProtectedTagName(err) {
                                ctx.Error(http.StatusUnprocessableEntity, "ProtectedTagName", err)
+                       } else if git.IsErrNotExist(err) {
+                               ctx.Error(http.StatusNotFound, "ErrNotExist", fmt.Errorf("target \"%v\" not found: %w", rel.Target, err))
                        } else {
                                ctx.Error(http.StatusInternalServerError, "CreateRelease", err)
                        }
index 399fdc79c010d904901b6d5bc812f36fef0de2ed..5c021404b8fdcfc6e777e5fc186584ccf9e7cc2e 100644 (file)
@@ -65,7 +65,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel
 
                        commit, err := gitRepo.GetCommit(rel.Target)
                        if err != nil {
-                               return false, fmt.Errorf("createTag::GetCommit[%v]: %w", rel.Target, err)
+                               return false, err
                        }
 
                        if len(msg) > 0 {
index d912452802a56eb0f8f79a0a39f6e9c265226497..b3d4928b7ba2a632e24c3bf876b3b859679f8b91 100644 (file)
@@ -208,6 +208,24 @@ func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) {
        createNewReleaseUsingAPI(t, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
 }
 
+func TestAPICreateReleaseGivenInvalidTarget(t *testing.T) {
+       defer tests.PrepareTestEnv(t)()
+
+       repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
+       owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
+       session := loginUser(t, owner.LowerName)
+       token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
+
+       urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases", owner.Name, repo.Name)
+       req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateReleaseOption{
+               TagName: "i-point-to-an-invalid-target",
+               Title:   "Invalid Target",
+               Target:  "invalid-target",
+       }).AddTokenAuth(token)
+
+       MakeRequest(t, req, http.StatusNotFound)
+}
+
 func TestAPIGetLatestRelease(t *testing.T) {
        defer tests.PrepareTestEnv(t)()