<commit_before>// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repo

import (
	"fmt"
	"net/http"
	"net/url"
	"strings"

	"github.com/gogs/git-module"
	api "github.com/gogs/go-gogs-client"
	jsoniter "github.com/json-iterator/go"
	"gopkg.in/macaron.v1"

	"gogs.io/gogs/internal/conf"
	"gogs.io/gogs/internal/context"
	"gogs.io/gogs/internal/db"
	"gogs.io/gogs/internal/db/errors"
	"gogs.io/gogs/internal/form"
	"gogs.io/gogs/internal/netutil"
)

const (
	tmplRepoSettingsWebhooks   = "repo/settings/webhook/base"
	tmplRepoSettingsWebhookNew = "repo/settings/webhook/new"
	tmplOrgSettingsWebhooks    = "org/settings/webhooks"
	tmplOrgSettingsWebhookNew  = "org/settings/webhook_new"
)

func InjectOrgRepoContext() macaron.Handler {
	return func(c *context.Context) {
		orCtx, err := getOrgRepoContext(c)
		if err != nil {
			c.Error(err, "get organization or repository context")
			return
		}
		c.Map(orCtx)
	}
}

type orgRepoContext struct {
	OrgID    int64
	RepoID   int64
	Link     string
	TmplList string
	TmplNew  string
}

// getOrgRepoContext determines whether this is a repo context or organization context.
func getOrgRepoContext(c *context.Context) (*orgRepoContext, error) {
	if len(c.Repo.RepoLink) > 0 {
		c.PageIs("RepositoryContext")
		return &orgRepoContext{
			RepoID:   c.Repo.Repository.ID,
			Link:     c.Repo.RepoLink,
			TmplList: tmplRepoSettingsWebhooks,
			TmplNew:  tmplRepoSettingsWebhookNew,
		}, nil
	}

	if len(c.Org.OrgLink) > 0 {
		c.PageIs("OrganizationContext")
		return &orgRepoContext{
			OrgID:    c.Org.Organization.ID,
			Link:     c.Org.OrgLink,
			TmplList: tmplOrgSettingsWebhooks,
			TmplNew:  tmplOrgSettingsWebhookNew,
		}, nil
	}

	return nil, errors.New("unable to determine context")
}

func Webhooks(c *context.Context, orCtx *orgRepoContext) {
	c.Title("repo.settings.hooks")
	c.PageIs("SettingsHooks")
	c.Data["Types"] = conf.Webhook.Types

	var err error
	var ws []*db.Webhook
	if orCtx.RepoID > 0 {
		c.Data["Description"] = c.Tr("repo.settings.hooks_desc")
		ws, err = db.GetWebhooksByRepoID(orCtx.RepoID)
	} else {
		c.Data["Description"] = c.Tr("org.settings.hooks_desc")
		ws, err = db.GetWebhooksByOrgID(orCtx.OrgID)<commit_msg>webhook: add missing `%s` URL value for webhook description (#6478)<commit_after>// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repo

import (
	"fmt"
	"net/http"
	"net/url"
	"strings"

	"github.com/gogs/git-module"
	api "github.com/gogs/go-gogs-client"
	jsoniter "github.com/json-iterator/go"
	"gopkg.in/macaron.v1"

	"gogs.io/gogs/internal/conf"
	"gogs.io/gogs/internal/context"
	"gogs.io/gogs/internal/db"
	"gogs.io/gogs/internal/db/errors"
	"gogs.io/gogs/internal/form"
	"gogs.io/gogs/internal/netutil"
)

const (
	tmplRepoSettingsWebhooks   = "repo/settings/webhook/base"
	tmplRepoSettingsWebhookNew = "repo/settings/webhook/new"
	tmplOrgSettingsWebhooks    = "org/settings/webhooks"
	tmplOrgSettingsWebhookNew  = "org/settings/webhook_new"
)

func InjectOrgRepoContext() macaron.Handler {
	return func(c *context.Context) {
		orCtx, err := getOrgRepoContext(c)
		if err != nil {
			c.Error(err, "get organization or repository context")
			return
		}
		c.Map(orCtx)
	}
}

type orgRepoContext struct {
	OrgID    int64
	RepoID   int64
	Link     string
	TmplList string
	TmplNew  string
}

// getOrgRepoContext determines whether this is a repo context or organization context.
func getOrgRepoContext(c *context.Context) (*orgRepoContext, error) {
	if len(c.Repo.RepoLink) > 0 {
		c.PageIs("RepositoryContext")
		return &orgRepoContext{
			RepoID:   c.Repo.Repository.ID,
			Link:     c.Repo.RepoLink,
			TmplList: tmplRepoSettingsWebhooks,
			TmplNew:  tmplRepoSettingsWebhookNew,
		}, nil
	}

	if len(c.Org.OrgLink) > 0 {
		c.PageIs("OrganizationContext")
		return &orgRepoContext{
			OrgID:    c.Org.Organization.ID,
			Link:     c.Org.OrgLink,
			TmplList: tmplOrgSettingsWebhooks,
			TmplNew:  tmplOrgSettingsWebhookNew,
		}, nil
	}

	return nil, errors.New("unable to determine context")
}

func Webhooks(c *context.Context, orCtx *orgRepoContext) {
	c.Title("repo.settings.hooks")
	c.PageIs("SettingsHooks")
	c.Data["Types"] = conf.Webhook.Types

	var err error
	var ws []*db.Webhook
	if orCtx.RepoID > 0 {
		c.Data["Description"] = c.Tr("repo.settings.hooks_desc", "https://gogs.io/docs/features/webhook.html")
		ws, err = db.GetWebhooksByRepoID(orCtx.RepoID)
	} else {
		c.Data["Description"] = c.Tr("org.settings.hooks_desc")
		ws, err = db.GetWebhooksByOrgID(orCtx.OrgID)
{'fraction_non_alphanumeric': 0.12770470937632583, 'fraction_numerical': 0.005939753924480272, 'mean_word_length': 4.048179871520342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '752416', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1839, 'n_words': 349}