Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Generalize OutputField logic
天舟ecb4e0991f7d
diff --git a/frontend/src/miragejs/seeds/index.ts b/frontend/src/miragejs/seeds/index.tsindex 0063ab5df8..519dab9ead 100644--- a/frontend/src/miragejs/seeds/index.ts+++ b/frontend/src/miragejs/seeds/index.ts@@ -1,22 +1,13 @@import faker from "faker";-import environment from "../../store/modules/environment";import {Database,Environment,Instance,- Principal,- StageType,- Issue,DEFAULT_PROJECT_ID,ALL_DATABASE_NAME,- IssueNew,IssueStatus,- StageNew,- StageId,- DatabaseId,ProjectId,PrincipalId,- IssueId,TaskStatus,PipelineId,} from "../../types";@@ -179,6 +170,14 @@ const workspacesSeeder = (server: any) => {);// Issue+ let pipeline = server.create("pipeline", {+ name: `Pipeline hello world`,+ creatorId: ws1Dev1.id,+ updaterId: ws1Dev1.id,+ status: "PENDING",+ workspace: workspace1,+ });+let issue = server.create("issue", {type: "bytebase.general",name: "Hello, World!",@@ -191,6 +190,30 @@ const workspacesSeeder = (server: any) => {assigneeId: ws1Owner.id,subscriberIdList: [ws1DBA.id, ws1Dev2.id, ws1Dev1.id, ws1Owner.id],project: projectList1[0],+ pipeline,+ workspace: workspace1,+ });++ let stage = server.create("stage", {+ creatorId: ws1Dev1.id,+ updaterId: ws1Dev1.id,+ name: environmentList1[3].name,+ type: "bytebase.stage.database.create",+ status: "PENDING",+ environmentId: environmentList1[3].id,+ pipeline,+ workspace: workspace1,+ });++ server.create("task", {+ creatorId: ws1Dev1.id,+ updaterId: ws1Dev1.id,+ name: "Waiting approval",+ type: "bytebase.task.approve",+ status: "PENDING",+ databaseId: databaseList1[3].id,+ pipeline,+ stage,workspace: workspace1,});@@ -348,7 +371,7 @@ const workspacesSeeder = (server: any) => {const title = `Create database '${databaseList1[1].name}' for environment - ${environmentList1[1].name}`;- let pipeline = server.create("pipeline", {+ pipeline = server.create("pipeline", {name: `Pipeline ${title}`,creatorId: ws1Dev1.id,updaterId: ws1Dev1.id,@@ -364,37 +387,35 @@ const workspacesSeeder = (server: any) => {assigneeId: ws1Owner.id,subscriberIdList: [ws1DBA.id, ws1Dev2.id],payload: {- 5: projectList1[1].id,- 6: environmentList1[1].id,- 8: databaseList1[1].name,+ 100: "newdb1",},project: projectList1[1],pipeline,workspace: workspace1,});- // const stage = server.create("stage", {- // creatorId: ws1Dev1.id,- // updaterId: ws1Dev1.id,- // name: "Create database",- // type: "bytebase.stage.database.create",- // status: "PENDING",- // environmentId: environmentList1[1].id,- // pipeline,- // workspace: workspace1,- // });-- // server.create("task", {- // creatorId: ws1Dev1.id,- // updaterId: ws1Dev1.id,- // name: "Waiting approval",- // type: "bytebase.task.approve",- // status: "PENDING",- // databaseId: databaseList1[1].id,- // pipeline,- // stage,- // workspace: workspace1,- // });+ stage = server.create("stage", {+ creatorId: ws1Dev1.id,+ updaterId: ws1Dev1.id,+ name: "Create database",+ type: "bytebase.stage.database.create",+ status: "PENDING",+ environmentId: environmentList1[1].id,+ pipeline,+ workspace: workspace1,+ });++ server.create("task", {+ creatorId: ws1Dev1.id,+ updaterId: ws1Dev1.id,+ name: "Waiting approval",+ type: "bytebase.task.approve",+ status: "PENDING",+ databaseId: databaseList1[1].id,+ pipeline,+ stage,+ workspace: workspace1,+ });server.create("activity", {actionType: "bytebase.issue.create",diff --git a/frontend/src/plugins/issue/DatabaseCreateTemplate.ts b/frontend/src/plugins/issue/DatabaseCreateTemplate.tsindex 7e487e4b68..7be0686e8b 100644--- a/frontend/src/plugins/issue/DatabaseCreateTemplate.ts+++ b/frontend/src/plugins/issue/DatabaseCreateTemplate.ts@@ -7,7 +7,8 @@ import {IssueContext,INPUT_CUSTOM_FIELD_ID_BEGIN,} from "../types";-import { EMPTY_ID, IssueNew, UNKNOWN_ID } from "../../types";+import { EMPTY_ID, Issue, IssueNew, Pipeline, UNKNOWN_ID } from "../../types";+import { activeEnvironment, fullDatabasePath } from "../../utils";const INPUT_DATABASE_NAME = INPUT_CUSTOM_FIELD_ID_BEGIN;const OUTPUT_DATABASE_FIELD_ID = OUTPUT_CUSTOM_FIELD_ID_BEGIN;@@ -70,7 +71,38 @@ const template: IssueTemplate = {}const requestedName = ctx.issue.payload[INPUT_DATABASE_NAME];const database = ctx.store.getters["database/databaseById"](databaseId);- return database && database.name == requestedName;+ return database.name == requestedName;+ },+ actionText: "+ Create",+ actionLink: (ctx: IssueContext): string => {+ const queryParamList: string[] = [];++ const issue = ctx.issue as Issue;++ queryParamList.push(`project=${issue.project.id}`);++ const environment = activeEnvironment(issue.pipeline!);+ queryParamList.push(`environment=${environment.id}`);++ const databaseName = issue.payload[INPUT_DATABASE_NAME];+ queryParamList.push(`name=${databaseName}`);++ queryParamList.push(`issue=${issue.id}`);++ queryParamList.push(`from=${issue.type}`);++ return "/db/new?" + queryParamList.join("&");+ },+ viewLink: (ctx: IssueContext): string => {+ const databaseId = ctx.issue.payload[OUTPUT_DATABASE_FIELD_ID];+ const database = ctx.store.getters["database/databaseById"](databaseId);+ if (database.id != UNKNOWN_ID) {+ return fullDatabasePath(database);+ }+ return "";+ },+ resolveStatusText: (resolved: boolean): string => {+ return resolved ? "(Created)" : "(To be created)";},},],diff --git a/frontend/src/plugins/issue/DatabaseGrantTemplate.ts b/frontend/src/plugins/issue/DatabaseGrantTemplate.tsindex 19fe9f9d65..92332d915b 100644--- a/frontend/src/plugins/issue/DatabaseGrantTemplate.ts+++ b/frontend/src/plugins/issue/DatabaseGrantTemplate.ts@@ -8,7 +8,7 @@ import {OUTPUT_CUSTOM_FIELD_ID_BEGIN,} from "../types";import { IssueNew, EnvironmentId, UNKNOWN_ID, Issue } from "../../types";-import { allowDatabaseAccess } from "../../utils";+import { allowDatabaseAccess, fullDatabasePath } from "../../utils";const INPUT_READ_ONLY_FIELD_ID = INPUT_CUSTOM_FIELD_ID_BEGIN;const OUTPUT_DATABASE_FIELD_ID = OUTPUT_CUSTOM_FIELD_ID_BEGIN;@@ -58,18 +58,58 @@ const template: IssueTemplate = {],outputFieldList: [{- // This is the same ID as the INPUT database field because the granted database should be the same- // as the requested database.id: OUTPUT_DATABASE_FIELD_ID,name: "Granted database",type: "Database",resolved: (ctx: IssueContext): boolean => {- const databaseId = ctx.issue.payload[OUTPUT_DATABASE_FIELD_ID];- const database = ctx.store.getters["database/databaseById"](databaseId);+ const issue = ctx.issue as Issue;+ const database = issue.pipeline.stageList[0].taskList[0].database;const creator = (ctx.issue as Issue).creator;const type = ctx.issue.payload[INPUT_READ_ONLY_FIELD_ID] ? "RO" : "RW";return allowDatabaseAccess(database, creator, type);},+ actionText: "+ Grant",+ actionLink: (ctx: IssueContext): string => {+ const queryParamList: string[] = [];++ const issue = ctx.issue as Issue;+ const database = issue.pipeline.stageList[0].taskList[0].database;+ const readOnly = issue.payload[INPUT_READ_ONLY_FIELD_ID];+ let dataSourceId;+ for (const dataSource of database.dataSourceList) {+ if (readOnly && dataSource.type == "RO") {+ dataSourceId = dataSource.id;+ break;+ } else if (!readOnly && dataSource.type == "RW") {+ dataSourceId = dataSource.id;+ break;+ }+ }++ if (dataSourceId) {+ queryParamList.push(`database=${database.id}`);++ queryParamList.push(`datasource=${dataSourceId}`);++ queryParamList.push(`grantee=${issue.creator.id}`);++ queryParamList.push(`issue=${issue.id}`);++ return "/db/grant?" + queryParamList.join("&");+ }+ return "";+ },+ viewLink: (ctx: IssueContext): string => {+ const issue = ctx.issue as Issue;+ const database = issue.pipeline.stageList[0].taskList[0].database;+ if (database.id != UNKNOWN_ID) {+ return fullDatabasePath(database);+ }+ return "";+ },+ resolveStatusText: (resolved: boolean): string => {+ return resolved ? "(Granted)" : "(To be granted)";+ },},],};diff --git a/frontend/src/plugins/types.ts b/frontend/src/plugins/types.tsindex 02ac9af476..25a4847dc8 100644--- a/frontend/src/plugins/types.ts+++ b/frontend/src/plugins/types.ts@@ -94,6 +94,14 @@ export type OutputField = {resolved: (ctx: IssueContext) => boolean;// Same as InputField.placeholderplaceholder?: string;+ // Link text+ actionText: string;+ // Link to the place fullfilling this field resource+ actionLink: (ctx: IssueContext) => string;+ // Link to view the field resource+ viewLink: (ctx: IssueContext) => string;+ // Corresponding text based on whether the field is resolved+ resolveStatusText: (resolved: boolean) => string;};// Field payload for "Database" and "NewDatabase" fielddiff --git a/frontend/src/views/IssueOutputPanel.vue b/frontend/src/views/IssueOutputPanel.vueindex 9535b33aca..133ccad348 100644--- a/frontend/src/views/IssueOutputPanel.vue+++ b/frontend/src/views/IssueOutputPanel.vue@@ -11,17 +11,13 @@<div class="flex flex-col space-y-1"><div class="textlabel">{{ field.name }}- <span v-if="field.required" class="text-red-600">*</span>+ <span class="text-red-600">*</span><template v-if="allowEditDatabase"><router-link- :to="databaseActionLink(field)"+ :to="field.actionLink(issueContext)"class="ml-2 normal-link">- {{- issue.type == "bytebase.database.create"- ? "+ Create"- : "+ Grant"- }}+ {{ field.actionText }}</router-link></template></div>@@ -97,32 +93,20 @@}"/>- <template v-if="databaseViewLink(field)">+ <template v-if="field.viewLink(issueContext)"><router-link- :to="databaseViewLink(field)"+ :to="field.viewLink(issueContext)"class="ml-2 normal-link text-sm">View</router-link></template>- <template v-if="issue.type == 'bytebase.database.create'">- <div- v-if="field.resolved(issueContext)"- class="text-sm text-success"- >- (Created)- </div>- <div v-else class="text-sm text-error">(To be created)</div>- </template>- <template v-else-if="issue.type == 'bytebase.database.grant'">- <div- v-if="field.resolved(issueContext)"- class="text-sm text-success"- >- (Granted)- </div>- <div v-else class="text-sm text-error">(To be granted)</div>- </template>+ <div v-if="field.resolved(issueContext)" class="text-sm text-success">+ {{ field.resolveStatusText(true) }}+ </div>+ <div v-else class="text-sm text-error">+ {{ field.resolveStatusText(false) }}+ </div></div></div></template>@@ -136,7 +120,7 @@ import { useRouter } from "vue-router";import isEqual from "lodash-es/isEqual";import { toClipboard } from "@soerenmartius/vue3-clipboard";import DatabaseSelect from "../components/DatabaseSelect.vue";-import { fullDatabasePath } from "../utils";+import { activeEnvironment, fullDatabasePath } from "../utils";import { OutputField, IssueBuiltinFieldId, IssueContext } from "../plugins";import { DatabaseId, EnvironmentId, Issue, UNKNOWN_ID } from "../types";@@ -170,7 +154,7 @@ export default {const environmentId = computed((): EnvironmentId => {- return props.issue.payload[IssueBuiltinFieldId.ENVIRONMENT];+ return activeEnvironment(props.issue.pipeline).id;});@@ -203,74 +187,6 @@ export default {return link?.trim().length > 0;};- const databaseActionLink = (field: OutputField): string => {- const queryParamList: string[] = [];-- if (props.issue.type == "bytebase.database.create") {- if (environmentId.value) {- queryParamList.push(`environment=${environmentId.value}`);- }-- const databaseName = props.issue.payload[field.id];- queryParamList.push(`name=${databaseName}`);-- queryParamList.push(`issue=${props.issue.id}`);-- queryParamList.push(`from=${props.issue.type}`);-- return "/db/new?" + queryParamList.join("&");- }-- if (props.issue.type == "bytebase.database.grant") {- const databaseId = props.issue.payload[IssueBuiltinFieldId.DATABASE];- if (databaseId) {- const database = store.getters["database/databaseById"](databaseId);- if (database) {- // TODO: Hard-code from DatabaseGrantTemplate- const READ_ONLY_ID = 100;- const readOnly = props.issue.payload[READ_ONLY_ID];- let dataSourceId;- for (const dataSource of database.dataSourceList) {- if (readOnly && dataSource.type == "RO") {- dataSourceId = dataSource.id;- break;- } else if (!readOnly && dataSource.type == "RW") {- dataSourceId = dataSource.id;- break;- }- }-- if (dataSourceId) {- queryParamList.push(`database=${databaseId}`);-- queryParamList.push(`datasource=${dataSourceId}`);-- queryParamList.push(`grantee=${props.issue.creator.id}`);-- queryParamList.push(`issue=${props.issue.id}`);-- return "/db/grant?" + queryParamList.join("&");- }- }- }- }-- return "";- };-- const databaseViewLink = (field: OutputField): string => {- if (field.type == "Database") {- const databaseId = fieldValue(field);- if (databaseId) {- const database = store.getters["database/databaseById"](databaseId);- if (database) {- return fullDatabasePath(database);- }- }- }- return "";- };-const copyText = (field: OutputField) => {toClipboard(props.issue.payload[field.id]).then(() => {store.dispatch("notification/pushNotification", {@@ -312,8 +228,6 @@ export default {issueContext,allowEditDatabase,isValidLink,- databaseActionLink,- databaseViewLink,copyText,goToLink,trySaveCustomField,