diff --git a/src/events/consumer.ts b/src/events/consumer.ts index 6aad84c..533914c 100644 --- a/src/events/consumer.ts +++ b/src/events/consumer.ts @@ -13,7 +13,7 @@ export default class EventsConsumer { async process(event: Event): Promise { 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, diff --git a/src/github/client.ts b/src/github/client.ts index 0fef895..847b88c 100644 --- a/src/github/client.ts +++ b/src/github/client.ts @@ -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.