I am using octokitjs to get the sha of a file and then write to the file. I have around 8 files in that Github repository. I need to write to files one by one. Here is my code:
async function separateCars() {
await rss[0].originalItems.forEach(item => {
const title = item.title
//if else condition to separate car by its type
})
}
const commitCars = async(car) => {
const newCar = transformRssToMD(car.items);
async function getSHA(path) {
try {
const result = await octokit.repos.getContent({
owner: "bob",
repo: "car-repo",
path: path
});
const sha = result?.data?.sha;
return sha;
} catch (error) {
console.log(error);
}
}
function getPath(car) {
if(car.title === 'volvo'){
return `volvo.md`;
} else if(car.title === 'audi'){
return `audi.md`
} else if(car.title === 'benz'){
return `benz.md`
} else if(car.title === 'bmw'){
return `bmw.md`
} else if(car.title === 'tesla'){
return `tesla.md`
} else if(car.title === 'honda'){
return `honda.md`
} else if(car.title === 'tata'){
return `tata.md`
} else if(car.title === 'suzuki'){
return `suzuki.md`
}
}
async function commitToGH(car){
const path = getPath(car);
const sha = await getSHA(path);
try {
const result = await octokit.repos.createOrUpdateFileContents({
owner: "bob",
repo: "car-repo",
path: path,
message: `latest cars updated`,
content: Base64.encode(`${newCars}`),
branch: 'main',
sha,
});
console.log("result", result);
return result?.status || 500;
} catch (error) {
console.log(error);
}
}
const results = await commitToGH(car);
}
async function callCommits(){
await separateCars().then(()=> {
commitCars(vovlo); commitCars(tesla); commitCars(bmw);
commitCars(tata); commitCars(suzuki); commitCars(audi);
commitCars(honda); commitCars(benz);
});
}
callCommits();
}
Here the getPath gets called 8 times as expected. But getsha is only gets called randomly like 2 times in execution or 4 times in an execution. And the createorUpdateFileContents also only gets called randomly. I am struck with this problem for 2 days now.
What am I doing wrong? any issue with async-await?