integration support for harness p2-comments
Signed-off-by: Calvin Lee <cjlee@ualberta.ca>
This commit is contained in:
@@ -20,14 +20,14 @@ integrations:
|
||||
harness:
|
||||
- host: app.harness.io
|
||||
token: ${HARNESS_CODE_BEARER_TOKEN}
|
||||
apiKey: ${HARNESS_CODE_APIKEY}
|
||||
```
|
||||
|
||||
Directly under the `harnessCode` key is a list of provider configurations, where you
|
||||
can list the Gitea instances you want to be able to fetch
|
||||
data from. Each entry is a structure with up to four elements:
|
||||
Directly under the `harness` key is a list of provider configurations, where you
|
||||
can list the Harness instances you want to be able to fetch
|
||||
|
||||
check out https://developer.harness.io/docs/platform/automation/api/add-and-manage-api-keys/ for more information
|
||||
|
||||
- `host`: The host of the Harness Code instance that you want to match on.
|
||||
- `baseUrl` (optional): Needed if the Harness Code instance is not reachable at
|
||||
the base of the `host` option (e.g. `https://app.harness.io`). This is the address that you would open in a browser.
|
||||
- `username` (optional): The gitea username to use in API requests.
|
||||
- `token` (optional): The password or api token to authenticate with.
|
||||
- `apiKey` (optional): The apiKey to authenticate with.
|
||||
|
||||
@@ -118,7 +118,7 @@ export class HarnessUrlReader implements UrlReader {
|
||||
toString() {
|
||||
const { host } = this.integration.config;
|
||||
return `harness{host=${host},authed=${Boolean(
|
||||
this.integration.config.token,
|
||||
this.integration.config.token || this.integration.config.apiKey,
|
||||
)}}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user