mirror of
https://github.com/silvanocerza/github-gitless-sync.git
synced 2026-07-22 12:10:28 +00:00
Add method to upload single file to Github
This commit is contained in:
parent
c1cac5b74f
commit
f3418b0eeb
1 changed files with 31 additions and 0 deletions
|
|
@ -116,4 +116,35 @@ export default class GithubClient {
|
|||
this.vault.createBinary(destinationFile, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a single file to GitHub.
|
||||
* All the file information needed to upload the file is take form the metadata store.
|
||||
*
|
||||
* @param owner Owner of the repo
|
||||
* @param repo Name of the repo
|
||||
* @param branch Branch to download from
|
||||
* @param local file to upload
|
||||
*/
|
||||
async uploadFile(owner: string, repo: string, branch: string, file: TFile) {
|
||||
const remotePath = this.metadataStore.data[file.path].remotePath;
|
||||
const buffer = await this.vault.readBinary(file);
|
||||
const content = Buffer.from(buffer).toString("base64");
|
||||
const res = await fetch(
|
||||
`https://api.github.com/repos/${owner}/${repo}/contents/${remotePath}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify({
|
||||
message: "Edit file",
|
||||
branch: branch,
|
||||
content: content,
|
||||
sha: this.metadataStore.data[file.path].sha,
|
||||
}),
|
||||
},
|
||||
);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to upload file: ${res.statusText}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue