forked from dpim/wf-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-sync.js
74 lines (63 loc) · 2.4 KB
/
gh-sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
dotenv.config();
const repoOwner = 'victoriaplummer';
const repoName = 'examples-host';
const folderPath = './copies';
const destinationFolderPath = 'public/examples';
async function updateRepo() {
try {
// Read the contents of the local folder
const folderContents = fs.readdirSync(folderPath);
// Authenticate with GitHub (you might need to generate a personal access token)
const authToken = process.env.GITHUB_PERSONAL_ACCESS_TOKEN;
const headers = {
Authorization: `token ${authToken}`,
'Content-Type': 'application/json',
};
// Get the latest commit SHA of the main branch
const commitResponse = await axios.get(`https://api.github.com/repos/${repoOwner}/${repoName}/commits/main`, {
headers: headers,
});
const latestCommitSHA = commitResponse.data.sha;
// Create a new tree with the updated files
const treeData = {
base_tree: latestCommitSHA,
tree: folderContents.map((filename) => ({
path: path.join(destinationFolderPath, filename), // Set the destination folder path
mode: '100644',
type: 'blob',
content: fs.readFileSync(path.join(folderPath, filename), 'utf-8'),
})),
};
// Create a new tree with the updated files
const treeResult = await axios.post(`https://api.github.com/repos/${repoOwner}/${repoName}/git/trees`, treeData, {
headers: headers,
});
// Create a new commit with the updated tree
const newCommitData = {
message: 'Updated folder contents',
parents: [latestCommitSHA],
tree: treeResult.data.sha,
};
const newCommitResult = await axios.post(`https://api.github.com/repos/${repoOwner}/${repoName}/git/commits`, newCommitData, {
headers: headers,
});
// Update the main branch to point to the new commit
const updateRefResponse = await axios.patch(`https://api.github.com/repos/${repoOwner}/${repoName}/git/refs/heads/main`, {
sha: newCommitResult.data.sha,
}, {
headers: headers,
});
if (updateRefResponse.status === 200) {
console.log('Folder contents updated successfully.');
} else {
console.error('Failed to update folder contents:', updateRefResponse.statusText);
}
} catch (error) {
console.error('An error occurred:', error);
}
}
updateRepo();