Skip to content

Commit

Permalink
Move ToHook to ../webhook to prevent an import cycle and a new package
Browse files Browse the repository at this point in the history
as per @lunny
  • Loading branch information
delvh committed Jan 1, 2023
1 parent 8bb9921 commit db7db12
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 93 deletions.
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
_ "code.gitea.io/gitea/modules/markup/markdown"
_ "code.gitea.io/gitea/modules/markup/orgmode"

// register webhook notifier
_ "code.gitea.io/gitea/services/webhooknotifier"

"github.com/urfave/cli"
)

Expand Down
18 changes: 9 additions & 9 deletions routers/api/v1/org/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package org
import (
"net/http"

"code.gitea.io/gitea/models/webhook"
webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/convert"
webhook_service "code.gitea.io/gitea/services/webhook"
)

// ListHooks list an organziation's webhooks
Expand Down Expand Up @@ -39,26 +39,26 @@ func ListHooks(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/HookList"

opts := &webhook.ListWebhookOptions{
opts := &webhook_model.ListWebhookOptions{
ListOptions: utils.GetListOptions(ctx),
OrgID: ctx.Org.Organization.ID,
}

count, err := webhook.CountWebhooksByOpts(opts)
count, err := webhook_model.CountWebhooksByOpts(opts)
if err != nil {
ctx.InternalServerError(err)
return
}

orgHooks, err := webhook.ListWebhooksByOpts(ctx, opts)
orgHooks, err := webhook_model.ListWebhooksByOpts(ctx, opts)
if err != nil {
ctx.InternalServerError(err)
return
}

hooks := make([]*api.Hook, len(orgHooks))
for i, hook := range orgHooks {
hooks[i], err = convert.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook)
hooks[i], err = webhook_service.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook)
if err != nil {
ctx.InternalServerError(err)
return
Expand Down Expand Up @@ -99,7 +99,7 @@ func GetHook(ctx *context.APIContext) {
return
}

apiHook, err := convert.ToHook(org.AsUser().HomeLink(), hook)
apiHook, err := webhook_service.ToHook(org.AsUser().HomeLink(), hook)
if err != nil {
ctx.InternalServerError(err)
return
Expand Down Expand Up @@ -200,8 +200,8 @@ func DeleteHook(ctx *context.APIContext) {

org := ctx.Org.Organization
hookID := ctx.ParamsInt64(":id")
if err := webhook.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
if webhook.IsErrWebhookNotExist(err) {
if err := webhook_model.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
if webhook_model.IsErrWebhookNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "DeleteWebhookByOrgID", err)
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func ListHooks(ctx *context.APIContext) {

apiHooks := make([]*api.Hook, len(hooks))
for i := range hooks {
apiHooks[i], err = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
apiHooks[i], err = webhook_service.ToHook(ctx.Repo.RepoLink, hooks[i])
if err != nil {
ctx.InternalServerError(err)
return
Expand Down Expand Up @@ -116,7 +116,7 @@ func GetHook(ctx *context.APIContext) {
if err != nil {
return
}
apiHook, err := convert.ToHook(repo.RepoLink, hook)
apiHook, err := webhook_service.ToHook(repo.RepoLink, hook)
if err != nil {
ctx.InternalServerError(err)
return
Expand Down
3 changes: 1 addition & 2 deletions routers/api/v1/utils/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
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/convert"
webhook_service "code.gitea.io/gitea/services/webhook"
)

Expand Down Expand Up @@ -99,7 +98,7 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
// 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 := convert.ToHook(repoLink, hook)
apiHook, err := webhook_service.ToHook(repoLink, hook)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ToHook", err)
return nil, false
Expand Down
35 changes: 0 additions & 35 deletions services/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
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/gitdiff"
"code.gitea.io/gitea/services/webhook"
)

// ToEmail convert models.EmailAddress to api.Email
Expand Down Expand Up @@ -243,38 +240,6 @@ func ToGPGKeyEmail(email *user_model.EmailAddress) *api.GPGKeyEmail {
}
}

// ToHook convert models.Webhook to api.Hook
func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
config := map[string]string{
"url": w.URL,
"content_type": w.ContentType.Name(),
}
if w.Type == webhook_module.SLACK {
s := webhook.GetSlackHook(w)
config["channel"] = s.Channel
config["username"] = s.Username
config["icon_url"] = s.IconURL
config["color"] = s.Color
}

authorizationHeader, err := w.HeaderAuthorization()
if err != nil {
return nil, err
}

return &api.Hook{
ID: w.ID,
Type: w.Type,
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
Active: w.IsActive,
Config: config,
Events: w.EventsArray(),
AuthorizationHeader: authorizationHeader,
Updated: w.UpdatedUnix.AsTime(),
Created: w.CreatedUnix.AsTime(),
}, nil
}

// ToGitHook convert git.Hook to api.GitHook
func ToGitHook(h *git.Hook) *api.GitHook {
return &api.GitHook{
Expand Down
35 changes: 35 additions & 0 deletions services/webhook/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"net/url"
"strings"

webhook_model "code.gitea.io/gitea/models/webhook"
"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"
)

type linkFormatter = func(string, string) string
Expand Down Expand Up @@ -223,3 +225,36 @@ func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFo

return text, issueTitle, color
}

// ToHook convert models.Webhook to api.Hook
// This function is not part of the convert package to prevent an import cycle
func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
config := map[string]string{
"url": w.URL,
"content_type": w.ContentType.Name(),
}
if w.Type == webhook_module.SLACK {
s := GetSlackHook(w)
config["channel"] = s.Channel
config["username"] = s.Username
config["icon_url"] = s.IconURL
config["color"] = s.Color
}

authorizationHeader, err := w.HeaderAuthorization()
if err != nil {
return nil, err
}

return &api.Hook{
ID: w.ID,
Type: w.Type,
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
Active: w.IsActive,
Config: config,
Events: w.EventsArray(),
AuthorizationHeader: authorizationHeader,
Updated: w.UpdatedUnix.AsTime(),
Created: w.CreatedUnix.AsTime(),
}, nil
}
Loading

0 comments on commit db7db12

Please sign in to comment.