4
0

github.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package webhooks
  2. import (
  3. "github.com/OliveTin/OliveTin/internal/config"
  4. )
  5. // ApplyGitHubTemplate applies GitHub-specific template configurations
  6. // This allows users to use simple template names instead of configuring everything manually
  7. func ApplyGitHubTemplate(cfg *config.WebhookConfig, template string) {
  8. applier := getTemplateApplier(template)
  9. if applier != nil {
  10. applier(cfg)
  11. }
  12. }
  13. type templateApplier func(*config.WebhookConfig)
  14. func getTemplateApplier(template string) templateApplier {
  15. templateMap := map[string]templateApplier{
  16. "github-push": applyGitHubPushTemplate,
  17. "github-pr": applyGitHubPRTemplate,
  18. "github-pull-request": applyGitHubPRTemplate,
  19. "github-release": applyGitHubReleaseTemplate,
  20. "github-workflow": applyGitHubWorkflowTemplate,
  21. }
  22. return templateMap[template]
  23. }
  24. func setDefaultAuth(cfg *config.WebhookConfig) {
  25. if cfg.AuthHeader == "" {
  26. cfg.AuthHeader = "X-Hub-Signature-256"
  27. }
  28. if cfg.AuthType == "" {
  29. cfg.AuthType = "hmac-sha256"
  30. }
  31. }
  32. func ensureMatchHeaders(cfg *config.WebhookConfig) {
  33. if len(cfg.MatchHeaders) == 0 {
  34. cfg.MatchHeaders = make(map[string]string)
  35. }
  36. }
  37. func ensureExtract(cfg *config.WebhookConfig) {
  38. if len(cfg.Extract) == 0 {
  39. cfg.Extract = make(map[string]string)
  40. }
  41. }
  42. func setExtractIfMissing(cfg *config.WebhookConfig, key, value string) {
  43. if _, exists := cfg.Extract[key]; !exists {
  44. cfg.Extract[key] = value
  45. }
  46. }
  47. func setMatchHeaderIfMissing(cfg *config.WebhookConfig, key, value string) {
  48. if _, exists := cfg.MatchHeaders[key]; !exists {
  49. cfg.MatchHeaders[key] = value
  50. }
  51. }
  52. func applyGitHubPushTemplate(cfg *config.WebhookConfig) {
  53. setDefaultAuth(cfg)
  54. ensureMatchHeaders(cfg)
  55. setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "push")
  56. ensureExtract(cfg)
  57. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  58. setExtractIfMissing(cfg, "git_ref", "$.ref")
  59. setExtractIfMissing(cfg, "git_commit", "$.head_commit.id")
  60. setExtractIfMissing(cfg, "git_branch", "$.ref")
  61. setExtractIfMissing(cfg, "git_message", "$.head_commit.message")
  62. setExtractIfMissing(cfg, "git_author", "$.head_commit.author.name")
  63. }
  64. func applyGitHubPRTemplate(cfg *config.WebhookConfig) {
  65. setDefaultAuth(cfg)
  66. ensureMatchHeaders(cfg)
  67. setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "pull_request")
  68. ensureExtract(cfg)
  69. setExtractIfMissing(cfg, "pr_number", "$.number")
  70. setExtractIfMissing(cfg, "pr_title", "$.pull_request.title")
  71. setExtractIfMissing(cfg, "pr_author", "$.pull_request.user.login")
  72. setExtractIfMissing(cfg, "pr_action", "$.action")
  73. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  74. setExtractIfMissing(cfg, "pr_state", "$.pull_request.state")
  75. setExtractIfMissing(cfg, "pr_head_sha", "$.pull_request.head.sha")
  76. }
  77. func applyGitHubReleaseTemplate(cfg *config.WebhookConfig) {
  78. setDefaultAuth(cfg)
  79. ensureMatchHeaders(cfg)
  80. setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "release")
  81. ensureExtract(cfg)
  82. setExtractIfMissing(cfg, "release_action", "$.action")
  83. setExtractIfMissing(cfg, "release_tag", "$.release.tag_name")
  84. setExtractIfMissing(cfg, "release_name", "$.release.name")
  85. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  86. setExtractIfMissing(cfg, "release_author", "$.release.author.login")
  87. }
  88. func applyGitHubWorkflowTemplate(cfg *config.WebhookConfig) {
  89. setDefaultAuth(cfg)
  90. ensureMatchHeaders(cfg)
  91. setMatchHeaderIfMissing(cfg, "X-GitHub-Event", "workflow_run")
  92. ensureExtract(cfg)
  93. setExtractIfMissing(cfg, "workflow_name", "$.workflow_run.name")
  94. setExtractIfMissing(cfg, "workflow_status", "$.workflow_run.status")
  95. setExtractIfMissing(cfg, "workflow_conclusion", "$.workflow_run.conclusion")
  96. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  97. setExtractIfMissing(cfg, "git_commit", "$.workflow_run.head_sha")
  98. setExtractIfMissing(cfg, "git_branch", "$.workflow_run.head_branch")
  99. }