-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create datasource for artifactory registry (#11602)
- Loading branch information
1 parent
a7b34cf
commit 9fea985
Showing
10 changed files
with
423 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
lib/datasource/artifactory/__fixtures__/releases-as-files.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> | ||
<html> | ||
<head> | ||
<meta name="robots" content="noindex" /> | ||
<title>Repository Title</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Index</h1> | ||
<pre>Name Last modified Size</pre><hr/> | ||
<pre> | ||
<a href="..">..</a> | ||
<a href="1.0.0">1.0.0</a> 21-Jul-2021 20:08 - | ||
<a href="1.0.1">1.0.1</a> 23-Aug-2021 20:03 - | ||
<a href="1.0.2">1.0.2</a> 21-Jul-2021 20:09 - | ||
<a href="1.0.3">1.0.3</a> 06-Feb-2021 09:54 - | ||
</pre> | ||
<hr/> | ||
<address style="font-size:small;">Artifactory Port 8080</address> | ||
</body> | ||
</html> |
21 changes: 21 additions & 0 deletions
21
lib/datasource/artifactory/__fixtures__/releases-as-folders.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> | ||
<html> | ||
<head> | ||
<meta name="robots" content="noindex" /> | ||
<title>Repository Title</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Index</h1> | ||
<pre>Name Last modified Size</pre><hr/> | ||
<pre> | ||
<a href="../">../</a> | ||
<a href="1.0.0/">1.0.0/</a> 21-Jul-2021 20:08 - | ||
<a href="1.0.1/">1.0.1/</a> 23-Aug-2021 20:03 - | ||
<a href="1.0.2/">1.0.2/</a> 21-Jul-2021 20:09 - | ||
<a href="1.0.3/">1.0.3/</a> 06-Feb-2021 09:54 - | ||
</pre> | ||
<hr/> | ||
<address style="font-size:small;">Artifactory Port 8080</address> | ||
</body> | ||
</html> |
80 changes: 80 additions & 0 deletions
80
lib/datasource/artifactory/__snapshots__/index.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`datasource/artifactory/index getReleases parses real data (files): without slash at the end 1`] = ` | ||
Object { | ||
"registryUrl": "https://jfrog.company.com/artifactory", | ||
"releases": Array [ | ||
Object { | ||
"releaseTimestamp": "2021-07-21T20:08:00.000Z", | ||
"version": "1.0.0", | ||
}, | ||
Object { | ||
"releaseTimestamp": "2021-08-23T20:03:00.000Z", | ||
"version": "1.0.1", | ||
}, | ||
Object { | ||
"releaseTimestamp": "2021-07-21T20:09:00.000Z", | ||
"version": "1.0.2", | ||
}, | ||
Object { | ||
"releaseTimestamp": "2021-02-06T09:54:00.000Z", | ||
"version": "1.0.3", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`datasource/artifactory/index getReleases parses real data (folders): with slash at the end 1`] = ` | ||
Object { | ||
"registryUrl": "https://jfrog.company.com/artifactory", | ||
"releases": Array [ | ||
Object { | ||
"releaseTimestamp": "2021-07-21T20:08:00.000Z", | ||
"version": "1.0.0", | ||
}, | ||
Object { | ||
"releaseTimestamp": "2021-08-23T20:03:00.000Z", | ||
"version": "1.0.1", | ||
}, | ||
Object { | ||
"releaseTimestamp": "2021-07-21T20:09:00.000Z", | ||
"version": "1.0.2", | ||
}, | ||
Object { | ||
"releaseTimestamp": "2021-02-06T09:54:00.000Z", | ||
"version": "1.0.3", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`datasource/artifactory/index getReleases parses real data (merge strategy with 2 registries) 1`] = ` | ||
Object { | ||
"releases": Array [ | ||
Object { | ||
"registryUrl": "https://jfrog.company.com/artifactory", | ||
"releaseTimestamp": "2021-07-21T20:08:00.000Z", | ||
"version": "1.0.0", | ||
}, | ||
Object { | ||
"registryUrl": "https://jfrog.company.com/artifactory", | ||
"releaseTimestamp": "2021-08-23T20:03:00.000Z", | ||
"version": "1.0.1", | ||
}, | ||
Object { | ||
"registryUrl": "https://jfrog.company.com/artifactory", | ||
"releaseTimestamp": "2021-07-21T20:09:00.000Z", | ||
"version": "1.0.2", | ||
}, | ||
Object { | ||
"registryUrl": "https://jfrog.company.com/artifactory", | ||
"releaseTimestamp": "2021-02-06T09:54:00.000Z", | ||
"version": "1.0.3", | ||
}, | ||
Object { | ||
"registryUrl": "https://jfrog.company.com/artifactory/production", | ||
"version": "1.3.0", | ||
}, | ||
], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const datasource = 'artifactory'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { getPkgReleases } from '..'; | ||
import * as httpMock from '../../../test/http-mock'; | ||
import { loadFixture } from '../../../test/util'; | ||
import { EXTERNAL_HOST_ERROR } from '../../constants/error-messages'; | ||
import { logger } from '../../logger'; | ||
import { joinUrlParts } from '../../util/url'; | ||
import { ArtifactoryDatasource } from '.'; | ||
|
||
const datasource = ArtifactoryDatasource.id; | ||
|
||
const testRegistryUrl = 'https://jfrog.company.com/artifactory'; | ||
const testLookupName = 'project'; | ||
const testConfig = { | ||
registryUrls: [testRegistryUrl], | ||
depName: testLookupName, | ||
}; | ||
const fixtureReleasesAsFolders = loadFixture('releases-as-folders.html'); | ||
const fixtureReleasesAsFiles = loadFixture('releases-as-files.html'); | ||
|
||
function getPath(folder: string): string { | ||
return `/${folder}`; | ||
} | ||
|
||
describe('datasource/artifactory/index', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('getReleases', () => { | ||
it('parses real data (folders): with slash at the end', async () => { | ||
httpMock | ||
.scope(testRegistryUrl) | ||
.get(getPath(testLookupName)) | ||
.reply(200, fixtureReleasesAsFolders); | ||
const res = await getPkgReleases({ | ||
...testConfig, | ||
datasource, | ||
lookupName: testLookupName, | ||
}); | ||
expect(res.releases).toHaveLength(4); | ||
expect(res).toMatchSnapshot({ | ||
registryUrl: 'https://jfrog.company.com/artifactory', | ||
}); | ||
}); | ||
|
||
it('parses real data (files): without slash at the end', async () => { | ||
httpMock | ||
.scope(testRegistryUrl) | ||
.get(getPath(testLookupName)) | ||
.reply(200, fixtureReleasesAsFiles); | ||
const res = await getPkgReleases({ | ||
...testConfig, | ||
datasource, | ||
lookupName: testLookupName, | ||
}); | ||
expect(res.releases).toHaveLength(4); | ||
expect(res).toMatchSnapshot({ | ||
registryUrl: 'https://jfrog.company.com/artifactory', | ||
}); | ||
}); | ||
|
||
it('parses real data (merge strategy with 2 registries)', async () => { | ||
const secondRegistryUrl: string = joinUrlParts( | ||
testRegistryUrl, | ||
'production' | ||
); | ||
httpMock | ||
.scope(testRegistryUrl) | ||
.get(getPath(testLookupName)) | ||
.reply(200, fixtureReleasesAsFiles); | ||
httpMock | ||
.scope(secondRegistryUrl) | ||
.get(getPath(testLookupName)) | ||
.reply(200, '<html>\n<h1>Header</h1>\n<a>1.3.0</a>\n<hmtl/>'); | ||
const res = await getPkgReleases({ | ||
registryUrls: [testRegistryUrl, secondRegistryUrl], | ||
depName: testLookupName, | ||
datasource, | ||
lookupName: testLookupName, | ||
}); | ||
expect(res.releases).toHaveLength(5); | ||
expect(res).toMatchSnapshot(); | ||
}); | ||
|
||
it('returns null without registryUrl + warning', async () => { | ||
const res = await getPkgReleases({ | ||
datasource, | ||
depName: testLookupName, | ||
lookupName: testLookupName, | ||
}); | ||
expect(logger.warn).toHaveBeenCalledTimes(1); | ||
expect(logger.warn).toHaveBeenCalledWith( | ||
{ lookupName: 'project' }, | ||
'artifactory datasource requires custom registryUrl. Skipping datasource' | ||
); | ||
expect(res).toBeNull(); | ||
}); | ||
|
||
it('returns null for empty 200 OK', async () => { | ||
httpMock | ||
.scope(testRegistryUrl) | ||
.get(getPath(testLookupName)) | ||
.reply(200, '<html>\n<h1>Header wo. nodes</h1>\n<hmtl/>'); | ||
expect( | ||
await getPkgReleases({ | ||
...testConfig, | ||
datasource, | ||
lookupName: testLookupName, | ||
}) | ||
).toBeNull(); | ||
}); | ||
|
||
it('404 returns null', async () => { | ||
httpMock.scope(testRegistryUrl).get(getPath(testLookupName)).reply(404); | ||
expect( | ||
await getPkgReleases({ | ||
...testConfig, | ||
datasource, | ||
lookupName: testLookupName, | ||
}) | ||
).toBeNull(); | ||
expect(logger.warn).toHaveBeenCalledTimes(1); | ||
expect(logger.warn).toHaveBeenCalledWith( | ||
{ | ||
lookupName: 'project', | ||
registryUrl: 'https://jfrog.company.com/artifactory', | ||
}, | ||
'artifactory: `Not Found` error' | ||
); | ||
}); | ||
|
||
it('throws for error diff than 404', async () => { | ||
httpMock.scope(testRegistryUrl).get(getPath(testLookupName)).reply(502); | ||
await expect( | ||
getPkgReleases({ | ||
...testConfig, | ||
datasource, | ||
lookupName: testLookupName, | ||
}) | ||
).rejects.toThrow(EXTERNAL_HOST_ERROR); | ||
}); | ||
|
||
it('throws no Http error', async () => { | ||
httpMock | ||
.scope(testRegistryUrl) | ||
.get(getPath(testLookupName)) | ||
.replyWithError('unknown error'); | ||
const res = await getPkgReleases({ | ||
...testConfig, | ||
datasource, | ||
lookupName: testLookupName, | ||
}); | ||
expect(res).toBeNull(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.