2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import type { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
2021-06-05 13:43:54 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
addConfigFields,
|
|
|
|
addFields,
|
|
|
|
cloneFields,
|
|
|
|
commitFields,
|
|
|
|
logFields,
|
|
|
|
pushFields,
|
|
|
|
tagFields,
|
|
|
|
} from './descriptions';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { LogOptions, SimpleGit, SimpleGitOptions } from 'simple-git';
|
|
|
|
import simpleGit from 'simple-git';
|
2021-06-05 13:43:54 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { access, mkdir } from 'fs/promises';
|
2021-06-05 13:43:54 -07:00
|
|
|
|
|
|
|
import { URL } from 'url';
|
|
|
|
|
|
|
|
export class Git implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Git',
|
|
|
|
name: 'git',
|
|
|
|
icon: 'file:git.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Control git.',
|
|
|
|
defaults: {
|
|
|
|
name: 'Git',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'gitPassword',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['gitPassword'],
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Authenticate',
|
|
|
|
value: 'gitPassword',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'None',
|
|
|
|
value: 'none',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['clone', 'push'],
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: 'none',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The way to authenticate',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-06-05 13:43:54 -07:00
|
|
|
default: 'log',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Add',
|
|
|
|
value: 'add',
|
|
|
|
description: 'Add a file or folder to commit',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Add a file or folder to commit',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Add Config',
|
|
|
|
value: 'addConfig',
|
|
|
|
description: 'Add configuration property',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Add configuration property',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Clone',
|
|
|
|
value: 'clone',
|
|
|
|
description: 'Clone a repository',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Clone a repository',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Commit',
|
|
|
|
value: 'commit',
|
|
|
|
description: 'Commit files or folders to git',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Commit files or folders to git',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Fetch',
|
|
|
|
value: 'fetch',
|
|
|
|
description: 'Fetch from remote repository',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Fetch from remote repository',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'List Config',
|
|
|
|
value: 'listConfig',
|
|
|
|
description: 'Return current configuration',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Return current configuration',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Log',
|
|
|
|
value: 'log',
|
|
|
|
description: 'Return git commit history',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Return git commit history',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Pull',
|
|
|
|
value: 'pull',
|
|
|
|
description: 'Pull from remote repository',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Pull from remote repository',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push',
|
|
|
|
value: 'push',
|
|
|
|
description: 'Push to remote repository',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Push to remote repository',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Push Tags',
|
|
|
|
value: 'pushTags',
|
|
|
|
description: 'Push Tags to remote repository',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Push tags to remote repository',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Status',
|
|
|
|
value: 'status',
|
|
|
|
description: 'Return status of current repository',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Return status of current repository',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Tag',
|
|
|
|
value: 'tag',
|
|
|
|
description: 'Create a new tag',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Create a new tag',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'User Setup',
|
|
|
|
value: 'userSetup',
|
|
|
|
description: 'Set the user',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Set up a user',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
displayName: 'Repository Path',
|
|
|
|
name: 'repositoryPath',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['clone'],
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: '/tmp/repository',
|
|
|
|
required: true,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Local path of the git repository to operate on',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'New Repository Path',
|
|
|
|
name: 'repositoryPath',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['clone'],
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: '/tmp/repository',
|
|
|
|
required: true,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Local path to which the git repository should be cloned into',
|
2021-06-05 13:43:54 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
...addFields,
|
|
|
|
...addConfigFields,
|
|
|
|
...cloneFields,
|
|
|
|
...commitFields,
|
|
|
|
...logFields,
|
|
|
|
...pushFields,
|
|
|
|
...tagFields,
|
|
|
|
// ...userSetupFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const prepareRepository = async (repositoryPath: string): Promise<string> => {
|
2021-06-05 13:43:54 -07:00
|
|
|
const authentication = this.getNodeParameter('authentication', 0) as string;
|
|
|
|
|
|
|
|
if (authentication === 'gitPassword') {
|
2022-04-14 23:00:47 -07:00
|
|
|
const gitCredentials = await this.getCredentials('gitPassword');
|
2021-06-05 13:43:54 -07:00
|
|
|
|
|
|
|
const url = new URL(repositoryPath);
|
|
|
|
url.username = gitCredentials.username as string;
|
|
|
|
url.password = gitCredentials.password as string;
|
|
|
|
|
|
|
|
return url.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return repositoryPath;
|
|
|
|
};
|
|
|
|
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2022-11-08 06:28:21 -08:00
|
|
|
let _item: INodeExecutionData;
|
2021-06-05 13:43:54 -07:00
|
|
|
const returnItems: INodeExecutionData[] = [];
|
|
|
|
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
|
|
try {
|
2022-11-08 06:28:21 -08:00
|
|
|
_item = items[itemIndex];
|
2021-06-05 13:43:54 -07:00
|
|
|
|
|
|
|
const repositoryPath = this.getNodeParameter('repositoryPath', itemIndex, '') as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
const options = this.getNodeParameter('options', itemIndex, {});
|
2021-06-05 13:43:54 -07:00
|
|
|
|
|
|
|
if (operation === 'clone') {
|
|
|
|
// Create repository folder if it does not exist
|
|
|
|
try {
|
|
|
|
await access(repositoryPath);
|
|
|
|
} catch (error) {
|
|
|
|
await mkdir(repositoryPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const gitOptions: Partial<SimpleGitOptions> = {
|
|
|
|
baseDir: repositoryPath,
|
|
|
|
};
|
|
|
|
|
|
|
|
const git: SimpleGit = simpleGit(gitOptions)
|
|
|
|
// Tell git not to ask for any information via the terminal like for
|
|
|
|
// example the username. As nobody will be able to answer it would
|
|
|
|
// n8n keep on waiting forever.
|
|
|
|
.env('GIT_TERMINAL_PROMPT', '0');
|
|
|
|
|
|
|
|
if (operation === 'add') {
|
|
|
|
// ----------------------------------
|
|
|
|
// add
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const pathsToAdd = this.getNodeParameter('pathsToAdd', itemIndex, '') as string;
|
|
|
|
|
|
|
|
await git.add(pathsToAdd.split(','));
|
|
|
|
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'addConfig') {
|
|
|
|
// ----------------------------------
|
|
|
|
// addConfig
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const key = this.getNodeParameter('key', itemIndex, '') as string;
|
|
|
|
const value = this.getNodeParameter('value', itemIndex, '') as string;
|
|
|
|
let append = false;
|
|
|
|
|
|
|
|
if (options.mode === 'append') {
|
|
|
|
append = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
await git.addConfig(key, value, append);
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'clone') {
|
|
|
|
// ----------------------------------
|
|
|
|
// clone
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
let sourceRepository = this.getNodeParameter('sourceRepository', itemIndex, '') as string;
|
2021-08-20 09:57:30 -07:00
|
|
|
sourceRepository = await prepareRepository(sourceRepository);
|
2021-06-05 13:43:54 -07:00
|
|
|
|
|
|
|
await git.clone(sourceRepository, '.');
|
|
|
|
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'commit') {
|
|
|
|
// ----------------------------------
|
|
|
|
// commit
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const message = this.getNodeParameter('message', itemIndex, '') as string;
|
|
|
|
|
|
|
|
let pathsToAdd: string[] | undefined = undefined;
|
|
|
|
if (options.files !== undefined) {
|
|
|
|
pathsToAdd = (options.pathsToAdd as string).split(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
await git.commit(message, pathsToAdd);
|
|
|
|
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'fetch') {
|
|
|
|
// ----------------------------------
|
|
|
|
// fetch
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
await git.fetch();
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'log') {
|
|
|
|
// ----------------------------------
|
|
|
|
// log
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const logOptions: LogOptions = {};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
logOptions.maxCount = this.getNodeParameter('limit', itemIndex, 100);
|
2021-06-05 13:43:54 -07:00
|
|
|
}
|
|
|
|
if (options.file) {
|
|
|
|
logOptions.file = options.file as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const log = await git.log(logOptions);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
returnItems.push(
|
|
|
|
// @ts-ignore
|
|
|
|
...this.helpers.returnJsonArray(log.all).map((item) => {
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
pairedItem: { item: itemIndex },
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'pull') {
|
|
|
|
// ----------------------------------
|
|
|
|
// pull
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
await git.pull();
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'push') {
|
|
|
|
// ----------------------------------
|
|
|
|
// push
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
if (options.repository) {
|
2021-08-20 09:57:30 -07:00
|
|
|
const targetRepository = await prepareRepository(options.targetRepository as string);
|
2021-06-05 13:43:54 -07:00
|
|
|
await git.push(targetRepository);
|
|
|
|
} else {
|
|
|
|
const authentication = this.getNodeParameter('authentication', 0) as string;
|
|
|
|
if (authentication === 'gitPassword') {
|
|
|
|
// Try to get remote repository path from git repository itself to add
|
|
|
|
// authentication data
|
|
|
|
const config = await git.listConfig();
|
|
|
|
let targetRepository;
|
|
|
|
for (const fileName of Object.keys(config.values)) {
|
|
|
|
if (config.values[fileName]['remote.origin.url']) {
|
|
|
|
targetRepository = config.values[fileName]['remote.origin.url'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
targetRepository = await prepareRepository(targetRepository as string);
|
2021-06-05 13:43:54 -07:00
|
|
|
await git.push(targetRepository);
|
|
|
|
} else {
|
|
|
|
await git.push();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'pushTags') {
|
|
|
|
// ----------------------------------
|
|
|
|
// pushTags
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
await git.pushTags();
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'listConfig') {
|
|
|
|
// ----------------------------------
|
|
|
|
// listConfig
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const config = await git.listConfig();
|
|
|
|
|
|
|
|
const data = [];
|
|
|
|
for (const fileName of Object.keys(config.values)) {
|
|
|
|
data.push({
|
|
|
|
_file: fileName,
|
|
|
|
...config.values[fileName],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
returnItems.push(
|
|
|
|
...this.helpers.returnJsonArray(data).map((item) => {
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
pairedItem: { item: itemIndex },
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'status') {
|
|
|
|
// ----------------------------------
|
|
|
|
// status
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const status = await git.status();
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
returnItems.push(
|
|
|
|
// @ts-ignore
|
|
|
|
...this.helpers.returnJsonArray([status]).map((item) => {
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
pairedItem: { item: itemIndex },
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
2021-06-05 13:43:54 -07:00
|
|
|
} else if (operation === 'tag') {
|
|
|
|
// ----------------------------------
|
|
|
|
// tag
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const name = this.getNodeParameter('name', itemIndex, '') as string;
|
|
|
|
|
|
|
|
await git.addTag(name);
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-06-03 08:25:07 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
error: error.toString(),
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-06-05 13:43:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prepareOutputData(returnItems);
|
|
|
|
}
|
|
|
|
}
|