Add pushToGithub Feature

This commit is contained in:
Ali Salehi 2024-09-08 16:42:33 +03:30
parent 557ce72961
commit 5c3e137d58
3 changed files with 84 additions and 0 deletions

View File

@ -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 (
<>

View File

@ -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",

View File

@ -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. ❌');
}
}