From 63c64c7eae4c255601ba2022ee0de8f00760af97 Mon Sep 17 00:00:00 2001 From: noah Date: Sat, 26 Feb 2022 14:19:14 +0900 Subject: [PATCH] Fix to build the webhook URL with the proxy host --- internal/interactor/interactor.go | 10 ++++++++- internal/interactor/interactor_test.go | 31 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 internal/interactor/interactor_test.go diff --git a/internal/interactor/interactor.go b/internal/interactor/interactor.go index 4766f0ef..bc3d7d8a 100644 --- a/internal/interactor/interactor.go +++ b/internal/interactor/interactor.go @@ -83,7 +83,7 @@ func NewInteractor(c *InteractorConfig) *Interactor { i.LockInteractor = (*LockInteractor)(i.common) i.RepoInteractor = &RepoInteractor{ service: i.common, - WebhookURL: fmt.Sprintf("%s://%s/hooks", c.ServerProxyProto, c.ServerProxyHost), + WebhookURL: c.BuildWebhookURL(), WebhookSSL: c.ServerProxyProto == "https", WebhookSecret: c.WebhookSecret, } @@ -112,3 +112,11 @@ func NewInteractor(c *InteractorConfig) *Interactor { return i } + +func (c *InteractorConfig) BuildWebhookURL() string { + if c.ServerProxyProto != "" && c.ServerProxyHost != "" { + return fmt.Sprintf("%s://%s/hooks", c.ServerProxyProto, c.ServerProxyHost) + } + + return fmt.Sprintf("%s://%s/hooks", c.ServerProto, c.ServerHost) +} diff --git a/internal/interactor/interactor_test.go b/internal/interactor/interactor_test.go new file mode 100644 index 00000000..114947df --- /dev/null +++ b/internal/interactor/interactor_test.go @@ -0,0 +1,31 @@ +package interactor_test + +import ( + "testing" + + i "github.com/gitploy-io/gitploy/internal/interactor" +) + +func TestInteractorConfig_BuildWebhookURL(t *testing.T) { + t.Run("Return the webhook URL built with the proxy host.", func(t *testing.T) { + c := &i.InteractorConfig{ + ServerProxyHost: "hook.cloud.gitploy.io", + ServerProxyProto: "https", + } + wanted := "https://hook.cloud.gitploy.io/hooks" + if ret := c.BuildWebhookURL(); ret != wanted { + t.Fatalf("BuildWebhookURL = %v, wanted %v", ret, wanted) + } + }) + + t.Run("Return the webhook URL built with the server host.", func(t *testing.T) { + c := &i.InteractorConfig{ + ServerHost: "cloud.gitploy.io", + ServerProto: "https", + } + wanted := "https://cloud.gitploy.io/hooks" + if ret := c.BuildWebhookURL(); ret != wanted { + t.Fatalf("BuildWebhookURL = %v, wanted %v", ret, wanted) + } + }) +}