Add Git node (#1820)

*  Add Git-Node

* 👕 Fix lint issue

* 🎨 add git icon

*  Fix indentation in package.json files

*  Add support for pushTags

*  Add support for addConfig, fetch, listConfig and status

*  Add support for clone

*  Remove not needed code

*  Add proper continueOnFail support & alphabetize options

* 🐛 Remove console.log

*  Improve Git Node

*  Improve Git Node

* 🐳 Add git to Docker images

*  Auto create folder if not exists and clarify addConfig mode

Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
This commit is contained in:
Jan 2021-06-05 15:43:54 -05:00 committed by GitHub
parent ea9f956f0d
commit 3667b0dd01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 768 additions and 3 deletions

View file

@ -29,7 +29,7 @@ FROM node:14.15-alpine
USER root
RUN apk add --update graphicsmagick tzdata tini su-exec
RUN apk add --update graphicsmagick tzdata tini su-exec git
WORKDIR /data

View file

@ -6,7 +6,7 @@ RUN if [ -z "$N8N_VERSION" ] ; then echo "The N8N_VERSION argument is missing!"
RUN \
apt-get update && \
apt-get -y install graphicsmagick gosu
apt-get -y install graphicsmagick gosu git
# Set a custom user to not have n8n run as root
USER root

View file

@ -6,7 +6,7 @@ RUN if [ -z "$N8N_VERSION" ] ; then echo "The N8N_VERSION argument is missing!"
RUN \
apt-get update && \
apt-get -y install graphicsmagick gosu
apt-get -y install graphicsmagick gosu git
RUN npm_config_user=root npm install -g full-icu n8n@${N8N_VERSION}

View file

@ -0,0 +1,28 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class GitPassword implements ICredentialType {
name = 'gitPassword';
displayName = 'Git';
properties = [
{
displayName: 'Username',
name: 'username',
type: 'string' as NodePropertyTypes,
default: '',
decription: 'The username to authenticate with.',
},
{
displayName: 'Password',
name: 'password',
type: 'string' as NodePropertyTypes,
typeOptions: {
password: true,
},
default: '',
description: 'The password to use in combination with the user',
},
];
}

View file

@ -0,0 +1,437 @@
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
addConfigFields,
addFields,
cloneFields,
commitFields,
logFields,
pushFields,
tagFields,
} from './descriptions';
import simpleGit, {
LogOptions,
SimpleGit,
SimpleGitOptions,
} from 'simple-git';
import {
access,
mkdir,
} from 'fs/promises';
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',
color: '#808080',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'gitPassword',
required: true,
displayOptions: {
show: {
authentication: [
'gitPassword',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Authenticate',
value: 'gitPassword',
},
{
name: 'None',
value: 'none',
},
],
displayOptions: {
show: {
operation: [
'clone',
'push',
],
},
},
default: 'none',
description: 'The way to authenticate.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
default: 'log',
description: 'Operation to perform',
options: [
{
name: 'Add',
value: 'add',
description: 'Add a file or folder to commit',
},
{
name: 'Add Config',
value: 'addConfig',
description: 'Add configuration property',
},
{
name: 'Clone',
value: 'clone',
description: 'Clone a repository',
},
{
name: 'Commit',
value: 'commit',
description: 'Commit files or folders to git',
},
{
name: 'Fetch',
value: 'fetch',
description: 'Fetch from remote repository',
},
{
name: 'List Config',
value: 'listConfig',
description: 'Return current configuration',
},
{
name: 'Log',
value: 'log',
description: 'Return git commit history',
},
{
name: 'Pull',
value: 'pull',
description: 'Pull from remote repository',
},
{
name: 'Push',
value: 'push',
description: 'Push to remote repository',
},
{
name: 'Push Tags',
value: 'pushTags',
description: 'Push Tags to remote repository',
},
{
name: 'Status',
value: 'status',
description: 'Return status of current repository',
},
{
name: 'Tag',
value: 'tag',
description: 'Create a new tag',
},
{
name: 'User Setup',
value: 'userSetup',
description: 'Set the user',
},
],
},
{
displayName: 'Repository Path',
name: 'repositoryPath',
type: 'string',
displayOptions: {
hide: {
operation: [
'clone',
],
},
},
default: '',
placeholder: '/tmp/repository',
required: true,
description: 'Local path of the git repository to operate on.',
},
{
displayName: 'New Repository Path',
name: 'repositoryPath',
type: 'string',
displayOptions: {
show: {
operation: [
'clone',
],
},
},
default: '',
placeholder: '/tmp/repository',
required: true,
description: 'Local path to which the git repository should be cloned into.',
},
...addFields,
...addConfigFields,
...cloneFields,
...commitFields,
...logFields,
...pushFields,
...tagFields,
// ...userSetupFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const prepareRepository = (repositoryPath: string): string => {
const authentication = this.getNodeParameter('authentication', 0) as string;
if (authentication === 'gitPassword') {
const gitCredentials = this.getCredentials('gitPassword') as IDataObject;
const url = new URL(repositoryPath);
url.username = gitCredentials.username as string;
url.password = gitCredentials.password as string;
return url.toString();
}
return repositoryPath;
};
const operation = this.getNodeParameter('operation', 0) as string;
let item: INodeExecutionData;
const returnItems: INodeExecutionData[] = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
item = items[itemIndex];
const repositoryPath = this.getNodeParameter('repositoryPath', itemIndex, '') as string;
const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject;
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(','));
returnItems.push({ json: { success: true } });
} 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);
returnItems.push({ json: { success: true } });
} else if (operation === 'clone') {
// ----------------------------------
// clone
// ----------------------------------
let sourceRepository = this.getNodeParameter('sourceRepository', itemIndex, '') as string;
sourceRepository = prepareRepository(sourceRepository);
await git.clone(sourceRepository, '.');
returnItems.push({ json: { success: true } });
} 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);
returnItems.push({ json: { success: true } });
} else if (operation === 'fetch') {
// ----------------------------------
// fetch
// ----------------------------------
await git.fetch();
returnItems.push({ json: { success: true } });
} else if (operation === 'log') {
// ----------------------------------
// log
// ----------------------------------
const logOptions: LogOptions = {};
const returnAll = this.getNodeParameter('returnAll', itemIndex, false) as boolean;
if (returnAll === false) {
logOptions.maxCount = this.getNodeParameter('limit', itemIndex, 100) as number;
}
if (options.file) {
logOptions.file = options.file as string;
}
const log = await git.log(logOptions);
// @ts-ignore
returnItems.push(...this.helpers.returnJsonArray(log.all));
} else if (operation === 'pull') {
// ----------------------------------
// pull
// ----------------------------------
await git.pull();
returnItems.push({ json: { success: true } });
} else if (operation === 'push') {
// ----------------------------------
// push
// ----------------------------------
if (options.repository) {
const targetRepository = prepareRepository(options.targetRepository as string);
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;
}
}
targetRepository = prepareRepository(targetRepository as string);
await git.push(targetRepository);
} else {
await git.push();
}
}
returnItems.push({ json: { success: true } });
} else if (operation === 'pushTags') {
// ----------------------------------
// pushTags
// ----------------------------------
await git.pushTags();
returnItems.push({ json: { success: true } });
} 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
returnItems.push(...this.helpers.returnJsonArray(data));
} else if (operation === 'status') {
// ----------------------------------
// status
// ----------------------------------
const status = await git.status();
// @ts-ignore
returnItems.push(...this.helpers.returnJsonArray([status]));
} else if (operation === 'tag') {
// ----------------------------------
// tag
// ----------------------------------
const name = this.getNodeParameter('name', itemIndex, '') as string;
await git.addTag(name);
returnItems.push({ json: { success: true } });
}
} catch (error) {
if (this.continueOnFail()) {
returnItems.push({ json: { error: error.toString() } });
continue;
}
throw error;
}
}
return this.prepareOutputData(returnItems);
}
}

