Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: enhance the checking of the returned status code when creating the webhook (#2285)

h3n4le9226f76501f
diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.go
index e5bad04594..3ad5965ea8 100644
--- a/plugin/vcs/github/github.go
+++ b/plugin/vcs/github/github.go
@@ -800,13 +800,16 @@ func (p *Provider) CreateWebhook(ctx context.Context, oauthCtx common.OauthConte
if code == http.StatusNotFound {
return "", common.Errorf(common.NotFound, "failed to create webhook through URL %s", url)
- } else if code >= 300 {
- return "",
- fmt.Errorf("failed to create webhook through URL %s, status code: %d, body: %s",
- url,
- code,
- body,
- )
+ }
+
+ // GitHub returns 201 HTTP status codes upon successful webhook creation,
+ // see https://docs.github.com/en/rest/webhooks/repos#create-a-repository-webhook for details.
+ if code != http.StatusCreated {
+ return "", fmt.Errorf("failed to create webhook through URL %s, status code: %d, body: %s",
+ url,
+ code,
+ body,
+ )
}
var webhookInfo WebhookInfo
diff --git a/plugin/vcs/github/github_test.go b/plugin/vcs/github/github_test.go
index b944d2fcfa..8a387058b0 100644
--- a/plugin/vcs/github/github_test.go
+++ b/plugin/vcs/github/github_test.go
@@ -999,7 +999,7 @@ func TestProvider_CreateWebhook(t *testing.T) {
MockRoundTrip: func(r *http.Request) (*http.Response, error) {
assert.Equal(t, "/repos/1/hooks", r.URL.Path)
return &http.Response{
- StatusCode: http.StatusOK,
+ StatusCode: http.StatusCreated,
// Example response taken from https://docs.github.com/en/rest/webhooks/repos#create-a-repository-webhook
Body: io.NopCloser(strings.NewReader(`
{
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index 3972f4160c..88f2ca68b6 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -797,13 +797,15 @@ func (p *Provider) CreateWebhook(ctx context.Context, oauthCtx common.OauthConte
if code == http.StatusNotFound {
return "", common.Errorf(common.NotFound, "failed to create webhook through URL %s", url)
- } else if code >= 300 {
+ }
+ // GitLab returns 201 HTTP status codes upon successful webhook creation,
+ // see https://docs.gitlab.com/ee/api/#status-codes for details.
+ if code != http.StatusCreated {
reason := fmt.Sprintf("failed to create webhook through URL %s, status code: %d, body: %s",
url,
code,
body,
)
-
// Add helper tips if the status code is 422, refer to https://github.com/bytebase/bytebase/issues/101 for more context.
if code == http.StatusUnprocessableEntity {
reason += ".\n\nIf GitLab and Bytebase are in the same private network, " +
diff --git a/plugin/vcs/gitlab/gitlab_test.go b/plugin/vcs/gitlab/gitlab_test.go
index b607365d33..fd0cff0c94 100644
--- a/plugin/vcs/gitlab/gitlab_test.go
+++ b/plugin/vcs/gitlab/gitlab_test.go
@@ -673,7 +673,7 @@ func TestProvider_CreateWebhook(t *testing.T) {
MockRoundTrip: func(r *http.Request) (*http.Response, error) {
assert.Equal(t, "/api/v4/projects/1/hooks", r.URL.Path)
return &http.Response{
- StatusCode: http.StatusOK,
+ StatusCode: http.StatusCreated,
// Example response taken from https://docs.gitlab.com/ee/api/projects.html#get-project-hook
Body: io.NopCloser(strings.NewReader(`
{
diff --git a/tests/fake/github.go b/tests/fake/github.go
index 9bf3dff16a..e7eeec3b4b 100644
--- a/tests/fake/github.go
+++ b/tests/fake/github.go
@@ -86,7 +86,7 @@ func (gh *GitHub) createRepositoryWebhook(c echo.Context) error {
return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to marshal response body for creating repository webhook: %v", err))
}
gh.nextWebhookID++
- return c.String(http.StatusOK, string(buf))
+ return c.String(http.StatusCreated, string(buf))
}
func (gh *GitHub) getRepositoryCommit(c echo.Context) error {