Merge pull request #20214 from mecseid/feature/implement-gitea-readtree

Implement readTree of Gitea provider to support Techdocs
This commit is contained in:
Fredrik Adelöw
2023-11-15 10:13:24 +01:00
committed by GitHub
9 changed files with 373 additions and 30 deletions
+30
View File
@@ -419,12 +419,30 @@ export function getGerritRequestOptions(config: GerritIntegrationConfig): {
headers?: Record<string, string>;
};
// @public
export function getGiteaArchiveUrl(
config: GiteaIntegrationConfig,
url: string,
): string;
// @public
export function getGiteaEditContentsUrl(
config: GiteaIntegrationConfig,
url: string,
): string;
// @public
export function getGiteaFileContentsUrl(
config: GiteaIntegrationConfig,
url: string,
): string;
// @public
export function getGiteaLatestCommitUrl(
config: GiteaIntegrationConfig,
url: string,
): string;
// @public
export function getGiteaRequestOptions(config: GiteaIntegrationConfig): {
headers?: Record<string, string>;
@@ -658,6 +676,18 @@ export function parseGerritGitilesUrl(
// @public
export function parseGerritJsonResponse(response: Response): Promise<unknown>;
// @public
export function parseGiteaUrl(
config: GiteaIntegrationConfig,
url: string,
): {
url: string;
owner: string;
name: string;
ref: string;
path: string;
};
// @public
export type PersonalAccessTokenCredential = AzureCredentialBase & {
kind: 'PersonalAccessToken';
@@ -18,9 +18,12 @@ import { setupServer } from 'msw/node';
import { setupRequestMockHandlers } from '../helpers';
import { GiteaIntegrationConfig } from './config';
import {
getGiteaArchiveUrl,
getGiteaEditContentsUrl,
getGiteaFileContentsUrl,
getGiteaLatestCommitUrl,
getGiteaRequestOptions,
parseGiteaUrl,
} from './core';
describe('gitea core', () => {
@@ -59,6 +62,38 @@ describe('gitea core', () => {
});
});
describe('getGiteaArchiveUrl', () => {
it('can create an url from arguments', () => {
const config: GiteaIntegrationConfig = {
host: 'gitea.example.com',
};
expect(
getGiteaArchiveUrl(
config,
'https://gitea.example.com/owner/repo/src/branch/branch_name',
),
).toEqual(
'https://gitea.example.com/api/v1/repos/owner/repo/archive/branch_name.tar.gz',
);
});
});
describe('getGiteaLatestCommitUrl', () => {
it('can create an url from arguments', () => {
const config: GiteaIntegrationConfig = {
host: 'gitea.example.com',
};
expect(
getGiteaLatestCommitUrl(
config,
'https://gitea.example.com/owner/repo/src/branch/branch_name/',
),
).toEqual(
'https://gitea.example.com/api/v1/repos/owner/repo/git/commits/branch_name',
);
});
});
describe('getGerritRequestOptions', () => {
it('adds token header when only a password is specified', () => {
const authRequest: GiteaIntegrationConfig = {
@@ -90,4 +125,61 @@ describe('gitea core', () => {
).toEqual(basicAuthentication);
});
});
describe('parseGiteaUrl', () => {
it('can fetch gitea url', () => {
const config: GiteaIntegrationConfig = {
host: 'gitea.example.com',
};
expect(
parseGiteaUrl(
config,
'https://gitea.example.com/owner/repo/src/branch/branch_name/',
),
).toEqual({
url: 'https://gitea.example.com',
owner: 'owner',
name: 'repo',
ref: 'branch_name',
path: '',
});
});
it('provide path without starting slash', () => {
const config: GiteaIntegrationConfig = {
host: 'gitea.example.com',
};
expect(
parseGiteaUrl(
config,
'https://gitea.example.com/owner/repo/src/branch/branch_name/simple/path',
),
).toEqual({
url: 'https://gitea.example.com',
owner: 'owner',
name: 'repo',
ref: 'branch_name',
path: 'simple/path',
});
});
it('use base url if provided', () => {
const config: GiteaIntegrationConfig = {
host: 'gitea.example.com',
baseUrl: 'https://base-gitea.example.com',
};
expect(
parseGiteaUrl(
config,
'https://base-gitea.example.com/owner/repo/src/branch/branch_name/',
),
).toEqual({
url: 'https://base-gitea.example.com',
owner: 'owner',
name: 'repo',
ref: 'branch_name',
path: '',
});
});
});
});
+83 -20
View File
@@ -33,16 +33,8 @@ export function getGiteaEditContentsUrl(
config: GiteaIntegrationConfig,
url: string,
) {
try {
const baseUrl = config.baseUrl ?? `https://${config.host}`;
const [_blank, owner, name, _src, _branch, ref, ...path] = url
.replace(baseUrl, '')
.split('/');
const pathWithoutSlash = path.join('/').replace(/^\//, '');
return `${baseUrl}/${owner}/${name}/_edit/${ref}/${pathWithoutSlash}`;
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}
const giteaUrl = parseGiteaUrl(config, url);
return `${giteaUrl.url}/${giteaUrl.owner}/${giteaUrl.name}/_edit/${giteaUrl.ref}/${giteaUrl.path}`;
}
/**
@@ -63,17 +55,52 @@ export function getGiteaFileContentsUrl(
config: GiteaIntegrationConfig,
url: string,
) {
try {
const baseUrl = config.baseUrl ?? `https://${config.host}`;
const [_blank, owner, name, _src, _branch, ref, ...path] = url
.replace(baseUrl, '')
.split('/');
const pathWithoutSlash = path.join('/').replace(/^\//, '');
const giteaUrl = parseGiteaUrl(config, url);
return `${giteaUrl.url}/api/v1/repos/${giteaUrl.owner}/${giteaUrl.name}/contents/${giteaUrl.path}?ref=${giteaUrl.ref}`;
}
return `${baseUrl}/api/v1/repos/${owner}/${name}/contents/${pathWithoutSlash}?ref=${ref}`;
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}
/**
* 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
* or: https://gitea.com/api/v1/repos/a/b/archive/branchname.tar.gz
*
* @param url - A URL pointing to a repository/path
* @param config - The relevant provider config
* @public
*/
export function getGiteaArchiveUrl(
config: GiteaIntegrationConfig,
url: string,
) {
const giteaUrl = parseGiteaUrl(config, url);
return `${giteaUrl.url}/api/v1/repos/${giteaUrl.owner}/${giteaUrl.name}/archive/${giteaUrl.ref}.tar.gz`;
}
/**
* Given a URL pointing to a repository branch, returns a URL
* for latest commit information.
*
* @remarks
*
* Converts
* from: https://gitea.com/a/b/src/branchname
* or: https://gitea.com/api/v1/repos/a/b/git/commits/branchname
*
* @param url - A URL pointing to a repository branch
* @param config - The relevant provider config
* @public
*/
export function getGiteaLatestCommitUrl(
config: GiteaIntegrationConfig,
url: string,
) {
const giteaUrl = parseGiteaUrl(config, url);
return `${giteaUrl.url}/api/v1/repos/${giteaUrl.owner}/${giteaUrl.name}/git/commits/${giteaUrl.ref}`;
}
/**
@@ -104,3 +131,39 @@ export function getGiteaRequestOptions(config: GiteaIntegrationConfig): {
headers,
};
}
/**
* Return parsed git url properties.
*
* @param config - A Gitea provider config
* @param url - A URL pointing to a repository
* @public
*/
export function parseGiteaUrl(
config: GiteaIntegrationConfig,
url: string,
): {
url: string;
owner: string;
name: string;
ref: string;
path: string;
} {
const baseUrl = config.baseUrl ?? `https://${config.host}`;
try {
const [_blank, owner, name, _src, _branch, ref, ...path] = url
.replace(baseUrl, '')
.split('/');
const pathWithoutSlash = path.join('/').replace(/^\//, '');
return {
url: baseUrl,
owner: owner,
name: name,
ref: ref,
path: pathWithoutSlash,
};
} catch (e) {
throw new Error(`Incorrect URL: ${url}, ${e}`);
}
}
+8 -1
View File
@@ -14,6 +14,13 @@
* limitations under the License.
*/
export { GiteaIntegration } from './GiteaIntegration';
export { getGiteaRequestOptions, getGiteaFileContentsUrl } from './core';
export {
getGiteaEditContentsUrl,
getGiteaFileContentsUrl,
getGiteaArchiveUrl,
getGiteaLatestCommitUrl,
getGiteaRequestOptions,
parseGiteaUrl,
} from './core';
export { readGiteaConfig } from './config';
export type { GiteaIntegrationConfig } from './config';