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-07-28 00:51:07 -07:00
|
|
|
import { useSettingsStore } from '@/stores/settings.store';
|
2024-06-18 10:15:12 -07:00
|
|
|
import { useRootStore } from '@/stores/root.store';
|
2023-06-20 10:13:18 -07:00
|
|
|
import * as vcApi from '@/api/sourceControl';
|
2023-09-14 05:40:34 -07:00
|
|
|
import type { SourceControlPreferences, SshKeyTypes } from '@/Interface';
|
|
|
|
import type { TupleToUnion } from '@/utils/typeHelpers';
|
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();
|
|
|
|
|
2024-07-19 05:35:36 -07:00
|
|
|
const isEnterpriseSourceControlEnabled = computed(
|
|
|
|
() => settingsStore.isEnterpriseFeatureEnabled[EnterpriseEditionFeature.SourceControl],
|
2023-04-21 02:25:39 -07:00
|
|
|
);
|
2023-09-14 05:40:34 -07:00
|
|
|
|
|
|
|
const sshKeyTypes: SshKeyTypes = ['ed25519', 'rsa'];
|
|
|
|
const sshKeyTypesWithLabel = reactive(
|
|
|
|
sshKeyTypes.map((value) => ({ value, label: value.toUpperCase() })),
|
|
|
|
);
|
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-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-09-14 05:40:34 -07:00
|
|
|
keyGeneratorType: 'ed25519',
|
2023-04-26 08:52:53 -07:00
|
|
|
});
|
|
|
|
|
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-07-26 00:25:01 -07:00
|
|
|
const pushWorkfolder = async (data: {
|
|
|
|
commitMessage: string;
|
|
|
|
fileNames?: Array<{
|
|
|
|
conflict: boolean;
|
|
|
|
file: string;
|
|
|
|
id: string;
|
|
|
|
location: string;
|
|
|
|
name: string;
|
|
|
|
status: string;
|
|
|
|
type: string;
|
|
|
|
updatedAt?: string | undefined;
|
|
|
|
}>;
|
|
|
|
force: boolean;
|
|
|
|
}) => {
|
2023-04-26 08:52:53 -07:00
|
|
|
state.commitMessage = data.commitMessage;
|
2024-06-18 10:15:12 -07:00
|
|
|
await vcApi.pushWorkfolder(rootStore.restApiContext, {
|
2023-07-26 00:25:01 -07:00
|
|
|
force: data.force,
|
2023-05-31 06:01:57 -07:00
|
|
|
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) => {
|
2024-06-18 10:15:12 -07:00
|
|
|
return await vcApi.pullWorkfolder(rootStore.restApiContext, { 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 =
|
2023-07-28 04:55:16 -07:00
|
|
|
(action: typeof vcApi.savePreferences) =>
|
2023-06-20 10:13:18 -07:00
|
|
|
async (preferences: Partial<SourceControlPreferences>) => {
|
2024-06-18 10:15:12 -07:00
|
|
|
const data = await action(rootStore.restApiContext, preferences);
|
2023-06-06 02:23:53 -07:00
|
|
|
setPreferences(data);
|
|
|
|
};
|
|
|
|
|
2023-05-31 06:01:57 -07:00
|
|
|
const getBranches = async () => {
|
2024-06-18 10:15:12 -07:00
|
|
|
const data = await vcApi.getBranches(rootStore.restApiContext);
|
2023-05-31 06:01:57 -07:00
|
|
|
setPreferences(data);
|
|
|
|
};
|
|
|
|
|
2023-04-26 08:52:53 -07:00
|
|
|
const getPreferences = async () => {
|
2024-06-18 10:15:12 -07:00
|
|
|
const data = await vcApi.getPreferences(rootStore.restApiContext);
|
2023-04-26 08:52:53 -07:00
|
|
|
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) => {
|
2024-06-18 10:15:12 -07:00
|
|
|
await vcApi.disconnect(rootStore.restApiContext, keepKeyPair);
|
2023-05-31 06:01:57 -07:00
|
|
|
setPreferences({ connected: false, branches: [] });
|
|
|
|
};
|
|
|
|
|
2023-09-14 05:40:34 -07:00
|
|
|
const generateKeyPair = async (keyGeneratorType?: TupleToUnion<SshKeyTypes>) => {
|
2024-06-18 10:15:12 -07:00
|
|
|
await vcApi.generateKeyPair(rootStore.restApiContext, keyGeneratorType);
|
|
|
|
const data = await vcApi.getPreferences(rootStore.restApiContext); // To be removed once the API is updated
|
2023-05-31 06:01:57 -07:00
|
|
|
|
|
|
|
preferences.publicKey = data.publicKey;
|
|
|
|
|
|
|
|
return { publicKey: data.publicKey };
|
|
|
|
};
|
|
|
|
|
|
|
|
const getStatus = async () => {
|
2024-06-18 10:15:12 -07:00
|
|
|
return await vcApi.getStatus(rootStore.restApiContext);
|
2023-05-31 06:01:57 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const getAggregatedStatus = async () => {
|
2024-06-18 10:15:12 -07:00
|
|
|
return await vcApi.getAggregatedStatus(rootStore.restApiContext);
|
2023-05-31 06:01:57 -07:00
|
|
|
};
|
|
|
|
|
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-09-14 05:40:34 -07:00
|
|
|
sshKeyTypesWithLabel,
|
2023-04-21 02:25:39 -07:00
|
|
|
};
|
|
|
|
});
|