add support for readTree

Signed-off-by: Calvin Lee <cjlee@ualberta.ca>
This commit is contained in:
Calvin Lee
2024-05-07 00:03:03 -06:00
parent dadc9960f9
commit 395b9738c4
9 changed files with 388 additions and 67 deletions
+27
View File
@@ -513,12 +513,24 @@ export function getGitLabRequestOptions(config: GitLabIntegrationConfig): {
headers: Record<string, string>;
};
// @public
export function getHarnessArchiveUrl(
config: HarnessIntegrationConfig,
url: string,
): string;
// @public
export function getHarnessFileContentsUrl(
config: HarnessIntegrationConfig,
url: string,
): string;
// @public
export function getHarnessLatestCommitUrl(
config: HarnessIntegrationConfig,
url: string,
): string;
// @public
export function getHarnessRequestOptions(config: HarnessIntegrationConfig): {
headers?: Record<string, string>;
@@ -764,6 +776,21 @@ export function parseGiteaUrl(
path: string;
};
// @public
export function parseHarnessUrl(
config: HarnessIntegrationConfig,
url: string,
): {
baseUrl: string;
accountId: string;
orgName: string;
projectName: string;
refString: string;
repoName: string;
path: string;
refDashStr: string;
};
// @public
export type PersonalAccessTokenCredential = AzureCredentialBase & {
kind: 'PersonalAccessToken';
+82 -1
View File
@@ -18,9 +18,12 @@ import { setupServer } from 'msw/node';
import { setupRequestMockHandlers } from '../helpers';
import { HarnessIntegrationConfig } from './config';
import {
getHarnessArchiveUrl,
getHarnessEditContentsUrl,
getHarnessFileContentsUrl,
getHarnessLatestCommitUrl,
getHarnessRequestOptions,
parseHarnessUrl,
} from './core';
describe('Harness code core', () => {
@@ -38,7 +41,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/+/raw/all-apis.yaml?routingId=accountId&git_ref=refMain',
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projName/repoName/+/raw/all-apis.yaml?routingId=accountId&git_ref=refs/heads/refMain',
);
});
});
@@ -59,6 +62,38 @@ describe('Harness code core', () => {
});
});
describe('getHarnessArchiveUrl', () => {
it('can create an url from arguments', () => {
const config: HarnessIntegrationConfig = {
host: 'app.harness.io',
};
expect(
getHarnessArchiveUrl(
config,
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projectName/repos/repoName/files/branchName',
),
).toEqual(
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projectName/repoName/+/archive/branchName.zip',
);
});
});
describe('getGiteaLatestCommitUrl', () => {
it('can create an url from arguments', () => {
const config: HarnessIntegrationConfig = {
host: 'app.harness.io',
};
expect(
getHarnessLatestCommitUrl(
config,
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projectName/repos/repoName/files/branchName',
),
).toEqual(
'https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projectName/repoName/+/content?routingId=accountId&include_commit=true&git_ref=refs/heads/branchName',
);
});
});
describe('getHarnessRequestOptions', () => {
it('adds token header when only a token is specified', () => {
const authRequest: HarnessIntegrationConfig = {
@@ -88,4 +123,50 @@ describe('Harness code core', () => {
).toEqual('a');
});
});
describe('parseHarnessUrl', () => {
it('can fetch harness url', () => {
const config: HarnessIntegrationConfig = {
host: 'app.harness.io',
};
expect(
parseHarnessUrl(
config,
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projectName/repos/repoName/files/branchName',
),
).toEqual({
accountId: 'accountId',
baseUrl: 'https://app.harness.io',
orgName: 'orgName',
path: '',
projectName: 'projectName',
refDashStr: '',
refString: '',
repoName: 'repoName',
branch: 'branchName',
});
});
it('provide path without starting slash', () => {
const config: HarnessIntegrationConfig = {
host: 'app.harness.io',
};
expect(
parseHarnessUrl(
config,
'https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projectName/repos/repoName/files/branchName/simple/path',
),
).toEqual({
accountId: 'accountId',
baseUrl: 'https://app.harness.io',
orgName: 'orgName',
path: 'path',
projectName: 'projectName',
refDashStr: 'branchName-simple',
refString: 'branchName/simple',
repoName: 'repoName',
branch: 'branchName/simple/path',
});
});
});
});
+118 -54
View File
@@ -33,31 +33,8 @@ export function getHarnessEditContentsUrl(
config: HarnessIntegrationConfig,
url: string,
) {
try {
const baseUrl = `https://${config.host}`;
const [
_blank,
_ng,
_account,
accountId,
_module,
_moduleName,
_org,
orgName,
_projects,
projectName,
_repos,
repoName,
_files,
_ref,
_branch,
...path
] = url.replace(baseUrl, '').split('/');
const pathWithoutSlash = path.join('/').replace(/^\//, '');
return `${baseUrl}/gateway/code/api/v1/repos/${accountId}/${orgName}/${projectName}/${repoName}/+/edit/${pathWithoutSlash}`;
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}
const parsedUrl = parseHarnessUrl(config, url);
return `${parsedUrl.baseUrl}/gateway/code/api/v1/repos/${parsedUrl.accountId}/${parsedUrl.orgName}/${parsedUrl.projectName}/${parsedUrl.repoName}/+/edit/${parsedUrl.path}`;
}
/**
@@ -77,35 +54,52 @@ export function getHarnessFileContentsUrl(
config: HarnessIntegrationConfig,
url: string,
) {
try {
const baseUrl = `https://${config.host}`;
const [
_blank,
_ng,
_account,
accountId,
_module,
_moduleName,
_org,
orgName,
_projects,
projectName,
_repos,
repoName,
_files,
_ref,
_branch,
...path
] = url.replace(baseUrl, '').split('/');
const urlParts = url.replace(baseUrl, '').split('/');
const refAndPath = urlParts.slice(13);
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}/+/raw/${pathWithoutSlash}?routingId=${accountId}&git_ref=${refString}`;
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}
const parsedUrl = parseHarnessUrl(config, url);
return `${parsedUrl.baseUrl}/gateway/code/api/v1/repos/${parsedUrl.accountId}/${parsedUrl.orgName}/${parsedUrl.projectName}/${parsedUrl.repoName}/+/raw/${parsedUrl.path}?routingId=${parsedUrl.accountId}&git_ref=refs/heads/${parsedUrl.refString}`;
}
/**
* Given a URL pointing to a repository/path, returns a URL
* for archive contents of the repository.
*
* @remarks
*
* Converts
* from: https://gitea.com/a/b/src/branchname
* to: https://gitea.com/api/v1/repos/a/b/archive/branchname.zip
*
* @param url - A URL pointing to a repository/path
* @param config - The relevant provider config
* @public
*/
export function getHarnessArchiveUrl(
config: HarnessIntegrationConfig,
url: string,
) {
const parsedUrl = parseHarnessUrl(config, url);
return `${parsedUrl.baseUrl}/gateway/code/api/v1/repos/${parsedUrl.accountId}/${parsedUrl.orgName}/${parsedUrl.projectName}/${parsedUrl.repoName}/+/archive/${parsedUrl.branch}.zip`;
}
/**
* Given a URL pointing to a repository branch, returns a URL
* for latest commit information.
*
* @remarks
*
* Converts
* from: https://app.harness.io/ng/account/accountId/module/code/orgs/orgName/projects/projectName/repos/repoName/files/branchName
* to: https://app.harness.io/gateway/code/api/v1/repos/accountId/orgName/projectName/repoName/+/content?routingId=accountId&include_commit=true&git_ref=refs/heads/branchName
*
* @param url - A URL pointing to a repository branch
* @param config - The relevant provider config
* @public
*/
export function getHarnessLatestCommitUrl(
config: HarnessIntegrationConfig,
url: string,
) {
const parsedUrl = parseHarnessUrl(config, url);
return `${parsedUrl.baseUrl}/gateway/code/api/v1/repos/${parsedUrl.accountId}/${parsedUrl.orgName}/${parsedUrl.projectName}/${parsedUrl.repoName}/+/content?routingId=${parsedUrl.accountId}&include_commit=true&git_ref=refs/heads/${parsedUrl.refString}`;
}
/**
@@ -130,3 +124,73 @@ export function getHarnessRequestOptions(config: HarnessIntegrationConfig): {
headers,
};
}
/**
* Return parsed git url properties.
*
* @param config - A Gitea provider config
* @param url - A URL pointing to a repository
* @public
*/
export function parseHarnessUrl(
config: HarnessIntegrationConfig,
url: string,
): {
baseUrl: string;
accountId: string;
orgName: string;
projectName: string;
refString: string;
repoName: string;
path: string;
refDashStr: string;
branch: string;
} {
const baseUrl = `https://${config.host}`;
try {
const pathUrl = new URL(url);
const pathSegments = pathUrl.pathname
.split('/')
.filter(segment => segment !== '');
const urlParts = pathUrl.pathname.split('/');
const [
_ng,
_account,
accountId,
_module,
_moduleName,
_org,
orgName,
_projects,
projectName,
_repos,
repoName,
_files,
_ref,
_branch,
..._path
] = pathSegments;
const refAndPath = urlParts.slice(
urlParts.findIndex(i => i === 'files') + 1,
);
const refIndex = refAndPath.findIndex(item => item === '~');
const refString = refAndPath.slice(0, refIndex).join('/');
const pathWithoutSlash = refAndPath
.slice(refIndex + 1)
.join('/')
.replace(/^\//, '');
return {
baseUrl: baseUrl,
accountId: accountId,
orgName: orgName,
projectName: projectName,
refString: refString,
path: pathWithoutSlash,
repoName: repoName,
refDashStr: refAndPath.slice(0, refIndex).join('-'),
branch: refAndPath.join('/'),
};
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}
}
+7 -1
View File
@@ -14,6 +14,12 @@
* limitations under the License.
*/
export { HarnessIntegration } from './HarnessIntegration';
export { getHarnessRequestOptions, getHarnessFileContentsUrl } from './core';
export {
getHarnessRequestOptions,
getHarnessFileContentsUrl,
getHarnessArchiveUrl,
getHarnessLatestCommitUrl,
parseHarnessUrl,
} from './core';
export { readHarnessConfig } from './config';
export type { HarnessIntegrationConfig } from './config';