4
0

github.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // The names here must match config.WebhookTemplateNames, which the config
  15. // check uses to decide whether a webhook needs its own authType.
  16. func templateAppliers() map[string]templateApplier {
  17. return map[string]templateApplier{
  18. "github-push": applyGitHubPushTemplate,
  19. "github-pr": applyGitHubPRTemplate,
  20. "github-pull-request": applyGitHubPRTemplate,
  21. "github-release": applyGitHubReleaseTemplate,
  22. "github-workflow": applyGitHubWorkflowTemplate,
  23. }
  24. }
  25. func getTemplateApplier(template string) templateApplier {
  26. return templateAppliers()[template]
  27. }
  28. func setDefaultAuth(cfg *config.WebhookConfig) {
  29. if cfg.AuthHeader == "" {
  30. cfg.AuthHeader = "X-Hub-Signature-256"
  31. }
  32. if cfg.AuthType == "" {
  33. cfg.AuthType = "hmac-sha256"
  34. }
  35. }
  36. func ensureMatchHeaders(cfg *config.WebhookConfig) {
  37. if len(cfg.MatchHeaders) == 0 {
  38. cfg.MatchHeaders = make(map[string]string)
  39. }
  40. }
  41. func ensureExtract(cfg *config.WebhookConfig) {
  42. if len(cfg.Extract) == 0 {
  43. cfg.Extract = make(map[string]string)
  44. }
  45. }
  46. func setExtractIfMissing(cfg *config.WebhookConfig, key, value string) {
  47. if _, exists := cfg.Extract[key]; !exists {
  48. cfg.Extract[key] = value
  49. }
  50. }
  51. func setGitHubEventHeaderIfMissing(cfg *config.WebhookConfig, event string) {
  52. if _, exists := cfg.MatchHeaders["X-GitHub-Event"]; !exists {
  53. cfg.MatchHeaders["X-GitHub-Event"] = event
  54. }
  55. }
  56. func applyGitHubPushTemplate(cfg *config.WebhookConfig) {
  57. setDefaultAuth(cfg)
  58. ensureMatchHeaders(cfg)
  59. setGitHubEventHeaderIfMissing(cfg, "push")
  60. ensureExtract(cfg)
  61. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  62. setExtractIfMissing(cfg, "git_ref", "$.ref")
  63. setExtractIfMissing(cfg, "git_commit", "$.head_commit.id")
  64. setExtractIfMissing(cfg, "git_branch", "$.ref")
  65. setExtractIfMissing(cfg, "git_message", "$.head_commit.message")
  66. setExtractIfMissing(cfg, "git_author", "$.head_commit.author.name")
  67. }
  68. func applyGitHubPRTemplate(cfg *config.WebhookConfig) {
  69. setDefaultAuth(cfg)
  70. ensureMatchHeaders(cfg)
  71. setGitHubEventHeaderIfMissing(cfg, "pull_request")
  72. ensureExtract(cfg)
  73. setExtractIfMissing(cfg, "pr_number", "$.number")
  74. setExtractIfMissing(cfg, "pr_title", "$.pull_request.title")
  75. setExtractIfMissing(cfg, "pr_author", "$.pull_request.user.login")
  76. setExtractIfMissing(cfg, "pr_action", "$.action")
  77. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  78. setExtractIfMissing(cfg, "pr_state", "$.pull_request.state")
  79. setExtractIfMissing(cfg, "pr_head_sha", "$.pull_request.head.sha")
  80. }
  81. func applyGitHubReleaseTemplate(cfg *config.WebhookConfig) {
  82. setDefaultAuth(cfg)
  83. ensureMatchHeaders(cfg)
  84. setGitHubEventHeaderIfMissing(cfg, "release")
  85. ensureExtract(cfg)
  86. setExtractIfMissing(cfg, "release_action", "$.action")
  87. setExtractIfMissing(cfg, "release_tag", "$.release.tag_name")
  88. setExtractIfMissing(cfg, "release_name", "$.release.name")
  89. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  90. setExtractIfMissing(cfg, "release_author", "$.release.author.login")
  91. }
  92. func applyGitHubWorkflowTemplate(cfg *config.WebhookConfig) {
  93. setDefaultAuth(cfg)
  94. ensureMatchHeaders(cfg)
  95. setGitHubEventHeaderIfMissing(cfg, "workflow_run")
  96. ensureExtract(cfg)
  97. setExtractIfMissing(cfg, "workflow_name", "$.workflow_run.name")
  98. setExtractIfMissing(cfg, "workflow_status", "$.workflow_run.status")
  99. setExtractIfMissing(cfg, "workflow_conclusion", "$.workflow_run.conclusion")
  100. setExtractIfMissing(cfg, "git_repository", "$.repository.full_name")
  101. setExtractIfMissing(cfg, "git_commit", "$.workflow_run.head_sha")
  102. setExtractIfMissing(cfg, "git_branch", "$.workflow_run.head_branch")
  103. }