From 5c3e137d58c16fae81f4146641eb83643e3ab6c8 Mon Sep 17 00:00:00 2001 From: Ali Salehi Date: Sun, 8 Sep 2024 16:42:33 +0330 Subject: [PATCH] Add pushToGithub Feature --- src/components/EditorHeader/ControlPanel.jsx | 21 +++++++ src/i18n/locales/en.js | 1 + src/utils/exportSQL/sentToGithub.js | 62 ++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/utils/exportSQL/sentToGithub.js diff --git a/src/components/EditorHeader/ControlPanel.jsx b/src/components/EditorHeader/ControlPanel.jsx index 593e69d..4643457 100644 --- a/src/components/EditorHeader/ControlPanel.jsx +++ b/src/components/EditorHeader/ControlPanel.jsx @@ -73,6 +73,7 @@ import { jsonToMermaid } from "../../utils/exportAs/mermaid"; import { isRtl } from "../../i18n/utils/rtl"; import { jsonToDocumentation } from "../../utils/exportAs/documentation"; import { IdContext } from "../Workspace"; +import { pushToGitHub } from "../../utils/exportSQL/sentToGithub.js"; export default function ControlPanel({ diagramId, @@ -937,6 +938,25 @@ export default function ControlPanel({ })); }, }, + + push_to_github:{ + function: () => { + const src = jsonToMySQL({ + tables: tables, + references: relationships, + types: types, + database: database, + }); + + pushToGitHub(src); + + setExportData((prev) => ({ + ...prev, + data: src, + extension: "sql", + })); + }, + }, export_as: { children: [ { @@ -1377,6 +1397,7 @@ export default function ControlPanel({ preventDefault: true, }); useHotkeys("ctrl+alt+w, meta+alt+w", fitWindow, { preventDefault: true }); + useHotkeys("ctrl+alt+s", menu.file.push_to_github.function, { preventDefault: true }); return ( <> diff --git a/src/i18n/locales/en.js b/src/i18n/locales/en.js index cebfac6..de12fad 100644 --- a/src/i18n/locales/en.js +++ b/src/i18n/locales/en.js @@ -25,6 +25,7 @@ const en = { import_from_source: "Import from SQL", export_as: "Export as", export_source: "Export SQL", + push_to_github: "Push To Github", models: "Models", exit: "Exit", edit: "Edit", diff --git a/src/utils/exportSQL/sentToGithub.js b/src/utils/exportSQL/sentToGithub.js new file mode 100644 index 0000000..75f0efc --- /dev/null +++ b/src/utils/exportSQL/sentToGithub.js @@ -0,0 +1,62 @@ +import axios from 'axios'; + +const TOKEN = 'YOUR_GITHUB_TOKEN'; +const REPO_OWNER = 'GITHUB_USERNAME'; +const REPO_NAME = 'GITHUB_REPOSITORY'; +const FILE_PATH = 'database.sql'; + +export async function pushToGitHub(content) { + + const headers = { + 'Authorization': `token ${TOKEN}`, + 'Accept': 'application/vnd.github.v3+json', + 'Content-Type': 'application/json', + }; + + try { + // Step 1: Create a Blob + let response = await axios.post(`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/git/blobs`, { + content: content, + encoding: 'utf-8' + }, {headers}); + const blobSha = response.data.sha; + + // Step 2: Get the latest commit SHA + response = await axios.get(`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/git/refs/heads/master`, {headers}); + const latestCommitSha = response.data.object.sha; + + // Step 3: Create a Tree + response = await axios.post(`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/git/trees`, { + base_tree: latestCommitSha, + tree: [ + { + path: FILE_PATH, + mode: '100644', + type: 'blob', + sha: blobSha, + } + ] + }, {headers}); + const treeSha = response.data.sha; + + // Step 4: Create a Commit + const currentTime = new Date().toISOString(); + response = await axios.post(`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/git/commits`, { + message: 'new DB updated at' + currentTime, + tree: treeSha, + parents: [latestCommitSha] + }, {headers}); + const commitSha = response.data.sha; + + // Step 5: Update the reference + await axios.patch(`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/git/refs/heads/master`, { + sha: commitSha, + force: true, + }, {headers}); + + alert('Commit created successfully. ✅'); + } catch (error) { + alert('Error creating commit. ❌'); + } + +}