2023-11-28 03:15:08 -08:00
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
2024-12-06 03:53:14 -08:00
|
|
|
import type { IRestApiContext } from '@/Interface';
|
2023-05-31 06:01:57 -07:00
|
|
|
import type {
|
2023-06-20 10:13:18 -07:00
|
|
|
SourceControlAggregatedFile,
|
|
|
|
SourceControlPreferences,
|
|
|
|
SourceControlStatus,
|
2023-09-14 05:40:34 -07:00
|
|
|
SshKeyTypes,
|
2024-12-06 03:53:14 -08:00
|
|
|
} from '@/types/sourceControl.types';
|
|
|
|
|
2023-11-28 03:15:08 -08:00
|
|
|
import { makeRestApiRequest } from '@/utils/apiUtils';
|
2023-09-14 05:40:34 -07:00
|
|
|
import type { TupleToUnion } from '@/utils/typeHelpers';
|
2023-04-26 08:52:53 -07:00
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const sourceControlApiRoot = '/source-control';
|
2023-04-26 08:52:53 -07:00
|
|
|
|
2023-06-06 02:23:53 -07:00
|
|
|
const createPreferencesRequestFn =
|
|
|
|
(method: 'POST' | 'PATCH') =>
|
|
|
|
async (
|
|
|
|
context: IRestApiContext,
|
2023-06-20 10:13:18 -07:00
|
|
|
preferences: Partial<SourceControlPreferences>,
|
|
|
|
): Promise<SourceControlPreferences> =>
|
2024-01-18 01:28:01 -08:00
|
|
|
await makeRestApiRequest(context, method, `${sourceControlApiRoot}/preferences`, preferences);
|
2023-06-06 02:23:53 -07:00
|
|
|
|
2023-05-31 06:01:57 -07:00
|
|
|
export const pushWorkfolder = async (
|
|
|
|
context: IRestApiContext,
|
|
|
|
data: IDataObject,
|
|
|
|
): Promise<void> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'POST', `${sourceControlApiRoot}/push-workfolder`, data);
|
2023-04-26 08:52:53 -07:00
|
|
|
};
|
|
|
|
|
2023-05-31 06:01:57 -07:00
|
|
|
export const pullWorkfolder = async (
|
2023-04-26 08:52:53 -07:00
|
|
|
context: IRestApiContext,
|
2023-05-31 06:01:57 -07:00
|
|
|
data: IDataObject,
|
|
|
|
): Promise<void> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'POST', `${sourceControlApiRoot}/pull-workfolder`, data);
|
2023-04-26 08:52:53 -07:00
|
|
|
};
|
|
|
|
|
2023-05-31 06:01:57 -07:00
|
|
|
export const getBranches = async (
|
|
|
|
context: IRestApiContext,
|
|
|
|
): Promise<{ branches: string[]; currentBranch: string }> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'GET', `${sourceControlApiRoot}/get-branches`);
|
2023-04-26 08:52:53 -07:00
|
|
|
};
|
|
|
|
|
2023-06-06 02:23:53 -07:00
|
|
|
export const savePreferences = createPreferencesRequestFn('POST');
|
|
|
|
export const updatePreferences = createPreferencesRequestFn('PATCH');
|
2023-04-26 08:52:53 -07:00
|
|
|
|
2023-05-10 08:10:03 -07:00
|
|
|
export const getPreferences = async (
|
|
|
|
context: IRestApiContext,
|
2023-06-20 10:13:18 -07:00
|
|
|
): Promise<SourceControlPreferences> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'GET', `${sourceControlApiRoot}/preferences`);
|
2023-04-26 08:52:53 -07:00
|
|
|
};
|
2023-05-31 06:01:57 -07:00
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
export const getStatus = async (context: IRestApiContext): Promise<SourceControlStatus> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'GET', `${sourceControlApiRoot}/status`);
|
2023-05-31 06:01:57 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getAggregatedStatus = async (
|
|
|
|
context: IRestApiContext,
|
2023-07-26 00:25:01 -07:00
|
|
|
options: {
|
|
|
|
direction: 'push' | 'pull';
|
|
|
|
preferLocalVersion: boolean;
|
|
|
|
verbose: boolean;
|
|
|
|
} = { direction: 'push', preferLocalVersion: true, verbose: false },
|
2023-06-20 10:13:18 -07:00
|
|
|
): Promise<SourceControlAggregatedFile[]> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'GET', `${sourceControlApiRoot}/get-status`, options);
|
2023-05-31 06:01:57 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export const disconnect = async (
|
|
|
|
context: IRestApiContext,
|
|
|
|
keepKeyPair: boolean,
|
|
|
|
): Promise<string> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'POST', `${sourceControlApiRoot}/disconnect`, {
|
2023-05-31 06:01:57 -07:00
|
|
|
keepKeyPair,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-09-14 05:40:34 -07:00
|
|
|
export const generateKeyPair = async (
|
|
|
|
context: IRestApiContext,
|
|
|
|
keyGeneratorType?: TupleToUnion<SshKeyTypes>,
|
|
|
|
): Promise<string> => {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'POST', `${sourceControlApiRoot}/generate-key-pair`, {
|
2023-09-14 05:40:34 -07:00
|
|
|
keyGeneratorType,
|
|
|
|
});
|
2023-05-31 06:01:57 -07:00
|
|
|
};
|