Skip to content

Commit

Permalink
Allow prerelease in version
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Aug 5, 2020
1 parent c655f50 commit 22c86c8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
5 changes: 3 additions & 2 deletions x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
getInstallationObject,
} from '../../services/epm/packages';
import { IngestManagerError, getHTTPResponseCode } from '../../errors';
import { splitPkgKey } from '../../services/epm/registry';

export const getCategoriesHandler: RequestHandler<
undefined,
Expand Down Expand Up @@ -131,7 +132,7 @@ export const getInfoHandler: RequestHandler<TypeOf<typeof GetInfoRequestSchema.p
const { pkgkey } = request.params;
const savedObjectsClient = context.core.savedObjects.client;
// TODO: change epm API to /packageName/version so we don't need to do this
const [pkgName, pkgVersion] = pkgkey.split('-');
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
const res = await getPackageInfo({ savedObjectsClient, pkgName, pkgVersion });
const body: GetInfoResponse = {
response: res,
Expand All @@ -155,7 +156,7 @@ export const installPackageHandler: RequestHandler<
const savedObjectsClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const { pkgkey } = request.params;
const [pkgName, pkgVersion] = pkgkey.split('-');
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
try {
const res = await installPackage({
savedObjectsClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function installPackage({
force?: boolean;
}): Promise<AssetReference[]> {
// TODO: change epm API to /packageName/version so we don't need to do this
const [pkgName, pkgVersion] = pkgkey.split('-');
const { pkgName, pkgVersion } = Registry.splitPkgKey(pkgkey);
// TODO: calls to getInstallationObject, Registry.fetchInfo, and Registry.fetchFindLatestPackge
// and be replaced by getPackageInfo after adjusting for it to not group/use archive assets
const latestPackage = await Registry.fetchFindLatestPackage(pkgName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getInstallation, savedObjectTypes } from './index';
import { deletePipeline } from '../elasticsearch/ingest_pipeline/';
import { installIndexPatterns } from '../kibana/index_pattern/install';
import { packageConfigService, appContextService } from '../..';
import { splitPkgKey } from '../registry';

export async function removeInstallation(options: {
savedObjectsClient: SavedObjectsClientContract;
Expand All @@ -21,7 +22,7 @@ export async function removeInstallation(options: {
}): Promise<AssetReference[]> {
const { savedObjectsClient, pkgkey, callCluster } = options;
// TODO: the epm api should change to /name/version so we don't need to do this
const [pkgName] = pkgkey.split('-');
const { pkgName } = splitPkgKey(pkgkey);
const installation = await getInstallation({ savedObjectsClient, pkgName });
if (!installation) throw Boom.badRequest(`${pkgName} is not installed`);
if (installation.removable === false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { AssetParts } from '../../../types';
import { pathParts } from './index';
import { pathParts, splitPkgKey } from './index';

const testPaths = [
{
Expand Down Expand Up @@ -48,3 +48,30 @@ test('testPathParts', () => {
expect(pathParts(value.path)).toStrictEqual(value.assetParts as AssetParts);
}
});

describe('splitPkgKey tests', () => {
it('returns an empty name if the delimiter is not found', () => {
const pkgkey = 'awesome_package';
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
expect(pkgName).toBe('');
expect(pkgVersion).toBe(pkgkey);
});

it('returns an empty name if there is nothing before the delimiter', () => {
const { pkgName, pkgVersion } = splitPkgKey('-0.0.1-dev1');
expect(pkgName).toBe('');
expect(pkgVersion).toBe('0.0.1-dev1');
});

it('returns the name and version if the delimiter is found once', () => {
const { pkgName, pkgVersion } = splitPkgKey('awesome-0.1.0');
expect(pkgName).toBe('awesome');
expect(pkgVersion).toBe('0.1.0');
});

it('returns the name and version if the delimiter is found multiple times', () => {
const { pkgName, pkgVersion } = splitPkgKey('endpoint-0.13.0-alpha.1+abcd');
expect(pkgName).toBe('endpoint');
expect(pkgVersion).toBe('0.13.0-alpha.1+abcd');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ export interface CategoriesParams {
experimental?: boolean;
}

/**
* Extract the package name and package version from a string.
*
* @param pkgkey a string containing the package name delimited by the package version
*/
export function splitPkgKey(pkgkey: string): { pkgName: string; pkgVersion: string } {
// this will return an empty string if `indexOf` returns -1
const pkgName = pkgkey.substr(0, pkgkey.indexOf('-'));
// this will return the entire string if `indexOf` return -1
const pkgVersion = pkgkey.substr(pkgkey.indexOf('-') + 1);
return { pkgName, pkgVersion };
}

export const pkgToPkgKey = ({ name, version }: { name: string; version: string }) =>
`${name}-${version}`;

Expand Down

0 comments on commit 22c86c8

Please sign in to comment.