summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/utils
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
commitdd136858f1ea40ad3c94191d647487fa4f31926c (patch)
tree58fec94a7b2a12510c9664b21793f1ed560c6518 /routers/api/v1/utils
parentInitial commit. (diff)
downloadforgejo-debian.tar.xz
forgejo-debian.zip
Adding upstream version 9.0.0.upstream/9.0.0upstreamdebian
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'routers/api/v1/utils')
-rw-r--r--routers/api/v1/utils/block.go65
-rw-r--r--routers/api/v1/utils/git.go99
-rw-r--r--routers/api/v1/utils/hook.go419
-rw-r--r--routers/api/v1/utils/page.go18
4 files changed, 601 insertions, 0 deletions
diff --git a/routers/api/v1/utils/block.go b/routers/api/v1/utils/block.go
new file mode 100644
index 0000000..34fad96
--- /dev/null
+++ b/routers/api/v1/utils/block.go
@@ -0,0 +1,65 @@
+// Copyright 2023 The Forgejo Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package utils
+
+import (
+ "net/http"
+
+ user_model "code.gitea.io/gitea/models/user"
+ api "code.gitea.io/gitea/modules/structs"
+ "code.gitea.io/gitea/services/context"
+ user_service "code.gitea.io/gitea/services/user"
+)
+
+// ListUserBlockedUsers lists the blocked users of the provided doer.
+func ListUserBlockedUsers(ctx *context.APIContext, doer *user_model.User) {
+ count, err := user_model.CountBlockedUsers(ctx, doer.ID)
+ if err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+
+ blockedUsers, err := user_model.ListBlockedUsers(ctx, doer.ID, GetListOptions(ctx))
+ if err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+
+ apiBlockedUsers := make([]*api.BlockedUser, len(blockedUsers))
+ for i, blockedUser := range blockedUsers {
+ apiBlockedUsers[i] = &api.BlockedUser{
+ BlockID: blockedUser.ID,
+ Created: blockedUser.CreatedUnix.AsTime(),
+ }
+ if err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+ }
+
+ ctx.SetTotalCountHeader(count)
+ ctx.JSON(http.StatusOK, apiBlockedUsers)
+}
+
+// BlockUser blocks the blockUser from the doer.
+func BlockUser(ctx *context.APIContext, doer, blockUser *user_model.User) {
+ err := user_service.BlockUser(ctx, doer.ID, blockUser.ID)
+ if err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+
+ ctx.Status(http.StatusNoContent)
+}
+
+// UnblockUser unblocks the blockUser from the doer.
+func UnblockUser(ctx *context.APIContext, doer, blockUser *user_model.User) {
+ err := user_model.UnblockUser(ctx, doer.ID, blockUser.ID)
+ if err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+
+ ctx.Status(http.StatusNoContent)
+}
diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go
new file mode 100644
index 0000000..4e25137
--- /dev/null
+++ b/routers/api/v1/utils/git.go
@@ -0,0 +1,99 @@
+// Copyright 2021 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package utils
+
+import (
+ gocontext "context"
+ "fmt"
+ "net/http"
+
+ "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/gitrepo"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/services/context"
+)
+
+// ResolveRefOrSha resolve ref to sha if exist
+func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
+ if len(ref) == 0 {
+ ctx.Error(http.StatusBadRequest, "ref not given", nil)
+ return ""
+ }
+
+ sha := ref
+ // Search branches and tags
+ for _, refType := range []string{"heads", "tags"} {
+ refSHA, lastMethodName, err := searchRefCommitByType(ctx, refType, ref)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, lastMethodName, err)
+ return ""
+ }
+ if refSHA != "" {
+ sha = refSHA
+ break
+ }
+ }
+
+ sha = MustConvertToSHA1(ctx, ctx.Repo, sha)
+
+ if ctx.Repo.GitRepo != nil {
+ err := ctx.Repo.GitRepo.AddLastCommitCache(ctx.Repo.Repository.GetCommitsCountCacheKey(ref, ref != sha), ctx.Repo.Repository.FullName(), sha)
+ if err != nil {
+ log.Error("Unable to get commits count for %s in %s. Error: %v", sha, ctx.Repo.Repository.FullName(), err)
+ }
+ }
+
+ return sha
+}
+
+// GetGitRefs return git references based on filter
+func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
+ if ctx.Repo.GitRepo == nil {
+ return nil, "", fmt.Errorf("no open git repo found in context")
+ }
+ if len(filter) > 0 {
+ filter = "refs/" + filter
+ }
+ refs, err := ctx.Repo.GitRepo.GetRefsFiltered(filter)
+ return refs, "GetRefsFiltered", err
+}
+
+func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) {
+ refs, lastMethodName, err := GetGitRefs(ctx, refType+"/"+filter) // Search by type
+ if err != nil {
+ return "", lastMethodName, err
+ }
+ if len(refs) > 0 {
+ return refs[0].Object.String(), "", nil // Return found SHA
+ }
+ return "", "", nil
+}
+
+// ConvertToObjectID returns a full-length SHA1 from a potential ID string
+func ConvertToObjectID(ctx gocontext.Context, repo *context.Repository, commitID string) (git.ObjectID, error) {
+ objectFormat := repo.GetObjectFormat()
+ if len(commitID) == objectFormat.FullLength() && objectFormat.IsValid(commitID) {
+ sha, err := git.NewIDFromString(commitID)
+ if err == nil {
+ return sha, nil
+ }
+ }
+
+ gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo.Repository)
+ if err != nil {
+ return objectFormat.EmptyObjectID(), fmt.Errorf("RepositoryFromContextOrOpen: %w", err)
+ }
+ defer closer.Close()
+
+ return gitRepo.ConvertToGitID(commitID)
+}
+
+// MustConvertToSHA1 returns a full-length SHA1 string from a potential ID string, or returns origin input if it can't convert to SHA1
+func MustConvertToSHA1(ctx gocontext.Context, repo *context.Repository, commitID string) string {
+ sha, err := ConvertToObjectID(ctx, repo, commitID)
+ if err != nil {
+ return commitID
+ }
+ return sha.String()
+}
diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go
new file mode 100644
index 0000000..f1abd49
--- /dev/null
+++ b/routers/api/v1/utils/hook.go
@@ -0,0 +1,419 @@
+// Copyright 2016 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package utils
+
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+ "strings"
+
+ "code.gitea.io/gitea/models/db"
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/models/webhook"
+ "code.gitea.io/gitea/modules/json"
+ "code.gitea.io/gitea/modules/setting"
+ api "code.gitea.io/gitea/modules/structs"
+ "code.gitea.io/gitea/modules/util"
+ webhook_module "code.gitea.io/gitea/modules/webhook"
+ "code.gitea.io/gitea/services/context"
+ webhook_service "code.gitea.io/gitea/services/webhook"
+)
+
+// ListOwnerHooks lists the webhooks of the provided owner
+func ListOwnerHooks(ctx *context.APIContext, owner *user_model.User) {
+ opts := &webhook.ListWebhookOptions{
+ ListOptions: GetListOptions(ctx),
+ OwnerID: owner.ID,
+ }
+
+ hooks, count, err := db.FindAndCount[webhook.Webhook](ctx, opts)
+ if err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+
+ apiHooks := make([]*api.Hook, len(hooks))
+ for i, hook := range hooks {
+ apiHooks[i], err = webhook_service.ToHook(owner.HomeLink(), hook)
+ if err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+ }
+
+ ctx.SetTotalCountHeader(count)
+ ctx.JSON(http.StatusOK, apiHooks)
+}
+
+// GetOwnerHook gets an user or organization webhook. Errors are written to ctx.
+func GetOwnerHook(ctx *context.APIContext, ownerID, hookID int64) (*webhook.Webhook, error) {
+ w, err := webhook.GetWebhookByOwnerID(ctx, ownerID, hookID)
+ if err != nil {
+ if webhook.IsErrWebhookNotExist(err) {
+ ctx.NotFound()
+ } else {
+ ctx.Error(http.StatusInternalServerError, "GetWebhookByOwnerID", err)
+ }
+ return nil, err
+ }
+ return w, nil
+}
+
+// GetRepoHook get a repo's webhook. If there is an error, write to `ctx`
+// accordingly and return the error
+func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhook, error) {
+ w, err := webhook.GetWebhookByRepoID(ctx, repoID, hookID)
+ if err != nil {
+ if webhook.IsErrWebhookNotExist(err) {
+ ctx.NotFound()
+ } else {
+ ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err)
+ }
+ return nil, err
+ }
+ return w, nil
+}
+
+// checkCreateHookOption check if a CreateHookOption form is valid. If invalid,
+// write the appropriate error to `ctx`. Return whether the form is valid
+func checkCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
+ if !webhook_service.IsValidHookTaskType(form.Type) {
+ ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Invalid hook type: %s", form.Type))
+ return false
+ }
+ for _, name := range []string{"url", "content_type"} {
+ if _, ok := form.Config[name]; !ok {
+ ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: "+name)
+ return false
+ }
+ }
+ if !webhook.IsValidHookContentType(form.Config["content_type"]) {
+ ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
+ return false
+ }
+ return true
+}
+
+// AddSystemHook add a system hook
+func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) {
+ hook, ok := addHook(ctx, form, 0, 0)
+ if ok {
+ h, err := webhook_service.ToHook(setting.AppSubURL+"/admin", hook)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
+ return
+ }
+ ctx.JSON(http.StatusCreated, h)
+ }
+}
+
+// AddOwnerHook adds a hook to an user or organization
+func AddOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.CreateHookOption) {
+ hook, ok := addHook(ctx, form, owner.ID, 0)
+ if !ok {
+ return
+ }
+ apiHook, ok := toAPIHook(ctx, owner.HomeLink(), hook)
+ if !ok {
+ return
+ }
+ ctx.JSON(http.StatusCreated, apiHook)
+}
+
+// AddRepoHook add a hook to a repo. Writes to `ctx` accordingly
+func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
+ repo := ctx.Repo
+ hook, ok := addHook(ctx, form, 0, repo.Repository.ID)
+ if !ok {
+ return
+ }
+ apiHook, ok := toAPIHook(ctx, repo.RepoLink, hook)
+ if !ok {
+ return
+ }
+ ctx.JSON(http.StatusCreated, apiHook)
+}
+
+// toAPIHook converts the hook to its API representation.
+// If there is an error, write to `ctx` accordingly. Return (hook, ok)
+func toAPIHook(ctx *context.APIContext, repoLink string, hook *webhook.Webhook) (*api.Hook, bool) {
+ apiHook, err := webhook_service.ToHook(repoLink, hook)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "ToHook", err)
+ return nil, false
+ }
+ return apiHook, true
+}
+
+func issuesHook(events []string, event string) bool {
+ return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventIssues), true)
+}
+
+func pullHook(events []string, event string) bool {
+ return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventPullRequest), true)
+}
+
+// addHook add the hook specified by `form`, `ownerID` and `repoID`. If there is
+// an error, write to `ctx` accordingly. Return (webhook, ok)
+func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64) (*webhook.Webhook, bool) {
+ var isSystemWebhook bool
+ if !checkCreateHookOption(ctx, form) {
+ return nil, false
+ }
+
+ if len(form.Events) == 0 {
+ form.Events = []string{"push"}
+ }
+ if form.Config["is_system_webhook"] != "" {
+ sw, err := strconv.ParseBool(form.Config["is_system_webhook"])
+ if err != nil {
+ ctx.Error(http.StatusUnprocessableEntity, "", "Invalid is_system_webhook value")
+ return nil, false
+ }
+ isSystemWebhook = sw
+ }
+ w := &webhook.Webhook{
+ OwnerID: ownerID,
+ RepoID: repoID,
+ URL: form.Config["url"],
+ ContentType: webhook.ToHookContentType(form.Config["content_type"]),
+ Secret: form.Config["secret"],
+ HTTPMethod: "POST",
+ IsSystemWebhook: isSystemWebhook,
+ HookEvent: &webhook_module.HookEvent{
+ ChooseEvents: true,
+ HookEvents: webhook_module.HookEvents{
+ Create: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true),
+ Delete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true),
+ Fork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true),
+ Issues: issuesHook(form.Events, "issues_only"),
+ IssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)),
+ IssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)),
+ IssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)),
+ IssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)),
+ Push: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true),
+ PullRequest: pullHook(form.Events, "pull_request_only"),
+ PullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)),
+ PullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)),
+ PullRequestMilestone: pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)),
+ PullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)),
+ PullRequestReview: pullHook(form.Events, "pull_request_review"),
+ PullRequestReviewRequest: pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)),
+ PullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)),
+ Wiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true),
+ Repository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true),
+ Release: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true),
+ },
+ BranchFilter: form.BranchFilter,
+ },
+ IsActive: form.Active,
+ Type: form.Type,
+ }
+ err := w.SetHeaderAuthorization(form.AuthorizationHeader)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "SetHeaderAuthorization", err)
+ return nil, false
+ }
+ if w.Type == webhook_module.SLACK {
+ channel, ok := form.Config["channel"]
+ if !ok {
+ ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
+ return nil, false
+ }
+ channel = strings.TrimSpace(channel)
+
+ if !webhook_service.IsValidSlackChannel(channel) {
+ ctx.Error(http.StatusBadRequest, "", "Invalid slack channel name")
+ return nil, false
+ }
+
+ meta, err := json.Marshal(&webhook_service.SlackMeta{
+ Channel: channel,
+ Username: form.Config["username"],
+ IconURL: form.Config["icon_url"],
+ Color: form.Config["color"],
+ })
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
+ return nil, false
+ }
+ w.Meta = string(meta)
+ }
+
+ if err := w.UpdateEvent(); err != nil {
+ ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
+ return nil, false
+ } else if err := webhook.CreateWebhook(ctx, w); err != nil {
+ ctx.Error(http.StatusInternalServerError, "CreateWebhook", err)
+ return nil, false
+ }
+ return w, true
+}
+
+// EditSystemHook edit system webhook `w` according to `form`. Writes to `ctx` accordingly
+func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
+ hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
+ return
+ }
+ if !editHook(ctx, form, hook) {
+ ctx.Error(http.StatusInternalServerError, "editHook", err)
+ return
+ }
+ updated, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
+ return
+ }
+ h, err := webhook_service.ToHook(setting.AppURL+"/admin", updated)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
+ return
+ }
+ ctx.JSON(http.StatusOK, h)
+}
+
+// EditOwnerHook updates a webhook of an user or organization
+func EditOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.EditHookOption, hookID int64) {
+ hook, err := GetOwnerHook(ctx, owner.ID, hookID)
+ if err != nil {
+ return
+ }
+ if !editHook(ctx, form, hook) {
+ return
+ }
+ updated, err := GetOwnerHook(ctx, owner.ID, hookID)
+ if err != nil {
+ return
+ }
+ apiHook, ok := toAPIHook(ctx, owner.HomeLink(), updated)
+ if !ok {
+ return
+ }
+ ctx.JSON(http.StatusOK, apiHook)
+}
+
+// EditRepoHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
+func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
+ repo := ctx.Repo
+ hook, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
+ if err != nil {
+ return
+ }
+ if !editHook(ctx, form, hook) {
+ return
+ }
+ updated, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
+ if err != nil {
+ return
+ }
+ apiHook, ok := toAPIHook(ctx, repo.RepoLink, updated)
+ if !ok {
+ return
+ }
+ ctx.JSON(http.StatusOK, apiHook)
+}
+
+// editHook edit the webhook `w` according to `form`. If an error occurs, write
+// to `ctx` accordingly and return the error. Return whether successful
+func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webhook) bool {
+ if form.Config != nil {
+ if url, ok := form.Config["url"]; ok {
+ w.URL = url
+ }
+ if ct, ok := form.Config["content_type"]; ok {
+ if !webhook.IsValidHookContentType(ct) {
+ ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
+ return false
+ }
+ w.ContentType = webhook.ToHookContentType(ct)
+ }
+
+ if w.Type == webhook_module.SLACK {
+ if channel, ok := form.Config["channel"]; ok {
+ meta, err := json.Marshal(&webhook_service.SlackMeta{
+ Channel: channel,
+ Username: form.Config["username"],
+ IconURL: form.Config["icon_url"],
+ Color: form.Config["color"],
+ })
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
+ return false
+ }
+ w.Meta = string(meta)
+ }
+ }
+ }
+
+ // Update events
+ if len(form.Events) == 0 {
+ form.Events = []string{"push"}
+ }
+ w.PushOnly = false
+ w.SendEverything = false
+ w.ChooseEvents = true
+ w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true)
+ w.Push = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true)
+ w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true)
+ w.Delete = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true)
+ w.Fork = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true)
+ w.Repository = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true)
+ w.Wiki = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true)
+ w.Release = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true)
+ w.BranchFilter = form.BranchFilter
+
+ err := w.SetHeaderAuthorization(form.AuthorizationHeader)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "SetHeaderAuthorization", err)
+ return false
+ }
+
+ // Issues
+ w.Issues = issuesHook(form.Events, "issues_only")
+ w.IssueAssign = issuesHook(form.Events, string(webhook_module.HookEventIssueAssign))
+ w.IssueLabel = issuesHook(form.Events, string(webhook_module.HookEventIssueLabel))
+ w.IssueMilestone = issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone))
+ w.IssueComment = issuesHook(form.Events, string(webhook_module.HookEventIssueComment))
+
+ // Pull requests
+ w.PullRequest = pullHook(form.Events, "pull_request_only")
+ w.PullRequestAssign = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign))
+ w.PullRequestLabel = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel))
+ w.PullRequestMilestone = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone))
+ w.PullRequestComment = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment))
+ w.PullRequestReview = pullHook(form.Events, "pull_request_review")
+ w.PullRequestReviewRequest = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest))
+ w.PullRequestSync = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))
+
+ if err := w.UpdateEvent(); err != nil {
+ ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
+ return false
+ }
+
+ if form.Active != nil {
+ w.IsActive = *form.Active
+ }
+
+ if err := webhook.UpdateWebhook(ctx, w); err != nil {
+ ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err)
+ return false
+ }
+ return true
+}
+
+// DeleteOwnerHook deletes the hook owned by the owner.
+func DeleteOwnerHook(ctx *context.APIContext, owner *user_model.User, hookID int64) {
+ if err := webhook.DeleteWebhookByOwnerID(ctx, owner.ID, hookID); err != nil {
+ if webhook.IsErrWebhookNotExist(err) {
+ ctx.NotFound()
+ } else {
+ ctx.Error(http.StatusInternalServerError, "DeleteWebhookByOwnerID", err)
+ }
+ return
+ }
+ ctx.Status(http.StatusNoContent)
+}
diff --git a/routers/api/v1/utils/page.go b/routers/api/v1/utils/page.go
new file mode 100644
index 0000000..024ba7b
--- /dev/null
+++ b/routers/api/v1/utils/page.go
@@ -0,0 +1,18 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package utils
+
+import (
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/services/context"
+ "code.gitea.io/gitea/services/convert"
+)
+
+// GetListOptions returns list options using the page and limit parameters
+func GetListOptions(ctx *context.APIContext) db.ListOptions {
+ return db.ListOptions{
+ Page: ctx.FormInt("page"),
+ PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
+ }
+}