View file

@ -0,0 +1,71 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const addConfigFields = [
{
displayName: 'Key',
name: 'key',
type: 'string',
displayOptions: {
show: {
operation: [
'addConfig',
],
},
},
default: '',
placeholder: 'user.email',
description: 'Name of the key to set.',
required: true,
},
{
displayName: 'Value',
name: 'value',
type: 'string',
displayOptions: {
show: {
operation: [
'addConfig',
],
},
},
default: '',
placeholder: 'name@example.com',
description: 'Value of the key to set.',
required: true,
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
operation: [
'addConfig',
],
},
},
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Mode',
name: 'mode',
type: 'options',
options: [
{
name: 'Append',
value: 'append',
},
{
name: 'Set',
value: 'set',
},
],
default: 'set',
description: 'Append setting rather than set it in the local config.',
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,22 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const addFields = [
{
displayName: 'Paths to Add',
name: 'pathsToAdd',
type: 'string',
displayOptions: {
show: {
operation: [
'add',
],
},
},
default: '',
placeholder: 'README.md',
description: 'Comma separated list of paths (absolute or relative to Repository Path) of files or folders to add.',
required: true,
},
] as INodeProperties[];

View file

@ -0,0 +1,22 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const cloneFields = [
{
displayName: 'Source Repository',
name: 'sourceRepository',
type: 'string',
displayOptions: {
show: {
operation: [
'clone',
],
},
},
default: '',
placeholder: 'https://github.com/n8n-io/n8n',
description: 'The URL or path of the repository to clone.',
required: true,
},
] as INodeProperties[];

