{
  "name": "[SUB] [IT] - BACKUP WORKFLOWS TO GITHUB",
  "nodes": [
    {
      "parameters": {
        "content": "## WORKFLOW BACKUP TO GITHUB\n\nCe sub-workflow sauvegarde automatiquement les workflows n8n vers un repository GitHub.\n\n### Flux\n1. Trigger (n8n ou manual) -> Configuration variables\n2. Récupération du workflow via API n8n\n3. Création ou mise à jour du fichier sur GitHub\n\n### Credentials requis\n- **n8n API**: Accès à l'instance n8n\n- **GitHub OAuth2**: Accès au repository de backup\n\n### Configuration\nModifier le node SET CONFIG GITHUB REPOSITORY pour personnaliser:\n- OWNER_GITHUB: Organisation ou username\n- REPOSITORY_GITHUB: Nom du repo",
        "height": 884,
        "width": 450,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -832,
        352
      ],
      "typeVersion": 1,
      "id": "80f3d48e-32e6-4545-8df2-b2d6d83c8198",
      "name": "DOC - WORKFLOW OVERVIEW"
    },
    {
      "parameters": {
        "content": "## TRIGGERS\n\n**N8N TRIGGER**: Automatique lors de update/activate\n**EXECUTE WORKFLOW TRIGGER**: Appel depuis autre workflow\n\nLes deux entrées alimentent le même flux de backup.",
        "height": 244,
        "width": 300,
        "color": 2
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        688
      ],
      "typeVersion": 1,
      "id": "fbe5b77c-1879-42cf-a875-e42d14cb4e08",
      "name": "DOC - TRIGGERS"
    },
    {
      "parameters": {
        "content": "## GITHUB BACKUP FLOW\n\n1. **CREATE FILE**: Tente de créer le fichier JSON\n2. **Si erreur (fichier existe)**: GET FILE pour récupérer le SHA\n3. **UPDATE FILE**: Met à jour avec le nouveau contenu\n\nLe message de commit utilise la description de version du workflow.",
        "height": 276,
        "width": 680,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -368,
        352
      ],
      "typeVersion": 1,
      "id": "9b6e2c06-1746-4142-99d7-637e19bd7503",
      "name": "DOC - GITHUB BACKUP"
    },
    {
      "parameters": {
        "content": "## CONFIGURATION\n\nModifier ces valeurs pour votre environnement:\n- Owner GitHub\n- Repository name",
        "height": 172,
        "width": 280
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        448,
        704
      ],
      "typeVersion": 1,
      "id": "df69deb3-c284-4dc9-b822-9e7c118adc1a",
      "name": "DOC - CONFIG"
    },
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        96,
        1024
      ],
      "id": "48e7c2d3-cc8f-449a-b03e-6f736b8caba9",
      "name": "RECEIVE CALL FROM PARENT WORKFLOW",
      "notesInFlow": true,
      "notes": "Point d'entrée pour appel depuis un autre workflow (ex: ERROR HANDLER)"
    },
    {
      "parameters": {
        "events": [
          "update",
          "activate"
        ]
      },
      "type": "n8n-nodes-base.n8nTrigger",
      "typeVersion": 1,
      "position": [
        96,
        768
      ],
      "id": "a3a3ae43-06e0-4deb-b024-64d89ad6eafc",
      "name": "TRIGGER ON WORKFLOW UPDATE OR ACTIVATION",
      "notesInFlow": true,
      "notes": "Déclenché automatiquement lors de la modification ou activation d'un workflow"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "8b3bb531-18c6-4f8d-a5e4-503cd00d7836",
              "name": "OWNER_GITHUB",
              "value": "VERSIONNING-N8N",
              "type": "string"
            },
            {
              "id": "fb9730e9-e63e-41aa-b615-5a2896d321c9",
              "name": "REPOSITORY_GITHUB",
              "value": "n8n.ocadefusion.fr",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        368,
        896
      ],
      "id": "c81f723e-5efb-4c7b-afb4-ba88b54b0100",
      "name": "SET CONFIG GITHUB REPOSITORY",
      "notesInFlow": true,
      "notes": "Configure le owner et repository GitHub pour le backup"
    },
    {
      "parameters": {
        "operation": "get",
        "workflowId": {
          "__rl": true,
          "value": "={{$json.workflow_id}}",
          "mode": "id"
        },
        "requestOptions": {}
      },
      "type": "n8n-nodes-base.n8n",
      "typeVersion": 1,
      "position": [
        528,
        896
      ],
      "id": "dbc63e46-4ade-412a-8c84-cb4eddb2950e",
      "name": "GET WORKFLOW DETAILS FROM N8N API",
      "notesInFlow": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "n8nApi": {
          "id": "4Muq5STAvFEAfES9",
          "name": "N8N Ocade Fusion"
        }
      },
      "notes": "Récupère le JSON complet du workflow via l'API n8n"
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "file",
        "owner": {
          "__rl": true,
          "value": "={{ $('SET CONFIG GITHUB REPOSITORY').item.json.OWNER_GITHUB }}",
          "mode": "name"
        },
        "repository": {
          "__rl": true,
          "value": "={{ $('SET CONFIG GITHUB REPOSITORY').item.json.REPOSITORY_GITHUB }}",
          "mode": "name"
        },
        "filePath": "={{ $json.id }}.json",
        "fileContent": "={{ JSON.stringify($json, null, 2) }}",
        "commitMessage": "={{ $json.activeVersion.description }} \n=============================\n{{ $json.activeVersion.name }} | {{ $json.activeVersion.authors }}\n\n"
      },
      "type": "n8n-nodes-base.github",
      "typeVersion": 1.1,
      "position": [
        720,
        896
      ],
      "id": "93deb1a7-8f97-4cef-ba95-161ff913c2e3",
      "name": "CREATE FILE ON GITHUB",
      "webhookId": "82a62f5d-61d6-46e7-b655-44f5d9121e79",
      "notesInFlow": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "githubOAuth2Api": {
          "id": "YJEsHavOHt0iR1QN",
          "name": "GitHub valentin.charrier&hotmail.fr"
        }
      },
      "onError": "continueErrorOutput",
      "notes": "Tente de créer le fichier. Si existe déjà, passe à la branche erreur pour update."
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "file",
        "operation": "get",
        "owner": {
          "__rl": true,
          "value": "={{ $('SET CONFIG GITHUB REPOSITORY').item.json.OWNER_GITHUB }}",
          "mode": "name"
        },
        "repository": {
          "__rl": true,
          "value": "={{ $('SET CONFIG GITHUB REPOSITORY').item.json.REPOSITORY_GITHUB }}",
          "mode": "name"
        },
        "filePath": "={{ $json.id }}.json",
        "asBinaryProperty": false,
        "additionalParameters": {}
      },
      "type": "n8n-nodes-base.github",
      "typeVersion": 1.1,
      "position": [
        944,
        992
      ],
      "id": "968ce5ae-364d-41c0-86a1-54f7313aff87",
      "name": "GET EXISTING FILE SHA FROM GITHUB",
      "webhookId": "8fb9c30d-84f9-4fd4-ad1e-3c960f721d39",
      "notesInFlow": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "githubOAuth2Api": {
          "id": "YJEsHavOHt0iR1QN",
          "name": "GitHub valentin.charrier&hotmail.fr"
        }
      },
      "notes": "Récupère le SHA du fichier existant pour pouvoir le mettre à jour"
    },
    {
      "parameters": {
        "jsCode": "const workflow = $('GET WORKFLOW DETAILS FROM N8N API').item.json;\nconst sha = $('GET EXISTING FILE SHA FROM GITHUB').item.json.sha;\n\nreturn [{\n  json: {\n    workflow_id: workflow.id,\n    content: btoa(unescape(encodeURIComponent(JSON.stringify(workflow, null, 2)))),\n    sha: sha,\n    message: (workflow.activeVersion?.description || 'Update') + '\\n=============================\\n' + (workflow.activeVersion?.name || '') + ' | ' + (workflow.activeVersion?.authors || '')\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1152,
        992
      ],
      "id": "050e5933-2a45-4483-8e32-17b9e1a4fd01",
      "name": "PREPARE UPDATE PAYLOAD WITH SHA",
      "notesInFlow": true,
      "notes": "Prépare le contenu encodé en base64 et le message de commit pour l'update"
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://api.github.com/repos/{{ $('SET CONFIG GITHUB REPOSITORY').item.json.OWNER_GITHUB }}/{{ $('SET CONFIG GITHUB REPOSITORY').item.json.REPOSITORY_GITHUB }}/contents/{{ $json.workflow_id }}.json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "githubOAuth2Api",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "message",
              "value": "={{ $json.message }}"
            },
            {
              "name": "content",
              "value": "={{ $json.content }}"
            },
            {
              "name": "sha",
              "value": "={{ $json.sha }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1376,
        992
      ],
      "id": "c0252e4f-3fe1-4a68-8562-5428729c392a",
      "name": "UPDATE FILE ON GITHUB VIA API",
      "notesInFlow": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "githubOAuth2Api": {
          "id": "YJEsHavOHt0iR1QN",
          "name": "GitHub valentin.charrier&hotmail.fr"
        }
      },
      "notes": "Met à jour le fichier existant sur GitHub avec le nouveau contenu"
    }
  ],
  "pinData": {},
  "connections": {
    "RECEIVE CALL FROM PARENT WORKFLOW": {
      "main": [
        [
          {
            "node": "SET CONFIG GITHUB REPOSITORY",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TRIGGER ON WORKFLOW UPDATE OR ACTIVATION": {
      "main": [
        [
          {
            "node": "SET CONFIG GITHUB REPOSITORY",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SET CONFIG GITHUB REPOSITORY": {
      "main": [
        [
          {
            "node": "GET WORKFLOW DETAILS FROM N8N API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GET WORKFLOW DETAILS FROM N8N API": {
      "main": [
        [
          {
            "node": "CREATE FILE ON GITHUB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CREATE FILE ON GITHUB": {
      "main": [
        [],
        [
          {
            "node": "GET EXISTING FILE SHA FROM GITHUB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GET EXISTING FILE SHA FROM GITHUB": {
      "main": [
        [
          {
            "node": "PREPARE UPDATE PAYLOAD WITH SHA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PREPARE UPDATE PAYLOAD WITH SHA": {
      "main": [
        [
          {
            "node": "UPDATE FILE ON GITHUB VIA API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "timeSavedMode": "fixed",
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false,
    "errorWorkflow": "Jy7rTWWvNIWB30os"
  },
  "versionId": "3b4ff5b1-e0c4-4e2c-8840-89c142fa1a2f",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "9ed09ab91fbf542fcf50302dd736797c3f1ce41ba182bf3640612d82acc05052"
  },
  "id": "g8S7y933beqthWWz",
  "tags": []
}