Skip to content

Commit

Permalink
[upload] rework artifactory with multiple repository support. Resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Dec 9, 2021
1 parent 5d8eb92 commit ecbf391
Show file tree
Hide file tree
Showing 23 changed files with 694 additions and 85 deletions.
Expand Up @@ -17,6 +17,8 @@
*/
package org.jreleaser.model;

import org.jreleaser.util.FileType;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -60,7 +62,7 @@ public boolean isFailed() {
@Override
public Set<String> getSupportedExtensions() {
Set<String> set = new LinkedHashSet<>();
set.add(".zip");
set.add(FileType.ZIP.extension());
return set;
}

Expand Down
Expand Up @@ -17,13 +17,13 @@
*/
package org.jreleaser.model;

import org.jreleaser.util.FileType;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.jreleaser.util.Constants.KEY_ARTIFACT_ARCHIVE_FORMAT;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_FILE_NAME;
Expand Down Expand Up @@ -255,27 +255,11 @@ public Map<String, Object> artifactProps(JReleaserContext context, Artifact arti
.filter(k -> !k.startsWith("artifactSkip"))
.forEach(k -> props.put(k, artifactProps.get(k)));
String artifactFileName = artifact.getEffectivePath(context).getFileName().toString();
String filename = getFilename(artifactFileName, getSupportedExtensions());
String filename = getFilename(artifactFileName, FileType.getSupportedExtensions());
props.put(KEY_ARTIFACT_PLATFORM, platform);
props.put(KEY_ARTIFACT_FILE_NAME, artifactFileName);
props.put(KEY_ARTIFACT_NAME, filename);
props.put(KEY_ARTIFACT_ARCHIVE_FORMAT, artifactFileName.substring(filename.length()) + 1);
return props;
}

private Set<String> getSupportedExtensions() {
Set<String> set = new LinkedHashSet<>();
set.add(".tar.bz2");
set.add(".tar.gz");
set.add(".tgz");
set.add(".tar");
set.add(".zip");
set.add(".rpm");
set.add(".dep");
set.add(".pkg");
set.add(".dmg");
set.add(".exe");
set.add(".msi");
return set;
}
}
Expand Up @@ -17,10 +17,16 @@
*/
package org.jreleaser.model;

