Commit 0173e6dd authored by Bruno Borges's avatar Bruno Borges
Browse files

Infer distribution from asdf .tool-versions vendor prefix (#1084)

* Infer distribution from asdf .tool-versions vendor prefix

asdf-java encodes the JDK vendor as a prefix on the version string in
.tool-versions (e.g. `java temurin-17.0.3+7`). Capture that prefix and
map it to a setup-java distribution, mirroring the existing .sdkmanrc
behavior. Unknown prefixes warn and fall back to the distribution input.

Fixes #1081

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]>

---------

Co-authored-by: default avatarCopilot App <[email protected]>
Co-authored-by: default avatarCopilot Autofix powered by AI <[email protected]>
(cherry picked from commit 4db08ef6)
parent f45cd82b
Loading
Loading
Loading
Loading
+66
Changes for __tests__/util.test.ts: 66 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -243,6 +243,72 @@ describe('getVersionFromFileContent', () => {
      );
    });
  });

  describe('.tool-versions', () => {
    it.each([
      ['java temurin-17.0.3+7', '17.0.3+7', 'temurin'],
      ['java temurin-jre-17.0.3+7', '17.0.3+7', 'temurin'],
      ['java adoptopenjdk-11.0.16+8', '11.0.16+8', 'temurin'],
      ['java adoptopenjdk-openj9-11.0.16+8', '11.0.16+8', 'temurin'],
      ['java zulu-11.56.19', '11.56.19', 'zulu'],
      ['java corretto-17.0.13.11.1', '17', 'corretto'], // corretto -> major only
      ['java liberica-11.0.15+10', '11.0.15+10', 'liberica'],
      ['java microsoft-11.0.13.8.1', '11.0.13', 'microsoft'],
      ['java semeru-openj9-11.0.25+9', '11.0.25+9', 'semeru'],
      ['java ibm-openj9-11.0.25+9', '11.0.25+9', 'semeru'],
      ['java dragonwell-17.0.13.0.13+11', '17.0.13', 'dragonwell'],
      ['java graalvm-22.3.0+java17', '22.3.0+java17', 'graalvm'],
      ['java graalvm-community-22.3.0', '22.3.0', 'graalvm-community'],
      ['java oracle-graalvm-21.0.5', '21.0.5', 'graalvm'],
      ['java oracle-21.0.5', '21.0.5', 'oracle'],
      ['java sapmachine-21.0.5', '21.0.5', 'sapmachine'],
      ['java kona-17.0.13', '17.0.13', 'kona'],
      ['java jetbrains-21.0.5', '21.0.5', 'jetbrains']
    ])(
      'parsing %s should return version %s and distribution %s',
      (content: string, expectedVersion: string, expectedDist: string) => {
        const actual = getVersionFromFileContent(
          content,
          'openjdk',
          '.tool-versions'
        );
        expect(actual?.version).toBe(expectedVersion);
        expect(actual?.distribution).toBe(expectedDist);
      }
    );

    it.each([
      ['java 17.0.7', '17.0.7'],
      ['java 17', '17'],
      ['java 1.8', '8'],
      ['java 21-ea', '21-ea']
    ])(
      'parsing prefix-less %s should return version %s and no distribution',
      (content: string, expectedVersion: string) => {
        const actual = getVersionFromFileContent(
          content,
          'temurin',
          '.tool-versions'
        );
        expect(actual?.version).toBe(expectedVersion);
        expect(actual?.distribution).toBeUndefined();
      }
    );

    it('should warn and return undefined distribution for unsupported vendor', () => {
      const warnSpy = jest.spyOn(core, 'warning');
      const actual = getVersionFromFileContent(
        'java openjdk-17.0.7',
        'temurin',
        '.tool-versions'
      );
      expect(actual?.version).toBe('17.0.7');
      expect(actual?.distribution).toBeUndefined();
      expect(warnSpy).toHaveBeenCalledWith(
        expect.stringContaining('Unknown asdf distribution identifier')
      );
    });
  });
});

