Update locally saved file SHA after it's uploaded

This commit is contained in:
Silvano Cerza 2025-01-09 17:59:42 +01:00
parent 76c2b99092
commit 6205a12453
2 changed files with 33 additions and 2 deletions

View file

@ -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,

View file

@ -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.