I am working on a project where I have to read around 3000+ JSON files from an Azure DevOps repository and store their contents in a string which I can use later.
Below is the method I am using to fetch the list of items in my repository. I am not allowed to presume any directory hierarchy so I am setting the recursionLevel parameter to full.
function getItems(repositoryId: string, project?: string, scopePath?: string, recursionLevel?: VersionControlRecursionType, includeContentMetadata?: boolean, latestProcessedChange?: boolean, download?: boolean, includeLinks?: boolean, versionDescriptor?: GitVersionDescriptor)
The above function returns a Promise of Git item list from where I can get the relative path of every file from the root of the repository
After getting a list of items I want to read them one by one and store their contents in a string. For that I am using this method:
function getItemText(repositoryId: string, path: string, project?: string, scopePath?: string, recursionLevel?: VersionControlRecursionType, includeContentMetadata?: boolean, latestProcessedChange?: boolean, download?: boolean, versionDescriptor?: GitVersionDescriptor, includeContent?: boolean, resolveLfs?: boolean)
This function is also available in the same link I shared earlier. You have to scroll down a bit. It returns the content as Promise of string.
My code to read all the files:
AdoClient.getInstance().getItems(this.repoID, this.projectName, this.commonData.inputBranchName).then(data=>{
data.forEach(element => {
if(element.gitObjectType==3)
{
AdoClient.getInstance().getItemText(this.repoID,element.path,this.commonData.inputBranchName).then(element=>{ content.concat(element);
});})});
In the above code:
- gitObjectType=3 means a file
- The functions getItems() and getItemText() that you see are actually my functions. I wrote a definition to just pass the 3 required parameters. In the AdoClient file I have the original call to the functions using GitRestClient object just as you have in the doc.
This is the originial function call in AdoClient file:
public async getItemText(repoId: string, filePath: string, branchName: string) {
return this.gitClient.then(client => client.getItemText(repoId, filePath, null, null, 'none', false, false, false, {version: branchName, versionOptions: 0, versionType: 0}));
}
public async getItems(repositoryId: string, project: string,branch: string){
return this.gitClient.then(client=>client.getItems(repositoryId, project, "/ServiceGroupRoot/", 'full', true, false, false, true, {version: branch, versionOptions: 0, versionType: 0}));
}
Also please note /ServiceGroupRoot/ is the root directory in my repository.
When I am doing this I am not being able to get all the contents inside the content variable and also Google chrome is showing ERR_INSUFFICIENT_RESOURCES due to 3000+ ajax calls at the same time. This error is not coming in Mozilla tho. But in Mozilla when I am printing the content it shows empty string. What I want is a way to order my execution steps so that I can have all the contents in my content string. Is there a way to run a loop of Promise calls and make them execute one by one? By one by one I mean finish one then start the next one and not load everything at once or is there some other approach I have to follow?