{
  "name": "process-backup",
  "nodes": [
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        -32,
        128
      ],
      "id": "a08bc566-2e68-4a2d-9a4a-1dc0183afaef",
      "name": "Start"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "343cb6d7-3a7f-43eb-8c0e-71c6b463715f",
              "name": "repoOwner",
              "value": "",
              "type": "string"
            },
            {
              "id": "be8de35d-805b-4d73-9441-07d401e60dd5",
              "name": "repoName",
              "value": "n8n-backup",
              "type": "string"
            },
            {
              "id": "613e19de-6481-408c-83d8-aafc73616e5a",
              "name": "repoPath",
              "value": "workflows/",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        208,
        0
      ],
      "id": "05f7e0de-3adf-4f8c-9e91-1a4473102493",
      "name": "Repo Data"
    },
    {
      "parameters": {
        "resource": "file",
        "operation": "get",
        "owner": {
          "__rl": true,
          "value": "={{ $json.repoOwner }}",
          "mode": "name"
        },
        "repository": {
          "__rl": true,
          "value": "={{ $json.repoName }}",
          "mode": "name"
        },
        "filePath": "={{ $json.repoPath }}{{ $('Start').item.json.id }}.json",
        "asBinaryProperty": false,
        "additionalParameters": {}
      },
      "type": "n8n-nodes-base.github",
      "typeVersion": 1.1,
      "position": [
        416,
        0
      ],
      "id": "54960bfc-5f12-4f80-b0b6-e3cf3e5e0ad2",
      "name": "Get a file",
      "alwaysOutputData": true,
      "credentials": {
        "githubApi": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "d66f24ca-d326-4a24-a994-5269d13beb76",
              "leftValue": "={{ $json.content }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              }
            },
            {
              "id": "92886415-8c78-497f-af28-d4e5a81c3813",
              "leftValue": "={{ $json.error }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notExists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        624,
        0
      ],
      "id": "f794dba4-2ce5-4b8f-9cd0-e2f7143dcb1e",
      "name": "If"
    },
    {
      "parameters": {
        "url": "={{ $json.download_url }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        864,
        -48
      ],
      "id": "360332b2-de79-47d6-9744-9a28728af462",
      "name": "HTTP Request"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        1120,
        112
      ],
      "id": "04e81050-58c8-4c60-9608-653954544c3b",
      "name": "Merge"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{$json.github_status}}",
                    "rightValue": "new",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "2665c9e9-7a2c-484e-a124-7a86d7c81c8f"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "New"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "5eab05fb-95de-435e-b00a-b89c60f57e5b",
                    "leftValue": "={{$json.github_status}}",
                    "rightValue": "different",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Different"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "06251f57-04b7-4722-a002-84b87a1b4989",
                    "leftValue": "={{$json.github_status}}",
                    "rightValue": "same",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Same"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        1520,
        96
      ],
      "id": "0d838728-b166-4807-a276-551cdef44701",
      "name": "Switch"
    },
    {
      "parameters": {
        "resource": "file",
        "owner": {
          "__rl": true,
          "value": "={{ $('Repo Data').item.json.repoOwner }}",
          "mode": "name"
        },
        "repository": {
          "__rl": true,
          "value": "={{ $('Repo Data').item.json.repoName }}",
          "mode": "name"
        },
        "filePath": "={{ $('Repo Data').item.json.repoPath }}{{$('Start').first().json.id}}.json",
        "fileContent": "={{$('isDiffOrNew').item.json[\"n8n_data_stringy\"]}}",
        "commitMessage": "={{$('Start').first().json.name}} ({{$json.github_status}})"
      },
      "type": "n8n-nodes-base.github",
      "typeVersion": 1.1,
      "position": [
        1792,
        -96
      ],
      "id": "68718506-2064-4bc3-9290-d474911bdfac",
      "name": "Create a file",
      "credentials": {
        "githubApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const orderJsonKeys = (jsonObj) => {\n  const ordered = {};\n  Object.keys(jsonObj).sort().forEach(key => {\n    ordered[key] = jsonObj[key];\n  });\n  return ordered;\n}\n\n// Check if file returned with content\nif (Object.keys($input.all()[0].json).includes(\"content\")) {\n  // Decode base64 content and parse JSON\n  const origWorkflow = JSON.parse(Buffer.from($input.all()[0].json.content, 'base64').toString());\n  const n8nWorkflow = $input.all()[1].json;\n  \n  // Order JSON objects\n  const orderedOriginal = orderJsonKeys(origWorkflow);\n  const orderedActual = orderJsonKeys(n8nWorkflow);\n\n  // Determine difference\n  if (JSON.stringify(orderedOriginal) === JSON.stringify(orderedActual)) {\n    $input.all()[0].json.github_status = \"same\";\n  } else {\n    $input.all()[0].json.github_status = \"different\";\n    $input.all()[0].json.n8n_data_stringy = JSON.stringify(orderedActual, null, 2);\n  }\n  $input.all()[0].json.content_decoded = orderedOriginal;\n// No file returned / new workflow\n} else if (Object.keys($input.all()[0].json).includes(\"data\")) {\n  const origWorkflow = JSON.parse($input.all()[0].json.data);\n  const n8nWorkflow = $input.all()[1].json;\n  \n  // Order JSON objects\n  const orderedOriginal = orderJsonKeys(origWorkflow);\n  const orderedActual = orderJsonKeys(n8nWorkflow);\n\n  // Determine difference\n  if (JSON.stringify(orderedOriginal) === JSON.stringify(orderedActual)) {\n    $input.all()[0].json.github_status = \"same\";\n  } else {\n    $input.all()[0].json.github_status = \"different\";\n    $input.all()[0].json.n8n_data_stringy = JSON.stringify(orderedActual, null, 2);\n  }\n  $input.all()[0].json.content_decoded = orderedOriginal;\n\n} else {\n  // Order JSON object\n  const n8nWorkflow = $input.all()[1].json;\n  const orderedActual = orderJsonKeys(n8nWorkflow);\n  \n  // Proper formatting\n  $input.all()[0].json.github_status = \"new\";\n  $input.all()[0].json.n8n_data_stringy = JSON.stringify(orderedActual, null, 2);\n}\n\n// Return items\nreturn $input.all();"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1328,
        112
      ],
      "id": "dc0a7e7d-5394-49ab-9c3b-7f5a7cbd9f3c",
      "name": "isDiffOrNew"
    },
    {
      "parameters": {
        "resource": "file",
        "operation": "edit",
        "owner": {
          "__rl": true,
          "value": "={{ $('Repo Data').item.json.repoOwner }}",
          "mode": "name"
        },
        "repository": {
          "__rl": true,
          "value": "={{ $('Repo Data').item.json.repoName }}",
          "mode": "name"
        },
        "filePath": "={{ $('Repo Data').item.json.repoPath }}{{$('Start').first().json.id}}.json",
        "fileContent": "={{$('isDiffOrNew').item.json[\"n8n_data_stringy\"]}}",
        "commitMessage": "={{$('Start').first().json.name}} ({{ $('Start').item.json.isArchived ? '(Archived)' : $json.github_status }})"
      },
      "type": "n8n-nodes-base.github",
      "typeVersion": 1.1,
      "position": [
        1792,
        112
      ],
      "id": "4cfc65df-2638-4618-b777-4080a4c441a4",
      "name": "Edit a file",
      "credentials": {
        "githubApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1808,
        320
      ],
      "id": "79921a22-fbb6-41df-b7a5-5b90b89f0512",
      "name": "No Operation, do nothing"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "5cfc1054-7d76-4582-864c-88e4bfef6ff7",
              "name": "Done",
              "value": true,
              "type": "boolean"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2240,
        112
      ],
      "id": "1fa25305-141b-4f76-a072-589666542dfd",
      "name": "Return"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Repo Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Repo Data": {
      "main": [
        [
          {
            "node": "Get a file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a file": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "isDiffOrNew",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Create a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Edit a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isDiffOrNew": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a file": {
      "main": [
        [
          {
            "node": "Return",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit a file": {
      "main": [
        [
          {
            "node": "Return",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Operation, do nothing": {
      "main": [
        [
          {
            "node": "Return",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "74a35377-3b24-4493-b0fc-4f1cea8d2242",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "WoiLAEnDme6HVamh",
  "tags": []
}