Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Implement RepositorySelectionPanel
天舟b7152fa3a0ae
diff --git a/frontend/src/components/ProjectRepositorySetupPanel.vue b/frontend/src/components/ProjectRepositorySetupPanel.vueindex 736f1517c9..6991630575 100644--- a/frontend/src/components/ProjectRepositorySetupPanel.vue+++ b/frontend/src/components/ProjectRepositorySetupPanel.vue@@ -5,16 +5,11 @@@finish="finishSetup"><template v-slot:0="{ next }">- <RepositoryVCSPanel- @select-vcs="- (vcs) => {- next();- selectVCS(vcs);- }- "- />+ <RepositoryVCSPanel :config="state.config" @next="next()" />+ </template>+ <template v-slot:1="{ next }">+ <RepositorySelectionPanel :config="state.config" @next="next()" /></template>- <template v-slot:1> select repo </template><template v-slot:2> configure deploy </template></BBStepTab></template>@@ -23,9 +18,9 @@import { reactive } from "@vue/reactivity";import { BBStepTabItem } from "../bbkit/types";import { useStore } from "vuex";-import { computed, watchEffect } from "@vue/runtime-core";import RepositoryVCSPanel from "./RepositoryVCSPanel.vue";-import { ProjectRepoConfig, UNKNOWN_ID, VCS } from "../types";+import RepositorySelectionPanel from "./RepositorySelectionPanel.vue";+import { ProjectRepoConfig, Repository, unknown, VCS } from "../types";interface LocalState {config: ProjectRepoConfig;@@ -33,7 +28,7 @@ interface LocalState {const stepList: BBStepTabItem[] = [{ title: "Choose Git provider", hideNext: true },- { title: "Select repository" },+ { title: "Select repository", hideNext: true },{ title: "Configure deploy" },];@@ -41,29 +36,19 @@ export default {name: "ProjectRepositorySetupPanel",components: {RepositoryVCSPanel,+ RepositorySelectionPanel,},setup(props, ctx) {const store = useStore();const state = reactive<LocalState>({config: {- vcsId: UNKNOWN_ID,+ vcs: unknown("VCS") as VCS,+ code: "",+ accessToken: "",+ repository: unknown("REPOSITORY") as Repository,},});- const prepareVCSList = () => {- store.dispatch("vcs/fetchVCSList");- };-- watchEffect(prepareVCSList);-- const vcsList = computed(() => {- return store.getters["vcs/vcsList"]();- });-- const selectVCS = (vcs: VCS) => {- state.config.vcsId = vcs.id;- };-const selectStep = (step: number) => {console.log("select step", step);};@@ -75,7 +60,6 @@ export default {return {state,stepList,- selectVCS,selectStep,finishSetup,};diff --git a/frontend/src/components/RepositorySelectionPanel.vue b/frontend/src/components/RepositorySelectionPanel.vuenew file mode 100644index 0000000000..dcce020404--- /dev/null+++ b/frontend/src/components/RepositorySelectionPanel.vue@@ -0,0 +1,111 @@+<template>+ <div class="space-y-2 divide-y divide-block-border">+ <div class="flex justify-end items-center">+ <BBTableSearch+ :placeholder="'Search repository'"+ @change-text="(text) => changeSearchText(text)"+ />+ </div>+ <div class="bg-white shadow overflow-hidden sm:rounded-md">+ <ul class="divide-y divide-control-border">+ <li+ v-for="(repository, index) in repositoryList"+ :key="index"+ class="block hover:bg-control-bg-hover cursor-pointer"+ @click.prevent="selectRepository(repository)"+ >+ <div class="flex items-center px-4 py-4 sm:px-6">+ <div class="min-w-0 flex-1 flex items-center">+ {{ repository.fullPath }}+ </div>+ <div>+ <!-- Heroicon name: solid/chevron-right -->+ <svg+ class="h-5 w-5 text-control"+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 20 20"+ fill="currentColor"+ aria-hidden="true"+ >+ <path+ fill-rule="evenodd"+ d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"+ clip-rule="evenodd"+ />+ </svg>+ </div>+ </div>+ </li>+ </ul>+ </div>+ </div>+</template>++<script lang="ts">+import { reactive } from "@vue/reactivity";+import { useStore } from "vuex";+import { computed, PropType, watchEffect } from "@vue/runtime-core";+import { ProjectRepoConfig, Repository } from "../types";++interface LocalState {+ repositoryList: Repository[];+ searchText: string;+}++export default {+ name: "RepositorySelectionPanel",+ emits: ["next"],+ props: {+ config: {+ required: true,+ type: Object as PropType<ProjectRepoConfig>,+ },+ },+ components: {},+ setup(props, { emit }) {+ const store = useStore();+ const state = reactive<LocalState>({+ repositoryList: [],+ searchText: "",+ });++ const prepareRepositoryList = () => {+ store+ .dispatch("gitlab/fetchProjectList", {+ vcs: props.config.vcs,+ token: props.config.accessToken,+ })+ .then((list) => {+ state.repositoryList = list;+ });+ };++ watchEffect(prepareRepositoryList);++ const repositoryList = computed(() => {+ if (state.searchText == "") {+ return state.repositoryList;+ }+ return state.repositoryList.filter((repository: Repository) => {+ return repository.fullPath.toLowerCase().includes(state.searchText);+ });+ });++ const selectRepository = (repository: Repository) => {+ props.config.repository = repository;+ emit("next");+ };++ const changeSearchText = (searchText: string) => {+ state.searchText = searchText;+ };++ return {+ state,+ selectRepository,+ repositoryList,+ changeSearchText,+ };+ },+};+</script>diff --git a/frontend/src/components/RepositoryVCSPanel.vue b/frontend/src/components/RepositoryVCSPanel.vueindex 4c0396c23d..c9797dd39c 100644--- a/frontend/src/components/RepositoryVCSPanel.vue+++ b/frontend/src/components/RepositoryVCSPanel.vue@@ -16,12 +16,19 @@<script lang="ts">import { reactive } from "@vue/reactivity";import { useStore } from "vuex";-import { computed, onUnmounted, watchEffect } from "@vue/runtime-core";+import {+ computed,+ onUnmounted,+ PropType,+ watchEffect,+} from "@vue/runtime-core";import isEmpty from "lodash-es/isEmpty";import {OAuthWindowEvent,OAuthWindowEventPayload,openWindowForVCSOAuth,+ ProjectRepoConfig,+ redirectURL,VCS,} from "../types";@@ -29,12 +36,15 @@ interface LocalState {selectedVCS?: VCS;}-var newWindow;-export default {name: "RepositoryVCSPanel",- emits: ["select-vcs"],- props: {},+ emits: ["next"],+ props: {+ config: {+ required: true,+ type: Object as PropType<ProjectRepoConfig>,+ },+ },components: {},setup(props, { emit }) {const store = useStore();@@ -53,7 +63,18 @@ export default {const eventListener = (event: Event) => {const payload = (event as CustomEvent).detail as OAuthWindowEventPayload;if (isEmpty(payload.error)) {- emit("select-vcs", state.selectedVCS);+ props.config.code = payload.code;+ store+ .dispatch("gitlab/exchangeToken", {+ vcs: state.selectedVCS,+ code: payload.code,+ redirectURL: redirectURL(),+ })+ .then((token: string) => {+ props.config.vcs = state.selectedVCS!;+ props.config.accessToken = token;+ emit("next");+ });} else {store.dispatch("notification/pushNotification", {module: "bytebase",diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.tsindex 495c88769d..8193cfd9f1 100644--- a/frontend/src/store/index.ts+++ b/frontend/src/store/index.ts@@ -20,6 +20,7 @@ import pipeline from "./modules/pipeline";import stage from "./modules/stage";import task from "./modules/task";import vcs from "./modules/vcs";+import gitlab from "./modules/gitlab";// Following states are persisted in local storageimport uistate from "./modules/uistate";@@ -55,6 +56,7 @@ export const store: Store<any> = createStore({stage,task,vcs,+ gitlab,uistate,router,command,diff --git a/frontend/src/store/modules/gitlab.ts b/frontend/src/store/modules/gitlab.tsnew file mode 100644index 0000000000..b5b3411c74--- /dev/null+++ b/frontend/src/store/modules/gitlab.ts@@ -0,0 +1,64 @@+import axios from "axios";+import { Repository, VCS } from "../../types";++const GITLAB_API_PATH = "api/v4";++const getters = {};++function convertGitLabProject(project: any): Repository {+ return {+ id: project.id,+ name: project.name,+ fullPath: project.path_with_namespace,+ webURL: project.web_url,+ };+}++const actions = {+ async exchangeToken(+ {}: any,+ { vcs, code, redirectURL }: { vcs: VCS; code: string; redirectURL: string }+ ): Promise<string> {+ console.log(+ "req",+ `${vcs.instanceURL}/oauth/token?client_id=${vcs.applicationId}&client_secret=${vcs.secret}&code=${code}&redirect_uri=${redirectURL}&grant_type=authorization_code`+ );+ const data = (+ await axios.post(+ `${vcs.instanceURL}/oauth/token?client_id=${vcs.applicationId}&client_secret=${vcs.secret}&code=${code}&redirect_uri=${redirectURL}&grant_type=authorization_code`+ )+ ).data;+ return data.access_token;+ },++ async fetchProjectList(+ {}: any,+ { vcs, token }: { vcs: VCS; token: string }+ ): Promise<Repository[]> {+ console.log(+ "req",+ `${vcs.instanceURL}/${GITLAB_API_PATH}/projects?membership=true&simple=true`+ );+ const data = (+ await axios.get(+ `${vcs.instanceURL}/${GITLAB_API_PATH}/projects?membership=true&simple=true`,+ {+ headers: {+ Authorization: "Bearer " + token,+ },+ }+ )+ ).data;++ return data.map((item: any) => convertGitLabProject(item));+ },+};++const mutations = {};++export default {+ namespaced: true,+ getters,+ actions,+ mutations,+};diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.tsindex 8d77003ecc..277a55946f 100644--- a/frontend/src/types/common.ts+++ b/frontend/src/types/common.ts@@ -12,6 +12,7 @@ import { Activity } from "./activity";import { Message } from "./message";import { Bookmark } from "./bookmark";import { VCS } from "./vcs";+import { Repository } from "./repository";// System bot idexport const SYSTEM_BOT_ID = 1;@@ -89,7 +90,8 @@ export type ResourceType =| "ACTIVITY"| "MESSAGE"| "BOOKMARK"- | "VCS";+ | "VCS"+ | "REPOSITORY";export const unknown = (type: ResourceType@@ -109,7 +111,8 @@ export const unknown = (| Activity| Message| Bookmark- | VCS => {+ | VCS+ | Repository => {// Have to omit creator and updater to avoid recursion.const UNKNOWN_PRINCIPAL: Principal = {id: UNKNOWN_ID,@@ -314,6 +317,13 @@ export const unknown = (secret: "",};+ const UNKONWN_REPOSITORY: Repository = {+ id: UNKNOWN_ID,+ name: "",+ fullPath: "",+ webURL: "",+ };+switch (type) {case "PRINCIPAL":return UNKNOWN_PRINCIPAL;@@ -347,6 +357,8 @@ export const unknown = (return UNKNOWN_BOOKMARK;case "VCS":return UNKNOWN_VCS;+ case "REPOSITORY":+ return UNKONWN_REPOSITORY;}};@@ -369,7 +381,8 @@ export const empty = (| Activity| Message| Bookmark- | VCS => {+ | VCS+ | Repository => {// Have to omit creator and updater to avoid recursion.const EMPTY_PRINCIPAL: Principal = {id: EMPTY_ID,@@ -572,6 +585,13 @@ export const empty = (secret: "",};+ const EMPTY_REPOSITORY: Repository = {+ id: EMPTY_ID,+ name: "",+ fullPath: "",+ webURL: "",+ };+switch (type) {case "PRINCIPAL":return EMPTY_PRINCIPAL;@@ -605,5 +625,7 @@ export const empty = (return EMPTY_BOOKMARK;case "VCS":return EMPTY_VCS;+ case "REPOSITORY":+ return EMPTY_REPOSITORY;}};diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.tsindex f6a9813ce4..1d6c8dac84 100644--- a/frontend/src/types/index.ts+++ b/frontend/src/types/index.ts@@ -15,6 +15,7 @@ export * from "./bookmark";export * from "./activity";export * from "./message";export * from "./vcs";+export * from "./repository";export * from "./auth";export * from "./notification";export * from "./plan";diff --git a/frontend/src/types/oauth.ts b/frontend/src/types/oauth.tsindex 55c9fc7a05..a245da3ec5 100644--- a/frontend/src/types/oauth.ts+++ b/frontend/src/types/oauth.ts@@ -22,7 +22,7 @@ export function openWindowForVCSOAuth(vcs: VCS): Window | null {vcs.applicationId}&redirect_uri=${encodeURIComponent(redirectURL()- )}&response_type=code&state=${stateQueryParameter}`,+ )}&state=${stateQueryParameter}&response_type=code&scope=api`,"oauth","location=yes,left=200,top=200,height=640,width=480,scrollbars=yes,status=yes");diff --git a/frontend/src/types/project.ts b/frontend/src/types/project.tsindex 6de46a6147..accf402371 100644--- a/frontend/src/types/project.ts+++ b/frontend/src/types/project.ts@@ -1,6 +1,8 @@import { RowStatus } from "./common";-import { MemberId, PrincipalId, ProjectId, VCSId } from "./id";+import { MemberId, PrincipalId, ProjectId } from "./id";import { Principal } from "./principal";+import { Repository } from "./repository";+import { VCS } from "./vcs";export type ProjectRoleType = "OWNER" | "DEVELOPER";@@ -78,5 +80,8 @@ export type ProjectMemberPatch = {};export type ProjectRepoConfig = {- vcsId: VCSId;+ vcs: VCS;+ code: string;+ accessToken: string;+ repository: Repository;};diff --git a/frontend/src/types/repository.ts b/frontend/src/types/repository.tsnew file mode 100644index 0000000000..49a9869996--- /dev/null+++ b/frontend/src/types/repository.ts@@ -0,0 +1,11 @@+import { RepositoryId } from "./id";++export type Repository = {+ id: RepositoryId;+ // e.g. sample-project+ name: string;+ // e.g. bytebase/sample-project+ fullPath: string;+ // e.g. http://gitlab.bytebase.com/bytebase/sample-project+ webURL: string;+};