Skip to content

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

refactor: re-implement policy store module with pinia (#1042)

Liu Jif88e58f9f628
diff --git a/frontend/src/components/DatabaseBackupPanel.vue b/frontend/src/components/DatabaseBackupPanel.vue
index 5f3af9cd0d..40cbc421d3 100644
--- a/frontend/src/components/DatabaseBackupPanel.vue
+++ b/frontend/src/components/DatabaseBackupPanel.vue
@@ -141,7 +141,6 @@ import {
PropType,
defineComponent,
} from "vue";
-import { useStore } from "vuex";
import {
Backup,
BackupCreate,
@@ -158,7 +157,7 @@ import BackupTable from "../components/BackupTable.vue";
import DatabaseBackupCreateForm from "../components/DatabaseBackupCreateForm.vue";
import { cloneDeep, isEqual } from "lodash-es";
import { useI18n } from "vue-i18n";
-import { pushNotification, useBackupStore } from "@/store";
+import { pushNotification, useBackupStore, usePolicyStore } from "@/store";
interface LocalState {
showCreateBackupModal: boolean;
@@ -191,9 +190,9 @@ export default defineComponent({
},
},
setup(props) {
- const store = useStore();
- const { t } = useI18n();
const backupStore = useBackupStore();
+ const policyStore = usePolicyStore();
+ const { t } = useI18n();
const state = reactive<LocalState>({
showCreateBackupModal: false,
@@ -217,7 +216,7 @@ export default defineComponent({
watchEffect(prepareBackupList);
const prepareBackupPolicy = () => {
- store.dispatch("policy/fetchPolicyByEnvironmentAndType", {
+ policyStore.fetchPolicyByEnvironmentAndType({
environmentId: props.database.instance.environment.id,
type: "bb.policy.backup-plan",
});
@@ -296,11 +295,12 @@ export default defineComponent({
});
const backupPolicy = computed(() => {
- const policy = store.getters["policy/policyByEnvironmentIdAndType"](
+ const policy = policyStore.getPolicyByEnvironmentIdAndType(
props.database.instance.environment.id,
"bb.policy.backup-plan"
);
- return (policy.payload as PolicyBackupPlanPolicyPayload).schedule;
+ const payload = policy?.payload;
+ return (payload as PolicyBackupPlanPolicyPayload | undefined)?.schedule;
});
const allowDisableAutoBackup = computed(() => {
diff --git a/frontend/src/store/modules/environment.ts b/frontend/src/store/modules/environment.ts
index 288f119a9f..5c9a40ac68 100644
--- a/frontend/src/store/modules/environment.ts
+++ b/frontend/src/store/modules/environment.ts
@@ -11,6 +11,7 @@ import {
RowStatus,
unknown,
} from "../../types";
+import { usePolicyStore } from "../pinia-modules";
import { getPrincipalFromIncludedList } from "./principal";
function convert(
@@ -91,17 +92,12 @@ const actions = {
commit("upsertEnvironmentList", environmentList);
+ const policyStore = usePolicyStore();
for (const environment of environmentList) {
- await dispatch(
- "policy/fetchPolicyByEnvironmentAndType",
- {
- environmentId: environment.id,
- type: "bb.policy.pipeline-approval",
- },
- {
- root: true,
- }
- );
+ await policyStore.fetchPolicyByEnvironmentAndType({
+ environmentId: environment.id,
+ type: "bb.policy.pipeline-approval",
+ });
}
return environmentList;
@@ -123,16 +119,10 @@ const actions = {
commit("upsertEnvironmentList", [createdEnvironment]);
- await dispatch(
- "policy/fetchPolicyByEnvironmentAndType",
- {
- environmentId: createdEnvironment.id,
- type: "bb.policy.pipeline-approval",
- },
- {
- root: true,
- }
- );
+ await usePolicyStore().fetchPolicyByEnvironmentAndType({
+ environmentId: createdEnvironment.id,
+ type: "bb.policy.pipeline-approval",
+ });
return createdEnvironment;
},
diff --git a/frontend/src/store/modules/policy.ts b/frontend/src/store/modules/policy.ts
deleted file mode 100644
index bc4a9d14b5..0000000000
--- a/frontend/src/store/modules/policy.ts
+++ /dev/null
@@ -1,145 +0,0 @@
-import axios from "axios";
-import {
- Environment,
- EnvironmentId,
- PolicyState,
- ResourceIdentifier,
- ResourceObject,
- unknown,
-} from "../../types";
-import { Policy, PolicyType, PolicyUpsert } from "../../types/policy";
-import { getPrincipalFromIncludedList } from "./principal";
-
-function convert(
- policy: ResourceObject,
- includedList: ResourceObject[],
- rootGetters: any
-): Policy {
- const environmentId = (
- policy.relationships!.environment.data as ResourceIdentifier
- ).id;
- let environment: Environment = unknown("ENVIRONMENT") as Environment;
- environment.id = parseInt(environmentId);
-
- for (const item of includedList || []) {
- if (
- item.type == "environment" &&
- (policy.relationships!.environment.data as ResourceIdentifier).id ==
- item.id
- ) {
- environment = rootGetters["environment/convert"](item, includedList);
- }
- }
-
- return {
- ...(policy.attributes as Omit<
- Policy,
- "id" | "environment" | "payload" | "creator" | "updater"
- >),
- id: parseInt(policy.id),
- creator: getPrincipalFromIncludedList(
- policy.relationships!.creator.data,
- includedList
- ),
- updater: getPrincipalFromIncludedList(
- policy.relationships!.updater.data,
- includedList
- ),
- environment,
- payload: JSON.parse((policy.attributes.payload as string) || "{}"),
- };
-}
-
-const state: () => PolicyState = () => ({
- policyMapByEnvironmentId: new Map(),
-});
-
-const getters = {
- policyByEnvironmentIdAndType:
- (state: PolicyState) =>
- (environmentId: EnvironmentId, type: PolicyType): Policy | undefined => {
- const map = state.policyMapByEnvironmentId.get(environmentId);
- if (map) {
- return map.get(type);
- }
- return undefined;
- },
-};
-
-const actions = {
- async fetchPolicyByEnvironmentAndType(
- { commit, rootGetters }: any,
- { environmentId, type }: { environmentId: EnvironmentId; type: PolicyType }
- ): Promise<Policy> {
- const data = (
- await axios.get(`/api/policy/environment/${environmentId}?type=${type}`)
- ).data;
- const policy = convert(data.data, data.included, rootGetters);
- commit("setPolicyByEnvironmentId", { environmentId, policy });
-
- return policy;
- },
-
- async upsertPolicyByEnvironmentAndType(
- { commit, rootGetters }: any,
- {
- environmentId,
- type,
- policyUpsert,
- }: {
- environmentId: EnvironmentId;
- type: PolicyType;
- policyUpsert: PolicyUpsert;
- }
- ): Promise<Policy> {
- const data = (
- await axios.patch(
- `/api/policy/environment/${environmentId}?type=${type}`,
- {
- data: {
- type: "policyUpsert",
- attributes: {
- payload: JSON.stringify(policyUpsert.payload),
- },
- },
- }
- )
- ).data;
- const policy = convert(data.data, data.included, rootGetters);
-
- commit("setPolicyByEnvironmentId", { environmentId, policy });
-
- return policy;
- },
-};
-
-const mutations = {
- setPolicyByEnvironmentId(
- state: PolicyState,
- {
- environmentId,
- policy,
- }: {
- environmentId: EnvironmentId;
- policy: Policy;
- }
- ) {
- const map = state.policyMapByEnvironmentId.get(environmentId);
- if (map) {
- map.set(policy.type, policy);
- } else {
- state.policyMapByEnvironmentId.set(
- environmentId,
- new Map([[policy.type, policy]])
- );
- }
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index adc28fff5d..fae2197d3a 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -8,6 +8,7 @@ export * from "./issueSubscriber";
export * from "./label";
export * from "./notification";
export * from "./oauth";
+export * from "./policy";
export * from "./router";
export * from "./setting";
export * from "./subscription";
diff --git a/frontend/src/store/pinia-modules/policy.ts b/frontend/src/store/pinia-modules/policy.ts
new file mode 100644
index 0000000000..ab51788d3e
--- /dev/null
+++ b/frontend/src/store/pinia-modules/policy.ts
@@ -0,0 +1,130 @@
+import axios from "axios";
+import {
+ Environment,
+ EnvironmentId,
+ PolicyState,
+ ResourceIdentifier,
+ ResourceObject,
+ unknown,
+} from "@/types";
+import { Policy, PolicyType, PolicyUpsert } from "@/types/policy";
+import { getPrincipalFromIncludedList } from "../modules/principal";
+import { store } from "../index";
+import { defineStore } from "pinia";
+
+function convert(
+ policy: ResourceObject,
+ includedList: ResourceObject[]
+): Policy {
+ const environmentId = (
+ policy.relationships!.environment.data as ResourceIdentifier
+ ).id;
+ let environment: Environment = unknown("ENVIRONMENT") as Environment;
+ environment.id = parseInt(environmentId);
+
+ for (const item of includedList || []) {
+ if (
+ item.type == "environment" &&
+ (policy.relationships!.environment.data as ResourceIdentifier).id ==
+ item.id
+ ) {
+ environment = store.getters["environment/convert"](item, includedList);
+ }
+ }
+
+ return {
+ ...(policy.attributes as Omit<
+ Policy,
+ "id" | "environment" | "payload" | "creator" | "updater"
+ >),
+ id: parseInt(policy.id),
+ creator: getPrincipalFromIncludedList(
+ policy.relationships!.creator.data,
+ includedList
+ ),
+ updater: getPrincipalFromIncludedList(
+ policy.relationships!.updater.data,
+ includedList
+ ),
+ environment,
+ payload: JSON.parse((policy.attributes.payload as string) || "{}"),
+ };
+}
+
+export const usePolicyStore = defineStore("policy", {
+ state: (): PolicyState => ({
+ policyMapByEnvironmentId: new Map(),
+ }),
+ actions: {
+ getPolicyByEnvironmentIdAndType(
+ environmentId: EnvironmentId,
+ type: PolicyType
+ ): Policy | undefined {
+ const map = this.policyMapByEnvironmentId.get(environmentId);
+ if (map) {
+ return map.get(type);
+ }
+ return undefined;
+ },
+ setPolicyByEnvironmentId({
+ environmentId,
+ policy,
+ }: {
+ environmentId: EnvironmentId;
+ policy: Policy;
+ }) {
+ const map = this.policyMapByEnvironmentId.get(environmentId);
+ if (map) {
+ map.set(policy.type, policy);
+ } else {
+ this.policyMapByEnvironmentId.set(
+ environmentId,
+ new Map([[policy.type, policy]])
+ );
+ }
+ },
+ async fetchPolicyByEnvironmentAndType({
+ environmentId,
+ type,
+ }: {
+ environmentId: EnvironmentId;
+ type: PolicyType;
+ }): Promise<Policy> {
+ const data = (
+ await axios.get(`/api/policy/environment/${environmentId}?type=${type}`)
+ ).data;
+ const policy = convert(data.data, data.included);
+ this.setPolicyByEnvironmentId({ environmentId, policy });
+
+ return policy;
+ },
+ async upsertPolicyByEnvironmentAndType({
+ environmentId,
+ type,
+ policyUpsert,
+ }: {
+ environmentId: EnvironmentId;
+ type: PolicyType;
+ policyUpsert: PolicyUpsert;
+ }): Promise<Policy> {
+ const data = (
+ await axios.patch(
+ `/api/policy/environment/${environmentId}?type=${type}`,
+ {
+ data: {
+ type: "policyUpsert",
+ attributes: {
+ payload: JSON.stringify(policyUpsert.payload),
+ },
+ },
+ }
+ )
+ ).data;
+ const policy = convert(data.data, data.included);
+
+ this.setPolicyByEnvironmentId({ environmentId, policy });
+
+ return policy;
+ },
+ },
+});
diff --git a/frontend/src/views/EnvironmentDashboard.vue b/frontend/src/views/EnvironmentDashboard.vue
index b58939ce05..a517276a04 100644
--- a/frontend/src/views/EnvironmentDashboard.vue
+++ b/frontend/src/views/EnvironmentDashboard.vue
@@ -89,9 +89,16 @@ import {
PolicyUpsert,
DefaultApporvalPolicy,
DefaultSchedulePolicy,
+ PipelineApporvalPolicyPayload,
+ PolicyBackupPlanPolicyPayload,
} from "../types";
import { BBTabItem } from "../bbkit/types";
-import { useRegisterCommand, useUIStateStore, hasFeature } from "@/store";
+import {
+ useRegisterCommand,
+ useUIStateStore,
+ hasFeature,
+ usePolicyStore,
+} from "@/store";
const DEFAULT_NEW_ENVIRONMENT: EnvironmentCreate = {
name: "New Env",
@@ -132,6 +139,7 @@ export default defineComponent({
setup() {
const store = useStore();
const uiStateStore = useUIStateStore();
+ const policyStore = usePolicyStore();
const router = useRouter();
const state = reactive<LocalState>({
@@ -228,14 +236,16 @@ export default defineComponent({
backupPolicy: Policy
) => {
if (
- approvalPolicy.payload.value !== DefaultApporvalPolicy &&
+ (approvalPolicy.payload as PipelineApporvalPolicyPayload).value !==
+ DefaultApporvalPolicy &&
!hasFeature("bb.feature.approval-policy")
) {
state.missingRequiredFeature = "bb.feature.approval-policy";
return;
}
if (
- backupPolicy.payload.schedule !== DefaultSchedulePolicy &&
+ (backupPolicy.payload as PolicyBackupPlanPolicyPayload).schedule !==
+ DefaultSchedulePolicy &&
!hasFeature("bb.feature.backup-policy")
) {
state.missingRequiredFeature = "bb.feature.backup-policy";
@@ -246,12 +256,12 @@ export default defineComponent({
.dispatch("environment/createEnvironment", newEnvironment)
.then((environment: Environment) => {
Promise.all([
- store.dispatch("policy/upsertPolicyByEnvironmentAndType", {
+ policyStore.upsertPolicyByEnvironmentAndType({
environmentId: environment.id,
type: "bb.policy.pipeline-approval",
policyUpsert: { payload: approvalPolicy.payload },
}),
- store.dispatch("policy/upsertPolicyByEnvironmentAndType", {
+ policyStore.upsertPolicyByEnvironmentAndType({
environmentId: environment.id,
type: "bb.policy.backup-plan",
policyUpsert: { payload: backupPolicy.payload },
diff --git a/frontend/src/views/EnvironmentDetail.vue b/frontend/src/views/EnvironmentDetail.vue
index 65d196440d..28cb43a2d9 100644
--- a/frontend/src/views/EnvironmentDetail.vue
+++ b/frontend/src/views/EnvironmentDetail.vue
@@ -32,9 +32,11 @@ import {
PolicyType,
DefaultApporvalPolicy,
DefaultSchedulePolicy,
+ PipelineApporvalPolicyPayload,
+ PolicyBackupPlanPolicyPayload,
} from "../types";
import { idFromSlug } from "../utils";
-import { hasFeature } from "@/store";
+import { hasFeature, usePolicyStore } from "@/store";
interface LocalState {
environment: Environment;
@@ -61,6 +63,7 @@ export default defineComponent({
emits: ["archive"],
setup(props, { emit }) {
const store = useStore();
+ const policyStore = usePolicyStore();
const state = reactive<LocalState>({
environment: store.getters["environment/environmentById"](
@@ -70,8 +73,8 @@ export default defineComponent({
});
const preparePolicy = () => {
- store
- .dispatch("policy/fetchPolicyByEnvironmentAndType", {
+ policyStore
+ .fetchPolicyByEnvironmentAndType({
environmentId: (state.environment as Environment).id,
type: "bb.policy.pipeline-approval",
})
@@ -79,8 +82,8 @@ export default defineComponent({
state.approvalPolicy = policy;
});
- store
- .dispatch("policy/fetchPolicyByEnvironmentAndType", {
+ policyStore
+ .fetchPolicyByEnvironmentAndType({
environmentId: (state.environment as Environment).id,
type: "bb.policy.backup-plan",
})
@@ -140,7 +143,8 @@ export default defineComponent({
) => {
if (
type === "bb.policy.pipeline-approval" &&
- policy.payload.value !== DefaultApporvalPolicy &&
+ (policy.payload as PipelineApporvalPolicyPayload).value !==
+ DefaultApporvalPolicy &&
!hasFeature("bb.feature.approval-policy")
) {
state.missingRequiredFeature = "bb.feature.approval-policy";
@@ -148,14 +152,15 @@ export default defineComponent({
}
if (
type === "bb.policy.backup-plan" &&
- policy.payload.schedule !== DefaultSchedulePolicy &&
+ (policy.payload as PolicyBackupPlanPolicyPayload).schedule !==
+ DefaultSchedulePolicy &&
!hasFeature("bb.feature.backup-policy")
) {
state.missingRequiredFeature = "bb.feature.backup-policy";
return;
}
- store
- .dispatch("policy/upsertPolicyByEnvironmentAndType", {
+ policyStore
+ .upsertPolicyByEnvironmentAndType({
environmentId,
type: type,
policyUpsert: {