Unverified Commit 174d4b56 authored by Bruno Borges's avatar Bruno Borges Committed by GitHub
Browse files

Support pinning java-version as "latest" (#1093)

* Support pinning java-version as "latest"

Add a `latest` alias for the `java-version` input that floats to the newest
available stable (GA) release. It is normalized to the SemVer wildcard at the
base-installer layer and always resolves from remote (like `check-latest: true`).

List-based distributions resolve it automatically via the existing newest-first
matching. Corretto selects its newest available major; Oracle and GraalVM look up
the newest GA major via the Adoptium API and request it, failing with an actionable
error if that major isn't published yet. The jdkfile distribution rejects `latest`.

Closes #832

Co-authored-by: default avatarCopilot App <[email protected]>

* Clarify latest note for oracle/graalvm version resolution vs download

Co-authored-by: default avatarCopilot App <[email protected]>

* graalvm-community: float latest to its own newest GA release

GraalVM Community publishes its releases on GitHub, so the `latest`
alias now matches against that real release list using the SemVer
wildcard instead of asking the Adoptium API for the newest GA major.
This prevents `latest` from hard-failing when GraalVM lags behind a
freshly released Java major (e.g. Adoptium reports 26 before GraalVM
ships it). Oracle GraalVM has no listing endpoint, so it keeps deriving
the newest major from Adoptium and errors clearly if that major is not
yet published.

Co-authored-by: default avatarCopilot App <[email protected]>

* Potential fix for pull request finding

Co-authored-by: default avatarCopilot Autofix powered by AI <[email protected]>

* Give latest+qualifier inputs (e.g. latest-ea) a targeted error

Inputs like 'latest-ea' had their '-ea' suffix stripped and fell through
to the generic SemVer validation, failing with a confusing "'latest' is
not valid SemVer" message even though 'latest' is supported. Add an
explicit guard so any 'latest*' value other than exactly 'latest' throws
a targeted error explaining that 'latest' resolves GA releases only and
cannot be combined with '-ea' or other qualifiers.

Co-authored-by: default avatarCopilot App <[email protected]>

---------

Co-authored-by: default avatarCopilot App <[email protected]>
Co-authored-by: default avatarCopilot Autofix powered by AI <[email protected]>
parent 86ce8e6c
Loading
Loading
Loading
Loading
+8
Changes for README.md: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -106,6 +106,14 @@ The `java-version` input supports an exact version or a version range using [Sem
- more specific versions: `8.0.282+8`, `8.0.232`, `11.0`, `11.0.4`, `17.0`
- multi-field Java versions (JEP 322), such as: `11.0.9.1`, `18.0.1.1`
- early access (EA) versions: `15-ea`, `15.0.0-ea`
- the `latest` alias, which floats to the newest available stable (GA) release

> [!NOTE]
> - `latest` always resolves the newest version from the distribution's remote metadata (it behaves like `check-latest: true`), so it ignores any older version already present in the runner tool cache. This has the same performance trade-off described in [Check latest](#check-latest).
> - `latest` is only supported through the `java-version` input, not through `java-version-file`, and it resolves stable (GA) releases only — it cannot be combined with `-ea`.
> - The `jdkfile` distribution does not support `latest`, as it installs from a local file.
> - For `oracle` and `graalvm` (Oracle GraalVM), `latest` uses the Adoptium API only to determine the newest GA **major version number** — the JDK binary itself is still downloaded from the Oracle / GraalVM servers for that major. Because these distributions have no endpoint to list their own releases, if their servers haven't published the resolved major yet, the action fails and asks you to specify a concrete version. Note the Oracle JDK license caveat below still applies to a floating `latest`.
> - For `graalvm-community`, `latest` floats to the newest GA release published on GitHub, so it never depends on the Adoptium API and always resolves to the newest major that GraalVM Community actually ships.

#### Supported distributions
Currently, the following distributions are supported:
+46 −9
Changes for __tests__/distributors/base-installer.test.ts: 46 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -420,6 +420,29 @@ describe('setupJava', () => {
    expect(spyCoreInfo).not.toHaveBeenCalledWith('Trying to download...');
  });

  it('should resolve the latest version from remote when java-version is "latest", even if a version is cached', async () => {
    mockJavaBase = new EmptyJavaBase({
      version: 'latest',
      architecture: 'x86',
      packageType: 'jdk',
      checkLatest: false
    });

    await expect(mockJavaBase.setupJava()).resolves.toEqual({
      version: actualJavaVersion,
      path: javaPathInstalled
    });

    // `latest` must bypass the tool-cache short-circuit and always resolve remotely
    expect(spyCoreInfo).toHaveBeenCalledWith(
      'Trying to resolve the latest version from remote'
    );
    expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
    expect(spyCoreInfo).not.toHaveBeenCalledWith(
      `Resolved Java ${installedJavaVersion} from tool-cache`
    );
  });

  it.each([
    [
      {
@@ -744,15 +767,18 @@ describe('normalizeVersion', () => {
  const DummyJavaBase = JavaBase as any;

  it.each([
    ['11', {version: '11', stable: true}],
    ['11.0', {version: '11.0', stable: true}],
    ['11.0.10', {version: '11.0.10', stable: true}],
    ['11-ea', {version: '11', stable: false}],
    ['11.0.2-ea', {version: '11.0.2', stable: false}],
    ['18.0.1.1', {version: '18.0.1+1', stable: true}],
    ['11.0.9.1', {version: '11.0.9+1', stable: true}],
    ['12.0.2.1.0', {version: '12.0.2+1.0', stable: true}],
    ['18.0.1.1-ea', {version: '18.0.1+1', stable: false}]
    ['11', {version: '11', stable: true, latest: false}],
    ['11.0', {version: '11.0', stable: true, latest: false}],
    ['11.0.10', {version: '11.0.10', stable: true, latest: false}],
    ['11-ea', {version: '11', stable: false, latest: false}],
    ['11.0.2-ea', {version: '11.0.2', stable: false, latest: false}],
    ['18.0.1.1', {version: '18.0.1+1', stable: true, latest: false}],
    ['11.0.9.1', {version: '11.0.9+1', stable: true, latest: false}],
    ['12.0.2.1.0', {version: '12.0.2+1.0', stable: true, latest: false}],
    ['18.0.1.1-ea', {version: '18.0.1+1', stable: false, latest: false}],
    ['latest', {version: 'x', stable: true, latest: true}],
    ['LATEST', {version: 'x', stable: true, latest: true}],
    ['  Latest  ', {version: 'x', stable: true, latest: true}]
  ])('normalizeVersion from %s to %s', (input, expected) => {
    expect(DummyJavaBase.prototype.normalizeVersion.call(null, input)).toEqual(
      expected
@@ -767,6 +793,17 @@ describe('normalizeVersion', () => {
      `The string '${version}' is not valid SemVer notation for a Java version. Please check README file for code snippets and more detailed information`
    );
  });

  it.each(['latest-ea', 'latest.1', 'LATEST-EA', '  latest-ea  '])(
    'normalizeVersion should throw a targeted error for latest combined with a qualifier (%s)',
    version => {
      expect(
        DummyJavaBase.prototype.normalizeVersion.bind(null, version)
      ).toThrow(
        `The 'latest' alias resolves stable (GA) releases only and cannot be combined with '-ea' or other qualifiers (received '${version}'). Use 'latest' on its own, or specify a concrete version.`
      );
    }
  );
});

describe('createVersionNotFoundError', () => {
+18
Changes for __tests__/distributors/corretto-installer.test.ts: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -204,6 +204,24 @@ describe('getAvailableVersions', () => {
      expect(availableVersion.url).toBe(expectedLink);
    });

    it('with latest resolves to the newest available major version', async () => {
      const distribution = new CorrettoDistribution({
        version: 'latest',
        architecture: 'x64',
        packageType: 'jdk',
        checkLatest: false
      });
      mockPlatform(distribution, 'linux');

      const availableVersion =
        await distribution['findPackageForDownload']('x');
      expect(availableVersion).not.toBeNull();
      // 18 is the newest major present in the mocked Corretto index
      expect(availableVersion.url).toBe(
        'https://corretto.aws/downloads/resources/18.0.0.37.1/amazon-corretto-18.0.0.37.1-linux-x64.tar.gz'
      );
    });

    it('with unstable version expect to throw not supported error', async () => {
      const version = '18.0.1-ea';
      const distribution = new CorrettoDistribution({
+109
Changes for __tests__/distributors/graalvm-installer.test.ts: 109 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -417,6 +417,61 @@ describe('GraalVMDistribution', () => {
        );
      });

      describe('latest alias', () => {
        it('resolves the newest major version from the Adoptium API', async () => {
          const latestDistribution = new GraalVMDistribution({
            ...defaultOptions,
            version: 'latest'
          });
          (latestDistribution as any).http = mockHttpClient;
          jest
            .spyOn(latestDistribution, 'getPlatform')
            .mockReturnValue('linux');
          mockHttpClient.getJson.mockResolvedValue({
            statusCode: 200,
            result: {most_recent_feature_release: 25},
            headers: {}
          });
          mockHttpClient.head.mockResolvedValue({
            message: {statusCode: 200}
          });

          const result = await (
            latestDistribution as any
          ).findPackageForDownload('x');

          expect(result).toEqual({
            url: 'https://download.oracle.com/graalvm/25/latest/graalvm-jdk-25_linux-x64_bin.tar.gz',
            version: '25'
          });
        });

        it('throws an actionable error when the latest major is not yet available', async () => {
          const latestDistribution = new GraalVMDistribution({
            ...defaultOptions,
            version: 'latest'
          });
          (latestDistribution as any).http = mockHttpClient;
          jest
            .spyOn(latestDistribution, 'getPlatform')
            .mockReturnValue('linux');
          mockHttpClient.getJson.mockResolvedValue({
            statusCode: 200,
            result: {most_recent_feature_release: 25},
            headers: {}
          });
          mockHttpClient.head.mockResolvedValue({
            message: {statusCode: 404}
          });

          await expect(
            (latestDistribution as any).findPackageForDownload('x')
          ).rejects.toThrow(
            /is not yet available for the GraalVM distribution/
          );
        });
      });

      it('should throw error for JDK versions less than 17', async () => {
        await expect(
          (distribution as any).findPackageForDownload('11')
@@ -1115,6 +1170,60 @@ describe('GraalVMDistribution', () => {
        });
      });

      it('resolves latest to the newest GA across all Community majors without calling Adoptium', async () => {
        const latestCommunity = new GraalVMCommunityDistribution({
          ...defaultOptions,
          version: 'latest'
        });
        (latestCommunity as any).http = mockHttpClient;
        jest.spyOn(latestCommunity, 'getPlatform').mockReturnValue('linux');

        mockHttpClient.getJson.mockResolvedValue({
          result: [
            {
              draft: false,
              prerelease: false,
              assets: [
                {
                  name: 'graalvm-community-jdk-21.0.2_linux-x64_bin.tar.gz',
                  browser_download_url:
                    'https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-21.0.2/graalvm-community-jdk-21.0.2_linux-x64_bin.tar.gz'
                }
              ]
            },
            {
              draft: false,
              prerelease: false,
              assets: [
                {
                  name: 'graalvm-community-jdk-24.0.1_linux-x64_bin.tar.gz',
                  browser_download_url:
                    'https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-24.0.1/graalvm-community-jdk-24.0.1_linux-x64_bin.tar.gz'
                }
              ]
            }
          ],
          statusCode: 200,
          headers: {}
        });

        const result = await (latestCommunity as any).findPackageForDownload(
          'x'
        );

        expect(result).toEqual({
          url: 'https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-24.0.1/graalvm-community-jdk-24.0.1_linux-x64_bin.tar.gz',
          version: '24.0.1'
        });
        // The Community release list is authoritative, so the Adoptium
        // most_recent_feature_release endpoint must not be consulted.
        expect(mockHttpClient.getJson).toHaveBeenCalledTimes(1);
        expect(mockHttpClient.getJson).toHaveBeenCalledWith(
          expect.stringContaining('graalvm-ce-builds/releases'),
          expect.anything()
        );
      });

      it('should reject GraalVM Community early access requests', async () => {
        (communityDistribution as any).stable = false;

+14
Changes for __tests__/distributors/local-installer.test.ts: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -170,6 +170,20 @@ describe('setupJava', () => {
    jest.restoreAllMocks();
  });

  it('throws for the latest alias since jdkfile has no version list', async () => {
    const inputs = {
      version: 'latest',
      architecture: 'x86',
      packageType: 'jdk',
      checkLatest: false
    };

    mockJavaBase = new LocalDistribution(inputs, expectedJdkFile);
    await expect(mockJavaBase.setupJava()).rejects.toThrow(
      "The 'latest' version alias is not supported for the 'jdkfile' distribution. Please specify a concrete version."
    );
  });

  it('java is resolved from toolcache, jdkfile is untouched', async () => {
    const inputs = {
      version: actualJavaVersion,
Loading