Skip to content

Commit

Permalink
Add tests for grouped updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishnha committed Jun 8, 2023
1 parent b3648a3 commit 9e8fb5b
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/dependabot/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const baseDependency = {
newVersion: '',
compatScore: 0,
maintainerChanges: false,
dependencyGroup: '',
alertState: '',
ghsaId: '',
cvss: 0
Expand All @@ -38,6 +39,7 @@ test('when given a single dependency it sets its values', async () => {
newVersion: '1.1.3-beta',
compatScore: 43,
maintainerChanges: true,
dependencyGroup: '',
alertState: 'FIXED',
ghsaId: 'VERY_LONG_ID',
cvss: 4.6
Expand Down
47 changes: 47 additions & 0 deletions src/dependabot/update_metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ test('it returns the updated dependency information when there is a yaml fragmen
expect(updatedDependencies[0].alertState).toEqual('DISMISSED')
expect(updatedDependencies[0].ghsaId).toEqual('GHSA-III-BBB')
expect(updatedDependencies[0].cvss).toEqual(4.6)
expect(updatedDependencies[0].dependencyGroup).toEqual('')
})

test('it supports multiple dependencies within a single fragment', async () => {
Expand Down Expand Up @@ -122,6 +123,8 @@ test('it supports multiple dependencies within a single fragment', async () => {
expect(updatedDependencies[0].alertState).toEqual('DISMISSED')
expect(updatedDependencies[0].ghsaId).toEqual('GHSA-III-BBB')
expect(updatedDependencies[0].cvss).toEqual(4.6)
expect(updatedDependencies[0].dependencyGroup).toEqual('')
expect(updatedDependencies[0].dependencyGroup).toEqual('')

expect(updatedDependencies[1].dependencyName).toEqual('coffeescript')
expect(updatedDependencies[1].dependencyType).toEqual('indirect')
Expand All @@ -135,6 +138,7 @@ test('it supports multiple dependencies within a single fragment', async () => {
expect(updatedDependencies[1].alertState).toEqual('')
expect(updatedDependencies[1].ghsaId).toEqual('')
expect(updatedDependencies[1].cvss).toEqual(0)
expect(updatedDependencies[1].dependencyGroup).toEqual('')
})

test('it returns the updated dependency information when there is a leading v in the commit message versions', async () => {
Expand Down Expand Up @@ -170,6 +174,47 @@ test('it returns the updated dependency information when there is a leading v in
expect(updatedDependencies[0].alertState).toEqual('DISMISSED')
expect(updatedDependencies[0].ghsaId).toEqual('GHSA-III-BBB')
expect(updatedDependencies[0].cvss).toEqual(4.6)
expect(updatedDependencies[0].dependencyGroup).toEqual('')
})

test('it supports returning information about grouped updates', async () => {
const commitMessage =
'Bumps the docker group with 3 updates: [github.com/docker/cli](https://github.com/docker/cli), [github.com/docker/docker](https://github.com/docker/docker) and [github.com/moby/moby](https://github.com/moby/moby).\n' +
'\n' +
'Updates `github.com/docker/cli` from 24.0.1+incompatible to 24.0.2+incompatible\n' +
'- [Commits](docker/cli@v24.0.1...v24.0.2)\n' +
'\n' +
'Updates `github.com/docker/docker` from 24.0.1+incompatible to 24.0.2+incompatible\n' +
'- [Release notes](https://github.com/docker/docker/releases)\n' +
'- [Commits](moby/moby@v24.0.1...v24.0.2)\n' +
'\n' +
'Updates `github.com/moby/moby` from 24.0.1+incompatible to 24.0.2+incompatible\n' +
'- [Release notes](https://github.com/moby/moby/releases)\n' +
'- [Commits](moby/moby@v24.0.1...v24.0.2)\n' +
'\n' +
'---\n' +
'updated-dependencies:\n' +
'- dependency-name: github.com/docker/cli\n' +
' dependency-type: direct:production\n' +
' update-type: version-update:semver-patch\n' +
'- dependency-name: github.com/docker/docker\n' +
' dependency-type: direct:production\n' +
' update-type: version-update:semver-patch\n' +
'- dependency-name: github.com/moby/moby\n' +
' dependency-type: direct:production\n' +
' update-type: version-update:semver-patch\n' +
'...\n' +
'\n' +
'Signed-off-by: dependabot[bot] <support@github.com>\n'

const getAlert = async () => Promise.resolve({ alertState: 'DISMISSED', ghsaId: 'GHSA-III-BBB', cvss: 4.6 })
const getScore = async () => Promise.resolve(43)
const updatedDependencies = await updateMetadata.parse(commitMessage, '', 'dependabot/docker/gh-base-image/docker-1234566789', 'main', getAlert, getScore)

expect(updatedDependencies).toHaveLength(3)

expect(updatedDependencies[0].dependencyName).toEqual('github.com/docker/cli')
expect(updatedDependencies[0].dependencyGroup).toEqual('docker')
})

test('it only returns information within the first fragment if there are multiple yaml documents', async () => {
Expand Down Expand Up @@ -211,6 +256,7 @@ test('it only returns information within the first fragment if there are multipl
expect(updatedDependencies[0].alertState).toEqual('')
expect(updatedDependencies[0].ghsaId).toEqual('')
expect(updatedDependencies[0].cvss).toEqual(0)
expect(updatedDependencies[0].dependencyGroup).toEqual('')
})

test('it properly handles dependencies which contain slashes', async () => {
Expand Down Expand Up @@ -247,6 +293,7 @@ test('it properly handles dependencies which contain slashes', async () => {
expect(updatedDependencies[0].alertState).toEqual('')
expect(updatedDependencies[0].ghsaId).toEqual('')
expect(updatedDependencies[0].cvss).toEqual(0)
expect(updatedDependencies[0].dependencyGroup).toEqual('')
})

test('calculateUpdateType should handle all paths', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/dependabot/update_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function parse (commitMessage: string, body: string, branchName: st
const chunks = branchName.split(delim)
const prev = bumpFragment?.groups?.from ?? (updateFragment?.groups?.from ?? '')
const next = bumpFragment?.groups?.to ?? (updateFragment?.groups?.to ?? '')
const dependencyGroup = groupName?.groups?.name ?? ''
const dependencyGroup = groupName?.groups?.name ?? ''

if (data['updated-dependencies']) {
return await Promise.all(data['updated-dependencies'].map(async (dependency, index) => {
Expand Down
115 changes: 115 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ test('it sets the updated dependency as an output for subsequent actions when gi
newVersion: '4.2.2',
compatScore: 0,
maintainerChanges: false,
dependencyGroup: '',
alertState: '',
ghsaId: '',
cvss: 0
Expand All @@ -129,6 +130,7 @@ test('it sets the updated dependency as an output for subsequent actions when gi
expect(core.setOutput).toBeCalledWith('new-version', '4.2.2')
expect(core.setOutput).toBeCalledWith('compatibility-score', 0)
expect(core.setOutput).toBeCalledWith('maintainer-changes', false)
expect(core.setOutput).toBeCalledWith('dependency-group', '')
expect(core.setOutput).toBeCalledWith('alert-state', '')
expect(core.setOutput).toBeCalledWith('ghsa-id', '')
expect(core.setOutput).toBeCalledWith('cvss', 0)
Expand Down Expand Up @@ -179,6 +181,7 @@ test('it sets the updated dependency as an output for subsequent actions when th
directory: '/',
packageEcosystem: 'nuget',
maintainerChanges: false,
dependencyGroup: '',
targetBranch: 'main',
prevVersion: 'v4.0.1',
newVersion: 'v4.2.2',
Expand All @@ -200,11 +203,118 @@ test('it sets the updated dependency as an output for subsequent actions when th
expect(core.setOutput).toBeCalledWith('new-version', 'v4.2.2')
expect(core.setOutput).toBeCalledWith('compatibility-score', 0)
expect(core.setOutput).toBeCalledWith('maintainer-changes', false)
expect(core.setOutput).toBeCalledWith('dependency-group', '')
expect(core.setOutput).toBeCalledWith('alert-state', '')
expect(core.setOutput).toBeCalledWith('ghsa-id', '')
expect(core.setOutput).toBeCalledWith('cvss', 0)
})

test('it supports returning information about grouped updates', async () => {
const mockCommitMessage =
'Bumps the docker group with 3 updates: [github.com/docker/cli](https://github.com/docker/cli), [github.com/docker/docker](https://github.com/docker/docker) and [github.com/moby/moby](https://github.com/moby/moby).\n' +
'\n' +
'Updates `github.com/docker/cli` from 24.0.1+incompatible to 24.0.2+incompatible\n' +
'- [Commits](docker/cli@v24.0.1...v24.0.2)\n' +
'\n' +
'Updates `github.com/docker/docker` from 24.0.1+incompatible to 24.0.2+incompatible\n' +
'- [Release notes](https://github.com/docker/docker/releases)\n' +
'- [Commits](moby/moby@v24.0.1...v24.0.2)\n' +
'\n' +
'Updates `github.com/moby/moby` from 24.0.1+incompatible to 24.0.2+incompatible\n' +
'- [Release notes](https://github.com/moby/moby/releases)\n' +
'- [Commits](moby/moby@v24.0.1...v24.0.2)\n' +
'\n' +
'---\n' +
'updated-dependencies:\n' +
'- dependency-name: github.com/docker/cli\n' +
' dependency-type: direct:production\n' +
' update-type: version-update:semver-patch\n' +
'- dependency-name: github.com/docker/docker\n' +
' dependency-type: direct:production\n' +
' update-type: version-update:semver-patch\n' +
'- dependency-name: github.com/moby/moby\n' +
' dependency-type: direct:production\n' +
' update-type: version-update:semver-patch\n' +
'...\n' +
'\n' +
'Signed-off-by: dependabot[bot] <support@github.com>\n'

const mockAlert = { alertState: '', ghsaId: '', cvss: 0 }

jest.spyOn(core, 'getInput').mockReturnValue('mock-token')
jest.spyOn(util, 'getBranchNames').mockReturnValue({ headName: 'dependabot/docker/gh-base-image/docker-1234566789', baseName: 'trunk' })
jest.spyOn(dependabotCommits, 'getMessage').mockImplementation(jest.fn(
() => Promise.resolve(mockCommitMessage)
))
jest.spyOn(dependabotCommits, 'getAlert').mockImplementation(jest.fn(
() => Promise.resolve(mockAlert)
))
jest.spyOn(dependabotCommits, 'getCompatibility').mockImplementation(jest.fn(
() => Promise.resolve(34)
))
jest.spyOn(core, 'setOutput').mockImplementation(jest.fn())

await run()

expect(core.startGroup).toHaveBeenCalledWith(
expect.stringContaining('Outputting metadata for 3 updated dependencies')
)

expect(core.setOutput).toHaveBeenCalledWith(
'updated-dependencies-json',
[
{
dependencyName: 'github.com/docker/cli',
dependencyType: 'direct:production',
updateType: 'version-update:semver-patch',
directory: '/',
packageEcosystem: 'docker',
targetBranch: 'trunk',
prevVersion: '24.0.1',
newVersion: '24.0.2',
compatScore: 34,
maintainerChanges: false,
dependencyGroup: 'docker',
alertState: '',
ghsaId: '',
cvss: 0
},
{
dependencyName: 'github.com/docker/docker',
dependencyType: 'direct:production',
updateType: 'version-update:semver-patch',
directory: '/',
packageEcosystem: 'docker',
targetBranch: 'trunk',
prevVersion: '24.0.1',
newVersion: '24.0.2',
compatScore: 34,
maintainerChanges: false,
dependencyGroup: 'docker',
alertState: '',
ghsaId: '',
cvss: 0
},
{
dependencyName: 'github.com/moby/moby',
dependencyType: 'direct:production',
updateType: 'version-update:semver-patch',
directory: '/',
packageEcosystem: 'docker',
targetBranch: 'trunk',
prevVersion: '24.0.1',
newVersion: '24.0.2',
compatScore: 34,
maintainerChanges: false,
dependencyGroup: 'docker',
alertState: '',
ghsaId: '',
cvss: 0
}
]
)
})

test('it sets the updated dependency as an output for subsequent actions when given a commit message for library', async () => {
const mockCommitMessage =
'Update rubocop requirement from ~> 1.30.1 to ~> 1.31.0\n' +
Expand Down Expand Up @@ -253,6 +363,7 @@ test('it sets the updated dependency as an output for subsequent actions when gi
packageEcosystem: 'bundler',
targetBranch: 'main',
maintainerChanges: false,
dependencyGroup: '',
prevVersion: '1.30.1',
newVersion: '1.31.0',
compatScore: 0,
Expand All @@ -273,6 +384,7 @@ test('it sets the updated dependency as an output for subsequent actions when gi
expect(core.setOutput).toBeCalledWith('new-version', '1.31.0')
expect(core.setOutput).toBeCalledWith('compatibility-score', 0)
expect(core.setOutput).toBeCalledWith('maintainer-changes', false)
expect(core.setOutput).toBeCalledWith('dependency-group', '')
expect(core.setOutput).toBeCalledWith('alert-state', '')
expect(core.setOutput).toBeCalledWith('ghsa-id', '')
expect(core.setOutput).toBeCalledWith('cvss', 0)
Expand Down Expand Up @@ -332,6 +444,7 @@ test('if there are multiple dependencies, it summarizes them', async () => {
newVersion: '4.2.2',
compatScore: 34,
maintainerChanges: false,
dependencyGroup: '',
alertState: '',
ghsaId: '',
cvss: 0
Expand All @@ -347,6 +460,7 @@ test('if there are multiple dependencies, it summarizes them', async () => {
newVersion: '',
compatScore: 34,
maintainerChanges: false,
dependencyGroup: '',
alertState: '',
ghsaId: '',
cvss: 0
Expand All @@ -364,6 +478,7 @@ test('if there are multiple dependencies, it summarizes them', async () => {
expect(core.setOutput).toBeCalledWith('new-version', '4.2.2')
expect(core.setOutput).toBeCalledWith('compatibility-score', 34)
expect(core.setOutput).toBeCalledWith('maintainer-changes', false)
expect(core.setOutput).toBeCalledWith('dependency-group', '')
expect(core.setOutput).toBeCalledWith('alert-state', '')
expect(core.setOutput).toBeCalledWith('ghsa-id', '')
expect(core.setOutput).toBeCalledWith('cvss', 0)
Expand Down

0 comments on commit 9e8fb5b

Please sign in to comment.