Skip to content

Commit

Permalink
feat(repository-docs): create files array with paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Realtin authored and hulkoba committed Mar 26, 2018
1 parent e44a531 commit 678fec1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
26 changes: 13 additions & 13 deletions lib/repository-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
19 changes: 10 additions & 9 deletions test/jobs/create-initial-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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()
})

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
16 changes: 9 additions & 7 deletions test/jobs/github-event/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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({})
})

Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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')
})

Expand Down
26 changes: 10 additions & 16 deletions test/lib/repository-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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')
})

Expand All @@ -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 () => {
Expand Down

0 comments on commit 678fec1

Please sign in to comment.