Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Frontend VCS initial support
天舟8bbdb966445f
diff --git a/frontend/src/components/InstanceTable.vue b/frontend/src/components/InstanceTable.vueindex d8816c32aa..00c4acd67d 100644--- a/frontend/src/components/InstanceTable.vue+++ b/frontend/src/components/InstanceTable.vue@@ -108,8 +108,6 @@ export default {const clickInstance = function (section: number, row: number) {const instance = props.instanceList[row];- console.log("aaa", instance);- console.log("slug", instanceSlug(instance));router.push(`/instance/${instanceSlug(instance)}`);};diff --git a/frontend/src/components/VCSCard.vue b/frontend/src/components/VCSCard.vuenew file mode 100644index 0000000000..a4f7331e73--- /dev/null+++ b/frontend/src/components/VCSCard.vue@@ -0,0 +1,109 @@+<template>+ <div class="flex flex-row space-x-2">+ <template v-if="vcs.type.startsWith('GITLAB')">+ <img class="h-6 w-auto" src="../assets/gitlab-logo.svg" />+ </template>+ <h3 class="text-lg leading-6 font-medium text-gray-900">+ {{ vcs.name }}+ </h3>+ </div>+ <div class="mt-5 border-t border-gray-200">+ <dl class="divide-y divide-gray-200">+ <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4">+ <dt class="text-sm font-medium text-gray-500">Redirect URL</dt>+ <dd class="mt-1 flex text-sm text-gray-900 sm:mt-0 sm:col-span-2">+ <span class="flex-grow">{{ redirectURL(vcs) }}</span>+ </dd>+ </div>+ <div class="py-4 sm:grid sm:py-5 sm:grid-cols-3 sm:gap-4">+ <dt class="text-sm font-medium text-gray-500">Instance URL</dt>+ <dd class="mt-1 flex text-sm text-gray-900 sm:mt-0 sm:col-span-2">+ <span class="flex-grow">{{ vcs.instanceURL }}</span>+ </dd>+ </div>+ <div class="py-4 sm:grid sm:py-5 sm:grid-cols-3 sm:gap-4">+ <dt class="text-sm font-medium text-gray-500">API URL</dt>+ <dd class="mt-1 flex text-sm text-gray-900 sm:mt-0 sm:col-span-2">+ <span class="flex-grow">{{ vcs.apiURL }}</span>+ </dd>+ </div>+ <div class="py-4 sm:grid sm:py-5 sm:grid-cols-3 sm:gap-4">+ <dt class="text-sm font-medium text-gray-500">Application ID</dt>+ <dd class="mt-1 flex text-sm text-gray-900 sm:mt-0 sm:col-span-2">+ <span class="flex-grow">{{ vcs.applicationId }}</span>+ </dd>+ </div>+ <div class="py-4 sm:grid sm:py-5 sm:grid-cols-3 sm:gap-4">+ <dt class="text-sm font-medium text-gray-500">Secret</dt>+ <dd class="mt-1 flex text-sm text-gray-900 sm:mt-0 sm:col-span-2">+ <span class="flex-grow">{{ vcs.secret }}</span>+ </dd>+ </div>+ <div class="py-4 sm:grid sm:py-5 sm:grid-cols-3 sm:gap-4">+ <dt class="text-sm font-medium text-gray-500">OAuth Token</dt>+ <dd class="mt-1 flex text-sm text-gray-900 sm:mt-0 sm:col-span-2">+ <span class="flex-grow">{{ vcs.accessToken }}</span>+ <span v-if="!vcs.accessToken" class="ml-4 flex-shrink-0">+ <button+ type="button"+ class="+ bg-white+ rounded-md+ font-medium+ text-indigo-600+ hover:text-indigo-500+ focus:outline-none+ focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500+ "+ @click.prevent="linkVCS(vcs)"+ >+ <template v-if="vcs.type.startsWith('GITLAB')">+ Link this GitLab instance+ </template>+ </button>+ </span>+ </dd>+ </div>+ </dl>+ </div>+</template>++<script lang="ts">+import { reactive, PropType } from "vue";+import { VCS, redirectURL } from "../types";+import { randomString } from "../utils";++interface LocalState {}++export default {+ name: "VCSCard",+ components: {},+ props: {+ vcs: {+ required: true,+ type: Object as PropType<VCS>,+ },+ },+ setup(props, ctx) {+ const state = reactive<LocalState>({});++ const linkVCS = (vcs: VCS) => {+ const state = randomString(40);+ window.open(+ `${vcs.instanceURL}/oauth/authorize?client_id=${+ vcs.applicationId+ }&redirect_uri=${encodeURIComponent(+ redirectURL(vcs)+ )}&response_type=code&state=${state}`,+ "_blank"+ );+ };++ return {+ state,+ redirectURL,+ linkVCS,+ };+ },+};+</script>diff --git a/frontend/src/main.ts b/frontend/src/main.tsindex 7f7201cbc6..91d4f0a2f7 100644--- a/frontend/src/main.ts+++ b/frontend/src/main.ts@@ -108,7 +108,8 @@ axios.interceptors.request.use((request) => {!request.url!.startsWith("/api/pipeline") &&!request.url!.startsWith("/api/activity") &&!request.url!.startsWith("/api/bookmar") &&- !request.url!.startsWith("/api/sql"))+ !request.url!.startsWith("/api/sql") &&+ !request.url!.startsWith("/api/vcs"))) {request.url = request.url!.replace("/api", "/mock");}diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.tsindex 8cae35b721..495c88769d 100644--- a/frontend/src/store/index.ts+++ b/frontend/src/store/index.ts@@ -19,6 +19,7 @@ import issue from "./modules/issue";import pipeline from "./modules/pipeline";import stage from "./modules/stage";import task from "./modules/task";+import vcs from "./modules/vcs";// Following states are persisted in local storageimport uistate from "./modules/uistate";@@ -53,6 +54,7 @@ export const store: Store<any> = createStore({pipeline,stage,task,+ vcs,uistate,router,command,diff --git a/frontend/src/store/modules/vcs.ts b/frontend/src/store/modules/vcs.tsnew file mode 100644index 0000000000..c353609178--- /dev/null+++ b/frontend/src/store/modules/vcs.ts@@ -0,0 +1,175 @@+import axios from "axios";+import {+ VCSId,+ VCS,+ VCSCreate,+ VCSState,+ ResourceObject,+ unknown,+ VCSPatch,+ empty,+ EMPTY_ID,+ Principal,+} from "../../types";++function convert(+ vcs: ResourceObject,+ includedList: ResourceObject[],+ rootGetters: any+): VCS {+ const creator = vcs.attributes.creator as Principal;+ const updater = vcs.attributes.updater as Principal;+ return {+ ...(vcs.attributes as Omit<VCS, "id" | "creator" | "updater">),+ id: parseInt(vcs.id),+ creator,+ updater,+ };+}++const state: () => VCSState = () => ({+ vcsById: new Map(),+});++const getters = {+ convert:+ (state: VCSState, getters: any, rootState: any, rootGetters: any) =>+ (vcs: ResourceObject, includedList: ResourceObject[]): VCS => {+ return convert(vcs, includedList, rootGetters);+ },++ vcsList: (state: VCSState) => (): VCS[] => {+ const list = [];+ for (const [_, vcs] of state.vcsById) {+ list.push(vcs);+ }+ return list.sort((a: VCS, b: VCS) => {+ return b.createdTs - a.createdTs;+ });+ },++ vcsById:+ (state: VCSState) =>+ (vcsId: VCSId): VCS => {+ if (vcsId == EMPTY_ID) {+ return empty("VCS") as VCS;+ }++ return state.vcsById.get(vcsId) || (unknown("VCS") as VCS);+ },+};++const actions = {+ async fetchVCSList({ commit, rootGetters }: any) {+ const path = "/api/vcs";+ const data = (await axios.get(path)).data;+ const vcsList = data.data.map((vcs: ResourceObject) => {+ return convert(vcs, data.included, rootGetters);+ });++ commit("setVCSList", vcsList);++ return vcsList;+ },++ async fetchVCSById({ commit, rootGetters }: any, vcsId: VCSId) {+ // include=secret to return username/password when requesting the specific vcs id+ const data = (await axios.get(`/api/vcs/${vcsId}`)).data;+ const vcs = convert(data.data, data.included, rootGetters);++ commit("setVCSById", {+ vcsId,+ vcs,+ });+ return vcs;+ },++ async createVCS({ commit, rootGetters }: any, newVCS: VCSCreate) {+ const data = (+ await axios.post(`/api/vcs`, {+ data: {+ type: "VCSCreate",+ attributes: newVCS,+ },+ })+ ).data;+ const createdVCS = convert(data.data, data.included, rootGetters);++ commit("setVCSById", {+ vcsId: createdVCS.id,+ vcs: createdVCS,+ });++ return createdVCS;+ },++ async patchVCS(+ { commit, rootGetters }: any,+ {+ vcsId,+ vcsPatch,+ }: {+ vcsId: VCSId;+ vcsPatch: VCSPatch;+ }+ ) {+ const data = (+ await axios.patch(`/api/vcs/${vcsId}`, {+ data: {+ type: "vcsPatch",+ attributes: vcsPatch,+ },+ })+ ).data;+ const updatedVCS = convert(data.data, data.included, rootGetters);++ commit("setVCSById", {+ vcsId: updatedVCS.id,+ vcs: updatedVCS,+ });++ return updatedVCS;+ },++ async deleteVCSById(+ { commit }: { state: VCSState; commit: any },+ vcsId: VCSId+ ) {+ await axios.delete(`/api/vcs/${vcsId}`);++ commit("deleteVCSById", vcsId);+ },+};++const mutations = {+ setVCSList(state: VCSState, vcsList: VCS[]) {+ vcsList.forEach((vcs) => {+ state.vcsById.set(vcs.id, vcs);+ });+ },++ setVCSById(+ state: VCSState,+ {+ vcsId,+ vcs,+ }: {+ vcsId: VCSId;+ vcs: VCS;+ }+ ) {+ state.vcsById.set(vcsId, vcs);+ },++ deleteVCSById(state: VCSState, vcsId: VCSId) {+ state.vcsById.delete(vcsId);+ },+};++export default {+ namespaced: true,+ state,+ getters,+ actions,+ mutations,+};diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.tsindex 44e88f8859..45be805fb9 100644--- a/frontend/src/types/common.ts+++ b/frontend/src/types/common.ts@@ -11,6 +11,7 @@ import { Pipeline, Stage, Task } from "./pipeline";import { Activity } from "./activity";import { Message } from "./message";import { Bookmark } from "./bookmark";+import { VCS } from "./vcs";// System bot idexport const SYSTEM_BOT_ID = 1;@@ -87,7 +88,8 @@ export type ResourceType =| "TASK"| "ACTIVITY"| "MESSAGE"- | "BOOKMARK";+ | "BOOKMARK"+ | "VCS";export const unknown = (type: ResourceType@@ -106,7 +108,8 @@ export const unknown = (| Task| Activity| Message- | Bookmark => {+ | Bookmark+ | VCS => {// Have to omit creator and updater to avoid recursion.const UNKNOWN_PRINCIPAL: Principal = {id: UNKNOWN_ID,@@ -297,6 +300,22 @@ export const unknown = (link: "",};+ const UNKNOWN_VCS: VCS = {+ id: UNKNOWN_ID,+ creator: UNKNOWN_PRINCIPAL,+ updater: UNKNOWN_PRINCIPAL,+ createdTs: 0,+ updatedTs: 0,+ name: "",+ uuid: "",+ type: "GITLAB_SELF_HOST",+ instanceURL: "",+ apiURL: "",+ applicationId: "",+ secret: "",+ accessToken: "",+ };+switch (type) {case "PRINCIPAL":return UNKNOWN_PRINCIPAL;@@ -328,6 +347,8 @@ export const unknown = (return UNKNOWN_MESSAGE;case "BOOKMARK":return UNKNOWN_BOOKMARK;+ case "VCS":+ return UNKNOWN_VCS;}};@@ -349,7 +370,8 @@ export const empty = (| Task| Activity| Message- | Bookmark => {+ | Bookmark+ | VCS => {// Have to omit creator and updater to avoid recursion.const EMPTY_PRINCIPAL: Principal = {id: EMPTY_ID,@@ -538,6 +560,22 @@ export const empty = (link: "",};+ const EMPTY_VCS: VCS = {+ id: EMPTY_ID,+ creator: EMPTY_PRINCIPAL,+ updater: EMPTY_PRINCIPAL,+ createdTs: 0,+ updatedTs: 0,+ name: "",+ uuid: "",+ type: "GITLAB_SELF_HOST",+ instanceURL: "",+ apiURL: "",+ applicationId: "",+ secret: "",+ accessToken: "",+ };+switch (type) {case "PRINCIPAL":return EMPTY_PRINCIPAL;@@ -569,5 +607,7 @@ export const empty = (return EMPTY_MESSAGE;case "BOOKMARK":return EMPTY_BOOKMARK;+ case "VCS":+ return EMPTY_VCS;}};diff --git a/frontend/src/types/id.ts b/frontend/src/types/id.tsindex 55fcb3412f..78d2b84d6d 100644--- a/frontend/src/types/id.ts+++ b/frontend/src/types/id.ts@@ -37,6 +37,8 @@ export type DatabaseId = IdType;export type TableId = IdType;+export type VCSId = IdType;+export type CommandId = IdType;export type CommandRegisterId = IdType;diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.tsindex e42d6184d4..6b67bac388 100644--- a/frontend/src/types/store.ts+++ b/frontend/src/types/store.ts@@ -12,6 +12,7 @@ import {ProjectId,PrincipalId,DatabaseId,+ VCSId,} from "./id";import { Instance } from "./instance";import { Issue } from "./issue";@@ -22,6 +23,7 @@ import { Principal } from "./principal";import { Project } from "./project";import { Notification } from "./notification";import { Table } from "./table";+import { VCS } from "./vcs";export interface AuthState {currentUser: Principal;@@ -96,6 +98,10 @@ export interface TableState {tableListByDatabaseId: Map<DatabaseId, Table[]>;}+export interface VCSState {+ vcsById: Map<VCSId, VCS>;+}+export interface NotificationState {notificationByModule: Map<string, Notification[]>;}diff --git a/frontend/src/types/vcs.ts b/frontend/src/types/vcs.tsindex 0964bd174a..3e97258d12 100644--- a/frontend/src/types/vcs.ts+++ b/frontend/src/types/vcs.ts@@ -1,13 +1,60 @@+import { PrincipalId, VCSId } from "./id";+import { Principal } from "./principal";+export type VCSType = "GITLAB_SELF_HOST";export interface VCSConfig {- vcsType: VCSType;+ type: VCSType;name: string;instanceURL: string;applicationId: string;secret: string;}+export type VCS = {+ id: VCSId;++ // Standard fields+ creator: Principal;+ createdTs: number;+ updater: Principal;+ updatedTs: number;++ // Domain specific fields+ name: string;+ uuid: string;+ type: VCSType;+ instanceURL: string;+ apiURL: string;+ applicationId: string;+ secret: string;+ accessToken: string;+};++export type VCSCreate = {+ // Standard fields+ creatorId: PrincipalId;++ // Domain specific fields+ name: string;+ type: VCSType;+ instanceURL: string;+ applicationId: string;+ secret: string;+};++export type VCSPatch = {+ // Standard fields+ updaterId: PrincipalId;++ // Domain specific fields+ accessToken: string;+};+export function isValidApplicationIdOrSecret(str: string): boolean {return /^[a-zA-Z0-9_]{64}$/.test(str);}++export function redirectURL(vcs: VCS): string {+ return `https://bytebase.com/auth/${vcs.uuid}/callback`;+}diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.tsindex 68d62b0af8..2e81c52013 100644--- a/frontend/src/utils/util.ts+++ b/frontend/src/utils/util.ts@@ -50,7 +50,7 @@ export function urlfy(str: string) {return result;}-export function isUrl(str: string) {+export function isURL(str: string) {let url;try {diff --git a/frontend/src/views/SettingWorkspaceVCS.vue b/frontend/src/views/SettingWorkspaceVCS.vueindex 971d723ee9..61cbce4f50 100644--- a/frontend/src/views/SettingWorkspaceVCS.vue+++ b/frontend/src/views/SettingWorkspaceVCS.vue@@ -1,32 +1,40 @@<template>- <h1>VCS</h1>- <BBStepTab- :stepItemList="stepList"- :allowNext="allowNext"- @select-step="selectStep"- @finish="finishSetup"- >- <template v-slot:0>- <VCSProviderSelectionPanel :config="state.config" />+ <template v-if="vcsList.length > 0">+ <template v-for="(vcs, index) in vcsList" :key="index">+ <VCSCard :vcs="vcs" /></template>- <template v-slot:1>- <VCSProviderInfoPanel :config="state.config" />- </template>- <template v-slot:2>- <VCSProviderConfigPanel :config="state.config" />- </template>- </BBStepTab>+ </template>+ <template v-else>+ <BBStepTab+ :stepItemList="stepList"+ :allowNext="allowNext"+ @select-step="selectStep"+ @finish="finishSetup"+ >+ <template v-slot:0>+ <VCSProviderSelectionPanel :config="state.config" />+ </template>+ <template v-slot:1>+ <VCSProviderInfoPanel :config="state.config" />+ </template>+ <template v-slot:2>+ <VCSProviderConfigPanel :config="state.config" />+ </template>+ </BBStepTab>+ </template></template><script lang="ts">-import { computed } from "@vue/runtime-core";+import { computed, watchEffect } from "@vue/runtime-core";import { reactive } from "@vue/reactivity";import { BBStepTabItem } from "../bbkit/types";import VCSProviderSelectionPanel from "./VCSProviderSelectionPanel.vue";import VCSProviderInfoPanel from "./VCSProviderInfoPanel.vue";import VCSProviderConfigPanel from "./VCSProviderConfigPanel.vue";-import { isValidApplicationIdOrSecret, VCSConfig } from "../types";-import { isUrl } from "../utils";+import VCSCard from "../components/VCSCard.vue";+import { isValidApplicationIdOrSecret, VCSConfig, VCSCreate } from "../types";+import { isURL } from "../utils";+import { useStore } from "vuex";const stepList: BBStepTabItem[] = [{ title: "Choose Git provider" },@@ -46,11 +54,16 @@ export default {VCSProviderSelectionPanel,VCSProviderInfoPanel,VCSProviderConfigPanel,+ VCSCard,},setup(props, ctx) {+ const store = useStore();++ const currentUser = computed(() => store.getters["auth/currentUser"]());+const state = reactive<LocalState>({config: {- vcsType: "GITLAB_SELF_HOST",+ type: "GITLAB_SELF_HOST",name: "",instanceURL: "",applicationId: "",@@ -59,11 +72,21 @@ export default {currentStep: 0,});+ const prepareVCSList = () => {+ store.dispatch("vcs/fetchVCSList");+ };++ watchEffect(prepareVCSList);++ const vcsList = computed(() => {+ return store.getters["vcs/vcsList"]();+ });+const allowNext = computed((): boolean => {if (state.currentStep == 0) {return true;} else if (state.currentStep == 1) {- return isUrl(state.config.instanceURL);+ return isURL(state.config.instanceURL);} else if (state.currentStep == 2) {return (isValidApplicationIdOrSecret(state.config.applicationId) &&@@ -79,11 +102,17 @@ export default {const finishSetup = () => {console.log("finish", state.config);+ const vcsCreate: VCSCreate = {+ creatorId: currentUser.value.id,+ ...state.config,+ };+ store.dispatch("vcs/createVCS", vcsCreate);};return {stepList,state,+ vcsList,allowNext,selectStep,finishSetup,diff --git a/frontend/src/views/VCSProviderInfoPanel.vue b/frontend/src/views/VCSProviderInfoPanel.vueindex fa3f1b6d4d..d7438acfb7 100644--- a/frontend/src/views/VCSProviderInfoPanel.vue+++ b/frontend/src/views/VCSProviderInfoPanel.vue@@ -13,7 +13,7 @@class="mt-2 w-full":placeholder="'https://gitlab.example.com'":value="config.instanceURL"- @input="changeUrl($event.target.value)"+ @input="changeURL($event.target.value)"/><p v-if="state.showURLError" class="mt-2 text-sm text-error">Instance URL must begin with https:// or http://@@ -24,7 +24,7 @@import { onUnmounted, PropType, reactive } from "@vue/runtime-core";import isEmpty from "lodash-es/isEmpty";import { TEXT_VALIDATION_DELAY, VCSConfig } from "../types";-import { isUrl } from "../utils";+import { isURL } from "../utils";interface LocalState {urlValidationTimer?: ReturnType<typeof setTimeout>;@@ -42,37 +42,37 @@ export default {setup(props, ctx) {const state = reactive<LocalState>({showURLError:- !isEmpty(props.config.instanceURL) && !isUrl(props.config.instanceURL),+ !isEmpty(props.config.instanceURL) && !isURL(props.config.instanceURL),});onUnmounted(() => {clearInterval(state.urlValidationTimer);});- const changeUrl = (value: string) => {+ const changeURL = (value: string) => {props.config.instanceURL = value;clearInterval(state.urlValidationTimer);// If text becomes valid, we immediately clear the error.// otherwise, we delay TEXT_VALIDATION_DELAY to do the validation in case there is continous keystroke.- if (isUrl(props.config.instanceURL)) {+ if (isURL(props.config.instanceURL)) {state.showURLError = false;} else {state.urlValidationTimer = setTimeout(() => {// If error is already displayed, we hide the error only if there is valid input.// Otherwise, we hide the error if input is either empty or valid.if (state.showURLError) {- state.showURLError = !isUrl(props.config.instanceURL);+ state.showURLError = !isURL(props.config.instanceURL);} else {state.showURLError =!isEmpty(props.config.instanceURL) &&- !isUrl(props.config.instanceURL);+ !isURL(props.config.instanceURL);}}, TEXT_VALIDATION_DELAY);}};- return { state, changeUrl };+ return { state, changeURL };},};</script>diff --git a/frontend/src/views/VCSProviderSelectionPanel.vue b/frontend/src/views/VCSProviderSelectionPanel.vueindex 9388d8eb53..4aa87c0f40 100644--- a/frontend/src/views/VCSProviderSelectionPanel.vue+++ b/frontend/src/views/VCSProviderSelectionPanel.vue@@ -7,7 +7,7 @@type="radio"class="btn"value="GITLAB_SELF_HOST"- v-model="config.vcsType"+ v-model="config.type"/><img class="h-6 w-auto" src="../assets/gitlab-logo.svg" /><label class="whitespace-nowrap"@@ -60,7 +60,7 @@ export default {},setup(props, ctx) {const selectGitLabOnPrem = () => {- props.config.vcsType = "GITLAB_SELF_HOST";+ props.config.type = "GITLAB_SELF_HOST";};return { selectGitLabOnPrem };},