Skip to content

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

Change to store webhook_endpoint_id instead of the whole webhook url

天舟0fb4c97da836
diff --git a/backend/api/repository.go b/backend/api/repository.go
index dcc34ced8e..c4b64f9c0f 100644
--- a/backend/api/repository.go
+++ b/backend/api/repository.go
@@ -23,15 +23,15 @@ type Repository struct {
Project *Project `jsonapi:"relation,project"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- FullPath string `jsonapi:"attr,fullPath"`
- WebURL string `jsonapi:"attr,webURL"`
- BaseDirectory string `jsonapi:"attr,baseDirectory"`
- BranchFilter string `jsonapi:"attr,branchFilter"`
- ExternalId string `jsonapi:"attr,externalId"`
- WebhookId string
- WebhookURL string
- SecretToken string
+ Name string `jsonapi:"attr,name"`
+ FullPath string `jsonapi:"attr,fullPath"`
+ WebURL string `jsonapi:"attr,webURL"`
+ BaseDirectory string `jsonapi:"attr,baseDirectory"`
+ BranchFilter string `jsonapi:"attr,branchFilter"`
+ ExternalId string `jsonapi:"attr,externalId"`
+ ExternalWebhookId string
+ WebhookEndpointId string
+ SecretToken string
}
type RepositoryCreate struct {
@@ -44,15 +44,15 @@ type RepositoryCreate struct {
ProjectId int `jsonapi:"attr,projectId"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- FullPath string `jsonapi:"attr,fullPath"`
- WebURL string `jsonapi:"attr,webURL"`
- BaseDirectory string `jsonapi:"attr,baseDirectory"`
- BranchFilter string `jsonapi:"attr,branchFilter"`
- ExternalId string `jsonapi:"attr,externalId"`
- WebhookId string `jsonapi:"attr,webhookId"`
- WebhookURL string `jsonapi:"attr,webhookURL"`
- SecretToken string `jsonapi:"attr,secretToken"`
+ Name string `jsonapi:"attr,name"`
+ FullPath string `jsonapi:"attr,fullPath"`
+ WebURL string `jsonapi:"attr,webURL"`
+ BaseDirectory string `jsonapi:"attr,baseDirectory"`
+ BranchFilter string `jsonapi:"attr,branchFilter"`
+ ExternalId string `jsonapi:"attr,externalId"`
+ ExternalWebhookId string `jsonapi:"attr,externalWebhookId"`
+ WebhookEndpointId string `jsonapi:"attr,webhookEndpointId"`
+ SecretToken string `jsonapi:"attr,secretToken"`
}
type RepositoryFind struct {
diff --git a/backend/server/project.go b/backend/server/project.go
index 97529ab7fc..2c42303b88 100644
--- a/backend/server/project.go
+++ b/backend/server/project.go
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
+ "os"
"strconv"
"github.com/bytebase/bytebase"
@@ -238,17 +239,17 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
switch vcs.Type {
case "GITLAB_SELF_HOST":
webhookPut := gitlab.WebhookPut{
- URL: updatedRepository.WebhookURL,
+ URL: fmt.Sprintf("%s/%s/%s", os.Getenv("HOSTNAME"), gitLabWebhookPath, updatedRepository.WebhookEndpointId),
PushEventsBranchFilter: *repositoryPatch.BranchFilter,
}
json, err := json.Marshal(webhookPut)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal put request for updating webhook %s for project ID: %v", repository.WebhookId, projectId)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal put request for updating webhook %s for project ID: %v", repository.ExternalWebhookId, projectId)).SetInternal(err)
}
- resourcePath := fmt.Sprintf("/projects/%s/hooks/%s", repository.ExternalId, repository.WebhookId)
+ resourcePath := fmt.Sprintf("projects/%s/hooks/%s", repository.ExternalId, repository.ExternalWebhookId)
resp, err := gitlab.PUT(vcs.InstanceURL, resourcePath, vcs.AccessToken, bytes.NewBuffer(json))
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update webhook ID %s for project ID: %v", repository.WebhookId, projectId)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update webhook ID %s for project ID: %v", repository.ExternalWebhookId, projectId)).SetInternal(err)
}
// Just emits a warning since we have already updated the repository entry. We will have a separate process to reconcile the state.
@@ -320,9 +321,9 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
// If we delete it before we delete the repository, then if the repository deletion fails, we will have a broken repository with no webhook.
switch vcs.Type {
case "GITLAB_SELF_HOST":
- resp, err := gitlab.DELETE(vcs.InstanceURL, fmt.Sprintf("/projects/%s/hooks/%s", repository.ExternalId, repository.WebhookId), vcs.AccessToken)
+ resp, err := gitlab.DELETE(vcs.InstanceURL, fmt.Sprintf("projects/%s/hooks/%s", repository.ExternalId, repository.ExternalWebhookId), vcs.AccessToken)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete webhook ID %s for project ID: %v", repository.WebhookId, projectId)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete webhook ID %s for project ID: %v", repository.ExternalWebhookId, projectId)).SetInternal(err)
}
// Just emits a warning since we have already removed the repository entry. We will have a separate process to cleanup the orphaned webhook.
@@ -332,7 +333,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
zap.Int("project_id", projectId),
zap.Int("repository_id", repository.ID),
zap.String("gitlab_project_id", repository.ExternalId),
- zap.String("gitlab_webhook_id", repository.WebhookId))
+ zap.String("gitlab_webhook_id", repository.ExternalWebhookId))
}
}
diff --git a/backend/server/server.go b/backend/server/server.go
index bcba39da1e..b5592ed20e 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -7,6 +7,7 @@ import (
"fmt"
"io/fs"
"net/http"
+ "os"
"strings"
"time"
@@ -181,6 +182,8 @@ func (server *Server) Run() error {
const port int = 8080
// Sleep for 1 sec to make sure port is released between runs.
time.Sleep(time.Duration(1) * time.Second)
+
+ os.Setenv("HOSTNAME", fmt.Sprintf("http://localhost:%d", port))
return server.e.Start(fmt.Sprintf(":%d", port))
}
diff --git a/backend/server/webhook.go b/backend/server/webhook.go
index 3c6a111e0a..096d6db241 100644
--- a/backend/server/webhook.go
+++ b/backend/server/webhook.go
@@ -8,6 +8,10 @@ import (
"github.com/labstack/echo/v4"
)
+var (
+ gitLabWebhookPath = "hook/gitlab"
+)
+
func (s *Server) registerWebhookRoutes(g *echo.Group) {
g.POST("/gitlab/:uuid", func(c echo.Context) error {
return c.String(http.StatusOK, fmt.Sprintf("[%v] Pong from backend", time.Now().Format("2006-01-02 15:04:05")))
diff --git a/backend/store/migration/0001_init_schema.up.sql b/backend/store/migration/0001_init_schema.up.sql
index 7bd9853977..91dc9c4b71 100644
--- a/backend/store/migration/0001_init_schema.up.sql
+++ b/backend/store/migration/0001_init_schema.up.sql
@@ -718,15 +718,13 @@ CREATE TABLE repo (
external_id TEXT NOT NULL,
-- Push webhook id from the corresponding VCS provider.
-- For GitLab, this is the project webhook id. e.g. 123
- webhook_id TEXT NOT NULL,
- -- Webhook URL, we use this to identify the target repository receiving the webhook event
- webhook_url TEXT NOT NULL UNIQUE,
+ external_webhook_id TEXT NOT NULL,
+ -- Identify the target repository receiving the webhook event. This is a random string, and we don't store the full URL because the backend may change host.
+ webhook_endpoint_id TEXT NOT NULL UNIQUE,
-- For GitLab, webhook request contains this in the 'X-Gitlab-Token" header and we can compare it with the one stored in db to validate it sends to the expected endpoint.
secret_token TEXT NOT NULL
);
-CREATE INDEX idx_repo_webhook_url ON repo(webhook_url);
-
INSERT INTO
sqlite_sequence (name, seq)
VALUES
diff --git a/backend/store/repository.go b/backend/store/repository.go
index 76c9880dbb..f847334a25 100644
--- a/backend/store/repository.go
+++ b/backend/store/repository.go
@@ -152,12 +152,12 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
base_directory,
branch_filter,
external_id,
- webhook_id,
- webhook_url,
+ external_webhook_id,
+ webhook_endpoint_id,
secret_token
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, webhook_id, webhook_url, secret_token
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, external_webhook_id, webhook_endpoint_id, secret_token
`,
create.CreatorId,
create.CreatorId,
@@ -169,8 +169,8 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
create.BaseDirectory,
create.BranchFilter,
create.ExternalId,
- create.WebhookId,
- create.WebhookURL,
+ create.ExternalWebhookId,
+ create.WebhookEndpointId,
create.SecretToken,
)
@@ -195,8 +195,8 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
&repository.BaseDirectory,
&repository.BranchFilter,
&repository.ExternalId,
- &repository.WebhookId,
- &repository.WebhookURL,
+ &repository.ExternalWebhookId,
+ &repository.WebhookEndpointId,
&repository.SecretToken,
); err != nil {
return nil, FormatError(err)
@@ -233,8 +233,8 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
base_directory,
branch_filter,
external_id,
- webhook_id,
- webhook_url,
+ external_webhook_id,
+ webhook_endpoint_id,
secret_token
FROM repo
WHERE `+strings.Join(where, " AND "),
@@ -263,8 +263,8 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
&repository.BaseDirectory,
&repository.BranchFilter,
&repository.ExternalId,
- &repository.WebhookId,
- &repository.WebhookURL,
+ &repository.ExternalWebhookId,
+ &repository.WebhookEndpointId,
&repository.SecretToken,
); err != nil {
return nil, FormatError(err)
@@ -297,7 +297,7 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
UPDATE repo
SET `+strings.Join(set, ", ")+`
WHERE id = ?
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, webhook_id, webhook_url, secret_token
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, external_webhook_id, webhook_endpoint_id, secret_token
`,
args...,
)
@@ -322,8 +322,8 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
&repository.BaseDirectory,
&repository.BranchFilter,
&repository.ExternalId,
- &repository.WebhookId,
- &repository.WebhookURL,
+ &repository.ExternalWebhookId,
+ &repository.WebhookEndpointId,
&repository.SecretToken,
); err != nil {
return nil, FormatError(err)
diff --git a/backend/store/seed/0170_repo.sql b/backend/store/seed/0170_repo.sql
index f2fcb72ca2..7e8d972897 100644
--- a/backend/store/seed/0170_repo.sql
+++ b/backend/store/seed/0170_repo.sql
@@ -11,8 +11,8 @@ INSERT INTO
base_directory,
branch_filter,
external_id,
- webhook_id,
- webhook_url,
+ external_webhook_id,
+ webhook_endpoint_id,
secret_token
)
VALUES
@@ -31,6 +31,6 @@ VALUES
"7",
-- Refers to the webhook in bytebase-test/project1
"5",
- "http://gitlab.bytebase.com/hook/gitlab/8368dde4-e352-4696-9fef-26bd3af6ec40",
+ "8368dde4-e352-4696-9fef-26bd3af6ec40",
"VFN2lgKDRLWjJ25B"
);
\ No newline at end of file
diff --git a/frontend/src/components/RepositorySetupWizard.vue b/frontend/src/components/RepositorySetupWizard.vue
index a2253df09e..c383ebceff 100644
--- a/frontend/src/components/RepositorySetupWizard.vue
+++ b/frontend/src/components/RepositorySetupWizard.vue
@@ -26,6 +26,7 @@ import { computed, PropType } from "@vue/runtime-core";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import isEmpty from "lodash-es/isEmpty";
+import { v4 as uuidv4 } from "uuid";
import { BBStepTabItem } from "../bbkit/types";
import RepositoryVCSProviderPanel from "./RepositoryVCSProviderPanel.vue";
import RepositorySelectionPanel from "./RepositorySelectionPanel.vue";
@@ -119,10 +120,12 @@ export default {
const tryFinishSetup = (allowFinishCallback: () => void) => {
// The secret token to validate the gitlab webhook sender.
const secretToken = randomString(16);
+ const webhookEndpointId = uuidv4();
const createFunc = () => {
store
.dispatch("gitlab/createWebhook", {
vcs: state.config.vcs,
+ webhookEndpointId,
projectId: state.config.repositoryInfo.externalId,
branchFilter: state.config.repositoryConfig.branchFilter,
secretToken,
@@ -138,8 +141,8 @@ export default {
baseDirectory: state.config.repositoryConfig.baseDirectory,
branchFilter: state.config.repositoryConfig.branchFilter,
externalId: state.config.repositoryInfo.externalId,
- webhookId: webhook.id,
- webhookURL: webhook.url,
+ externalWebhookId: webhook.id,
+ webhookEndpointId,
secretToken,
};
store
diff --git a/frontend/src/store/modules/gitlab.ts b/frontend/src/store/modules/gitlab.ts
index f1712f4461..7686a062a6 100644
--- a/frontend/src/store/modules/gitlab.ts
+++ b/frontend/src/store/modules/gitlab.ts
@@ -1,5 +1,4 @@
import axios from "axios";
-import { v4 as uuidv4 } from "uuid";
import {
ExternalRepositoryInfo,
VCS,
@@ -83,6 +82,7 @@ const actions = {
{}: any,
{
vcs,
+ webhookEndpointId,
projectId,
branchFilter,
secretToken,
@@ -91,11 +91,12 @@ const actions = {
vcs: VCS;
projectId: string;
branchFilter: string;
+ webhookEndpointId: string;
secretToken: string;
accessToken: string;
}
): Promise<WebhookInfo> {
- const url = `${backendURL()}/${GITLAB_WEBHOOK_PATH}/${uuidv4()}`;
+ const url = `${backendURL()}/${GITLAB_WEBHOOK_PATH}/${webhookEndpointId}`;
const data = (
await axios.post(
`${vcs.instanceURL}/${GITLAB_API_PATH}/projects/${projectId}/hooks`,
@@ -123,7 +124,6 @@ const actions = {
return {
id: data.id.toString(),
- url,
};
},
};
diff --git a/frontend/src/types/repository.ts b/frontend/src/types/repository.ts
index a20f3e8124..6ec323844d 100644
--- a/frontend/src/types/repository.ts
+++ b/frontend/src/types/repository.ts
@@ -42,8 +42,8 @@ export type RepositoryCreate = {
baseDirectory: string;
branchFilter: string;
externalId: string;
- webhookId: string;
- webhookURL: string;
+ externalWebhookId: string;
+ webhookEndpointId: string;
secretToken: string;
};
@@ -70,7 +70,6 @@ export type ExternalRepositoryInfo = {
export type WebhookInfo = {
id: string;
- url: string;
};
export function baseDirectoryWebURL(repository: Repository): string {