Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 404 in the API if the requested webhooks were not found #24823

Merged
merged 6 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions routers/api/v1/admin/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package admin

import (
"errors"
"net/http"

"code.gitea.io/gitea/models/webhook"
Expand Down Expand Up @@ -74,7 +75,11 @@ func GetHook(ctx *context.APIContext) {
hookID := ctx.ParamsInt64(":id")
hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
if errors.Is(err, util.ErrNotExist) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
}
return
}
h, err := webhook_service.ToHook("/admin/", hook)
Expand Down Expand Up @@ -160,7 +165,7 @@ func DeleteHook(ctx *context.APIContext) {

hookID := ctx.ParamsInt64(":id")
if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil {
if webhook.IsErrWebhookNotExist(err) {
if errors.Is(err, util.ErrNotExist) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "DeleteDefaultSystemWebhook", err)
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/api_admin_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
"net/http"
"net/url"
"testing"

"code.gitea.io/gitea/tests"
)

func TestGetNotExistHook(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
defer tests.PrepareTestEnv(t)()

req := NewRequest(t, "GET", "/api/v1/admin/hooks/1234")
MakeRequest(t, req, http.StatusNotFound)
})
}