|
@@ -7,158 +7,106 @@ import (
|
|
|
// ApplyGitHubTemplate applies GitHub-specific template configurations
|
|
// ApplyGitHubTemplate applies GitHub-specific template configurations
|
|
|
// This allows users to use simple template names instead of configuring everything manually
|
|
// This allows users to use simple template names instead of configuring everything manually
|
|
|
func ApplyGitHubTemplate(cfg *config.WebhookConfig, template string) {
|
|
func ApplyGitHubTemplate(cfg *config.WebhookConfig, template string) {
|
|
|
- switch template {
|
|
|
|
|
- case "github-push":
|
|
|
|
|
- applyGitHubPushTemplate(cfg)
|
|
|
|
|
- case "github-pr", "github-pull-request":
|
|
|
|
|
- applyGitHubPRTemplate(cfg)
|
|
|
|
|
- case "github-release":
|
|
|
|
|
- applyGitHubReleaseTemplate(cfg)
|
|
|
|
|
- case "github-workflow":
|
|
|
|
|
- applyGitHubWorkflowTemplate(cfg)
|
|
|
|
|
|
|
+ applier := getTemplateApplier(template)
|
|
|
|
|
+ if applier != nil {
|
|
|
|
|
+ applier(cfg)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func applyGitHubPushTemplate(cfg *config.WebhookConfig) {
|
|
|
|
|
|
|
+type templateApplier func(*config.WebhookConfig)
|
|
|
|
|
+
|
|
|
|
|
+func getTemplateApplier(template string) templateApplier {
|
|
|
|
|
+ templateMap := map[string]templateApplier{
|
|
|
|
|
+ "github-push": applyGitHubPushTemplate,
|
|
|
|
|
+ "github-pr": applyGitHubPRTemplate,
|
|
|
|
|
+ "github-pull-request": applyGitHubPRTemplate,
|
|
|
|
|
+ "github-release": applyGitHubReleaseTemplate,
|
|
|
|
|
+ "github-workflow": applyGitHubWorkflowTemplate,
|
|
|
|
|
+ }
|
|
|
|
|
+ return templateMap[template]
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func setDefaultAuth(cfg *config.WebhookConfig) {
|
|
|
if cfg.AuthHeader == "" {
|
|
if cfg.AuthHeader == "" {
|
|
|
cfg.AuthHeader = "X-Hub-Signature-256"
|
|
cfg.AuthHeader = "X-Hub-Signature-256"
|
|
|
}
|
|
}
|
|
|
if cfg.AuthType == "" {
|
|
if cfg.AuthType == "" {
|
|
|
cfg.AuthType = "hmac-sha256"
|
|
cfg.AuthType = "hmac-sha256"
|
|
|
}
|
|
}
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func ensureMatchHeaders(cfg *config.WebhookConfig) {
|
|
|
if len(cfg.MatchHeaders) == 0 {
|
|
if len(cfg.MatchHeaders) == 0 {
|
|
|
cfg.MatchHeaders = make(map[string]string)
|
|
cfg.MatchHeaders = make(map[string]string)
|
|
|
}
|
|
}
|
|
|
- if _, exists := cfg.MatchHeaders["X-GitHub-Event"]; !exists {
|
|
|
|
|
- cfg.MatchHeaders["X-GitHub-Event"] = "push"
|
|
|
|
|
- }
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func ensureExtract(cfg *config.WebhookConfig) {
|
|
|
if len(cfg.Extract) == 0 {
|
|
if len(cfg.Extract) == 0 {
|
|
|
cfg.Extract = make(map[string]string)
|
|
cfg.Extract = make(map[string]string)
|
|
|
}
|
|
}
|
|
|
- if _, exists := cfg.Extract["git_repository"]; !exists {
|
|
|
|
|
- cfg.Extract["git_repository"] = "$.repository.full_name"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_ref"]; !exists {
|
|
|
|
|
- cfg.Extract["git_ref"] = "$.ref"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_commit"]; !exists {
|
|
|
|
|
- cfg.Extract["git_commit"] = "$.head_commit.id"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_branch"]; !exists {
|
|
|
|
|
- cfg.Extract["git_branch"] = "$.ref"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_message"]; !exists {
|
|
|
|
|
- cfg.Extract["git_message"] = "$.head_commit.message"
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func setExtractIfMissing(cfg *config.WebhookConfig, key, value string) {
|
|
|
|
|
+ if _, exists := cfg.Extract[key]; !exists {
|
|
|
|
|
+ cfg.Extract[key] = value
|
|
|
}
|
|
}
|
|
|
- if _, exists := cfg.Extract["git_author"]; !exists {
|
|
|
|
|
- cfg.Extract["git_author"] = "$.head_commit.author.name"
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func setMatchHeaderIfMissing(cfg *config.WebhookConfig, key, value string) {
|
|
|
|
|
+ if _, exists := cfg.MatchHeaders[key]; !exists {
|
|
|
|
|
+ cfg.MatchHeaders[key] = value
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func applyGitHubPushTemplate(cfg *config.WebhookConfig) {
|
|
|
|
|
+ setDefaultAuth(cfg)
|
|
|
|
|
+ ensureMatchHeaders(cfg)
|
|
|
|
|
+ setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "push")
|
|
|
|
|
+ ensureExtract(cfg)
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_ref", "$.ref")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_commit", "$.head_commit.id")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_branch", "$.ref")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_message", "$.head_commit.message")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_author", "$.head_commit.author.name")
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func applyGitHubPRTemplate(cfg *config.WebhookConfig) {
|
|
func applyGitHubPRTemplate(cfg *config.WebhookConfig) {
|
|
|
- if cfg.AuthHeader == "" {
|
|
|
|
|
- cfg.AuthHeader = "X-Hub-Signature-256"
|
|
|
|
|
- }
|
|
|
|
|
- if cfg.AuthType == "" {
|
|
|
|
|
- cfg.AuthType = "hmac-sha256"
|
|
|
|
|
- }
|
|
|
|
|
- if len(cfg.MatchHeaders) == 0 {
|
|
|
|
|
- cfg.MatchHeaders = make(map[string]string)
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.MatchHeaders["X-GitHub-Event"]; !exists {
|
|
|
|
|
- cfg.MatchHeaders["X-GitHub-Event"] = "pull_request"
|
|
|
|
|
- }
|
|
|
|
|
- if len(cfg.Extract) == 0 {
|
|
|
|
|
- cfg.Extract = make(map[string]string)
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["pr_number"]; !exists {
|
|
|
|
|
- cfg.Extract["pr_number"] = "$.number"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["pr_title"]; !exists {
|
|
|
|
|
- cfg.Extract["pr_title"] = "$.pull_request.title"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["pr_author"]; !exists {
|
|
|
|
|
- cfg.Extract["pr_author"] = "$.pull_request.user.login"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["pr_action"]; !exists {
|
|
|
|
|
- cfg.Extract["pr_action"] = "$.action"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_repository"]; !exists {
|
|
|
|
|
- cfg.Extract["git_repository"] = "$.repository.full_name"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["pr_state"]; !exists {
|
|
|
|
|
- cfg.Extract["pr_state"] = "$.pull_request.state"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["pr_head_sha"]; !exists {
|
|
|
|
|
- cfg.Extract["pr_head_sha"] = "$.pull_request.head.sha"
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ setDefaultAuth(cfg)
|
|
|
|
|
+ ensureMatchHeaders(cfg)
|
|
|
|
|
+ setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "pull_request")
|
|
|
|
|
+ ensureExtract(cfg)
|
|
|
|
|
+ setExtractIfMissing(cfg, "pr_number", "$.number")
|
|
|
|
|
+ setExtractIfMissing(cfg, "pr_title", "$.pull_request.title")
|
|
|
|
|
+ setExtractIfMissing(cfg, "pr_author", "$.pull_request.user.login")
|
|
|
|
|
+ setExtractIfMissing(cfg, "pr_action", "$.action")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
|
|
|
|
|
+ setExtractIfMissing(cfg, "pr_state", "$.pull_request.state")
|
|
|
|
|
+ setExtractIfMissing(cfg, "pr_head_sha", "$.pull_request.head.sha")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func applyGitHubReleaseTemplate(cfg *config.WebhookConfig) {
|
|
func applyGitHubReleaseTemplate(cfg *config.WebhookConfig) {
|
|
|
- if cfg.AuthHeader == "" {
|
|
|
|
|
- cfg.AuthHeader = "X-Hub-Signature-256"
|
|
|
|
|
- }
|
|
|
|
|
- if cfg.AuthType == "" {
|
|
|
|
|
- cfg.AuthType = "hmac-sha256"
|
|
|
|
|
- }
|
|
|
|
|
- if len(cfg.MatchHeaders) == 0 {
|
|
|
|
|
- cfg.MatchHeaders = make(map[string]string)
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.MatchHeaders["X-GitHub-Event"]; !exists {
|
|
|
|
|
- cfg.MatchHeaders["X-GitHub-Event"] = "release"
|
|
|
|
|
- }
|
|
|
|
|
- if len(cfg.Extract) == 0 {
|
|
|
|
|
- cfg.Extract = make(map[string]string)
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["release_action"]; !exists {
|
|
|
|
|
- cfg.Extract["release_action"] = "$.action"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["release_tag"]; !exists {
|
|
|
|
|
- cfg.Extract["release_tag"] = "$.release.tag_name"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["release_name"]; !exists {
|
|
|
|
|
- cfg.Extract["release_name"] = "$.release.name"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_repository"]; !exists {
|
|
|
|
|
- cfg.Extract["git_repository"] = "$.repository.full_name"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["release_author"]; !exists {
|
|
|
|
|
- cfg.Extract["release_author"] = "$.release.author.login"
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ setDefaultAuth(cfg)
|
|
|
|
|
+ ensureMatchHeaders(cfg)
|
|
|
|
|
+ setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "release")
|
|
|
|
|
+ ensureExtract(cfg)
|
|
|
|
|
+ setExtractIfMissing(cfg, "release_action", "$.action")
|
|
|
|
|
+ setExtractIfMissing(cfg, "release_tag", "$.release.tag_name")
|
|
|
|
|
+ setExtractIfMissing(cfg, "release_name", "$.release.name")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
|
|
|
|
|
+ setExtractIfMissing(cfg, "release_author", "$.release.author.login")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func applyGitHubWorkflowTemplate(cfg *config.WebhookConfig) {
|
|
func applyGitHubWorkflowTemplate(cfg *config.WebhookConfig) {
|
|
|
- if cfg.AuthHeader == "" {
|
|
|
|
|
- cfg.AuthHeader = "X-Hub-Signature-256"
|
|
|
|
|
- }
|
|
|
|
|
- if cfg.AuthType == "" {
|
|
|
|
|
- cfg.AuthType = "hmac-sha256"
|
|
|
|
|
- }
|
|
|
|
|
- if len(cfg.MatchHeaders) == 0 {
|
|
|
|
|
- cfg.MatchHeaders = make(map[string]string)
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.MatchHeaders["X-GitHub-Event"]; !exists {
|
|
|
|
|
- cfg.MatchHeaders["X-GitHub-Event"] = "workflow_run"
|
|
|
|
|
- }
|
|
|
|
|
- if len(cfg.Extract) == 0 {
|
|
|
|
|
- cfg.Extract = make(map[string]string)
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["workflow_name"]; !exists {
|
|
|
|
|
- cfg.Extract["workflow_name"] = "$.workflow_run.name"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["workflow_status"]; !exists {
|
|
|
|
|
- cfg.Extract["workflow_status"] = "$.workflow_run.status"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["workflow_conclusion"]; !exists {
|
|
|
|
|
- cfg.Extract["workflow_conclusion"] = "$.workflow_run.conclusion"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_repository"]; !exists {
|
|
|
|
|
- cfg.Extract["git_repository"] = "$.repository.full_name"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_commit"]; !exists {
|
|
|
|
|
- cfg.Extract["git_commit"] = "$.workflow_run.head_sha"
|
|
|
|
|
- }
|
|
|
|
|
- if _, exists := cfg.Extract["git_branch"]; !exists {
|
|
|
|
|
- cfg.Extract["git_branch"] = "$.workflow_run.head_branch"
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ setDefaultAuth(cfg)
|
|
|
|
|
+ ensureMatchHeaders(cfg)
|
|
|
|
|
+ setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "workflow_run")
|
|
|
|
|
+ ensureExtract(cfg)
|
|
|
|
|
+ setExtractIfMissing(cfg, "workflow_name", "$.workflow_run.name")
|
|
|
|
|
+ setExtractIfMissing(cfg, "workflow_status", "$.workflow_run.status")
|
|
|
|
|
+ setExtractIfMissing(cfg, "workflow_conclusion", "$.workflow_run.conclusion")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_commit", "$.workflow_run.head_sha")
|
|
|
|
|
+ setExtractIfMissing(cfg, "git_branch", "$.workflow_run.head_branch")
|
|
|
}
|
|
}
|