Unverified Commit 5f75b272 authored by Bruno Borges's avatar Bruno Borges Committed by GitHub
Browse files

Add Maven dependency-resolution repositories (#1240)

* Add Maven dependency repositories

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

Copilot-Session: 8897ad63-2d05-4a6d-8ccd-c1155348b59e

* Potential fix for pull request finding

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

---------

Co-authored-by: default avatarCopilot Autofix powered by AI <[email protected]>
Copilot-Session: 8897ad63-2d05-4a6d-8ccd-c1155348b59e
parent a42a52cf
Loading
Loading
Loading
Loading
+8
Changes for README.md: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -171,6 +171,9 @@ steps:
| `server-username-env-var` | Environment variable name for Maven repository username. | `GITHUB_ACTOR` |
| `server-password-env-var` | Environment variable name for Maven repository password or token. | `GITHUB_TOKEN` |
| `mvn-server-credentials` | Multiline Maven server credentials in the format `server-id:USERNAME_ENV:PASSWORD_ENV`. Replaces the single server configured by the three inputs above when set. | |
| `mvn-repositories` | Multiline Maven dependency repositories in the format `repository-id:repository-url:snapshots-enabled`. | |
| `mvn-repositories-include-central` | Include Maven Central in the generated dependency repositories profile. When `false`, Central is disabled unless an explicit `central` repository is declared. | `true` |
| `mvn-repositories-prioritize-central` | Place Maven Central before custom dependency repositories. Has no effect when Maven Central is excluded. | `true` |
| `settings-path` | Directory where `settings.xml` is written. | `~/.m2` |
| `overwrite-settings` | Overwrite an existing `settings.xml`. | `true` |
| `gpg-private-key` | GPG private key to import into an isolated temporary keyring. | |
@@ -453,6 +456,11 @@ steps:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

For dependencies hosted outside Maven Central, use `mvn-repositories` to add
resolution repositories to an active profile in the generated `settings.xml`.
Repository IDs can match `mvn-server-credentials` IDs when authentication is
required. See [Resolving Maven dependencies from custom repositories](docs/advanced-usage.md#resolving-maven-dependencies-from-custom-repositories).

### GPG signing

```yaml
+312
Changes for __tests__/auth.test.ts: 312 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -60,6 +60,15 @@ const settingsFile = path.join(m2Dir, MVN_SETTINGS_FILE);
const credentials = (id: string, username: string, password: string) => [
  {id, usernameEnvVar: username, passwordEnvVar: password}
];
const repositorySettings = (
  repositories: auth.MavenRepository[],
  includeCentral = true,
  prioritizeCentral = true
): auth.MavenRepositorySettings => ({
  repositories,
  includeCentral,
  prioritizeCentral
});

describe('auth tests', () => {
  let spyOSHomedir: any;
@@ -383,6 +392,179 @@ describe('auth tests', () => {
    ]);
  });

  it('generates an active profile with Central before custom repositories by default', () => {
    const parsed = parseXmlObject(
      auth.generate(
        credentials('packages', 'USERNAME', 'PASSWORD'),
        undefined,
        repositorySettings([
          {
            id: 'private',
            url: 'https://repo.example.com/maven',
            snapshotsEnabled: true
          }
        ])
      )
    ) as any;

    expect(parsed.settings.profiles.profile).toEqual({
      id: 'setup-java-repositories',
      repositories: {
        repository: [
          {
            id: 'central',
            url: 'https://repo.maven.apache.org/maven2',
            snapshots: {enabled: 'false'}
          },
          {
            id: 'private',
            url: 'https://repo.example.com/maven',
            snapshots: {enabled: 'true'}
          }
        ]
      }
    });
    expect(parsed.settings.activeProfiles.activeProfile).toBe(
      'setup-java-repositories'
    );
  });

  it('places custom repositories before Central when configured', () => {
    const parsed = parseXmlObject(
      auth.generate(
        credentials('packages', 'USERNAME', 'PASSWORD'),
        undefined,
        repositorySettings(
          [
            {
              id: 'first',
              url: 'https://first.example.com',
              snapshotsEnabled: false
            },
            {
              id: 'second',
              url: 'https://second.example.com',
              snapshotsEnabled: true
            }
          ],
          true,
          false
        )
      )
    ) as any;

    expect(
      parsed.settings.profiles.profile.repositories.repository.map(
        (repository: {id: string}) => repository.id
      )
    ).toEqual(['first', 'second', 'central']);
  });

  it('excludes Central from the generated repository profile when configured', () => {
    const parsed = parseXmlObject(
      auth.generate(
        credentials('packages', 'USERNAME', 'PASSWORD'),
        undefined,
        repositorySettings(
          [
            {
              id: 'private',
              url: 'https://repo.example.com',
              snapshotsEnabled: false
            }
          ],
          false
        )
      )
    ) as any;

    expect(parsed.settings.profiles.profile.repositories.repository).toEqual([
      {
        id: 'private',
        url: 'https://repo.example.com',
        snapshots: {enabled: 'false'}
      },
      {
        id: 'central',
        url: 'https://repo.maven.apache.org/maven2',
        releases: {enabled: 'false'},
        snapshots: {enabled: 'false'}
      }
    ]);
  });

  it('combines repository and GPG configuration in shared profile blocks', () => {
    const parsed = parseXmlObject(
      auth.generate(
        credentials('packages', 'USERNAME', 'PASSWORD'),
        'GPG_PASSPHRASE',
        repositorySettings(
          [
            {
              id: 'private',
              url: 'https://repo.example.com',
              snapshotsEnabled: false
            }
          ],
          false
        )
      )
    ) as any;

    expect(parsed.settings.profiles.profile).toEqual([
      {
        id: 'setup-java-repositories',
        repositories: {
          repository: [
            {
              id: 'private',
              url: 'https://repo.example.com',
              snapshots: {enabled: 'false'}
            },
            {
              id: 'central',
              url: 'https://repo.maven.apache.org/maven2',
              releases: {enabled: 'false'},
              snapshots: {enabled: 'false'}
            }
          ]
        }
      },
      {
        id: 'setup-java-gpg',
        properties: {['gpg.passphraseEnvName']: 'GPG_PASSPHRASE'}
      }
    ]);
    expect(parsed.settings.activeProfiles.activeProfile).toEqual([
      'setup-java-repositories',
      'setup-java-gpg'
    ]);
  });

  it('escapes repository values while preserving parsed semantics', () => {
    const repository = {
      id: `private&<>"'é`,
      url: `https://repo.example.com/a?x=1&y=<value>"'é`,
      snapshotsEnabled: true
    };
    const xml = auth.generate(
      credentials('packages', 'USERNAME', 'PASSWORD'),
      undefined,
      repositorySettings([repository], false)
    );
    const parsed = parseXmlObject(xml) as any;

    expect(
      parsed.settings.profiles.profile.repositories.repository.find(
        (entry: {id: string}) => entry.id === repository.id
      )
    ).toEqual({
      id: repository.id,
      url: repository.url,
      snapshots: {enabled: 'true'}
    });
  });

  it('parses and trims multiline Maven server credentials', () => {
    expect(
      auth.parseMavenServerCredentials([
@@ -404,6 +586,136 @@ describe('auth tests', () => {
    ]);
  });

  it('parses Maven repositories while preserving colons in URLs', () => {
    expect(
      auth.parseMavenRepositories(
        [
          '',
          ' private : https://repo.example.com:8443/maven : true ',
          'releases:https://repo.example.com/releases:false'
        ],
        true
      )
    ).toEqual([
      {
        id: 'private',
        url: 'https://repo.example.com:8443/maven',
        snapshotsEnabled: true
      },
      {
        id: 'releases',
        url: 'https://repo.example.com/releases',
        snapshotsEnabled: false
      }
    ]);
  });

  it.each([
    {
      entries: ['private'],
      error:
        'Invalid mvn-repositories entry at line 1. Expected format: repository-id:repository-url:snapshots-enabled'
    },
    {
      entries: ['private:https://repo.example.com'],
      error:
        "Invalid snapshots-enabled value '//repo.example.com' in mvn-repositories entry at line 1. Expected true or false"
    },
    {
      entries: ['private::true'],
      error:
        'Invalid mvn-repositories entry at line 1. repository-id, repository URL, and snapshots-enabled are required'
    },
    {
      entries: ['private:https://repo.example.com:sometimes'],
      error:
        "Invalid snapshots-enabled value 'sometimes' in mvn-repositories entry at line 1. Expected true or false"
    }
  ])('rejects malformed Maven repositories: $entries', ({entries, error}) => {
    expect(() => auth.parseMavenRepositories(entries, true)).toThrow(error);
  });

  it('rejects duplicate Maven repository ids', () => {
    expect(() =>
      auth.parseMavenRepositories(
        [
          'private:https://first.example.com:false',
          'private:https://second.example.com:true'
        ],
        true
      )
    ).toThrow("Duplicate repository-id 'private' in mvn-repositories input");
  });

  it('reserves the Central repository id only when automatic Central inclusion is enabled', () => {
    const central = 'central:https://custom.example.com/maven:false';

    expect(() => auth.parseMavenRepositories([central], true)).toThrow(
      "Repository-id 'central' is reserved when mvn-repositories-include-central is enabled"
    );
    expect(auth.parseMavenRepositories([central], false)).toEqual([
      {
        id: 'central',
        url: 'https://custom.example.com/maven',
        snapshotsEnabled: false
      }
    ]);
  });

  it('uses an explicit Central repository instead of adding a disabled override', () => {
    const parsed = parseXmlObject(
      auth.generate(
        credentials('packages', 'USERNAME', 'PASSWORD'),
        undefined,
        repositorySettings(
          auth.parseMavenRepositories(
            ['central:https://mirror.example.com/maven:true'],
            false
          ),
          false
        )
      )
    ) as any;

    expect(parsed.settings.profiles.profile.repositories.repository).toEqual({
      id: 'central',
      url: 'https://mirror.example.com/maven',
      snapshots: {enabled: 'true'}
    });
  });

  it('reads Maven repository settings and Central controls', () => {
    (core.getMultilineInput as jest.Mock).mockImplementation((name: string) =>
      name === 'mvn-repositories'
        ? ['private:https://repo.example.com:true']
        : []
    );
    (core.getInput as jest.Mock).mockImplementation((name: string) => {
      const inputs: Record<string, string> = {
        'mvn-repositories-include-central': 'false',
        'mvn-repositories-prioritize-central': 'false'
      };
      return inputs[name] ?? '';
    });

    expect(auth.getMavenRepositorySettings()).toEqual({
      repositories: [
        {
          id: 'private',
          url: 'https://repo.example.com',
          snapshotsEnabled: true
        }
      ],
      includeCentral: false,
      prioritizeCentral: false
    });
  });

  it('does not read repository controls when no repositories are configured', () => {
    expect(auth.getMavenRepositorySettings()).toBeUndefined();
    expect(core.getInput).not.toHaveBeenCalled();
  });

  it.each([
    {
      entries: ['releases:RELEASES_USERNAME'],
+11
Changes for action.yml: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -67,6 +67,17 @@ inputs:
  mvn-server-credentials:
    description: 'Multiline list of Maven server credentials in the format `server-id:USERNAME_ENV:PASSWORD_ENV`. When set, replaces the single server configured by server-id, server-username-env-var, and server-password-env-var.'
    required: false
  mvn-repositories:
    description: 'Multiline list of Maven dependency repositories in the format `repository-id:repository-url:snapshots-enabled`.'
    required: false
  mvn-repositories-include-central:
    description: 'Include Maven Central in the generated dependency repositories profile. When false, Central is disabled unless an explicit repository with ID `central` is declared.'
    required: false
    default: true
  mvn-repositories-prioritize-central:
    description: 'Place Maven Central before custom dependency repositories. Has no effect when Maven Central is excluded.'
    required: false
    default: true
  settings-path:
    description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
    required: false
+7 −1
Changes for dist/cleanup/index.js: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -30774,7 +30774,7 @@ module.exports = {
/* harmony export */   gk: () => (/* binding */ INPUT_CACHE),
/* harmony export */   wG: () => (/* binding */ INPUT_JOB_STATUS)
/* harmony export */ });
/* unused harmony exports MACOS_JAVA_CONTENT_POSTFIX, INPUT_JAVA_VERSION, INPUT_JAVA_VERSION_FILE, INPUT_ARCHITECTURE, INPUT_JAVA_PACKAGE, INPUT_DISTRIBUTION, INPUT_JDK_FILE, INPUT_JDK_FILE_DEPRECATED, INPUT_CHECK_LATEST, INPUT_FORCE_DOWNLOAD, INPUT_SET_DEFAULT, INPUT_PROBLEM_MATCHER, INPUT_VERIFY_SIGNATURE, INPUT_VERIFY_SIGNATURE_PUBLIC_KEY, INPUT_MVN_SERVER_CREDENTIALS, INPUT_SERVER_ID, INPUT_SERVER_USERNAME_ENV_VAR, INPUT_SERVER_PASSWORD_ENV_VAR, INPUT_SERVER_USERNAME_DEPRECATED, INPUT_SERVER_PASSWORD_DEPRECATED, INPUT_SETTINGS_PATH, INPUT_OVERWRITE_SETTINGS, INPUT_GPG_PRIVATE_KEY, INPUT_GPG_PASSPHRASE_ENV_VAR, INPUT_GPG_PASSPHRASE_DEPRECATED, INPUT_DEFAULT_SERVER_USERNAME, INPUT_DEFAULT_SERVER_PASSWORD, INPUT_DEFAULT_GPG_PRIVATE_KEY, INPUT_DEFAULT_GPG_PASSPHRASE, MAVEN_GPG_PASSPHRASE_DEFAULT_ENV, GPG_PASSPHRASE_PROFILE_ID, INPUT_CACHE_DEPENDENCY_PATH, INPUT_CACHE_PATH, M2_DIR, MVN_SETTINGS_FILE, MVN_TOOLCHAINS_FILE, INPUT_MVN_TOOLCHAIN_ID, INPUT_MVN_TOOLCHAIN_VENDOR, INPUT_SHOW_DOWNLOAD_PROGRESS, MAVEN_ARGS_ENV, MAVEN_NO_TRANSFER_PROGRESS_FLAG, MAVEN_NO_TRANSFER_PROGRESS_LONG_FLAG, DISTRIBUTIONS_ONLY_MAJOR_VERSION */
/* unused harmony exports MACOS_JAVA_CONTENT_POSTFIX, INPUT_JAVA_VERSION, INPUT_JAVA_VERSION_FILE, INPUT_ARCHITECTURE, INPUT_JAVA_PACKAGE, INPUT_DISTRIBUTION, INPUT_JDK_FILE, INPUT_JDK_FILE_DEPRECATED, INPUT_CHECK_LATEST, INPUT_FORCE_DOWNLOAD, INPUT_SET_DEFAULT, INPUT_PROBLEM_MATCHER, INPUT_VERIFY_SIGNATURE, INPUT_VERIFY_SIGNATURE_PUBLIC_KEY, INPUT_MVN_SERVER_CREDENTIALS, INPUT_MVN_REPOSITORIES, INPUT_MVN_REPOSITORIES_INCLUDE_CENTRAL, INPUT_MVN_REPOSITORIES_PRIORITIZE_CENTRAL, INPUT_SERVER_ID, INPUT_SERVER_USERNAME_ENV_VAR, INPUT_SERVER_PASSWORD_ENV_VAR, INPUT_SERVER_USERNAME_DEPRECATED, INPUT_SERVER_PASSWORD_DEPRECATED, INPUT_SETTINGS_PATH, INPUT_OVERWRITE_SETTINGS, INPUT_GPG_PRIVATE_KEY, INPUT_GPG_PASSPHRASE_ENV_VAR, INPUT_GPG_PASSPHRASE_DEPRECATED, INPUT_DEFAULT_SERVER_USERNAME, INPUT_DEFAULT_SERVER_PASSWORD, INPUT_DEFAULT_GPG_PRIVATE_KEY, INPUT_DEFAULT_GPG_PASSPHRASE, MAVEN_GPG_PASSPHRASE_DEFAULT_ENV, GPG_PASSPHRASE_PROFILE_ID, MAVEN_REPOSITORIES_PROFILE_ID, MAVEN_CENTRAL_REPOSITORY_ID, MAVEN_CENTRAL_REPOSITORY_URL, INPUT_CACHE_DEPENDENCY_PATH, INPUT_CACHE_PATH, M2_DIR, MVN_SETTINGS_FILE, MVN_TOOLCHAINS_FILE, INPUT_MVN_TOOLCHAIN_ID, INPUT_MVN_TOOLCHAIN_VENDOR, INPUT_SHOW_DOWNLOAD_PROGRESS, MAVEN_ARGS_ENV, MAVEN_NO_TRANSFER_PROGRESS_FLAG, MAVEN_NO_TRANSFER_PROGRESS_LONG_FLAG, DISTRIBUTIONS_ONLY_MAJOR_VERSION */
const MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home';
const INPUT_JAVA_VERSION = 'java-version';
const INPUT_JAVA_VERSION_FILE = 'java-version-file';
@@ -30790,6 +30790,9 @@ const INPUT_PROBLEM_MATCHER = 'problem-matcher';
const INPUT_VERIFY_SIGNATURE = 'verify-signature';
const INPUT_VERIFY_SIGNATURE_PUBLIC_KEY = 'verify-signature-public-key';
const INPUT_MVN_SERVER_CREDENTIALS = 'mvn-server-credentials';
const INPUT_MVN_REPOSITORIES = 'mvn-repositories';
const INPUT_MVN_REPOSITORIES_INCLUDE_CENTRAL = 'mvn-repositories-include-central';
const INPUT_MVN_REPOSITORIES_PRIORITIZE_CENTRAL = 'mvn-repositories-prioritize-central';
const INPUT_SERVER_ID = 'server-id';
const INPUT_SERVER_USERNAME_ENV_VAR = 'server-username-env-var';
const INPUT_SERVER_PASSWORD_ENV_VAR = 'server-password-env-var';
@@ -30810,6 +30813,9 @@ const INPUT_DEFAULT_GPG_PASSPHRASE = 'GPG_PASSPHRASE';
const MAVEN_GPG_PASSPHRASE_DEFAULT_ENV = 'MAVEN_GPG_PASSPHRASE';
// Id of the settings.xml profile used to set `gpg.passphraseEnvName`.
const GPG_PASSPHRASE_PROFILE_ID = 'setup-java-gpg';
const MAVEN_REPOSITORIES_PROFILE_ID = 'setup-java-repositories';
const MAVEN_CENTRAL_REPOSITORY_ID = 'central';
const MAVEN_CENTRAL_REPOSITORY_URL = 'https://repo.maven.apache.org/maven2';
const INPUT_CACHE = 'cache';
const INPUT_CACHE_JDK = 'cache-jdk';
const INPUT_CACHE_DEPENDENCY_PATH = 'cache-dependency-path';
+101 −5

File changed.

Preview size limit exceeded, changes collapsed.

Loading