Skip to content

Commit

Permalink
Allow for versionless integration details urls (#103484) (#103553)
Browse files Browse the repository at this point in the history
Default to either the installed version of an integration, or the latest
available version based on installation status when a version is not
included in the integration details URL.

Closes #93393

Co-authored-by: Kyle Pollich <kyle.pollich@elastic.co>
  • Loading branch information
kibanamachine and kpollich authored Jun 28, 2021
1 parent 8d1c01d commit 58bed92
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
10 changes: 9 additions & 1 deletion x-pack/plugins/fleet/server/services/epm/packages/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,17 @@ export async function getPackageInfo(options: {
Registry.fetchFindLatestPackage(pkgName),
]);

// If no package version is provided, use the installed version in the response
let responsePkgVersion = pkgVersion || savedObject?.attributes.install_version;

// If no installed version of the given package exists, default to the latest version of the package
if (!responsePkgVersion) {
responsePkgVersion = latestPackage.version;
}

const getPackageRes = await getPackageFromSource({
pkgName,
pkgVersion,
pkgVersion: responsePkgVersion,
savedObjectsClient,
installedPkg: savedObject?.attributes,
});
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/fleet/server/services/epm/registry/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ test('testPathParts', () => {
});

describe('splitPkgKey tests', () => {
it('throws an error if the delimiter is not found', () => {
expect(() => {
splitPkgKey('awesome_package');
}).toThrow();
});

it('throws an error if there is nothing before the delimiter', () => {
expect(() => {
splitPkgKey('-0.0.1-dev1');
Expand All @@ -71,6 +65,12 @@ describe('splitPkgKey tests', () => {
}).toThrow();
});

it('returns name and empty version if no delimiter is found', () => {
const { pkgName, pkgVersion } = splitPkgKey('awesome_package');
expect(pkgName).toBe('awesome_package');
expect(pkgVersion).toBe('');
});

it('returns the name and version if the delimiter is found once', () => {
const { pkgName, pkgVersion } = splitPkgKey('awesome-0.1.0');
expect(pkgName).toBe('awesome');
Expand Down
10 changes: 8 additions & 2 deletions x-pack/plugins/fleet/server/services/epm/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ export interface SearchParams {
* @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('-'));
// If no version is provided, use the provided package key as the
// package name and return an empty version value
if (!pkgkey.includes('-')) {
return { pkgName: pkgkey, pkgVersion: '' };
}

const pkgName = pkgkey.includes('-') ? pkgkey.substr(0, pkgkey.indexOf('-')) : pkgkey;

if (pkgName === '') {
throw new PackageKeyInvalidError('Package key parsing failed: package name was empty');
}
Expand Down

0 comments on commit 58bed92

Please sign in to comment.