From afc6e496e000967d0d5b6859621e5022e905d2df Mon Sep 17 00:00:00 2001 From: Daniel Beck <1831569+daniel-beck@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:50:30 +0100 Subject: [PATCH] Make it easier to use with custom repositories (#687) Co-authored-by: Daniel Beck --- .../update_center/ArtifactoryRepositoryImpl.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/jenkins/update_center/ArtifactoryRepositoryImpl.java b/src/main/java/io/jenkins/update_center/ArtifactoryRepositoryImpl.java index e8bf057ab..f473cfb66 100644 --- a/src/main/java/io/jenkins/update_center/ArtifactoryRepositoryImpl.java +++ b/src/main/java/io/jenkins/update_center/ArtifactoryRepositoryImpl.java @@ -46,14 +46,16 @@ public class ArtifactoryRepositoryImpl extends BaseMavenRepository { private static final Logger LOGGER = Logger.getLogger(ArtifactoryRepositoryImpl.class.getName()); - private static final String ARTIFACTORY_URL = "https://repo.jenkins-ci.org/"; - private static final String ARTIFACTORY_API_URL = "https://repo.jenkins-ci.org/api/"; + private static final String ARTIFACTORY_URL = Environment.getString("ARTIFACTORY_URL", "https://repo.jenkins-ci.org/"); + private static final String ARTIFACTORY_API_URL = Environment.getString("ARTIFACTORY_API_URL", "https://repo.jenkins-ci.org/api/"); + private static final String ARTIFACTORY_REPOSITORY = Environment.getString("ARTIFACTORY_REPOSITORY", "releases"); + private static final String ARTIFACTORY_AQL_URL = ARTIFACTORY_API_URL + "search/aql"; private static final String ARTIFACTORY_MANIFEST_URL = ARTIFACTORY_URL + "%s/%s!/META-INF/MANIFEST.MF"; private static final String ARTIFACTORY_ZIP_ENTRY_URL = ARTIFACTORY_URL + "%s/%s!%s"; private static final String ARTIFACTORY_FILE_URL = ARTIFACTORY_URL + "%s/%s"; - private static final String AQL_QUERY = "items.find({\"repo\":{\"$eq\":\"releases\"},\"$or\":[{\"name\":{\"$match\":\"*.hpi\"}},{\"name\":{\"$match\":\"*.jpi\"}},{\"name\":{\"$match\":\"*.war\"}},{\"name\":{\"$match\":\"*.pom\"}}]}).include(\"repo\", \"path\", \"name\", \"modified\", \"created\", \"sha256\", \"actual_sha1\", \"size\")"; + private static final String AQL_QUERY = "items.find({\"repo\":{\"$eq\":\"" + ARTIFACTORY_REPOSITORY + "\"},\"$or\":[{\"name\":{\"$match\":\"*.hpi\"}},{\"name\":{\"$match\":\"*.jpi\"}},{\"name\":{\"$match\":\"*.war\"}},{\"name\":{\"$match\":\"*.pom\"}}]}).include(\"repo\", \"path\", \"name\", \"modified\", \"created\", \"sha256\", \"actual_sha1\", \"size\")"; private final String username; private final String password; @@ -236,7 +238,7 @@ private String getUri(ArtifactCoordinates a) { @Override public Manifest getManifest(MavenArtifact artifact) throws IOException { - try (InputStream is = getFileContent(String.format(ARTIFACTORY_MANIFEST_URL, "releases", getUri(artifact.artifact)))) { + try (InputStream is = getFileContent(String.format(ARTIFACTORY_MANIFEST_URL, ARTIFACTORY_REPOSITORY, getUri(artifact.artifact)))) { return new Manifest(is); } } @@ -274,7 +276,7 @@ private File getFile(final String url) throws IOException { try { OkHttpClient.Builder builder = new OkHttpClient.Builder(); OkHttpClient client = builder.build(); - Request request = new Request.Builder().url(url).get().build(); + Request request = new Request.Builder().addHeader("Authorization", Credentials.basic(username, password)).url(url).get().build(); final Response response = client.newCall(request).execute(); if (response.isSuccessful()) { try (final ResponseBody body = HttpHelper.body(response)) { @@ -313,7 +315,7 @@ private File getFile(final String url) throws IOException { @Override public InputStream getZipFileEntry(MavenArtifact artifact, String path) throws IOException { - return getFileContent(String.format(ARTIFACTORY_ZIP_ENTRY_URL, "releases", getUri(artifact.artifact), StringUtils.prependIfMissing(path, "/"))); + return getFileContent(String.format(ARTIFACTORY_ZIP_ENTRY_URL, ARTIFACTORY_REPOSITORY, getUri(artifact.artifact), StringUtils.prependIfMissing(path, "/"))); } @Override @@ -324,7 +326,7 @@ public File resolve(ArtifactCoordinates artifact) throws IOException { if (localFile.exists()) { return localFile; } - return getFile(String.format(ARTIFACTORY_FILE_URL, "releases", uri)); + return getFile(String.format(ARTIFACTORY_FILE_URL, ARTIFACTORY_REPOSITORY, uri)); } private static final File LOCAL_REPO = new File(new File(System.getProperty("user.home")), ".m2/repository");