Skip to content

Commit 8fa69e9

Browse files
authored
Merge pull request #376 from Achal1607/automate-oracle-jdk-selection
Automate dropdown options of OracleJDK version in JDK downloader
2 parents 46f024d + 75ac5ea commit 8fa69e9

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

vscode/src/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ export namespace extConstants {
2626
}
2727

2828
export namespace jdkDownloaderConstants {
29-
export const JDK_RELEASES_TRACK_URL = `https://www.java.com/releases/releases.json`;
29+
30+
export const ORACLE_JDK_RELEASES_BASE_URL = `https://java.oraclecloud.com/currentJavaReleases`;
3031

3132
export const ORACLE_JDK_BASE_DOWNLOAD_URL = `https://download.oracle.com/java`;
3233

33-
export const ORACLE_JDK_DOWNLOAD_VERSIONS = ['23', '21'];
34-
34+
export const ORACLE_JDK_FALLBACK_VESIONS = ['24', '21'];
35+
3536
export const OPEN_JDK_VERSION_DOWNLOAD_LINKS: { [key: string]: string } = {
3637
"23": "https://download.java.net/java/GA/jdk23.0.2/6da2a6609d6e406f85c491fcb119101b/7/GPL/openjdk-23.0.2"
3738
};

vscode/src/test/unit/webviews/jdkDownloader/view.unit.test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as sinon from 'sinon';
2020
import { WebviewPanel, window } from 'vscode';
2121
import { JdkDownloaderView } from '../../../../webviews/jdkDownloader/view';
2222
import { checkTagContentNotEmpty, enableMockedLoggers, getMachineArch, getOsType } from '../../testUtils';
23+
import * as utils from '../../../../utils';
2324

2425
describe('JDK Downloader view tests', () => {
2526
let jdkDownloaderView: JdkDownloaderView;
@@ -44,6 +45,21 @@ describe('JDK Downloader view tests', () => {
4445
beforeEach(() => {
4546
createWebviewPanelStub = sandbox.stub(window, 'createWebviewPanel');
4647
onDidReceiveMessageStub = sandbox.stub();
48+
let versionsStub: sinon.SinonStub = sandbox.stub(utils, "httpsGet");
49+
versionsStub.returns(`{
50+
"items": [
51+
{
52+
"jdkDetails":{
53+
"jdkVersion": 23
54+
}
55+
},
56+
{
57+
"jdkDetails":{
58+
"jdkVersion": 21
59+
}
60+
}
61+
]
62+
}`);
4763

4864
webviewPanel = {
4965
webview: {
@@ -132,13 +148,6 @@ describe('JDK Downloader view tests', () => {
132148
expect(jdkDownloaderHtml).to.match(archOptionRegex);
133149
});
134150
});
135-
136-
it("should attach a message listener to the webview", () => {
137-
expect(onDidReceiveMessageStub.calledOnce).to.be.true;
138-
const listener = onDidReceiveMessageStub.firstCall.args[0];
139-
expect(listener).to.be.a('function');
140-
});
141-
142151
});
143152

144153
it("should dispose the webview", () => {

vscode/src/webviews/jdkDownloader/view.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { JdkDownloaderAction } from './action';
2121
import { downloaderCss } from './styles';
2222
import { l10n } from '../../localiser';
2323
import { LOGGER } from '../../logger';
24+
import { httpsGet, isError, isString } from '../../utils';
2425

2526
export class JdkDownloaderView {
2627
public static readonly OPEN_JDK_LABEL = "OpenJDK";
@@ -31,8 +32,9 @@ export class JdkDownloaderView {
3132
private jdkDownloaderWebView?: WebviewPanel;
3233
private osType?: string;
3334
private machineArch?: string;
35+
private oracleJdkVersions: string[] = [];
3436

35-
public createView = () => {
37+
public createView = async () => {
3638
try {
3739
LOGGER.log("Creating JDK downloader webview");
3840
this.jdkDownloaderWebView = window.createWebviewPanel(
@@ -43,6 +45,7 @@ export class JdkDownloaderView {
4345
enableScripts: true
4446
}
4547
);
48+
this.oracleJdkVersions = await this.getOracleJdkVersions();
4649
this.setDropdownOptions();
4750
this.jdkDownloaderWebView.webview.html = this.fetchJdkDownloadViewHtml();
4851
this.jdkDownloaderWebView.webview.onDidReceiveMessage(message => {
@@ -111,7 +114,7 @@ export class JdkDownloaderView {
111114
<br />
112115
<div class="jdk-version-dropdown">
113116
<select id="oracleJDKVersionDropdown">
114-
${this.getJdkVersionsHtml(jdkDownloaderConstants.ORACLE_JDK_DOWNLOAD_VERSIONS)}
117+
${this.getJdkVersionsHtml(this.oracleJdkVersions)}
115118
</select>
116119
</div>
117120
</div>
@@ -177,7 +180,29 @@ export class JdkDownloaderView {
177180
`
178181
}
179182

180-
private getJdkVersionsHtml = (jdkVersions: String[]) => {
183+
private getOracleJdkVersions = async (): Promise<string[]> => {
184+
try {
185+
LOGGER.log("Fetching Oracle JDK versions...");
186+
const availableVersions = await httpsGet(`${jdkDownloaderConstants.ORACLE_JDK_RELEASES_BASE_URL}?licenseType=NFTC&sortBy=jdkVersion&sortOrder=DESC`);
187+
if (isString(availableVersions)) {
188+
const availableVersionsObj = JSON.parse(availableVersions);
189+
if (availableVersionsObj?.items) {
190+
const jdkVersions = availableVersionsObj?.items?.map((version: any) => version.jdkDetails.jdkVersion);
191+
LOGGER.log(`Fetched Oracle JDK versions: ${jdkVersions}`);
192+
193+
return jdkVersions;
194+
}
195+
}
196+
LOGGER.warn(`Response of Oracle JDK versions is not as expected`);
197+
} catch (error) {
198+
const msg = `Some error occurred while fetching Oracle JDK versions: ${isError(error) ? error.message : null}`;
199+
LOGGER.warn(msg);
200+
}
201+
202+
return jdkDownloaderConstants.ORACLE_JDK_FALLBACK_VESIONS;
203+
}
204+
205+
private getJdkVersionsHtml = (jdkVersions: string[]) => {
181206
let htmlStr = "";
182207
jdkVersions.forEach((el: String, index: number) => {
183208
if (index === 0) {

0 commit comments

Comments
 (0)