Skip to content

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

Append the webhook url with a uuid and store the url in the db. The webhook url will be used to identify the linked repo/project.

天舟2080632c6446
diff --git a/backend/api/repository.go b/backend/api/repository.go
index 6952fe0d98..906a927d0a 100644
--- a/backend/api/repository.go
+++ b/backend/api/repository.go
@@ -30,10 +30,7 @@ type Repository struct {
BranchFilter string `jsonapi:"attr,branchFilter"`
ExternalId string `jsonapi:"attr,externalId"`
WebhookId string
- // Because we normalize the data, thus we fetch below info from vcs table.
- VCSName string `jsonapi:"attr,vcsName"`
- VCSType VCSType `jsonapi:"attr,vcsType"`
- InstanceURL string `jsonapi:"attr,instanceURL"`
+ WebhookURL string
}
type RepositoryCreate struct {
@@ -53,6 +50,7 @@ type RepositoryCreate struct {
BranchFilter string `jsonapi:"attr,branchFilter"`
ExternalId string `jsonapi:"attr,externalId"`
WebhookId string `jsonapi:"attr,webhookId"`
+ WebhookURL string `jsonapi:"attr,webhookURL"`
}
type RepositoryFind struct {
diff --git a/backend/store/migration/0001_init_schema.up.sql b/backend/store/migration/0001_init_schema.up.sql
index 8f3c5e07b6..dab3ade87e 100644
--- a/backend/store/migration/0001_init_schema.up.sql
+++ b/backend/store/migration/0001_init_schema.up.sql
@@ -713,9 +713,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_id TEXT NOT NULL,
+ -- Webhook URL, we use this to identify the target repository receiving the webhook event
+ webhook_url TEXT NOT NULL UNIQUE
);
+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 29994b26e1..d1c39fee0e 100644
--- a/backend/store/repository.go
+++ b/backend/store/repository.go
@@ -152,10 +152,11 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
base_directory,
branch_filter,
external_id,
- webhook_id
+ webhook_id,
+ webhook_url
)
- 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
+ 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
`,
create.CreatorId,
create.CreatorId,
@@ -168,6 +169,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
create.BranchFilter,
create.ExternalId,
create.WebhookId,
+ create.WebhookURL,
)
if err != nil {
@@ -192,6 +194,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
&repository.BranchFilter,
&repository.ExternalId,
&repository.WebhookId,
+ &repository.WebhookURL,
); err != nil {
return nil, FormatError(err)
}
@@ -227,7 +230,8 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
base_directory,
branch_filter,
external_id,
- webhook_id
+ webhook_id,
+ webhook_url
FROM repo
WHERE `+strings.Join(where, " AND "),
args...,
@@ -256,6 +260,7 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
&repository.BranchFilter,
&repository.ExternalId,
&repository.WebhookId,
+ &repository.WebhookURL,
); err != nil {
return nil, FormatError(err)
}
@@ -287,7 +292,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
+ 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
`,
args...,
)
@@ -313,6 +318,7 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
&repository.BranchFilter,
&repository.ExternalId,
&repository.WebhookId,
+ &repository.WebhookURL,
); err != nil {
return nil, FormatError(err)
}
diff --git a/backend/store/seed/0170_repo.sql b/backend/store/seed/0170_repo.sql
index d00116b529..dd73fb117e 100644
--- a/backend/store/seed/0170_repo.sql
+++ b/backend/store/seed/0170_repo.sql
@@ -8,8 +8,11 @@ INSERT INTO
name,
full_path,
web_url,
+ base_directory,
+ branch_filter,
external_id,
- webhook_id
+ webhook_id,
+ webhook_url
)
VALUES
(
@@ -21,8 +24,11 @@ VALUES
"project1",
"bytebase-test/project1",
"http://gitlab.bytebase.com/bytebase-test/project1",
+ "bytebase",
+ "feature/*",
-- Refers to the bytebase-test/project1
"7",
-- Refers to the webhook in bytebase-test/project1
- "5"
+ "5",
+ "http://gitlab.bytebase.com/hook/gitlab/8368dde4-e352-4696-9fef-26bd3af6ec40"
);
\ No newline at end of file
diff --git a/frontend/src/components/RepositorySelectionPanel.vue b/frontend/src/components/RepositorySelectionPanel.vue
index e39fe6a586..2b18613dd1 100644
--- a/frontend/src/components/RepositorySelectionPanel.vue
+++ b/frontend/src/components/RepositorySelectionPanel.vue
@@ -77,7 +77,7 @@ export default {
store
.dispatch("gitlab/fetchProjectList", {
vcs: props.config.vcs,
- token: props.config.accessToken,
+ token: props.config.token.accessToken,
})
.then((list) => {
state.repositoryList = list;
diff --git a/frontend/src/components/RepositorySetupWizard.vue b/frontend/src/components/RepositorySetupWizard.vue
index e9d0082d48..5ab42ffb43 100644
--- a/frontend/src/components/RepositorySetupWizard.vue
+++ b/frontend/src/components/RepositorySetupWizard.vue
@@ -36,6 +36,7 @@ import {
RepositoryCreate,
unknown,
VCS,
+ WebhookInfo,
} from "../types";
import { projectSlug } from "../utils";
@@ -75,7 +76,11 @@ export default {
config: {
vcs: unknown("VCS") as VCS,
code: "",
- accessToken: "",
+ token: {
+ accessToken: "",
+ expiresTs: 0,
+ refreshToken: "",
+ },
repositoryInfo: {
externalId: "",
name: "",
@@ -106,9 +111,9 @@ export default {
vcs: state.config.vcs,
projectId: state.config.repositoryInfo.externalId,
branchFilter: state.config.repositoryConfig.branchFilter,
- token: state.config.accessToken,
+ token: state.config.token.accessToken,
})
- .then((createdWebhookId) => {
+ .then((webhook: WebhookInfo) => {
const repositoryCreate: RepositoryCreate = {
vcsId: state.config.vcs.id,
projectId: props.project.id,
@@ -120,7 +125,8 @@ export default {
? state.config.repositoryInfo.defaultBranch
: state.config.repositoryConfig.branchFilter,
externalId: state.config.repositoryInfo.externalId,
- webhookId: createdWebhookId,
+ webhookId: webhook.id,
+ webhookURL: webhook.url,
};
store
.dispatch("repository/createRepository", repositoryCreate)
diff --git a/frontend/src/store/modules/gitlab.ts b/frontend/src/store/modules/gitlab.ts
index dd2281300a..77059a3c8c 100644
--- a/frontend/src/store/modules/gitlab.ts
+++ b/frontend/src/store/modules/gitlab.ts
@@ -1,9 +1,11 @@
import axios from "axios";
+import { v4 as uuidv4 } from "uuid";
import {
ExternalRepositoryInfo,
VCS,
OAuthConfig,
OAuthToken,
+ WebhookInfo,
} from "../../types";
const GITLAB_API_PATH = "api/v4";
@@ -85,12 +87,13 @@ const actions = {
branchFilter,
token,
}: { vcs: VCS; projectId: string; branchFilter: string; token: string }
- ): Promise<string> {
+ ): Promise<WebhookInfo> {
+ const url = `${vcs.instanceURL}/${GITLAB_WEBHOOK_PATH}/${uuidv4()}`;
const data = (
await axios.post(
`${vcs.instanceURL}/${GITLAB_API_PATH}/projects/${projectId}/hooks`,
{
- url: `${vcs.instanceURL}/${GITLAB_WEBHOOK_PATH}`,
+ url,
push_events: true,
// For now, there is no native dry run DDL support in mysql/postgres. One may wonder if we could wrap the DDL
// in a transaction and just not commit at the end, unfortunately there are side effects which are hard to control.
@@ -110,7 +113,10 @@ const actions = {
)
).data;
- return data.id.toString();
+ return {
+ id: data.id.toString(),
+ url,
+ };
},
};
diff --git a/frontend/src/types/repository.ts b/frontend/src/types/repository.ts
index 1f63cd2999..bba028cf36 100644
--- a/frontend/src/types/repository.ts
+++ b/frontend/src/types/repository.ts
@@ -42,6 +42,7 @@ export type RepositoryCreate = {
branchFilter: string;
externalId: string;
webhookId: string;
+ webhookURL: string;
};
export type RepositoryConfig = {
@@ -61,3 +62,8 @@ export type ExternalRepositoryInfo = {
// e.g. master
defaultBranch: string;
};
+
+export type WebhookInfo = {
+ id: string;
+ url: string;
+};