View file

@ -0,0 +1,45 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const commitFields = [
{
displayName: 'Message',
name: 'message',
type: 'string',
displayOptions: {
show: {
operation: [
'commit',
],
},
},
default: '',
description: 'The commit message to use.',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
operation: [
'commit',
],
},
},
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Paths to Add',
name: 'pathsToAdd',
type: 'string',
default: '',
placeholder: '/data/file1.json',
description: `Comma separated list of paths (absolute or relative to Repository Path) of<br />
files or folders to commit. If not set will all "added" files and folders be committed.`,
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,64 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const logFields = [
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: [
'log',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: [
'log',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 100,
},
default: 100,
description: 'How many results to return.',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
operation: [
'log',
],
},
},
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'File',
name: 'file',
type: 'string',
default: 'README.md',
description: 'The path (absolute or relative to Repository Path) of file or folder to get the history of.',
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,30 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const pushFields = [
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
operation: [
'push',
],
},
},
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Target Repository',
name: 'targetRepository',
type: 'string',
default: '',
placeholder: 'https://github.com/n8n-io/n8n',
description: 'The URL or path of the repository to push to.',
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,21 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const tagFields = [
{
displayName: 'Name',
name: 'name',
type: 'string',
displayOptions: {
show: {
operation: [
'tag',
],
},
},
default: '',
description: 'The name of the tag to create.',
required: true,
},
] as INodeProperties[];

View file

@ -0,0 +1,7 @@
export * from './AddDescription';
export * from './AddConfigDescription';
export * from './CloneDescription';
export * from './CommitDescription';
export * from './LogDescription';
export * from './PushDescription';
export * from './TagDescription';

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="97px" height="97px" viewBox="0 0 97 97" enable-background="new 0 0 97 97" xml:space="preserve">
<g>
<path fill="#F05133" d="M92.71,44.408L52.591,4.291c-2.31-2.311-6.057-2.311-8.369,0l-8.33,8.332L46.459,23.19
c2.456-0.83,5.272-0.273,7.229,1.685c1.969,1.97,2.521,4.81,1.67,7.275l10.186,10.185c2.465-0.85,5.307-0.3,7.275,1.671
c2.75,2.75,2.75,7.206,0,9.958c-2.752,2.751-7.208,2.751-9.961,0c-2.068-2.07-2.58-5.11-1.531-7.658l-9.5-9.499v24.997
c0.67,0.332,1.303,0.774,1.861,1.332c2.75,2.75,2.75,7.206,0,9.959c-2.75,2.749-7.209,2.749-9.957,0c-2.75-2.754-2.75-7.21,0-9.959
c0.68-0.679,1.467-1.193,2.307-1.537V36.369c-0.84-0.344-1.625-0.853-2.307-1.537c-2.083-2.082-2.584-5.14-1.516-7.698
L31.798,16.715L4.288,44.222c-2.311,2.313-2.311,6.06,0,8.371l40.121,40.118c2.31,2.311,6.056,2.311,8.369,0L92.71,52.779
C95.021,50.468,95.021,46.719,92.71,44.408z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

View file

@ -88,6 +88,7 @@
"dist/credentials/GetResponseOAuth2Api.credentials.js",
"dist/credentials/GhostAdminApi.credentials.js",
"dist/credentials/GhostContentApi.credentials.js",
"dist/credentials/GitPassword.credentials.js",
"dist/credentials/GithubApi.credentials.js",
"dist/credentials/GithubOAuth2Api.credentials.js",
"dist/credentials/GitlabApi.credentials.js",
@ -369,6 +370,7 @@
"dist/nodes/GetResponse/GetResponse.node.js",
"dist/nodes/GetResponse/GetResponseTrigger.node.js",
"dist/nodes/Ghost/Ghost.node.js",
"dist/nodes/Git/Git.node.js",
"dist/nodes/Github/Github.node.js",
"dist/nodes/Github/GithubTrigger.node.js",
"dist/nodes/Gitlab/Gitlab.node.js",
@ -650,6 +652,7 @@
"request": "^2.88.2",
"rhea": "^1.0.11",
"rss-parser": "^3.7.0",
"simple-git": "^2.36.2",
"snowflake-sdk": "^1.5.3",
"ssh2-sftp-client": "^5.2.1",
"tmp-promise": "^3.0.2",