{
  "id": "tGB3Dw1JjDFCrHuL",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Restore workflows from GitHub",
  "tags": [],
  "nodes": [
    {
      "id": "4da3bb85-e6f9-406b-a7d5-fee1064da2a4",
      "name": "Define Local Variables",
      "type": "n8n-nodes-base.set",
      "position": [
        200,
        -200
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "18dc3ecc-c1c7-4197-9268-3898636323bc",
              "name": "github_owner",
              "type": "string",
              "value": "duynb92"
            },
            {
              "id": "b3778554-4200-4e51-842a-dfca05be4f85",
              "name": "repo_name",
              "type": "string",
              "value": "n8n-workflows"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "4b36dfdb-6e69-4590-b47a-326198368a86",
      "name": "Github Trigger - When there is new pull request",
      "type": "n8n-nodes-base.githubTrigger",
      "position": [
        -20,
        -200
      ],
      "parameters": {
        "owner": {
          "__rl": true,
          "mode": "name",
          "value": "duynb92"
        },
        "events": [
          "pull_request"
        ],
        "options": {},
        "repository": {
          "__rl": true,
          "mode": "list",
          "value": "n8n-workflows",
          "cachedResultUrl": "https://github.com/duynb92/n8n-workflows",
          "cachedResultName": "n8n-workflows"
        }
      },
      "credentials": {
        "githubApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "74b7816b-48f2-4c1e-a636-fcbfb422a5e8",
      "name": "Check if trigger event is pull request merged",
      "type": "n8n-nodes-base.filter",
      "position": [
        420,
        -200
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "7f447d9f-6a55-4c4c-b9c5-d1f5b5fb1476",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $('Github Trigger - When there is new pull request').item.json.body.action }}",
              "rightValue": "closed"
            },
            {
              "id": "ab94d15e-ee80-4f78-8a1a-b9db0c2161ec",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $('Github Trigger - When there is new pull request').item.json.body.pull_request.merged }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "f72522ad-d28d-42ed-95a5-45927c264596",
      "name": "Fetch merged commit details via GitHub API",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        640,
        -200
      ],
      "parameters": {
        "url": "=https://api.github.com/repos/{{ $('Define Local Variables').item.json.github_owner }}/{{ $('Define Local Variables').item.json.repo_name }}/commits/{{ $('Github Trigger - When there is new pull request').item.json.body.pull_request.merge_commit_sha }}",
        "options": {},
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "githubApi"
      },
      "credentials": {
        "githubApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "a97abd34-fd58-4b81-bacb-3cab9d58499b",
      "name": "Get files changes details",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        860,
        -200
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "files"
      },
      "typeVersion": 1
    },
    {
      "id": "2aab8ec7-7bbf-4909-9175-d6ef503318ae",
      "name": "Filter modified workflows",
      "type": "n8n-nodes-base.filter",
      "position": [
        1080,
        -300
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cd4529cc-cf44-4fd9-aa27-49a5557a40ca",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.status }}",
              "rightValue": "modified"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "5b14e46f-6bac-4e15-8a66-45fccc1ae624",
      "name": "Filter created workflows",
      "type": "n8n-nodes-base.filter",
      "position": [
        1080,
        -100
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "b4d2e160-478b-45e0-ba5d-ea72c2f6d430",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.status }}",
              "rightValue": "added"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "e85cff90-626c-4673-8d94-0b5a8848d5be",
      "name": "Fetch workflow content from Git",
      "type": "n8n-nodes-base.github",
      "position": [
        1300,
        -100
      ],
      "parameters": {
        "owner": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Define Local Variables').item.json.github_owner }}"
        },
        "filePath": "={{ $json.filename }}",
        "resource": "file",
        "operation": "get",
        "repository": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Define Local Variables').item.json.repo_name }}"
        },
        "asBinaryProperty": false,
        "additionalParameters": {}
      },
      "credentials": {
        "githubApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "d0cc850d-3270-40f8-99ac-363ec8986487",
      "name": "Fetch workflow content from Git1",
      "type": "n8n-nodes-base.github",
      "position": [
        1300,
        -300
      ],
      "parameters": {
        "owner": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Define Local Variables').item.json.github_owner }}"
        },
        "filePath": "={{ $json.filename }}",
        "resource": "file",
        "operation": "get",
        "repository": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Define Local Variables').item.json.repo_name }}"
        },
        "asBinaryProperty": false,
        "additionalParameters": {}
      },
      "credentials": {
        "githubApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "157304a2-c198-4f8c-ac69-81f6adaad00b",
      "name": "Decode workflow content to json",
      "type": "n8n-nodes-base.set",
      "position": [
        1520,
        -300
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={{  $json.content.base64Decode() }}"
      },
      "typeVersion": 3.4
    },
    {
      "id": "8dee2b87-85c7-42d0-b35f-fbe0107de733",
      "name": "Decode workflow content to json1",
      "type": "n8n-nodes-base.set",
      "position": [
        1520,
        -100
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={{  $json.content.base64Decode() }}"
      },
      "typeVersion": 3.4
    },
    {
      "id": "2ffb439f-cacf-4af2-9f57-1f2ea94c5eae",
      "name": "Update workflow in n8n",
      "type": "n8n-nodes-base.n8n",
      "position": [
        1740,
        -300
      ],
      "parameters": {
        "operation": "update",
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "requestOptions": {},
        "workflowObject": "={{ $json.toJsonString() }}"
      },
      "credentials": {
        "n8nApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "21bab1a6-5481-4728-a3fc-3749db3e4ca6",
      "name": "Create new workflow in n8n",
      "type": "n8n-nodes-base.n8n",
      "position": [
        1740,
        -100
      ],
      "parameters": {
        "operation": "create",
        "requestOptions": {},
        "workflowObject": "={{ $json.toJsonString()  }}"
      },
      "credentials": {
        "n8nApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4092e6aa-183f-4f9e-ae77-8784e9817d5c",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -200,
        -580
      ],
      "parameters": {
        "width": 420,
        "height": 320,
        "content": "## Setup\n\n1. **Connect GitHub**: Use the `GitHub Trigger` node and configure GitHub API credentials.\nNote: I'd recommended to use *GitHub PAT (Personal Access Token) classic* with `repo` and `admin:repo_hook` permission scopes enabled.\n2. **Connect n8n API**: Provide your n8n API credentials in the `n8n` nodes. - [Check this doc](https://docs.n8n.io/api/authentication/)\n3. **Set repository variables**: Update `github_owner` and `repo_name` in the **Define Local Variables** node.\n4. **Enable webhook**: Make sure your GitHub repository has a webhook for `pull_request` events pointing to this workflow."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "f6e5573a-f754-4e54-8e78-792b68df4162",
  "connections": {
    "Define Local Variables": {
      "main": [
        [
          {
            "node": "Check if trigger event is pull request merged",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter created workflows": {
      "main": [
        [
          {
            "node": "Fetch workflow content from Git",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter modified workflows": {
      "main": [
        [
          {
            "node": "Fetch workflow content from Git1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get files changes details": {
      "main": [
        [
          {
            "node": "Filter modified workflows",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter created workflows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decode workflow content to json": {
      "main": [
        [
          {
            "node": "Update workflow in n8n",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch workflow content from Git": {
      "main": [
        [
          {
            "node": "Decode workflow content to json1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decode workflow content to json1": {
      "main": [
        [
          {
            "node": "Create new workflow in n8n",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch workflow content from Git1": {
      "main": [
        [
          {
            "node": "Decode workflow content to json",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch merged commit details via GitHub API": {
      "main": [
        [
          {
            "node": "Get files changes details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if trigger event is pull request merged": {
      "main": [
        [
          {
            "node": "Fetch merged commit details via GitHub API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Github Trigger - When there is new pull request": {
      "main": [
        [
          {
            "node": "Define Local Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}