2023-04-26 08:52:53 -07:00
|
|
|
import { computed, reactive } from 'vue';
|
2023-04-21 02:25:39 -07:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import { EnterpriseEditionFeature } from '@/constants';
|
2023-06-26 11:17:44 -07:00
|
|
|
import { useSettingsStore, useRootStore, useUsersStore } from '@/stores';
|
2023-06-20 10:13:18 -07:00
|
|
|
import * as vcApi from '@/api/sourceControl';
|
|
|
|
import type { SourceControlPreferences } from '@/Interface';
|
2023-04-21 02:25:39 -07:00
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
export const useSourceControlStore = defineStore('sourceControl', () => {
|
2023-04-26 08:52:53 -07:00
|
|
|
const rootStore = useRootStore();
|
2023-04-21 02:25:39 -07:00
|
|
|
const settingsStore = useSettingsStore();
|
2023-06-26 11:17:44 -07:00
|
|
|
const usersStore = useUsersStore();
|
2023-04-21 02:25:39 -07:00
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const isEnterpriseSourceControlEnabled = computed(() =>
|
|
|
|
settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.SourceControl),
|
2023-04-21 02:25:39 -07:00
|
|
|
);
|
2023-06-26 11:17:44 -07:00
|
|
|
const defaultAuthor = computed(() => {
|
|
|
|
const user = usersStore.currentUser;
|
|
|
|
return {
|
|
|
|
name: user?.fullName ?? `${user?.firstName} ${user?.lastName}`.trim(),
|
|
|
|
email: user?.email ?? '',
|
|
|
|
};
|
|
|
|
});
|
2023-04-21 02:25:39 -07:00
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const preferences = reactive<SourceControlPreferences>({
|
2023-05-31 06:01:57 -07:00
|
|
|
branchName: '',
|
2023-05-12 00:26:41 -07:00
|
|
|
branches: [],
|
2023-06-26 11:17:44 -07:00
|
|
|
authorName: defaultAuthor.value.name,
|
|
|
|
authorEmail: defaultAuthor.value.email,
|
2023-04-26 08:52:53 -07:00
|
|
|
repositoryUrl: '',
|
|
|
|
branchReadOnly: false,
|
2023-06-26 03:43:53 -07:00
|
|
|
branchColor: '#5296D6',
|
2023-04-26 08:52:53 -07:00
|
|
|
connected: false,
|
|
|
|
publicKey: '',
|
|
|
|
});
|
|
|
|
|
2023-05-31 06:01:57 -07:00
|
|
|
const state = reactive<{
|
|
|
|
commitMessage: string;
|
|
|
|
}>({
|
2023-04-26 08:52:53 -07:00
|
|
|
commitMessage: 'commit message',
|
|
|
|
});
|
|
|
|
|
2023-05-31 06:01:57 -07:00
|
|
|
const pushWorkfolder = async (data: { commitMessage: string; fileNames?: string[] }) => {
|
2023-04-26 08:52:53 -07:00
|
|
|
state.commitMessage = data.commitMessage;
|
2023-05-31 06:01:57 -07:00
|
|
|
await vcApi.pushWorkfolder(rootStore.getRestApiContext, {
|
|
|
|
message: data.commitMessage,
|
|
|
|
...(data.fileNames ? { fileNames: data.fileNames } : {}),
|
|
|
|
});
|
2023-04-26 08:52:53 -07:00
|
|
|
};
|
2023-05-31 06:01:57 -07:00
|
|
|
|
|
|
|
const pullWorkfolder = async (force: boolean) => {
|
|
|
|
await vcApi.pullWorkfolder(rootStore.getRestApiContext, { force });
|
2023-04-26 08:52:53 -07:00
|
|
|
};
|
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const setPreferences = (data: Partial<SourceControlPreferences>) => {
|
2023-04-26 08:52:53 -07:00
|
|
|
Object.assign(preferences, data);
|
|
|
|
};
|
|
|
|
|
2023-06-06 02:23:53 -07:00
|
|
|
const makePreferencesAction =
|
|
|
|
(action: typeof vcApi.savePreferences | typeof vcApi.updatePreferences) =>
|
2023-06-20 10:13:18 -07:00
|
|
|
async (preferences: Partial<SourceControlPreferences>) => {
|
2023-06-06 02:23:53 -07:00
|
|
|
const data = await action(rootStore.getRestApiContext, preferences);
|
|
|
|
setPreferences(data);
|
|
|
|
};
|
|
|
|
|
2023-05-31 06:01:57 -07:00
|
|
|
const getBranches = async () => {
|
|
|
|
const data = await vcApi.getBranches(rootStore.getRestApiContext);
|
|
|
|
setPreferences(data);
|
|
|
|
};
|
|
|
|
|
2023-04-26 08:52:53 -07:00
|
|
|
const getPreferences = async () => {
|
|
|
|
const data = await vcApi.getPreferences(rootStore.getRestApiContext);
|
|
|
|
setPreferences(data);
|
|
|
|
};
|
|
|
|
|
2023-06-06 02:23:53 -07:00
|
|
|
const savePreferences = makePreferencesAction(vcApi.savePreferences);
|
2023-04-26 08:52:53 -07:00
|
|
|
|
2023-06-06 02:23:53 -07:00
|
|
|
const updatePreferences = makePreferencesAction(vcApi.updatePreferences);
|
2023-05-31 06:01:57 -07:00
|
|
|
|
|
|
|
const disconnect = async (keepKeyPair: boolean) => {
|
|
|
|
await vcApi.disconnect(rootStore.getRestApiContext, keepKeyPair);
|
|
|
|
setPreferences({ connected: false, branches: [] });
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateKeyPair = async () => {
|
|
|
|
await vcApi.generateKeyPair(rootStore.getRestApiContext);
|
|
|
|
const data = await vcApi.getPreferences(rootStore.getRestApiContext); // To be removed once the API is updated
|
|
|
|
|
|
|
|
preferences.publicKey = data.publicKey;
|
|
|
|
|
|
|
|
return { publicKey: data.publicKey };
|
|
|
|
};
|
|
|
|
|
|
|
|
const getStatus = async () => {
|
|
|
|
return vcApi.getStatus(rootStore.getRestApiContext);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAggregatedStatus = async () => {
|
|
|
|
return vcApi.getAggregatedStatus(rootStore.getRestApiContext);
|
|
|
|
};
|
|
|
|
|
2023-04-21 02:25:39 -07:00
|
|
|
return {
|
2023-06-20 10:13:18 -07:00
|
|
|
isEnterpriseSourceControlEnabled,
|
2023-04-26 08:52:53 -07:00
|
|
|
state,
|
2023-05-12 00:26:41 -07:00
|
|
|
preferences,
|
2023-05-31 06:01:57 -07:00
|
|
|
pushWorkfolder,
|
|
|
|
pullWorkfolder,
|
2023-04-26 08:52:53 -07:00
|
|
|
getPreferences,
|
|
|
|
setPreferences,
|
2023-05-31 06:01:57 -07:00
|
|
|
generateKeyPair,
|
|
|
|
getBranches,
|
2023-04-26 08:52:53 -07:00
|
|
|
savePreferences,
|
2023-06-06 02:23:53 -07:00
|
|
|
updatePreferences,
|
2023-05-31 06:01:57 -07:00
|
|
|
disconnect,
|
|
|
|
getStatus,
|
|
|
|
getAggregatedStatus,
|
2023-04-21 02:25:39 -07:00
|
|
|
};
|
|
|
|
});
|