-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.mjs
More file actions
94 lines (80 loc) · 2.6 KB
/
Copy pathlib.mjs
File metadata and controls
94 lines (80 loc) · 2.6 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Octokit } from "@octokit/rest";
import dedent from "dedent";
export async function UploadLogEntry(filename, igc, comment, token, sportsTrackLiveUrl) {
const content = igc;
const path = filename.slice(0, filename.lastIndexOf('.'));
const octokit = new Octokit({
auth: token
});
const commentsText = dedent`
---
sportsTrackLiveUrl: ${sportsTrackLiveUrl}
---
${comment}
`;
// See: https://docs.github.com/en/free-pro-team@latest/rest/reference/git#get-a-reference
const lastCommit = (await octokit.request("/repos/{owner}/{repo}/git/refs/heads/origin", {
owner: "scottyob",
repo: "paragliding-logbook",
}));
const lastCommitSha = lastCommit.data.object.sha;
// We can publish multiple files in "blobs"
// Create a blob for our Flight
const flightBlob = (await octokit.request("POST /repos/{owner}/{repo}/git/blobs", {
content: content,
encoding: "base64",
owner: "scottyob",
repo: "paragliding-logbook"
})).data.sha;
const commentsMdxBlob = (await octokit.request("POST /repos/{owner}/{repo}/git/blobs", {
owner: "scottyob",
repo: "paragliding-logbook",
encoding: "utf-8",
content: commentsText,
})).data.sha;
// Upload the comments and flights file to GitHub
const addPathTree = (await octokit.request("POST /repos/{owner}/{repo}/git/trees", {
owner: "scottyob",
repo: "paragliding-logbook",
tree: [{
"path": `flights/${path}/${filename}`,
"mode": "100644",
"type": "blob",
"sha": flightBlob,
},
{
"path": `flights/${path}/comments.mdx`,
"mode": "100644",
"type": "blob",
"sha": commentsMdxBlob,
}
],
"base_tree": lastCommitSha,
})).data.sha;
// Get the previous commit
const parentBranch = (await octokit.request("GET /repos/{owner}/{repo}/git/refs/heads/{branch}", {
owner: "scottyob",
repo: "paragliding-logbook",
branch: "origin"
}));
console.log("Parent Branch: ", parentBranch);
const parentBranchSha = parentBranch.data.object.sha;
// Add a commit
const newCommit = (await octokit.request("POST /repos/{owner}/{repo}/git/commits", {
owner: "scottyob",
repo: "paragliding-logbook",
tree: addPathTree,
message: `Added flight ${path}`,
parents: [parentBranchSha],
}));
console.log("New Commit: ", newCommit);
const newCommitSha = newCommit.data.sha;
// Update the origin branch
await octokit.request("PATCH /repos/{owner}/{repo}/git/refs/heads/{branch}", {
owner: "scottyob",
repo: "paragliding-logbook",
branch: "origin",
sha: newCommitSha,
});
console.log("Done committing");
}