Skip to content

Commit

Permalink
refactor(platform): Extract getRawFile method (#9404)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 5, 2021
1 parent 430249d commit 1473047
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 53 deletions.
12 changes: 12 additions & 0 deletions lib/platform/azure/index.spec.ts
Expand Up @@ -1196,6 +1196,18 @@ describe('platform/azure', () => {
const res = await azure.getJsonFile('file.json');
expect(res).toEqual(data);
});
it('returns null for malformed JSON', async () => {
azureApi.gitApi.mockImplementationOnce(
() =>
({
getItemContent: jest.fn(() =>
Promise.resolve(Readable.from('!@#'))
),
} as any)
);
const res = await azure.getJsonFile('file.json');
expect(res).toBeNull();
});
it('returns null on errors', async () => {
azureApi.gitApi.mockImplementationOnce(
() =>
Expand Down
18 changes: 15 additions & 3 deletions lib/platform/azure/index.ts
Expand Up @@ -104,15 +104,27 @@ export async function getRepos(): Promise<string[]> {
return repos.map((repo) => `${repo.project.name}/${repo.name}`);
}

export async function getJsonFile(
export async function getRawFile(
fileName: string,
repo: string = config.repoId
): Promise<any | null> {
): Promise<string | null> {
try {
const azureApiGit = await azureApi.gitApi();
const buf = await azureApiGit.getItemContent(repo, fileName);
const str = await streamToString(buf);
return JSON.parse(str);
return str;
} catch (err) {
return null;
}
}

export async function getJsonFile(
fileName: string,
repo: string = config.repoId
): Promise<any | null> {
try {
const raw = await getRawFile(fileName, repo);
return raw && JSON.parse(raw);
} catch (err) {
return null;
}
Expand Down
146 changes: 114 additions & 32 deletions lib/platform/bitbucket-server/__snapshots__/index.spec.ts.snap
Expand Up @@ -1972,6 +1972,47 @@ Array [
]
`;

exports[`platform/bitbucket-server/index endpoint with no path getJsonFile() returns null for malformed JSON 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "stash.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
"x-atlassian-token": "no-check",
},
"method": "GET",
"url": "https://stash.renovatebot.com/rest/api/1.0/projects/SOME/repos/repo",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "stash.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
"x-atlassian-token": "no-check",
},
"method": "GET",
"url": "https://stash.renovatebot.com/rest/api/1.0/projects/SOME/repos/repo/branches/default",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "stash.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
"x-atlassian-token": "no-check",
},
"method": "GET",
"url": "https://stash.renovatebot.com/rest/api/1.0/projects/SOME/repos/repo/browse/file.json?limit=20000",
},
]
`;

exports[`platform/bitbucket-server/index endpoint with no path getJsonFile() returns null on errors 1`] = `
Array [
Object {
Expand Down Expand Up @@ -2310,22 +2351,6 @@ Array [

exports[`platform/bitbucket-server/index endpoint with no path getPr() returns null for no prNo 1`] = `Array []`;

exports[`platform/bitbucket-server/index endpoint with no path massageMarkdown() returns diff files 1`] = `"**foo**bartext"`;

exports[`platform/bitbucket-server/index endpoint with no path massageMarkdown() sanitizes HTML comments in the body 1`] = `
"---
- [ ] If you want to rebase/retry this PR, check this box
- [ ] <a href=\\"/some/link\\">Update renovate/renovate to 16.1.2</a>
---
Empty comment.
Followed by some information.
"
`;
exports[`platform/bitbucket-server/index endpoint with no path getPrList() has pr 1`] = `
Array [
Object {
Expand Down Expand Up @@ -2579,6 +2604,22 @@ Array [
]
`;

exports[`platform/bitbucket-server/index endpoint with no path massageMarkdown() returns diff files 1`] = `"**foo**bartext"`;

exports[`platform/bitbucket-server/index endpoint with no path massageMarkdown() sanitizes HTML comments in the body 1`] = `
"---
- [ ] If you want to rebase/retry this PR, check this box
- [ ] <a href=\\"/some/link\\">Update renovate/renovate to 16.1.2</a>
---
Empty comment.
Followed by some information.
"
`;
exports[`platform/bitbucket-server/index endpoint with no path mergePr() posts Merge 1`] = `
Array [
Object {
Expand Down Expand Up @@ -6062,6 +6103,47 @@ Array [
]
`;
exports[`platform/bitbucket-server/index endpoint with path getJsonFile() returns null for malformed JSON 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "stash.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
"x-atlassian-token": "no-check",
},
"method": "GET",
"url": "https://stash.renovatebot.com/vcs/rest/api/1.0/projects/SOME/repos/repo",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "stash.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
"x-atlassian-token": "no-check",
},
"method": "GET",
"url": "https://stash.renovatebot.com/vcs/rest/api/1.0/projects/SOME/repos/repo/branches/default",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "stash.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
"x-atlassian-token": "no-check",
},
"method": "GET",
"url": "https://stash.renovatebot.com/vcs/rest/api/1.0/projects/SOME/repos/repo/browse/file.json?limit=20000",
},
]
`;
exports[`platform/bitbucket-server/index endpoint with path getJsonFile() returns null on errors 1`] = `
Array [
Object {
Expand Down Expand Up @@ -6400,22 +6482,6 @@ Array [
exports[`platform/bitbucket-server/index endpoint with path getPr() returns null for no prNo 1`] = `Array []`;
exports[`platform/bitbucket-server/index endpoint with path massageMarkdown() returns diff files 1`] = `"**foo**bartext"`;
exports[`platform/bitbucket-server/index endpoint with path massageMarkdown() sanitizes HTML comments in the body 1`] = `
"---
- [ ] If you want to rebase/retry this PR, check this box
- [ ] <a href=\\"/some/link\\">Update renovate/renovate to 16.1.2</a>
---
Empty comment.
Followed by some information.
"
`;
exports[`platform/bitbucket-server/index endpoint with path getPrList() has pr 1`] = `
Array [
Object {
Expand Down Expand Up @@ -6669,6 +6735,22 @@ Array [
]
`;
exports[`platform/bitbucket-server/index endpoint with path massageMarkdown() returns diff files 1`] = `"**foo**bartext"`;
exports[`platform/bitbucket-server/index endpoint with path massageMarkdown() sanitizes HTML comments in the body 1`] = `
"---
- [ ] If you want to rebase/retry this PR, check this box
- [ ] <a href=\\"/some/link\\">Update renovate/renovate to 16.1.2</a>
---
Empty comment.
Followed by some information.
"
`;
exports[`platform/bitbucket-server/index endpoint with path mergePr() posts Merge 1`] = `
Array [
Object {
Expand Down
14 changes: 14 additions & 0 deletions lib/platform/bitbucket-server/index.spec.ts
Expand Up @@ -2107,6 +2107,20 @@ Followed by some information.
expect(res).toEqual(data);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for malformed JSON', async () => {
const scope = await initRepo();
scope
.get(
`${urlPath}/rest/api/1.0/projects/SOME/repos/repo/browse/file.json?limit=20000`
)
.reply(200, {
isLastPage: true,
lines: [{ text: '!@#' }],
});
const res = await bitbucket.getJsonFile('file.json');
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for long content', async () => {
const scope = await initRepo();
scope
Expand Down
18 changes: 15 additions & 3 deletions lib/platform/bitbucket-server/index.ts
Expand Up @@ -118,17 +118,17 @@ export async function getRepos(): Promise<string[]> {
}
}

export async function getJsonFile(
export async function getRawFile(
fileName: string,
repo: string = config.repository
): Promise<any | null> {
): Promise<string | null> {
try {
const [project, slug] = repo.split('/');
const fileUrl = `./rest/api/1.0/projects/${project}/repos/${slug}/browse/${fileName}?limit=20000`;
const res = await bitbucketServerHttp.getJson<FileData>(fileUrl);
const { isLastPage, lines, size } = res.body;
if (isLastPage) {
return JSON.parse(lines.map(({ text }) => text).join(''));
return lines.map(({ text }) => text).join('');
}
logger.warn({ size }, `The file is too big`);
} catch (err) {
Expand All @@ -137,6 +137,18 @@ export async function getJsonFile(
return null;
}

export async function getJsonFile(
fileName: string,
repo: string = config.repository
): Promise<any | null> {
try {
const raw = await getRawFile(fileName, repo);
return raw && JSON.parse(raw);
} catch (err) {
return null;
}
}

// Initialize BitBucket Server by getting base branch
export async function initRepo({
repository,
Expand Down
26 changes: 25 additions & 1 deletion lib/platform/bitbucket/__snapshots__/index.spec.ts.snap
Expand Up @@ -908,6 +908,21 @@ Array [
"method": "GET",
"url": "https://api.bitbucket.org/2.0/repositories/some/repo",
},
Object {
"headers": Object {
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "api.bitbucket.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.bitbucket.org/2.0/repositories/some/repo/src/HEAD/file.json",
},
]
`;

exports[`platform/bitbucket getJsonFile() returns null for malformed JSON 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
Expand All @@ -917,6 +932,16 @@ Array [
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.bitbucket.org/2.0/repositories/some/repo",
},
Object {
"headers": Object {
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "api.bitbucket.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.bitbucket.org/2.0/repositories/some/repo/src/HEAD/file.json",
},
]
Expand All @@ -937,7 +962,6 @@ Array [
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "api.bitbucket.org",
Expand Down
9 changes: 9 additions & 0 deletions lib/platform/bitbucket/index.spec.ts
Expand Up @@ -851,6 +851,15 @@ describe('platform/bitbucket', () => {
expect(res).toEqual(data);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for malformed JSON', async () => {
const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/src/HEAD/file.json')
.reply(200, '!@#');
const res = await bitbucket.getJsonFile('file.json');
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null on errors', async () => {
const scope = await initRepoMock();
scope
Expand Down
18 changes: 15 additions & 3 deletions lib/platform/bitbucket/index.ts
Expand Up @@ -99,21 +99,33 @@ export async function getRepos(): Promise<string[]> {
}
}

export async function getJsonFile(
export async function getRawFile(
fileName: string,
repo: string = config.repository
): Promise<any | null> {
): Promise<string | null> {
try {
// See: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/src/%7Bcommit%7D/%7Bpath%7D
const path = fileName;
const url = `/2.0/repositories/${repo}/src/HEAD/${path}`;
const res = await bitbucketHttp.getJson(url);
const res = await bitbucketHttp.get(url);
return res.body;
} catch (err) {
return null;
}
}

export async function getJsonFile(
fileName: string,
repo: string = config.repository
): Promise<any | null> {
try {
const raw = await getRawFile(fileName, repo);
return raw && JSON.parse(raw);
} catch (err) {
return null;
}
}

// Initialize bitbucket by getting base branch and SHA
export async function initRepo({
repository,
Expand Down

0 comments on commit 1473047

Please sign in to comment.