integration support for harness p5-fixed api microsite-fix content api

Signed-off-by: Calvin Lee <cjlee@ualberta.ca>
This commit is contained in:
Calvin Lee
2024-04-29 12:41:19 -06:00
parent d01f3ab890
commit 9093f35e8a
4 changed files with 14 additions and 19 deletions
@@ -58,29 +58,24 @@ const createReader = (config: JsonObject): UrlReaderPredicateTuple[] => {
};
const responseBuffer = Buffer.from('Apache License');
const harnessApiResponse = (content: any) => {
return JSON.stringify({
content: {
data: Buffer.from(content).toString('base64'),
encoding: 'base64',
},
});
return content;
};
const handlers = [
rest.get(
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/content/all-apis.yaml',
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/raw/all-apis.yaml',
(_req, res, ctx) => {
return res(ctx.status(500), ctx.json({ message: 'Error!!!' }));
},
),
rest.get(
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/content/404error.yaml',
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/raw/404error.yaml',
(_req, res, ctx) => {
return res(ctx.status(404), ctx.json({ message: 'File not found.' }));
},
),
rest.get(
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/content/stream.TXT',
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/raw/stream.TXT',
(_req, res, ctx) => {
return res(
ctx.status(200),
@@ -90,7 +85,7 @@ const handlers = [
),
rest.get(
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/content/buffer.TXT',
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/:path+/raw/buffer.TXT',
(_req, res, ctx) => {
return res(
ctx.status(200),
@@ -173,7 +168,7 @@ describe('HarnessUrlReader', () => {
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projName/repos/repoName/files/refMain/~/404error.yaml',
),
).rejects.toThrow(
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projName/repos/repoName/files/refMain/~/404error.yaml x https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/+/content/404error.yaml?routingId=accountId&include_commit=false&ref=refMain, 404 Not Found',
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projName/repos/repoName/files/refMain/~/404error.yaml x https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/+/raw/404error.yaml?routingId=accountId&git_ref=refMain, 404 Not Found',
);
});
@@ -183,7 +178,7 @@ describe('HarnessUrlReader', () => {
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projName/repos/repoName/files/refMain/~/all-apis.yaml',
),
).rejects.toThrow(
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projName/repos/repoName/files/refMain/~/all-apis.yaml x https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/+/content/all-apis.yaml?routingId=accountId&include_commit=false&ref=refMain, 500 Internal Server Error',
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projName/repos/repoName/files/refMain/~/all-apis.yaml x https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/+/raw/all-apis.yaml?routingId=accountId&git_ref=refMain, 500 Internal Server Error',
);
});
});
@@ -79,18 +79,18 @@ export class HarnessUrlReader implements UrlReader {
}
if (response.ok) {
// Harness Code returns an object with the file contents encoded, not the file itself
const jsonResponse = await response.json();
if (jsonResponse?.content?.encoding === 'base64') {
// Harness Code returns the raw content object
const jsonResponse = { data: response.body };
if (jsonResponse) {
return ReadUrlResponseFactory.fromReadable(
Readable.from(Buffer.from(jsonResponse?.content?.data, 'base64')),
Readable.from(jsonResponse.data),
{
etag: response.headers.get('ETag') ?? undefined,
},
);
}
throw new Error(`Unknown encoding: ${jsonResponse?.content?.encoding}`);
throw new Error(`Unknown json: ${jsonResponse}`);
}
const message = `${url} x ${blobUrl}, ${response.status} ${response.statusText}`;
@@ -38,7 +38,7 @@ describe('Harness code core', () => {
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projName/repos/repoName/files/refMain/~/all-apis.yaml',
),
).toEqual(
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/+/content/all-apis.yaml?routingId=accountId&include_commit=false&ref=refMain',
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/+/raw/all-apis.yaml?routingId=accountId&git_ref=refMain',
);
});
});
+1 -1
View File
@@ -102,7 +102,7 @@ export function getHarnessFileContentsUrl(
const refIndex = refAndPath.findIndex(item => item === '~');
const refString = refAndPath.slice(0, refIndex);
const pathWithoutSlash = path.join('/').replace(/^\//, '');
return `${baseUrl}/gateway/code/api/v1/repos/${accountId}/${orgName}/${projectName}/${repoName}/+/content/${pathWithoutSlash}?routingId=${accountId}&include_commit=false&ref=${refString}`;
return `${baseUrl}/gateway/code/api/v1/repos/${accountId}/${orgName}/${projectName}/${repoName}/+/raw/${pathWithoutSlash}?routingId=${accountId}&git_ref=${refString}`;
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}