🏷️

Workflows/Github

Column
Date
Mar 19, 2022 05:07 PM
Tags
地址
{
  "active": true,
  "connections": {
    "dataArray": {
      "main": [
        [
          {
            "node": "OneAtATime",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "N8N Workflows": {
      "main": [
        [
          {
            "node": "dataArray",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GitHub": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "isDiffOrNew",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "N8N Workflow Detail": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "github_status": {
      "main": [
        [
          {
            "node": "same",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "different",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "new",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "same": {
      "main": [
        [
          {
            "node": "OneAtATime",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "different": {
      "main": [
        [
          {
            "node": "GitHub Edit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "new": {
      "main": [
        [
          {
            "node": "GitHub Create",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GitHub Edit": {
      "main": [
        [
          {
            "node": "OneAtATime",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GitHub Create": {
      "main": [
        [
          {
            "node": "OneAtATime",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isDiffOrNew": {
      "main": [
        [
          {
            "node": "github_status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OneAtATime": {
      "main": [
        [
          {
            "node": "GitHub",
            "type": "main",
            "index": 0
          },
          {
            "node": "N8N Workflow Detail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Globals": {
      "main": [
        [
          {
            "node": "N8N Workflows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily @ 18:00": {
      "main": [
        [
          {
            "node": "Globals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "createdAt": "2022-03-01T13:36:48.007Z",
  "id": "6",
  "name": "Github",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        280
      ]
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\nfor (item of items[0].json.data) {\n  newItems.push({json: item});\n}\nreturn newItems;"
      },
      "name": "dataArray",
      "type": "n8n-nodes-base.function",
      "position": [
        740,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "url": "http://localhost:5678/rest/workflows",
        "options": {}
      },
      "name": "N8N Workflows",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        600,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "file",
        "operation": "get",
        "owner": "={{$node[\"Globals\"].json[\"repo\"][\"owner\"]}}",
        "repository": "={{$node[\"Globals\"].json[\"repo\"][\"name\"]}}",
        "filePath": "={{$node[\"Globals\"].json[\"repo\"][\"path\"]}}{{$json[\"name\"]}}.json",
        "asBinaryProperty": false,
        "additionalParameters": {}
      },
      "name": "GitHub",
      "type": "n8n-nodes-base.github",
      "position": [
        1100,
        100
      ],
      "typeVersion": 1,
      "alwaysOutputData": true,
      "credentials": {
        "githubOAuth2Api": {
          "id": "3",
          "name": "Github account"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {},
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        1300,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "url": "=http://localhost:5678/rest/workflows/{{$json[\"id\"]}}",
        "options": {}
      },
      "name": "N8N Workflow Detail",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1100,
        440
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{$json[\"github_status\"]}}",
        "rules": {
          "rules": [
            {
              "value2": "same"
            },
            {
              "value2": "different",
              "output": 1
            },
            {
              "value2": "new",
              "output": 2
            }
          ]
        }
      },
      "name": "github_status",
      "type": "n8n-nodes-base.switch",
      "position": [
        1600,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {},
      "name": "same",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1800,
        100
      ],
      "typeVersion": 1
    },
    {
      "parameters": {},
      "name": "different",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1800,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {},
      "name": "new",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1800,
        440
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "file",
        "operation": "edit",
        "owner": "={{$node[\"Globals\"].json[\"repo\"][\"owner\"]}}",
        "repository": "={{$node[\"Globals\"].json[\"repo\"][\"name\"]}}",
        "filePath": "={{$node[\"Globals\"].json[\"repo\"][\"path\"]}}{{$node[\"N8N Workflow Detail\"].json[\"data\"][\"name\"]}}.json",
        "fileContent": "={{$node[\"isDiffOrNew\"].json[\"n8n_data_stringy\"]}}",
        "commitMessage": "=[N8N Backup] {{$node[\"N8N Workflow Detail\"].json[\"data\"][\"name\"]}}.json ({{$json[\"github_status\"]}})"
      },
      "name": "GitHub Edit",
      "type": "n8n-nodes-base.github",
      "position": [
        2000,
        160
      ],
      "typeVersion": 1,
      "credentials": {
        "githubOAuth2Api": {
          "id": "3",
          "name": "Github account"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "file",
        "owner": "={{$node[\"Globals\"].json[\"repo\"][\"owner\"]}}",
        "repository": "={{$node[\"Globals\"].json[\"repo\"][\"name\"]}}",
        "filePath": "={{$node[\"Globals\"].json[\"repo\"][\"path\"]}}{{$node[\"N8N Workflow Detail\"].json[\"data\"][\"name\"]}}.json",
        "fileContent": "={{$node[\"isDiffOrNew\"].json[\"n8n_data_stringy\"]}}",
        "commitMessage": "=[N8N Backup] {{$node[\"N8N Workflow Detail\"].json[\"data\"][\"name\"]}}.json ({{$json[\"github_status\"]}})"
      },
      "name": "GitHub Create",
      "type": "n8n-nodes-base.github",
      "position": [
        2000,
        440
      ],
      "typeVersion": 1,
      "credentials": {
        "githubOAuth2Api": {
          "id": "3",
          "name": "Github account"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// File Returned with Content\nif (Object.keys(items[0].json).includes(\"content\")) {\n  // Get JSON Objects\n  var origWorkflow = eval(\"(\"+Buffer.from(items[0].json.content, 'base64').toString()+\")\");\n  var n8nWorkflow = (items[1].json.data);\n  \n  // Order JSON Objects\n  var orderedOriginal = {}\n  var orderedActual = {}\n  \n  Object.keys(origWorkflow).sort().forEach(function(key) {\n    orderedOriginal[key] = origWorkflow[key];\n  });\n  \n  Object.keys(n8nWorkflow).sort().forEach(function(key) {\n    orderedActual[key] = n8nWorkflow[key];\n  });\n  \n  // Determine Difference\n  if ( JSON.stringify(orderedOriginal) === JSON.stringify(orderedActual) ) {\n    items[0].json.github_status = \"same\";\n    items[0].json.content_decoded = orderedOriginal;\n  } else {\n    items[0].json.github_status = \"different\";\n    items[0].json.content_decoded = orderedOriginal;\n    items[0].json.n8n_data_stringy = JSON.stringify(orderedActual, null, 2);\n  }\n// No File Returned / New Workflow\n} else {\n  // Order JSON Object\n  var n8nWorkflow = (items[1].json.data);\n  var orderedActual = {}\n  Object.keys(n8nWorkflow).sort().forEach(function(key) {\n    orderedActual[key] = n8nWorkflow[key];\n  });\n  \n  // Proper Formatting\n  items[0].json.github_status = \"new\";\n  items[0].json.n8n_data_stringy = JSON.stringify(orderedActual, null, 2);\n}\n\n// Return Items\nreturn items;"
      },
      "name": "isDiffOrNew",
      "type": "n8n-nodes-base.function",
      "position": [
        1440,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "OneAtATime",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        900,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "repo.owner",
              "value": "burakyccl"
            },
            {
              "name": "repo.name",
              "value": "n8nWorkflows"
            },
            {
              "name": "repo.path",
              "value": "n8nWorkflows/"
            }
          ]
        },
        "options": {}
      },
      "name": "Globals",
      "type": "n8n-nodes-base.set",
      "position": [
        440,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "hour": 18,
              "minute": 11
            }
          ]
        }
      },
      "name": "Daily @ 18:00",
      "type": "n8n-nodes-base.cron",
      "position": [
        260,
        100
      ],
      "typeVersion": 1
    }
  ],
  "settings": {},
  "staticData": null,
  "tags": [],
  "updatedAt": "2022-03-01T14:43:01.253Z"
}