Skip to content

Commit

Permalink
refactor(azure): Platform-wide getJsonFile (#9379)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 3, 2021
1 parent 425dcb5 commit 6074b99
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
15 changes: 1 addition & 14 deletions lib/platform/azure/azure-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getBranchNameWithoutRefsPrefix,
getBranchNameWithoutRefsheadsPrefix,
getNewBranchName,
streamToString,
} from './util';

const mergePolicyGuid = 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab'; // Magic GUID for merge strategy policy configurations
Expand Down Expand Up @@ -78,20 +79,6 @@ export async function getAzureBranchObj(
};
}

async function streamToString(stream: NodeJS.ReadableStream): Promise<string> {
const chunks: string[] = [];
/* eslint-disable promise/avoid-new */
const p = await new Promise<string>((resolve) => {
stream.on('data', (chunk: any) => {
chunks.push(chunk.toString());
});
stream.on('end', () => {
resolve(chunks.join(''));
});
});
return p;
}

// if no branchName, look globally
export async function getFile(
repoId: string,
Expand Down
23 changes: 15 additions & 8 deletions lib/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Readable } from 'stream';
import is from '@sindresorhus/is';
import {
GitPullRequestMergeStrategy,
Expand Down Expand Up @@ -1184,18 +1185,24 @@ describe('platform/azure', () => {
describe('getJsonFile()', () => {
it('returns file content', async () => {
const data = { foo: 'bar' };
azureHelper.getFile.mockResolvedValueOnce(JSON.stringify(data));
await initRepo({
repository: 'some-repo',
});
azureApi.gitApi.mockImplementationOnce(
() =>
({
getItemContent: jest.fn(() =>
Promise.resolve(Readable.from(JSON.stringify(data)))
),
} as any)
);
const res = await azure.getJsonFile('file.json');
expect(res).toEqual(data);
});
it('returns null on errors', async () => {
azureHelper.getFile.mockRejectedValueOnce('some error');
await initRepo({
repository: 'some-repo',
});
azureApi.gitApi.mockImplementationOnce(
() =>
({
getItemContent: jest.fn(() => Promise.reject('some error')),
} as any)
);
const res = await azure.getJsonFile('file.json');
expect(res).toBeNull();
});
Expand Down
16 changes: 9 additions & 7 deletions lib/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
getGitStatusContextFromCombinedName,
getNewBranchName,
getRenovatePRFormat,
streamToString,
} from './util';

interface Config {
Expand Down Expand Up @@ -103,14 +104,15 @@ export async function getRepos(): Promise<string[]> {
return repos.map((repo) => `${repo.project.name}/${repo.name}`);
}

export async function getJsonFile(fileName: string): Promise<any | null> {
export async function getJsonFile(
fileName: string,
repo: string = config.repoId
): Promise<any | null> {
try {
const json = await azureHelper.getFile(
config.repoId,
fileName,
config.defaultBranch
);
return JSON.parse(json);
const azureApiGit = await azureApi.gitApi();
const buf = await azureApiGit.getItemContent(repo, fileName);
const str = await streamToString(buf);
return JSON.parse(str);
} catch (err) {
return null;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/platform/azure/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Readable } from 'stream';
import {
getBranchNameWithoutRefsheadsPrefix,
getGitStatusContextCombinedName,
getGitStatusContextFromCombinedName,
getNewBranchName,
getRenovatePRFormat,
streamToString,
} from './util';

describe('platform/azure/helpers', () => {
Expand Down Expand Up @@ -107,4 +109,11 @@ describe('platform/azure/helpers', () => {
expect(res).toMatchSnapshot();
});
});

describe('streamToString', () => {
it('converts Readable stream to string', async () => {
const res = await streamToString(Readable.from('foobar'));
expect(res).toEqual('foobar');
});
});
});
16 changes: 16 additions & 0 deletions lib/platform/azure/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,19 @@ export function getRenovatePRFormat(azurePr: GitPullRequest): AzurePr {
...(isConflicted && { isConflicted }),
} as AzurePr;
}

export async function streamToString(
stream: NodeJS.ReadableStream
): Promise<string> {
const chunks: string[] = [];
/* eslint-disable promise/avoid-new */
const p = await new Promise<string>((resolve) => {
stream.on('data', (chunk: any) => {
chunks.push(chunk.toString());
});
stream.on('end', () => {
resolve(chunks.join(''));
});
});
return p;
}

0 comments on commit 6074b99

Please sign in to comment.