mirror of
https://github.com/silvanocerza/github-gitless-sync.git
synced 2026-07-22 05:41:36 +00:00
Update locally saved file SHA after it's uploaded
This commit is contained in:
parent
76c2b99092
commit
6205a12453
2 changed files with 33 additions and 2 deletions
|
|
@ -13,7 +13,7 @@ export default class EventsConsumer {
|
|||
|
||||
async process(event: Event): Promise<void> {
|
||||
if (event.type == "create" || event.type == "modify") {
|
||||
const res = await this.client.uploadFile(
|
||||
await this.client.uploadFile(
|
||||
this.owner,
|
||||
this.repo,
|
||||
this.branch,
|
||||
|
|
@ -21,9 +21,17 @@ export default class EventsConsumer {
|
|||
);
|
||||
// Reset dirty state
|
||||
this.metadataStore.data[event.filePath].dirty = false;
|
||||
// Gets the new SHA of the file
|
||||
const sha = await this.client.getFileSha(
|
||||
this.owner,
|
||||
this.repo,
|
||||
this.branch,
|
||||
event.filePath,
|
||||
);
|
||||
this.metadataStore.data[event.filePath].sha = sha;
|
||||
this.metadataStore.save();
|
||||
} else if (event.type == "delete") {
|
||||
const res = await this.client.deleteFile(
|
||||
await this.client.deleteFile(
|
||||
this.owner,
|
||||
this.repo,
|
||||
this.branch,
|
||||
|
|
|
|||
|
|
@ -155,6 +155,29 @@ export default class GithubClient {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the SHA of a file in the remote repo given its local path.
|
||||
*
|
||||
* @param owner Owner of the repo
|
||||
* @param repo Name of the repo
|
||||
* @param branch Branch to download from
|
||||
* @param filePath local file path to upload
|
||||
* @returns sha of the file as string
|
||||
*/
|
||||
async getFileSha(
|
||||
owner: string,
|
||||
repo: string,
|
||||
branch: string,
|
||||
filePath: string,
|
||||
) {
|
||||
const { remotePath } = this.metadataStore.data[filePath];
|
||||
const res = await requestUrl({
|
||||
url: `https://api.github.com/repos/${owner}/${repo}/contents/${remotePath}?ref=${branch}`,
|
||||
headers: this.headers(),
|
||||
});
|
||||
return res.json.sha;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single file from GitHub.
|
||||
* All the file information needed to delete the file is take form the metadata store.
|
||||
|
|
|
|||
Loading…
Reference in a new issue