describe('isGhes', () => {
+34 −1
Changes for docs/advanced-usage.md: 34 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -780,7 +780,27 @@ steps:
  
  Supported files are `.java-version`, `.tool-versions` and `.sdkmanrc`.
  * In `.java-version` file, only the version should be specified (e.g., 17.0.7). The `.java-version` file recognizes all variants of the version description according to [jenv](https://github.com/jenv/jenv).
  * In `.tool-versions` file, java version should be preceded by the java keyword (e.g., java 17.0.7). The `.tool-versions` file supports version specifications in accordance with [asdf](https://github.com/asdf-vm/asdf) standards, adhering to Semantic Versioning ([semver](https://semver.org/)).
  * In `.tool-versions` file, java version should be preceded by the java keyword (e.g., java 17.0.7). The `.tool-versions` file supports version specifications in accordance with [asdf](https://github.com/asdf-vm/asdf) standards, adhering to Semantic Versioning ([semver](https://semver.org/)). When the entry includes an [asdf-java](https://github.com/halcyon/asdf-java) vendor prefix (e.g. `java temurin-17.0.3+7`), setup-java can infer the `distribution` input automatically. Unrecognized vendor prefixes require setting `distribution` explicitly.

    Supported asdf-java vendor prefix mappings (packaging variants such as `-jre`, `-musl`, `-openj9`, `-crac`, `-javafx` are collapsed onto the base vendor):

    | asdf-java vendor prefix | setup-java distribution |
    | ----------------------- | ----------------------- |
    | `temurin` | `temurin` |
    | `adoptopenjdk` | `temurin` |
    | `zulu` | `zulu` |
    | `corretto` | `corretto` |
    | `liberica` | `liberica` |
    | `microsoft` | `microsoft` |
    | `semeru`, `ibm` | `semeru` |
    | `dragonwell` | `dragonwell` |
    | `graalvm`, `oracle-graalvm` | `graalvm` |
    | `graalvm-community` | `graalvm-community` |
    | `oracle` | `oracle` |
    | `sapmachine` | `sapmachine` |
    | `kona` | `kona` |
    | `jetbrains` | `jetbrains` |

  * In `.sdkmanrc` file, java version should be preceded by the `java=` prefix (e.g., `java=17.0.7-tem`). When a recognized SDKMAN distribution suffix is present, setup-java can infer the `distribution` input automatically. Unrecognized suffixes require setting `distribution` explicitly. The `.sdkmanrc` file supports version specifications in accordance with [file format](https://sdkman.io/usage#env-command), see [Sdkman! documentation](https://sdkman.io/jdks) for more information.

    Supported SDKMAN suffix mappings:
@@ -816,6 +836,19 @@ steps:
java=17.0.7-tem
```

**Example step using `asdf`** (distribution inferred from `.tool-versions`):
```yml
  - name: Setup java
    uses: actions/setup-java@v5
    with:
      java-version-file: '.tool-versions'
```

**Example `.tool-versions`**:
```
java temurin-17.0.7+7
```

Valid entry options (does not apply to `.sdkmanrc`):
```
major versions: 8, 11, 16, 17, 21
+57 −1
Changes for src/util.ts: 57 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -146,8 +146,10 @@ export function getVersionFromFileContent(

  const versionFileName = getFileName(versionFile);
  if (versionFileName == '.tool-versions') {
    // Capture an optional asdf-java vendor prefix (e.g. `temurin-`, `corretto-`)
    // in the `distribution` group so it can be mapped to a setup-java distribution.
    javaVersionRegExp =
      /^java\s+(?:\S*-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
      /^java\s+(?:(?<distribution>\S*)-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
  } else if (versionFileName == '.sdkmanrc') {
    // Match both version and optional distribution identifier
    javaVersionRegExp =
@@ -170,6 +172,17 @@ export function getVersionFromFileContent(
    );
  }

  // Extract distribution from asdf .tool-versions file
  if (versionFileName == '.tool-versions' && match?.groups?.distribution) {
    const asdfDist = match.groups.distribution;
    extractedDistribution = mapAsdfDistribution(asdfDist);
    if (extractedDistribution) {
      core.debug(
        `Parsed distribution '${extractedDistribution}' from asdf identifier '${asdfDist}'`
      );
    }
  }

  core.debug(
    `Parsed version '${capturedVersion}' from file '${versionFileName}'`
  );
@@ -235,6 +248,49 @@ function mapSdkmanDistribution(sdkmanDist: string): string | undefined {
  return mapped;
}

// Map asdf-java (.tool-versions) vendor identifiers to setup-java distribution names.
// asdf-java encodes the vendor as a prefix on the version string, e.g.
// `java temurin-17.0.3+7` or `java semeru-openj9-11.0.25+9`. Packaging variants
// (`-jre`, `-musl`, `-openj9`, `-crac`, `-javafx`, ...) are collapsed onto the
// base vendor since setup-java does not distinguish them here.
function mapAsdfDistribution(asdfDist: string): string | undefined {
  const normalized = asdfDist.toLowerCase();

  // Multi-segment vendors that map to a distinct setup-java distribution.
  if (normalized.startsWith('graalvm-community')) {
    return 'graalvm-community';
  }
  if (normalized.startsWith('oracle-graalvm')) {
    return 'graalvm';
  }

  const baseVendor = normalized.split('-')[0];
  const distributionMap: Record<string, string> = {
    temurin: 'temurin',
    adoptopenjdk: 'temurin',
    zulu: 'zulu',
    corretto: 'corretto',
    liberica: 'liberica',
    microsoft: 'microsoft',
    semeru: 'semeru',
    ibm: 'semeru',
    dragonwell: 'dragonwell',
    graalvm: 'graalvm',
    oracle: 'oracle',
    sapmachine: 'sapmachine',
    kona: 'kona',
    jetbrains: 'jetbrains'
  };

  const mapped = distributionMap[baseVendor];
  if (!mapped) {
    core.warning(
      `Unknown asdf distribution identifier '${asdfDist}'. Please specify the distribution explicitly.`
    );
  }
  return mapped;
}

// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content: string): string {
  return content.startsWith('1.') ? content.substring(2) : content;