diff --git a/lib/platform/vsts/vsts-helper.js b/lib/platform/vsts/vsts-helper.js index eab060472b855c83868fad425c53ab48db5506a2..aa8a28584adf7f55c8e851799bb6b7d2c6eb9c73 100644 --- a/lib/platform/vsts/vsts-helper.js +++ b/lib/platform/vsts/vsts-helper.js @@ -187,9 +187,7 @@ async function getFile(repoId, repoName, filePath, branchName) { ); if (item && item.readable) { - const buffer = item.read(); - // @ts-ignore - const fileContent = Buffer.from(buffer, 'base64').toString(); + const fileContent = await streamToString(item); try { const jTmp = JSON.parse(fileContent); if (jTmp.typeKey === 'GitItemNotFoundException') { @@ -207,6 +205,20 @@ async function getFile(repoId, repoName, filePath, branchName) { return null; // no file found } +async function streamToString(stream) { + const chunks = []; + /* eslint-disable promise/avoid-new */ + const p = await new Promise(resolve => { + stream.on('data', chunk => { + chunks.push(chunk.toString()); + }); + stream.on('end', () => { + resolve(chunks.join('')); + }); + }); + return p; +} + /** * * @param {string} str diff --git a/test/platform/vsts/vsts-helper.spec.js b/test/platform/vsts/vsts-helper.spec.js index 8f3eb70a502a83d788ae9a513c884899d7fa8419..c8473c432cb48110610d5f1631a509b8a8bb33ee 100644 --- a/test/platform/vsts/vsts-helper.spec.js +++ b/test/platform/vsts/vsts-helper.spec.js @@ -1,3 +1,5 @@ +const { Readable } = require('stream'); + describe('platform/vsts/helpers', () => { let vstsHelper; let gitApi; @@ -129,13 +131,22 @@ describe('platform/vsts/helpers', () => { describe('getChanges', () => { it('should be get the commit obj formated (file to update)', async () => { + let eventCount = 0; + const mockEventStream = new Readable({ + objectMode: true, + /* eslint-disable func-names */ + /* eslint-disable object-shorthand */ + read: function() { + if (eventCount < 1) { + eventCount += 1; + return this.push('{"hello": "test"}'); + } + return this.push(null); + }, + }); + gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => ({ - readable: true, - read: jest.fn(() => - Buffer.from('{"hello": "test"}').toString('base64') - ), - })), + getItemText: jest.fn(() => mockEventStream), })); const res = await vstsHelper.getChanges( @@ -173,15 +184,22 @@ describe('platform/vsts/helpers', () => { describe('getFile', () => { it('should return null error GitItemNotFoundException', async () => { + let eventCount = 0; + const mockEventStream = new Readable({ + objectMode: true, + /* eslint-disable func-names */ + /* eslint-disable object-shorthand */ + read: function() { + if (eventCount < 1) { + eventCount += 1; + return this.push('{"typeKey": "GitItemNotFoundException"}'); + } + return this.push(null); + }, + }); + gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => ({ - readable: true, - read: jest.fn(() => - Buffer.from('{"typeKey": "GitItemNotFoundException"}').toString( - 'base64' - ) - ), - })), + getItemText: jest.fn(() => mockEventStream), })); const res = await vstsHelper.getFile( @@ -194,15 +212,22 @@ describe('platform/vsts/helpers', () => { }); it('should return null error GitUnresolvableToCommitException', async () => { + let eventCount = 0; + const mockEventStream = new Readable({ + objectMode: true, + /* eslint-disable func-names */ + /* eslint-disable object-shorthand */ + read: function() { + if (eventCount < 1) { + eventCount += 1; + return this.push('{"typeKey": "GitUnresolvableToCommitException"}'); + } + return this.push(null); + }, + }); + gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => ({ - readable: true, - read: jest.fn(() => - Buffer.from( - '{"typeKey": "GitUnresolvableToCommitException"}' - ).toString('base64') - ), - })), + getItemText: jest.fn(() => mockEventStream), })); const res = await vstsHelper.getFile( @@ -215,13 +240,22 @@ describe('platform/vsts/helpers', () => { }); it('should return the file content because it is not a json', async () => { + let eventCount = 0; + const mockEventStream = new Readable({ + objectMode: true, + /* eslint-disable func-names */ + /* eslint-disable object-shorthand */ + read: function() { + if (eventCount < 1) { + eventCount += 1; + return this.push('{"hello"= "test"}'); + } + return this.push(null); + }, + }); + gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => ({ - readable: true, - read: jest.fn(() => - Buffer.from('{"hello"= "test"}').toString('base64') - ), - })), + getItemText: jest.fn(() => mockEventStream), })); const res = await vstsHelper.getFile(