Skip to content

Commit 6b2489e

Browse files
committed
Added fallback to previous release and validation
1 parent 06144b3 commit 6b2489e

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

jfrog-tasks-utils/utils.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,57 @@ function syncRequestWithRetry(method, url, options = {}, maxRetries = 3, retryDe
5757
}
5858

5959
/**
60-
* Fetches the latest JFrog CLI version from GitHub releases with retry mechanism.
60+
* Checks if the CLI binary exists on releases.jfrog.io for the given version.
61+
* @param {string} version - The CLI version to check
62+
* @returns {boolean} True if binary exists, false otherwise
63+
*/
64+
function isCliBinaryAvailable(version) {
65+
const binaryUrl = `https://releases.jfrog.io/artifactory/jfrog-cli/v2-jf/${version}/${cliPackage}/${fileName}`;
66+
try {
67+
console.log('Verifying CLI binary availability at: ' + binaryUrl);
68+
const res = syncRequestWithRetry('HEAD', binaryUrl, { timeout: 5000 });
69+
return res.statusCode === 200;
70+
} catch (ex) {
71+
console.warn('Failed to verify CLI binary availability: ' + ex.message);
72+
return false;
73+
}
74+
}
75+
76+
/**
77+
* Fetches the latest available JFrog CLI version from GitHub releases with retry mechanism.
78+
* Validates that the binary is available on releases.jfrog.io before returning.
79+
* If the latest release binary isn't available, falls back to the previous release.
6180
* Called once during module initialization. Result is cached in defaultJfrogCliVersion.
62-
* @returns {string} The CLI version (e.g., '2.89.0') or fallback if fetch fails
81+
* @returns {string} The CLI version (e.g., '2.89.0') or fallback if fetch fails or no binary available
6382
*/
6483
function fetchLatestCliVersion() {
6584
try {
66-
console.log('Fetching latest JFrog CLI version from https://api.github.com/repos/jfrog/jfrog-cli/releases/latest');
67-
const res = syncRequestWithRetry('GET', 'https://api.github.com/repos/jfrog/jfrog-cli/releases/latest', {
85+
console.log('Fetching JFrog CLI releases from https://api.github.com/repos/jfrog/jfrog-cli/releases');
86+
const res = syncRequestWithRetry('GET', 'https://api.github.com/repos/jfrog/jfrog-cli/releases?per_page=3', {
6887
headers: { 'User-Agent': 'jfrog-azure-devops-extension' },
6988
timeout: 5000,
7089
});
7190
if (res.statusCode === 200) {
72-
const release = JSON.parse(res.getBody('utf8'));
73-
const version = release.name;
74-
console.log('Fetched latest JFrog CLI version: ' + version);
75-
return version;
91+
const releases = JSON.parse(res.getBody('utf8'));
92+
console.log('Fetched ' + releases?.length ?? 0 + ' JFrog CLI releases');
93+
94+
// Try each release until we find one with an available binary
95+
for (const release of releases) {
96+
const version = release?.name;
97+
console.log('Checking CLI version: ' + version);
98+
99+
if (isCliBinaryAvailable(version)) {
100+
console.log('CLI binary verified available for version: ' + version);
101+
return version;
102+
}
103+
console.warn('CLI binary not yet available for version: ' + version);
104+
}
105+
console.warn('No CLI binaries available for last 3 releases, using fallback: ' + fallbackCliVersion);
106+
return fallbackCliVersion;
76107
}
77108
console.warn('Unexpected status code: ' + res.statusCode + ', using fallback version: ' + fallbackCliVersion);
78109
} catch (ex) {
79-
console.warn('Failed to fetch latest JFrog CLI version, using fallback: ' + fallbackCliVersion);
110+
console.warn('Failed to fetch JFrog CLI releases, using fallback: ' + fallbackCliVersion);
80111
}
81112
return fallbackCliVersion;
82113
}

0 commit comments

Comments
 (0)