import org.jreleaser.model.util.Templates;
import org.jreleaser.util.Env;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.util.stream.Collectors.toList;
import static org.jreleaser.util.Constants.HIDE;
import static org.jreleaser.util.Constants.UNSET;
import static org.jreleaser.util.StringUtils.isNotBlank;
Expand All @@ -29,9 +35,12 @@
* @author Andres Almiray
* @since 0.3.0
*/
public class Artifactory extends AbstractHttpUploader {
public class Artifactory extends AbstractUploader {
public static final String TYPE = "artifactory";

private final List<ArtifactoryRepository> repositories = new ArrayList<>();

private String host;
private String username;
private String password;
private HttpUploader.Authorization authorization;
Expand All @@ -42,19 +51,25 @@ public Artifactory() {

void setAll(Artifactory artifactory) {
super.setAll(artifactory);
this.host = artifactory.host;
this.username = artifactory.username;
this.password = artifactory.password;
this.authorization = artifactory.authorization;
setRepositories(artifactory.repositories);
}

public Authorization resolveAuthorization() {
public HttpUploader.Authorization resolveAuthorization() {
if (null == authorization) {
authorization = Authorization.BEARER;
authorization = HttpUploader.Authorization.BEARER;
}

return authorization;
}

public String getResolvedHost() {
return Env.resolve("ARTIFACTORY_" + Env.toVar(name) + "_HOST", host);
}

public String getResolvedUsername() {
return Env.resolve("ARTIFACTORY_" + Env.toVar(name) + "_USERNAME", username);
}
Expand All @@ -63,6 +78,14 @@ public String getResolvedPassword() {
return Env.resolve("ARTIFACTORY_" + Env.toVar(name) + "_PASSWORD", password);
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getUsername() {
return username;
}
Expand All @@ -79,34 +102,69 @@ public void setPassword(String password) {
this.password = password;
}

@Deprecated
public String getTarget() {
System.out.println("artifactory.target has been deprecated since 0.6.0 and will be removed in the future. Use artifactory.uploadUrl instead");
return getUploadUrl();
public HttpUploader.Authorization getAuthorization() {
return authorization;
}

@Deprecated
public void setTarget(String target) {
System.out.println("artifactory.target has been deprecated since 0.6.0 and will be removed in the future. Use artifactory.uploadUrl instead");
setUploadUrl(target);
public void setAuthorization(HttpUploader.Authorization authorization) {
this.authorization = authorization;
}

public Authorization getAuthorization() {
return authorization;
public void setAuthorization(String authorization) {
this.authorization = HttpUploader.Authorization.of(authorization);
}

public void setAuthorization(Authorization authorization) {
this.authorization = authorization;
public List<ArtifactoryRepository> getRepositories() {
return repositories;
}

public void setAuthorization(String authorization) {
this.authorization = Authorization.of(authorization);
public void setRepositories(List<ArtifactoryRepository> repositories) {
this.repositories.clear();
this.repositories.addAll(repositories);
}

public void addRepository(ArtifactoryRepository repository) {
if (null != repository) {
this.repositories.add(repository);
}
}

@Override
protected void asMap(Map<String, Object> props, boolean full) {
props.put("authorization", authorization);
props.put("host", getResolvedHost());
props.put("username", isNotBlank(getResolvedUsername()) ? HIDE : UNSET);
props.put("password", isNotBlank(getResolvedPassword()) ? HIDE : UNSET);
List<Map<String, Object>> repositories = this.repositories.stream()
.filter(d -> full || d.isEnabled())
.map(d -> d.asMap(full))
.collect(toList());
if (!repositories.isEmpty()) props.put("repositories", repositories);
}

@Override
public String getResolvedDownloadUrl(JReleaserContext context, Artifact artifact) {
return resolveUrl(context, artifact);
}

public String getResolvedUploadUrl(JReleaserContext context, Artifact artifact) {
return resolveUrl(context, artifact);
}

private String resolveUrl(JReleaserContext context, Artifact artifact) {
Map<String, Object> p = new LinkedHashMap<>(artifactProps(context, artifact));
p.put("artifactoryHost", host);

Optional<ArtifactoryRepository> repository = repositories.stream()
.filter(r -> r.handles(artifact))
.findFirst();

if (repository.isPresent()) {
p.put("repositoryPath", repository.get().getPath());
String url = "{{artifactoryHost}}/{{repositoryPath}}";
return Templates.resolve(url, p);
}

return "";
}
}
@@ -0,0 +1,128 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2021 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.model;

import org.jreleaser.util.FileType;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import static org.jreleaser.util.StringUtils.getFilename;

/**
* @author Andres Almiray
* @since 0.10.0
*/
public class ArtifactoryRepository implements Domain, Activatable {
private final Set<FileType> fileTypes = new LinkedHashSet<>();

private Active active;
private boolean enabled;
private String path;

void setAll(ArtifactoryRepository repository) {
this.active = repository.active;
this.enabled = repository.enabled;
this.path = repository.path;
setFileTypes(repository.fileTypes);
}

@Override
public boolean isEnabled() {
return enabled;
}

@Override
public void disable() {
active = Active.NEVER;
enabled = false;
}

public boolean resolveEnabled(Project project) {
if (null == active) {
active = Active.RELEASE;
}
enabled = active.check(project);
return enabled;
}

@Override
public Active getActive() {
return active;
}

@Override
public void setActive(Active active) {
this.active = active;
}

@Override
public void setActive(String str) {
this.active = Active.of(str);
}

@Override
public boolean isActiveSet() {
return active != null;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public Set<FileType> getFileTypes() {
return fileTypes;
}

public void setFileTypes(Set<FileType> fileTypes) {
this.fileTypes.clear();
this.fileTypes.addAll(fileTypes);
}

public void addFileType(FileType fileType) {
this.fileTypes.add(fileType);
}

@Override
public Map<String, Object> asMap(boolean full) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("enabled", isEnabled());
map.put("active", active);
map.put("path", path);
map.put("fileTypes", fileTypes);
return map;
}

public boolean handles(Artifact artifact) {
if (!enabled) return false;
if (fileTypes.isEmpty()) return true;

String artifactFileName = artifact.getResolvedPath().getFileName().toString();
String artifactName = getFilename(artifactFileName, FileType.getSupportedExtensions());
String archiveFormat = artifactFileName.substring(artifactName.length() + 1);
FileType fileType = FileType.of(archiveFormat);

return fileTypes.contains(fileType);
}
}
13 changes: 7 additions & 6 deletions core/jreleaser-model/src/main/java/org/jreleaser/model/Brew.java
Expand Up @@ -17,6 +17,7 @@
*/
package org.jreleaser.model;

import org.jreleaser.util.FileType;
import org.jreleaser.util.PlatformUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -198,12 +199,12 @@ public boolean supportsPlatform(String platform) {

@Override
public Set<String> getSupportedExtensions() {
Set<String> extensions = new LinkedHashSet<>();
extensions.add(".dmg");
extensions.add(".pkg");
extensions.add(".zip");
extensions.add(".jar");
return extensions;
Set<String> set = new LinkedHashSet<>();
set.add(FileType.DMG.extension());
set.add(FileType.PKG.extension());
set.add(FileType.ZIP.extension());
set.add(FileType.JAR.extension());
return set;
}

public static class Dependency {
Expand Down
Expand Up @@ -17,6 +17,7 @@
*/
package org.jreleaser.model;

import org.jreleaser.util.FileType;
import org.jreleaser.util.PlatformUtils;

import java.util.Collections;
Expand Down Expand Up @@ -107,10 +108,10 @@ public void setCommitAuthor(CommitAuthor commitAuthor) {

@Override
public Set<String> getSupportedExtensions() {
Set<String> extensions = new LinkedHashSet<>();
extensions.add(".zip");
extensions.add(".jar");
return extensions;
Set<String> set = new LinkedHashSet<>();
set.add(FileType.JAR.extension());
set.add(FileType.ZIP.extension());
return set;
}

public List<DockerSpec> getActiveSpecs() {
Expand Down

0 comments on commit ecbf391

Please sign in to comment.