-
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(presets): local bitbucket-server presets (#7000)
- Loading branch information
Showing
7 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
lib/config/presets/bitbucket-server/__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,92 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`config/presets/bitbucket-server/index fetchJSONFile() returns JSON 1`] = ` | ||
Object { | ||
"from": "api", | ||
} | ||
`; | ||
|
||
exports[`config/presets/bitbucket-server/index fetchJSONFile() returns JSON 2`] = ` | ||
Array [ | ||
Object { | ||
"headers": Object { | ||
"accept": "application/json", | ||
"accept-encoding": "gzip, deflate", | ||
"authorization": "Bearer abc", | ||
"host": "git.company.org", | ||
"user-agent": "https://github.com/renovatebot/renovate", | ||
"x-atlassian-token": "no-check", | ||
}, | ||
"method": "GET", | ||
"url": "https://git.company.org/rest/api/1.0/projects/some/repos/repo/browse/some-filename.json?limit=20000", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`config/presets/bitbucket-server/index fetchJSONFile() throws 404 1`] = ` | ||
Array [ | ||
Object { | ||
"headers": Object { | ||
"accept": "application/json", | ||
"accept-encoding": "gzip, deflate", | ||
"authorization": "Bearer abc", | ||
"host": "git.company.org", | ||
"user-agent": "https://github.com/renovatebot/renovate", | ||
"x-atlassian-token": "no-check", | ||
}, | ||
"method": "GET", | ||
"url": "https://git.company.org/rest/api/1.0/projects/some/repos/repo/browse/some-filename.json?limit=20000", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`config/presets/bitbucket-server/index fetchJSONFile() throws to big 1`] = ` | ||
Array [ | ||
Object { | ||
"headers": Object { | ||
"accept": "application/json", | ||
"accept-encoding": "gzip, deflate", | ||
"authorization": "Bearer abc", | ||
"host": "git.company.org", | ||
"user-agent": "https://github.com/renovatebot/renovate", | ||
"x-atlassian-token": "no-check", | ||
}, | ||
"method": "GET", | ||
"url": "https://git.company.org/rest/api/1.0/projects/some/repos/repo/browse/some-filename.json?limit=20000", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`config/presets/bitbucket-server/index fetchJSONFile() throws to invalid 1`] = ` | ||
Array [ | ||
Object { | ||
"headers": Object { | ||
"accept": "application/json", | ||
"accept-encoding": "gzip, deflate", | ||
"authorization": "Bearer abc", | ||
"host": "git.company.org", | ||
"user-agent": "https://github.com/renovatebot/renovate", | ||
"x-atlassian-token": "no-check", | ||
}, | ||
"method": "GET", | ||
"url": "https://git.company.org/rest/api/1.0/projects/some/repos/repo/browse/some-filename.json?limit=20000", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`config/presets/bitbucket-server/index getPresetFromEndpoint() uses custom endpoint 1`] = ` | ||
Array [ | ||
Object { | ||
"headers": Object { | ||
"accept": "application/json", | ||
"accept-encoding": "gzip, deflate", | ||
"authorization": "Bearer abc", | ||
"host": "api.github.example.org", | ||
"user-agent": "https://github.com/renovatebot/renovate", | ||
"x-atlassian-token": "no-check", | ||
}, | ||
"method": "GET", | ||
"url": "https://api.github.example.org/rest/api/1.0/projects/some/repos/repo/browse/default.json?limit=20000", | ||
}, | ||
] | ||
`; |
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,123 @@ | ||
import * as httpMock from '../../../../test/httpMock'; | ||
import { getName, mocked } from '../../../../test/util'; | ||
import * as _hostRules from '../../../util/host-rules'; | ||
import { PRESET_DEP_NOT_FOUND } from '../util'; | ||
import * as bitbucketServer from '.'; | ||
|
||
jest.mock('../../../util/host-rules'); | ||
|
||
const hostRules = mocked(_hostRules); | ||
|
||
const bitbucketApiHost = 'https://git.company.org'; | ||
const basePath = '/rest/api/1.0/projects/some/repos/repo/browse'; | ||
|
||
describe(getName(__filename), () => { | ||
beforeEach(() => { | ||
httpMock.setup(); | ||
hostRules.find.mockReturnValue({ token: 'abc' }); | ||
}); | ||
|
||
afterEach(() => httpMock.reset()); | ||
|
||
describe('fetchJSONFile()', () => { | ||
it('returns JSON', async () => { | ||
httpMock | ||
.scope(bitbucketApiHost) | ||
.get(`${basePath}/some-filename.json`) | ||
.query({ limit: 20000 }) | ||
.reply(200, { | ||
isLastPage: true, | ||
lines: ['{"from":"api"}'], | ||
}); | ||
|
||
const res = await bitbucketServer.fetchJSONFile( | ||
'some/repo', | ||
'some-filename.json', | ||
bitbucketApiHost | ||
); | ||
expect(res).toMatchSnapshot(); | ||
expect(httpMock.getTrace()).toMatchSnapshot(); | ||
}); | ||
|
||
it('throws 404', async () => { | ||
httpMock | ||
.scope(bitbucketApiHost) | ||
.get(`${basePath}/some-filename.json`) | ||
.query({ limit: 20000 }) | ||
.reply(404); | ||
|
||
await expect( | ||
bitbucketServer.fetchJSONFile( | ||
'some/repo', | ||
'some-filename.json', | ||
bitbucketApiHost | ||
) | ||
).rejects.toThrow(PRESET_DEP_NOT_FOUND); | ||
expect(httpMock.getTrace()).toMatchSnapshot(); | ||
}); | ||
|
||
it('throws to big', async () => { | ||
httpMock | ||
.scope(bitbucketApiHost) | ||
.get(`${basePath}/some-filename.json`) | ||
.query({ limit: 20000 }) | ||
.reply(200, { | ||
isLastPage: false, | ||
size: 50000, | ||
lines: ['{"from":"api"}'], | ||
}); | ||
|
||
await expect( | ||
bitbucketServer.fetchJSONFile( | ||
'some/repo', | ||
'some-filename.json', | ||
bitbucketApiHost | ||
) | ||
).rejects.toThrow('invalid preset JSON'); | ||
expect(httpMock.getTrace()).toMatchSnapshot(); | ||
}); | ||
|
||
it('throws to invalid', async () => { | ||
httpMock | ||
.scope(bitbucketApiHost) | ||
.get(`${basePath}/some-filename.json`) | ||
.query({ limit: 20000 }) | ||
.reply(200, { | ||
isLastPage: true, | ||
lines: ['{"from":"api"'], | ||
}); | ||
|
||
await expect( | ||
bitbucketServer.fetchJSONFile( | ||
'some/repo', | ||
'some-filename.json', | ||
bitbucketApiHost | ||
) | ||
).rejects.toThrow('invalid preset JSON'); | ||
expect(httpMock.getTrace()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('getPresetFromEndpoint()', () => { | ||
it('uses custom endpoint', async () => { | ||
httpMock | ||
.scope('https://api.github.example.org') | ||
.get(`${basePath}/default.json`) | ||
.query({ limit: 20000 }) | ||
.reply(200, { | ||
isLastPage: true, | ||
lines: ['{"from":"api"}'], | ||
}); | ||
expect( | ||
await bitbucketServer | ||
.getPresetFromEndpoint( | ||
'some/repo', | ||
'default', | ||
'https://api.github.example.org' | ||
) | ||
.catch(() => ({ from: 'api' })) | ||
).toEqual({ from: 'api' }); | ||
expect(httpMock.getTrace()).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
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,59 @@ | ||
import { logger } from '../../../logger'; | ||
import { ExternalHostError } from '../../../types/errors/external-host-error'; | ||
import { FileData } from '../../../types/platform/bitbucket-server'; | ||
import { | ||
BitbucketServerHttp, | ||
setBaseUrl, | ||
} from '../../../util/http/bitbucket-server'; | ||
import { Preset } from '../common'; | ||
import { PRESET_DEP_NOT_FOUND, fetchPreset } from '../util'; | ||
|
||
const http = new BitbucketServerHttp(); | ||
|
||
export async function fetchJSONFile( | ||
repo: string, | ||
fileName: string, | ||
endpoint: string | ||
): Promise<Preset> { | ||
const [projectKey, repositorySlug] = repo.split('/'); | ||
setBaseUrl(endpoint); | ||
const url = `/rest/api/1.0/projects/${projectKey}/repos/${repositorySlug}/browse/${fileName}?limit=20000`; | ||
let res: { body: FileData }; | ||
try { | ||
res = await http.getJson(url); | ||
} catch (err) { | ||
// istanbul ignore if: not testable with nock | ||
if (err instanceof ExternalHostError) { | ||
throw err; | ||
} | ||
logger.debug( | ||
{ statusCode: err.statusCode }, | ||
`Failed to retrieve ${fileName} from repo` | ||
); | ||
throw new Error(PRESET_DEP_NOT_FOUND); | ||
} | ||
if (!res.body.isLastPage) { | ||
logger.warn({ size: res.body.size }, 'Renovate config to big'); | ||
throw new Error('invalid preset JSON'); | ||
} | ||
try { | ||
const content = res.body.lines.join(); | ||
const parsed = JSON.parse(content); | ||
return parsed; | ||
} catch (err) { | ||
throw new Error('invalid preset JSON'); | ||
} | ||
} | ||
|
||
export function getPresetFromEndpoint( | ||
pkgName: string, | ||
filePreset: string, | ||
endpoint: string | ||
): Promise<Preset> { | ||
return fetchPreset({ | ||
pkgName, | ||
filePreset, | ||
endpoint, | ||
fetch: fetchJSONFile, | ||
}); | ||
} |
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
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
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
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,10 @@ | ||
/** | ||
* https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8297065392 | ||
*/ | ||
export interface FileData { | ||
isLastPage: boolean; | ||
|
||
lines: string[]; | ||
|
||
size: number; | ||
} |