integration support for harness p2-comments

Signed-off-by: Calvin Lee <cjlee@ualberta.ca>
This commit is contained in:
Calvin Lee
2024-04-21 20:31:45 -06:00
parent 4750bf6622
commit d422716946
8 changed files with 31 additions and 65 deletions
+2 -3
View File
@@ -552,7 +552,7 @@ export type GiteaIntegrationConfig = {
host: string;
baseUrl?: string;
username?: string;
token?: string;
password?: string;
};
// @public
@@ -711,8 +711,7 @@ export class HarnessIntegration implements ScmIntegration {
// @public
export type HarnessIntegrationConfig = {
host: string;
baseUrl?: string;
username?: string;
apiKey?: string;
token?: string;
};
@@ -25,8 +25,6 @@ describe('HarnessIntegration', () => {
harness: [
{
host: 'app.harness.io',
username: 'git',
baseUrl: 'https://app.harness.io/route',
token: '1234',
},
],
@@ -35,9 +33,6 @@ describe('HarnessIntegration', () => {
});
expect(integrations.list().length).toBe(1);
expect(integrations.list()[0].config.host).toBe('app.harness.io');
expect(integrations.list()[0].config.baseUrl).toBe(
'https://app.harness.io/route',
);
});
it('returns the basics', () => {
@@ -51,16 +51,14 @@ describe('readHarnessConfig', () => {
const output = readHarnessConfig(
buildConfig({
host: 'a.com',
baseUrl: 'https://a.com/route/api',
username: 'u',
token: 'p',
apiKey: 'a',
}),
);
expect(output).toEqual({
host: 'a.com',
baseUrl: 'https://a.com/route/api',
username: 'u',
token: 'p',
apiKey: 'a',
});
});
@@ -72,8 +70,6 @@ describe('readHarnessConfig', () => {
);
expect(output).toEqual({
host: 'a.com',
baseUrl: 'https://a.com',
username: undefined,
token: undefined,
});
});
@@ -95,14 +91,11 @@ describe('readHarnessConfig', () => {
readHarnessConfig(
await buildFrontendConfig({
host: 'a.com',
baseUrl: 'https://a.com/route',
username: 'u',
token: 'p',
}),
),
).toEqual({
host: 'a.com',
baseUrl: 'https://a.com/route',
});
});
});
+9 -23
View File
@@ -15,11 +15,10 @@
*/
import { Config } from '@backstage/config';
import { trimEnd } from 'lodash';
import { isValidHost } from '../helpers';
/**
* The configuration for a single Gitea integration.
* The configuration for a single Harness integration.
*
* @public
*/
@@ -28,23 +27,14 @@ export type HarnessIntegrationConfig = {
* The host of the target that this matches on, e.g. "app.harness.io"
*/
host: string;
/**
* The optional base URL of the Harness code instance. It is assumed that https
* is used and that the base path is "/" on the host. If that is not the
* case set the complete base url to the Harness code instance, e.g.
* "https://harnesscode.website.com/". This is the url that you would open
* in a browser.
*/
baseUrl?: string;
/**
* The username to use for requests to harness code.
*/
username?: string;
/**
* The password or http token to use for authentication.
*/
token?: string;
/**
* The API key to use for authentication.
*/
apiKey?: string;
};
/**
@@ -55,24 +45,20 @@ export type HarnessIntegrationConfig = {
export function readHarnessConfig(config: Config): HarnessIntegrationConfig {
const host = config.getString('host');
let baseUrl = config.getOptionalString('baseUrl');
const username = config.getOptionalString('username');
const token = config.getOptionalString('token');
const apiKey = config.getOptionalString('apiKey');
if (!isValidHost(host)) {
throw new Error(
`Invalid Harness Code integration config, '${host}' is not a valid host`,
);
}
if (baseUrl) {
baseUrl = trimEnd(baseUrl, '/');
} else {
baseUrl = `https://${host}`;
}
baseUrl = `https://${host}`;
return {
host,
baseUrl,
username,
apiKey,
token,
};
}
@@ -76,20 +76,16 @@ describe('Harness code core', () => {
).toBeUndefined();
});
it('adds basic auth when username and token are specified', () => {
it('adds basic auth when apikey and token are specified', () => {
const authRequest: HarnessIntegrationConfig = {
host: 'gerrit.com',
username: 'username',
token: 'P',
apiKey: 'a',
};
const basicAuthentication = `basic ${Buffer.from(
`${authRequest.username}:${authRequest.token}`,
).toString('base64')}`;
expect(
(getHarnessRequestOptions(authRequest).headers as any).Authorization,
).toEqual(basicAuthentication);
(getHarnessRequestOptions(authRequest).headers as any)['x-api-key'],
).toEqual('a');
});
});
});
+7 -10
View File
@@ -34,7 +34,7 @@ export function getHarnessEditContentsUrl(
url: string,
) {
try {
const baseUrl = config.baseUrl ?? `https://${config.host}`;
const baseUrl = `https://${config.host}`;
const [
_blank,
_ng,
@@ -61,9 +61,8 @@ export function getHarnessEditContentsUrl(
}
/**
* Given a URL pointing to a file, returns an api URL
* for fetching the contents of the data.
*
* Given a file path URL,
* it returns an API URL which returns the contents of the file.
* @remarks
*
* Converts
@@ -79,7 +78,7 @@ export function getHarnessFileContentsUrl(
url: string,
) {
try {
const baseUrl = config.baseUrl ?? `https://${config.host}`;
const baseUrl = `https://${config.host}`;
const [
_blank,
_ng,
@@ -115,16 +114,14 @@ export function getHarnessRequestOptions(config: HarnessIntegrationConfig): {
headers?: Record<string, string>;
} {
const headers: Record<string, string> = {};
const { username, token } = config;
const { token, apiKey } = config;
if (!token) {
return headers;
}
if (username) {
headers.Authorization = `basic ${Buffer.from(
`${username}:${token}`,
).toString('base64')}`;
if (apiKey) {
headers['x-api-key'] = apiKey;
} else {
headers.Authorization = `Bearer ${token}`;
}