Skip to content

Implement basic global webhooks from config #10545

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ PAGING_NUM = 10
PROXY_URL =
; Comma separated list of host names requiring proxy. Glob patterns (*) are accepted; use ** to match all hosts.
PROXY_HOSTS =
; A list of URLs to POST any events to as webhook type Gitea in JSON format
GLOBAL_WEBHOOKS =
; A secret to be applied to all global webhooks in order to prove the request is legitimate
GLOBAL_SECRET =

[mailer]
ENABLED = false
Expand Down
2 changes: 2 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ set name for unique queues. Individual queues will default to
- `PAGING_NUM`: **10**: Number of webhook history events that are shown in one page.
- `PROXY_URL`: ****: Proxy server URL, support http://, https//, socks://, blank will follow environment http_proxy/https_proxy
- `PROXY_HOSTS`: ****: Comma separated list of host names requiring proxy. Glob patterns (*) are accepted; use ** to match all hosts.
- `GLOBAL_HOOKS`: ****: Comma separated list of URLs to act as a system-wide webhook of type Gitea in JSON mode POSTing for all events. Useful for developing custom behaviour and plugins.
- `GLOBAL_SECRET`: ****: A string secret applied to all global hooks in order to help prove the request is legitimate.

## Mailer (`mailer`)

Expand Down
5 changes: 5 additions & 0 deletions modules/setting/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ var (
ProxyURL string
ProxyURLFixed *url.URL
ProxyHosts []string
GlobalWebhooks []string // A list of URLs to POST any events to as webhook type Gitea
GlobalSecret string
}{
QueueLength: 1000,
DeliverTimeout: 5,
SkipTLSVerify: false,
PagingNum: 10,
ProxyURL: "",
ProxyHosts: []string{},
GlobalWebhooks: []string{},
}
)

Expand All @@ -48,4 +51,6 @@ func newWebhookService() {
}
}
Webhook.ProxyHosts = sec.Key("PROXY_HOSTS").Strings(",")
Webhook.GlobalWebhooks = sec.Key("GLOBAL_WEBHOOKS").Strings(",")
Webhook.GlobalSecret = sec.Key("GLOBAL_SECRET").MustString("")
}
31 changes: 31 additions & 0 deletions modules/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ func PrepareWebhooks(repo *models.Repository, event models.HookEventType, p api.
return nil
}

// getGlobalWebhooks produces webhooks for each URL defined in the config
func getGlobalWebhooks() []*models.Webhook {
if len(setting.Webhook.GlobalWebhooks) == 0 {
return nil
}

var hooks []*models.Webhook
for _, url := range setting.Webhook.GlobalWebhooks {
// Set enough properties for the fake webhook to be delivered.
hook := &models.Webhook{
URL: url,
ContentType: models.ContentTypeJSON,
HookTaskType: models.GITEA,
HookEvent: &models.HookEvent{
SendEverything: true,
BranchFilter: "*",
},
Secret: setting.Webhook.GlobalSecret,
HTTPMethod: "POST",
}
hooks = append(hooks, hook)
}
return hooks
}

func prepareWebhooks(repo *models.Repository, event models.HookEventType, p api.Payloader) error {
ws, err := models.GetActiveWebhooksByRepoID(repo.ID)
if err != nil {
Expand All @@ -181,6 +206,12 @@ func prepareWebhooks(repo *models.Repository, event models.HookEventType, p api.
ws = append(ws, orgHooks...)
}

// Add any config-defined webhooks
globalHooks := getGlobalWebhooks()
if globalHooks != nil {
ws = append(ws, globalHooks...)
}

if len(ws) == 0 {
return nil
}
Expand Down