Skip to content

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

Version control detail view implementation

天舟7ade0a85f3a6
diff --git a/backend/api/vcs.go b/backend/api/vcs.go
index 8ced3a2e3f..f4a2006798 100644
--- a/backend/api/vcs.go
+++ b/backend/api/vcs.go
@@ -72,6 +72,11 @@ type VCSPatch struct {
// Standard fields
// Value is assigned from the jwt subject field passed by the client.
UpdaterId int
+
+ // Domain specific fields
+ Name *string `jsonapi:"attr,name"`
+ ApplicationId *string `jsonapi:"attr,applicationId"`
+ Secret *string `jsonapi:"attr,secret"`
}
type VCSDelete struct {
diff --git a/backend/store/vcs.go b/backend/store/vcs.go
index 077c81368a..ac39228de7 100644
--- a/backend/store/vcs.go
+++ b/backend/store/vcs.go
@@ -239,6 +239,15 @@ func findVCSList(ctx context.Context, tx *Tx, find *api.VCSFind) (_ []*api.VCS,
func patchVCS(ctx context.Context, tx *Tx, patch *api.VCSPatch) (*api.VCS, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = ?"}, []interface{}{patch.UpdaterId}
+ if v := patch.Name; v != nil {
+ set, args = append(set, "name = ?"), append(args, *v)
+ }
+ if v := patch.ApplicationId; v != nil {
+ set, args = append(set, "application_id = ?"), append(args, *v)
+ }
+ if v := patch.Secret; v != nil {
+ set, args = append(set, "secret = ?"), append(args, *v)
+ }
args = append(args, patch.ID)
// Execute update query with RETURNING.
diff --git a/frontend/src/components/VCSSetupWizard.vue b/frontend/src/components/VCSSetupWizard.vue
index bc02d222c7..cbe8a44957 100644
--- a/frontend/src/components/VCSSetupWizard.vue
+++ b/frontend/src/components/VCSSetupWizard.vue
@@ -57,7 +57,7 @@ const stepList: BBStepTabItem[] = [
interface LocalState {
config: VCSConfig;
currentStep: number;
- oauthResultCallback?: (success: boolean) => void;
+ oAuthResultCallback?: (success: boolean) => void;
}
export default {
@@ -101,14 +101,14 @@ export default {
code: payload.code,
})
.then((token: string) => {
- state.oauthResultCallback!(true);
+ state.oAuthResultCallback!(true);
})
.catch((error) => {
- state.oauthResultCallback!(false);
+ state.oAuthResultCallback!(false);
});
}
} else {
- state.oauthResultCallback!(false);
+ state.oAuthResultCallback!(false);
}
window.removeEventListener(OAuthWindowEvent, eventListener);
@@ -152,7 +152,7 @@ export default {
state.config.applicationId
);
if (newWindow) {
- state.oauthResultCallback = (success: boolean) => {
+ state.oAuthResultCallback = (success: boolean) => {
if (success) {
state.currentStep = newStep;
allowChangeCallback();
diff --git a/frontend/src/types/vcs.ts b/frontend/src/types/vcs.ts
index 9cf35b8153..b5866390ec 100644
--- a/frontend/src/types/vcs.ts
+++ b/frontend/src/types/vcs.ts
@@ -42,8 +42,10 @@ export type VCSCreate = {
};
export type VCSPatch = {
- // Standard fields
- updaterId: PrincipalId;
+ // Domain specific fields
+ name?: string;
+ applicationId?: string;
+ secret?: string;
};
export type VCSTokenCreate = {
diff --git a/frontend/src/views/SettingWorkspaceVCSCreate.vue b/frontend/src/views/SettingWorkspaceVCSCreate.vue
index dcbffa1e05..503ee739fb 100644
--- a/frontend/src/views/SettingWorkspaceVCSCreate.vue
+++ b/frontend/src/views/SettingWorkspaceVCSCreate.vue
@@ -1,11 +1,10 @@
<template>
<div class="mt-4 space-y-4 divide-y divide-block-border">
<div class="textinfolabel">
- Before any project can enable version control workflow to automatically
- apply DDL on database migration scripts changes in the Git repository,
- Bytebase first needs to integrate with the corresponding version control
- system (VCS) by registering as an OAuth application in that VCS. Below are
- the steps to configure this.
+ Before any project can enable version control workflow, Bytebase first
+ needs to integrate with the corresponding version control system (VCS) by
+ registering as an OAuth application in that VCS. Below are the steps to
+ configure this.
</div>
<div class="pt-4">
<VCSSetupWizard />
diff --git a/frontend/src/views/SettingWorkspaceVCSDetail.vue b/frontend/src/views/SettingWorkspaceVCSDetail.vue
index 5a0e82b7b2..e90a8738cf 100644
--- a/frontend/src/views/SettingWorkspaceVCSDetail.vue
+++ b/frontend/src/views/SettingWorkspaceVCSDetail.vue
@@ -1,12 +1,127 @@
-<template></template>
+<template>
+ <div class="mt-4 space-y-4">
+ <div class="flex justify-end">
+ <div
+ v-if="vcs.type == 'GITLAB_SELF_HOST'"
+ class="flex flex-row items-center space-x-2"
+ >
+ <div class="textlabel whitespace-nowrap">
+ Self-host GitLab Enterprise Edition (EE) or Community Edition (CE)
+ </div>
+ <img class="h-6 w-auto" src="../assets/gitlab-logo.svg" />
+ </div>
+ </div>
+
+ <div>
+ <label for="name" class="textlabel"> Display name </label>
+ <p class="mt-1 textinfolabel">
+ An optional display name to help identifying among different configs
+ using the same Git provider.
+ </p>
+ <input
+ id="name"
+ name="name"
+ type="text"
+ class="textfield mt-1 w-full"
+ v-model="state.name"
+ />
+ </div>
+
+ <div>
+ <label for="instanceurl" class="textlabel"> Instance URL </label>
+ <p class="mt-1 textinfolabel">
+ <template v-if="vcs.type == 'GITLAB_SELF_HOST'">
+ Your GitLab instance location.
+ </template>
+ </p>
+ <input
+ id="instanceurl"
+ name="instanceurl"
+ type="text"
+ class="textfield mt-1 w-full"
+ disabled="true"
+ :value="vcs.instanceURL"
+ />
+ </div>
+
+ <div>
+ <label for="applicationid" class="textlabel"> Application ID </label>
+ <p class="mt-1 textinfolabel">
+ <template v-if="vcs.type == 'GITLAB_SELF_HOST'">
+ Application ID for the registered GitLab system OAuth application.
+ <a :href="adminApplicationURL" target="_blank" class="normal-link"
+ >View in GitLab</a
+ >
+ </template>
+ </p>
+ <input
+ id="applicationid"
+ name="applicationid"
+ type="text"
+ class="textfield mt-1 w-full"
+ v-model="state.applicationId"
+ />
+ </div>
+
+ <div>
+ <label for="secret" class="textlabel"> Secret </label>
+ <p class="mt-1 textinfolabel">
+ <template v-if="vcs.type == 'GITLAB_SELF_HOST'">
+ Secret for the registered GitLab system OAuth application.
+ </template>
+ </p>
+ <input
+ id="secret"
+ name="secret"
+ type="text"
+ class="textfield mt-1 w-full"
+ placeholder="sensitive - write only"
+ v-model="state.secret"
+ />
+ </div>
+
+ <div class="pt-4 flex border-t justify-end">
+ <button
+ type="button"
+ class="btn-normal py-2 px-4"
+ @click.prevent="cancel"
+ >
+ Cancel
+ </button>
+ <button
+ type="button"
+ class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
+ :disabled="!allowUpdate"
+ @click.prevent="doUpdate"
+ >
+ Update
+ </button>
+ </div>
+ </div>
+</template>
<script lang="ts">
import { reactive, computed, watchEffect } from "vue";
import { useStore } from "vuex";
+import { useRouter } from "vue-router";
+import isEmpty from "lodash-es/isEmpty";
import { idFromSlug } from "../utils";
-import { VCS } from "../types";
+import {
+ VCS,
+ VCSPatch,
+ openWindowForOAuth,
+ OAuthWindowEvent,
+ OAuthWindowEventPayload,
+ OAuthConfig,
+ redirectURL,
+} from "../types";
-interface LocalState {}
+interface LocalState {
+ name: string;
+ applicationId: string;
+ secret: string;
+ oAuthResultCallback?: (success: boolean) => void;
+}
export default {
name: "SettingWorkspaceVCSDetail",
@@ -19,25 +134,158 @@ export default {
components: {},
setup(props, ctx) {
const store = useStore();
- const state = reactive<LocalState>({});
+ const router = useRouter();
const vcs = computed((): VCS => {
return store.getters["vcs/vcsById"](idFromSlug(props.vcsSlug));
});
+ const state = reactive<LocalState>({
+ name: vcs.value.name,
+ applicationId: vcs.value.applicationId,
+ secret: "",
+ });
+
+ const eventListener = (event: Event) => {
+ const payload = (event as CustomEvent).detail as OAuthWindowEventPayload;
+ if (isEmpty(payload.error)) {
+ if (vcs.value.type == "GITLAB_SELF_HOST") {
+ const oAuthConfig: OAuthConfig = {
+ endpoint: `${vcs.value.instanceURL}/oauth/token`,
+ applicationId: state.applicationId,
+ secret: state.secret,
+ redirectURL: redirectURL(),
+ };
+ store
+ .dispatch("gitlab/exchangeToken", {
+ oAuthConfig,
+ code: payload.code,
+ })
+ .then((token: string) => {
+ state.oAuthResultCallback!(true);
+ })
+ .catch((error) => {
+ state.oAuthResultCallback!(false);
+ });
+ }
+ } else {
+ state.oAuthResultCallback!(false);
+ }
+
+ window.removeEventListener(OAuthWindowEvent, eventListener);
+ };
+
const prepareRepositoryList = () => {
store.dispatch("vcs/fetchRepositoryListByVCS", vcs.value);
};
watchEffect(prepareRepositoryList);
+ const adminApplicationURL = computed(() => {
+ if (vcs.value.type == "GITLAB_SELF_HOST") {
+ return `${vcs.value.instanceURL}/admin/applications`;
+ }
+ return "";
+ });
+
const repositoryList = computed(() =>
store.getters["vcs/repositoryListByVCSId"](vcs.value.id)
);
+ const allowUpdate = computed(() => {
+ return (
+ state.name != vcs.value.name ||
+ state.applicationId != vcs.value.applicationId ||
+ !isEmpty(state.secret)
+ );
+ });
+
+ const doUpdate = () => {
+ if (
+ state.applicationId != vcs.value.applicationId ||
+ !isEmpty(state.secret)
+ ) {
+ const newWindow = openWindowForOAuth(
+ `${vcs.value.instanceURL}/oauth/authorize`,
+ vcs.value.applicationId
+ );
+ if (newWindow) {
+ state.oAuthResultCallback = (success: boolean) => {
+ if (success) {
+ const vcsPatch: VCSPatch = {};
+ if (state.name != vcs.value.name) {
+ vcsPatch.name = state.name;
+ }
+ if (state.applicationId != vcs.value.applicationId) {
+ vcsPatch.applicationId = state.applicationId;
+ }
+ if (!isEmpty(state.secret)) {
+ vcsPatch.secret = state.secret;
+ }
+ store
+ .dispatch("vcs/patchVCS", {
+ vcsId: vcs.value.id,
+ vcsPatch,
+ })
+ .then((vcs: VCS) => {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "SUCCESS",
+ title: `Successfully updated '${vcs.name}'`,
+ });
+ });
+ } else {
+ var description = "";
+ if (vcs.value.type == "GITLAB_SELF_HOST") {
+ // If application id mismatches, the OAuth workflow will stop early.
+ // So the only possibility to reach here is we have a matching application id, while
+ // we failed to exchange a token, and it's likely we are requesting with a wrong secret.
+ description =
+ "Please make sure Secret matches the one from your GitLab instance Application.";
+ }
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "CRITICAL",
+ title: `Failed to update '${vcs.value.name}'`,
+ description: description,
+ });
+ }
+ };
+ window.addEventListener(OAuthWindowEvent, eventListener, false);
+ }
+ } else if (state.name != vcs.value.name) {
+ const vcsPatch: VCSPatch = {
+ name: state.name,
+ };
+ store
+ .dispatch("vcs/patchVCS", {
+ vcsId: vcs.value.id,
+ vcsPatch,
+ })
+ .then((updatedVCS: VCS) => {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "SUCCESS",
+ title: `Successfully updated '${updatedVCS.name}'`,
+ });
+ });
+ }
+ };
+
+ const cancel = () => {
+ router.push({
+ name: "setting.workspace.version-control",
+ });
+ };
+
return {
state,
+ vcs,
repositoryList,
+ adminApplicationURL,
+ allowUpdate,
+ doUpdate,
+ cancel,
};
},
};