From 678fec15cf257b7542e594505547ab41b2696771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julia=20Kr=C3=BCger?= Date: Mon, 19 Feb 2018 14:57:20 +0100 Subject: [PATCH] feat(repository-docs): create files array with paths --- lib/repository-docs.js | 26 +++++++++++++------------- test/jobs/create-initial-branch.js | 19 ++++++++++--------- test/jobs/github-event/push.js | 16 +++++++++------- test/lib/repository-docs.js | 26 ++++++++++---------------- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/lib/repository-docs.js b/lib/repository-docs.js index 4a14a25f..d8d3c8ed 100644 --- a/lib/repository-docs.js +++ b/lib/repository-docs.js @@ -16,27 +16,27 @@ module.exports = { async function updateRepoDoc (installationId, doc) { const fullName = doc.fullName const files = await getFiles(installationId, fullName) - doc.files = _.mapValues(files, content => !!content) - // need a new function that will loop over all found package.json files and add them with their path as a key + + // handles multiple paths for files like this: + // files: [ + // package.json: ['package.json', 'backend/package.json', 'frontend/package.json'] + // package-lock.json: ['package-lock.json', 'backend/package-lock.json'] + // npm-shrinkwrap.json: [], + // yarn.lock: [] + // ] + doc.files = _.mapValues(files, fileType => fileType + .filter(file => !!file.content) + .map(file => file.path)) const pkg = formatPackageJson(files['package.json']) if (!pkg) { - _.unset(doc, ['packages', 'package.json']) + _.unset(doc, ['packages']) return doc } - _.set(doc, ['packages', 'package.json'], pkg) + _.set(doc, ['packages'], pkg) return doc } -// needs to handle multiple lock files in different paths -// creates an array of arrays -// files: [ -// package.json: ['./', './backend', './frontend'] -// package-lock.json: ['./', './backend'] -// npm-shrinkwrap.json: [], -// yarn.lock: [] -// ] - // need to add a top level config key with the contents of the greenkeeperrc function createDocs ({ repositories, accountId }) { return repositories.map(repo => updatedAt({ diff --git a/test/jobs/create-initial-branch.js b/test/jobs/create-initial-branch.js index bcc316fc..7663ef48 100644 --- a/test/jobs/create-initial-branch.js +++ b/test/jobs/create-initial-branch.js @@ -54,6 +54,7 @@ describe('create initial branch', () => { .get('/repos/finnp/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: encodePkg({ devDependencies }) }) .get('/repos/finnp/test') @@ -151,11 +152,13 @@ describe('create initial branch', () => { .get('/repos/finnp/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: encodePkg({ dependencies: {} }) }) .get('/repos/finnp/test/contents/package-lock.json') .reply(200, { path: 'package-lock.json', + name: 'package-lock.json', content: encodePkg({ who: 'cares' }) }) .get('/repos/finnp/test') @@ -166,15 +169,9 @@ describe('create initial branch', () => { const newJob = await createInitialBranch({repositoryId: 44}) expect(newJob).toBeFalsy() const repodoc = await repositories.get('44') - const files = repodoc.files - const expectedFiles = { - 'npm-shrinkwrap.json': false, - 'package-lock.json': true, - 'package.json': true, - 'yarn.lock': false - } - - expect(files).toMatchObject(expectedFiles) + expect(repodoc.files['package.json']).not.toHaveLength(0) + expect(repodoc.files['package-lock.json']).not.toHaveLength(0) + expect(repodoc.files['yarn.lock']).toHaveLength(0) expect(repodoc.enabled).toBeTruthy() }) @@ -205,11 +202,13 @@ describe('create initial branch', () => { .get('/repos/finnp/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: encodePkg({ dependencies: {} }) }) .get('/repos/finnp/test/contents/package-lock.json') .reply(200, { path: 'package-lock.json', + name: 'package-lock.json', content: encodePkg({ who: 'cares' }) }) .get('/repos/finnp/test') @@ -253,11 +252,13 @@ describe('create initial branch', () => { .get('/repos/finnp/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: encodePkg({ dependencies: {} }) }) .get('/repos/finnp/test/contents/package-lock.json') .reply(200, { path: 'package-lock.json', + name: 'package-lock.json', content: encodePkg({ who: 'cares' }) }) .get('/repos/finnp/test') diff --git a/test/jobs/github-event/push.js b/test/jobs/github-event/push.js index 81cb1ca8..5c9f7513 100644 --- a/test/jobs/github-event/push.js +++ b/test/jobs/github-event/push.js @@ -94,6 +94,7 @@ describe('github-event push', async () => { .get('/repos/finn/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: encodePkg({ name: 'testpkg', dependencies: { @@ -104,6 +105,7 @@ describe('github-event push', async () => { .get('/repos/finn/test/contents/package-lock.json') .reply(200, { path: 'package-lock.json', + name: 'package-lock.json', content: encodePkg({}) }) @@ -139,13 +141,10 @@ describe('github-event push', async () => { expect(job.repositoryId).toEqual('444') const repo = await repositories.get('444') - const expectedFiles = { - 'npm-shrinkwrap.json': false, - 'package-lock.json': true, - 'package.json': true, - 'yarn.lock': false - } - expect(repo.files).toMatchObject(expectedFiles) + + expect(repo.files['package.json'].length).toBeGreaterThan(0) + expect(repo.files['package-lock.json'].length).toBeGreaterThan(0) + expect(repo.files['npm-shrinkwrap.json']).toHaveLength(0) const expectedPackages = { 'package.json': { @@ -174,6 +173,7 @@ describe('github-event push', async () => { .get('/repos/finn/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: encodePkg({ name: 'testpkg', dependencies: { @@ -241,6 +241,7 @@ describe('github-event push', async () => { .get('/repos/finn/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: encodePkg({ name: 'testpkg', dependencies: { @@ -307,6 +308,7 @@ describe('github-event push', async () => { .get('/repos/finn/test/contents/package.json') .reply(200, { path: 'package.json', + name: 'package.json', content: Buffer.from('test').toString('base64') }) diff --git a/test/lib/repository-docs.js b/test/lib/repository-docs.js index 45df18fb..5ff48425 100644 --- a/test/lib/repository-docs.js +++ b/test/lib/repository-docs.js @@ -19,24 +19,22 @@ test('updateRepoDoc with package.json', async () => { .reply(200, { type: 'file', path: 'package.json', + name: 'package.json', content: Buffer.from(JSON.stringify({ name: 'test' })).toString('base64') }) .get('/repos/owner/repo/contents/package-lock.json') .reply(200, { type: 'file', path: 'package-lock.json', + name: 'package-lock.json', content: Buffer.from(JSON.stringify({ name: 'test2' })).toString('base64') }) const doc = await updateRepoDoc('123', { fullName: 'owner/repo' }) expect(doc.packages['package.json'].name).toEqual('test') - const expectedFiles = { - 'npm-shrinkwrap.json': false, - 'package-lock.json': true, - 'package.json': true, - 'yarn.lock': false - } - expect(doc.files).toMatchObject(expectedFiles) + expect(doc.files['package-lock.json']).toHaveLength(1) + expect(doc.files['package.json']).toHaveLength(1) + expect(doc.files['yarn.lock']).toHaveLength(0) }) test('get invalid package.json', async () => { @@ -51,6 +49,7 @@ test('get invalid package.json', async () => { .reply(200, { type: 'file', path: 'package.json', + name: 'package.json', content: Buffer.from('test').toString('base64') }) @@ -62,16 +61,11 @@ test('get invalid package.json', async () => { } } }) - expect(doc.packages).not.toContain('package.json') - expect(doc.packages['package.json']).toBeFalsy() - const expectedFiles = { - 'npm-shrinkwrap.json': false, - 'package-lock.json': false, - 'package.json': true, - 'yarn.lock': false - } - expect(doc.files).toMatchObject(expectedFiles) + expect(doc.packages).toMatchObject({}) + expect(doc.files['package.json']).toHaveLength(1) + expect(doc.files['package-lock.json']).toHaveLength(0) + expect(doc.files['yarn.lock']).toHaveLength(0) }) test('create docs', async () => {