@@ -9,6 +9,8 @@ import axios from 'axios';
9
9
import semver from 'semver' ;
10
10
import { map } from 'lodash' ;
11
11
import { PackagePolicy , CreatePackagePolicyResponse , API_VERSIONS } from '@kbn/fleet-plugin/common' ;
12
+ // @ts -expect-error we have to check types with "allowJs: false" for now, causing this import to fail
13
+ import { kibanaPackageJson } from '@kbn/repo-info' ;
12
14
import { KbnClient } from '@kbn/test' ;
13
15
import {
14
16
GetEnrollmentAPIKeysResponse ,
@@ -140,10 +142,23 @@ const isValidArtifactVersion = (version: string) => !!version.match(/^\d+\.\d+\.
140
142
* Returns the Agent version that is available for install (will check `artifacts-api.elastic.co/v1/versions`)
141
143
* that is equal to or less than `maxVersion`.
142
144
* @param kbnClient
145
+ * @param log
143
146
*/
144
147
145
- export const getLatestAvailableAgentVersion = async ( kbnClient : KbnClient ) : Promise < string > => {
146
- const kbnStatus = await kbnClient . status . get ( ) ;
148
+ export const getLatestAvailableAgentVersion = async (
149
+ kbnClient : KbnClient ,
150
+ log : ToolingLog
151
+ ) : Promise < string > => {
152
+ let currentVersion : string ;
153
+
154
+ try {
155
+ const kbnStatus = await kbnClient . status . get ( ) ;
156
+ currentVersion = kbnStatus . version . number ;
157
+ } catch {
158
+ log . warning ( chalk . bold ( 'Failed to get Kibana version, using package.json version' ) ) ;
159
+ currentVersion = kibanaPackageJson . version ;
160
+ }
161
+
147
162
const agentVersions = await pRetry (
148
163
async ( ) => {
149
164
const response = await axios . get ( 'https://artifacts-api.elastic.co/v1/versions' ) ;
@@ -157,9 +172,15 @@ export const getLatestAvailableAgentVersion = async (kbnClient: KbnClient): Prom
157
172
}
158
173
) . catch ( ( ) => null ) ;
159
174
175
+ if ( ! agentVersions ) {
176
+ log . warning (
177
+ chalk . bold ( 'Failed to get agent versions from artifacts-api, using package.json version' )
178
+ ) ;
179
+ }
180
+
160
181
const version = agentVersions
161
- ? semver . maxSatisfying ( agentVersions , `<=${ kbnStatus . version . number } ` )
162
- : kbnStatus . version . number ;
182
+ ? semver . maxSatisfying ( agentVersions , `<=${ currentVersion } ` )
183
+ : currentVersion ;
163
184
164
185
return `${ version } -SNAPSHOT` ;
165
186
} ;
0 commit comments