Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jfrog platform URL into Jenkins configuration page #455

Merged
merged 6 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ To run unit tests execute the following command:
Before running the integration tests, set the following environment variables.

*JENKINS_ARTIFACTORY_URL*<br>
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
*JENKINS_PLATFORM_URL*<br>
*JENKINS_ARTIFACTORY_USERNAME*<br>
*JENKINS_ARTIFACTORY_PASSWORD*<br>
*JENKINS_ARTIFACTORY_DOCKER_PUSH_DOMAIN* (For example, server-docker-local.jfrog.io)<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public boolean configure(StaplerRequest req, JSONObject json) {
}

/**
* Returns the list of {@link org.jfrog.hudson.ArtifactoryServer} configured.
* Returns the list of {@link org.jfrog.hudson.JfrogServers} configured.
*
* @return can be empty but never null.
*/
public List<ArtifactoryServer> getArtifactoryServers() {
return RepositoriesUtils.getArtifactoryServers();
public List<JfrogServers> getJfrogInstances() {
return RepositoriesUtils.getJfrogInstances();
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -95,7 +95,7 @@ protected RefreshServerResponse refreshResolversFromArtifactory(String url, Stri
String password, boolean overrideCredentials) {
RefreshServerResponse response = new RefreshServerResponse();
CredentialsConfig credentialsConfig = new CredentialsConfig(username, password, credentialsId, overrideCredentials);
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url, RepositoriesUtils.getArtifactoryServers());
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url);
try {
List<VirtualRepository> virtualRepositories = refreshVirtualRepositories(artifactoryServer, credentialsConfig);
response.setVirtualRepositories(virtualRepositories);
Expand Down Expand Up @@ -124,7 +124,7 @@ protected RefreshServerResponse refreshDeployersFromArtifactory(String url, Stri
String password, boolean overrideCredentials, boolean refreshUserPlugins) {
RefreshServerResponse response = new RefreshServerResponse();
CredentialsConfig credentialsConfig = new CredentialsConfig(username, password, credentialsId, overrideCredentials);
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url, RepositoriesUtils.getArtifactoryServers());
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url);
try {
response.setRepositories(refreshRepositories(artifactoryServer, credentialsConfig));
if (refreshUserPlugins) {
Expand Down
82 changes: 54 additions & 28 deletions src/main/java/org/jfrog/hudson/ArtifactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
package org.jfrog.hudson;

import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.google.common.collect.Lists;
import hudson.Extension;
import hudson.model.BuildableItem;
import hudson.model.BuildableItemWithBuildWrappers;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.util.Secret;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import hudson.util.XStream2;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONNull;
Expand All @@ -42,6 +44,7 @@
import org.jfrog.hudson.jfpipelines.JFrogPipelinesServer;
import org.jfrog.hudson.util.Credentials;
import org.jfrog.hudson.util.RepositoriesUtils;
import org.jfrog.hudson.util.converters.ArtifactoryBuilderConverter;
import org.jfrog.hudson.util.plugins.PluginsUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -73,6 +76,11 @@ public class ArtifactoryBuilder extends GlobalConfiguration {
public static final class DescriptorImpl extends Descriptor<GlobalConfiguration> {

private boolean useCredentialsPlugin;
private List<JfrogServers> jfrogInstances;
/**
* @deprecated: Use org.jfrog.hudson.ArtifactoryBuilder.DescriptorImpl#getJfrogInstances()
*/
@Deprecated
private List<ArtifactoryServer> artifactoryServers;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be marked transient as described in the Jenkins developer documentation. That it was not broke JCasC export.

private JFrogPipelinesServer jfrogPipelinesServer = new JFrogPipelinesServer();

Expand Down Expand Up @@ -102,13 +110,13 @@ public FormValidation doCheckServerId(@QueryParameter String value) {
if (value.length() == 0) {
return FormValidation.error("Please set server ID");
}
List<ArtifactoryServer> artifactoryServers = RepositoriesUtils.getArtifactoryServers();
if (artifactoryServers == null) {
List<JfrogServers> jfrogServers = RepositoriesUtils.getJfrogInstances();
if (jfrogServers == null) {
return FormValidation.ok();
}
int countServersByValueAsName = 0;
for (ArtifactoryServer server : artifactoryServers) {
if (server.getServerId().equals(value)) {
for (JfrogServers server : jfrogServers) {
if (server.getId().equals(value)) {
countServersByValueAsName++;
if (countServersByValueAsName > 1) {
return FormValidation.error("Duplicated server ID");
Expand Down Expand Up @@ -250,7 +258,7 @@ public boolean configure(StaplerRequest req, JSONObject o) throws FormException
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins != null && jenkins.hasPermission(Jenkins.ADMINISTER)) {
boolean useCredentialsPlugin = (Boolean) o.get("useCredentialsPlugin");
configureArtifactoryServers(req, o);
configureJfrogServers(req, o);
configureJFrogPipelinesServer(o);
if (useCredentialsPlugin && !this.useCredentialsPlugin) {
resetJobsCredentials();
Expand All @@ -263,21 +271,21 @@ public boolean configure(StaplerRequest req, JSONObject o) throws FormException
throw new FormException("User doesn't have permissions to save", "Server ID");
}

private void configureArtifactoryServers(StaplerRequest req, JSONObject o) throws FormException {
List<ArtifactoryServer> artifactoryServers = null;
Object artifactoryServerObj = o.get("artifactoryServer"); // an array or single object
if (!JSONNull.getInstance().equals(artifactoryServerObj)) {
artifactoryServers = req.bindJSONToList(ArtifactoryServer.class, artifactoryServerObj);
private void configureJfrogServers(StaplerRequest req, JSONObject o) throws FormException {
List<JfrogServers> jfrogInstances = Lists.newArrayList();
Object jfrogInstancesObj = o.get("jfrogInstances"); // an array or single object
if (!JSONNull.getInstance().equals(jfrogInstancesObj)) {
jfrogInstances = req.bindJSONToList(JfrogServers.class, jfrogInstancesObj);
}

if (!isServerIDConfigured(artifactoryServers)) {
throw new FormException("Please set the Artifactory server ID.", "ServerID");
if (!isJfrogServersIDConfigured(jfrogInstances)) {
throw new FormException("Please set the Instance ID.", "InstanceID");
}

if (isServerDuplicated(artifactoryServers)) {
throw new FormException("The Artifactory server ID you have entered is already configured", "Server ID");
if (isInstanceDuplicated(jfrogInstances)) {
throw new FormException("The Jfrog instance ID you have entered is already configured", "Instance ID");
}
setArtifactoryServers(artifactoryServers);
setJfrogInstances(jfrogInstances);
}

private void configureJFrogPipelinesServer(JSONObject o) {
Expand All @@ -291,7 +299,7 @@ private void configureJFrogPipelinesServer(JSONObject o) {
}

private void resetServersCredentials() {
for (ArtifactoryServer server : artifactoryServers) {
for (JfrogServers server : jfrogInstances) {
if (server.getResolverCredentialsConfig() != null) {
server.getResolverCredentialsConfig().deleteCredentials();
}
Expand Down Expand Up @@ -352,32 +360,50 @@ public void setUseCredentialsPlugin(boolean useCredentialsPlugin) {
this.useCredentialsPlugin = useCredentialsPlugin;
}

private boolean isServerDuplicated(List<ArtifactoryServer> artifactoryServers) {
private boolean isInstanceDuplicated(List<JfrogServers> JfrogInstances) {
Set<String> serversNames = new HashSet<>();
if (artifactoryServers == null) {
if (JfrogInstances == null) {
return false;
}
for (ArtifactoryServer server : artifactoryServers) {
String name = server.getServerId();
if (serversNames.contains(name)) {
for (JfrogServers instance : JfrogInstances) {
String id = instance.getId();
if (serversNames.contains(id)) {
return true;
}
serversNames.add(name);
serversNames.add(id);
}
return false;
}

private boolean isServerIDConfigured(List<ArtifactoryServer> artifactoryServers) {
if (artifactoryServers == null) {
private boolean isJfrogServersIDConfigured(List<JfrogServers> JfrogInstances) {
if (JfrogInstances == null) {
return true;
}
for (ArtifactoryServer server : artifactoryServers) {
String name = server.getServerId();
if (StringUtils.isBlank(name)) {
for (JfrogServers server : JfrogInstances) {
String platformId = server.getId();
String artifactoryId = server.getArtifactoryServer().getServerId();
if (StringUtils.isBlank(platformId) || StringUtils.isBlank(artifactoryId)) {
return false;
}
}
return true;
}

public List<JfrogServers> getJfrogInstances() {
return jfrogInstances;
}

public void setJfrogInstances(List<JfrogServers> jfrogInstances) {
this.jfrogInstances = jfrogInstances;
}

/**
* Page Converter
*/
public static final class ConverterImpl extends ArtifactoryBuilderConverter {
public ConverterImpl(XStream2 xstream) {
super(xstream);
}
}
}
}
12 changes: 5 additions & 7 deletions src/main/java/org/jfrog/hudson/ArtifactoryRedeployPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public DescriptorImpl getDescriptor() {
}

public ArtifactoryServer getArtifactoryServer() {
return RepositoriesUtils.getArtifactoryServer(getArtifactoryName(), getDescriptor().getArtifactoryServers());
return RepositoriesUtils.getArtifactoryServer(getArtifactoryName());
}

private Result getTreshold() {
Expand Down Expand Up @@ -525,9 +525,7 @@ public RefreshServerResponse refreshFromArtifactory(String url, String credentia
CredentialsConfig credentialsConfig = new CredentialsConfig(username, password, credentialsId, overrideCredentials);

try {
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(
url, getArtifactoryServers()
);
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url);
List<Repository> releaseRepositories = refreshRepositories(artifactoryServer, credentialsConfig);
List<PluginSettings> userPluginKeys = refreshUserPlugins(artifactoryServer, credentialsConfig);

Expand All @@ -552,12 +550,12 @@ public String getDisplayName() {
}

/**
* Returns the list of {@link ArtifactoryServer} configured.
* Returns the list of {@link JfrogServers} configured.
*
* @return can be empty but never null.
*/
public List<ArtifactoryServer> getArtifactoryServers() {
return RepositoriesUtils.getArtifactoryServers();
public List<JfrogServers> getJfrogInstances() {
return RepositoriesUtils.getJfrogInstances();
}

public boolean isJiraPluginEnabled() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jfrog/hudson/ArtifactoryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import static org.jfrog.hudson.util.ProxyUtils.createProxyConfiguration;

/**
* Represents an artifactory instance.
* Represents an instance of jenkins artifactory configuration page.
*
* @author Yossi Shaul
*/
Expand Down Expand Up @@ -92,7 +92,6 @@ public class ArtifactoryServer implements Serializable {

private CredentialsConfig resolverCredentialsConfig;

@DataBoundConstructor
public ArtifactoryServer(String serverId, String artifactoryUrl, CredentialsConfig deployerCredentialsConfig,
CredentialsConfig resolverCredentialsConfig, int timeout, boolean bypassProxy, Integer connectionRetry, Integer deploymentThreads) {
this.url = StringUtils.removeEnd(artifactoryUrl, "/");
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/org/jfrog/hudson/BuildInfoResultAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@
import hudson.model.Run;
import org.apache.commons.lang.StringUtils;
import org.jfrog.build.api.Build;
import org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient;
import org.jfrog.hudson.util.BuildUniqueIdentifierHelper;
import java.text.ParseException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Result of the redeploy publisher. Currently only a link to Artifactory build info.
*
* @author Yossi Shaul
*/
public class BuildInfoResultAction implements BuildBadgeAction {

private static final Logger log = Logger.getLogger(BuildInfoResultAction.class.getName());
private List<PublishedBuildDetails> publishedBuildsDetails = new CopyOnWriteArrayList<>();
private final Run build;
@Deprecated
Expand All @@ -49,11 +53,11 @@ public BuildInfoResultAction(Run build) {
public BuildInfoResultAction(String artifactoryUrl, Run build, String buildName) {
this(build);
String buildNumber = BuildUniqueIdentifierHelper.getBuildNumber(build);
publishedBuildsDetails.add(createBuildInfoIdentifier(artifactoryUrl, buildName, buildNumber));
publishedBuildsDetails.add(createBuildInfoIdentifier(artifactoryUrl, buildName, buildNumber, "", ""));
}

public void addBuildInfoResults(String artifactoryUrl, Build buildInfo) {
publishedBuildsDetails.add(createBuildInfoIdentifier(artifactoryUrl, build, buildInfo));
public void addBuildInfoResults(String artifactoryUrl, String platformUrl, Build buildInfo) {
publishedBuildsDetails.add(createBuildInfoIdentifier(artifactoryUrl, build, buildInfo, platformUrl));
}

public Run getBuild() {
Expand All @@ -79,16 +83,20 @@ public String getUrlName() {
String buildName = BuildUniqueIdentifierHelper.getBuildNameConsiderOverride(artifactoryRedeployPublisher, build);
return generateUrl(artifactoryRedeployPublisher.getArtifactoryName(), build, buildName);
} else if (publishedBuildsDetails.size() == 1) {
return publishedBuildsDetails.get(0).getBuildInfoUrl();
try {
return publishedBuildsDetails.get(0).getBuildInfoUrl();
} catch (ParseException e) {
log.log(Level.WARNING, "could not create build info URL", e);
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
}
}
return "published_builds";
}

private PublishedBuildDetails createBuildInfoIdentifier(String artifactoryUrl, String buildName, String buildNumber) {
return new PublishedBuildDetails(artifactoryUrl, Util.rawEncode(buildName), Util.rawEncode(buildNumber));
private PublishedBuildDetails createBuildInfoIdentifier(String artifactoryUrl, String buildName, String buildNumber, String platformUrl, String startedBuildTimestamp) {
return new PublishedBuildDetails(artifactoryUrl, Util.rawEncode(buildName), Util.rawEncode(buildNumber), platformUrl, startedBuildTimestamp);
}

private PublishedBuildDetails createBuildInfoIdentifier(String artifactoryUrl, Run build, Build buildInfo) {
private PublishedBuildDetails createBuildInfoIdentifier(String artifactoryUrl, Run build, Build buildInfo, String platformUrl) {
String buildName;
String buildNumber;
if (StringUtils.isNotEmpty(buildInfo.getName())) {
Expand All @@ -101,12 +109,11 @@ private PublishedBuildDetails createBuildInfoIdentifier(String artifactoryUrl, R
} else {
buildNumber = BuildUniqueIdentifierHelper.getBuildNumber(build);
}
return createBuildInfoIdentifier(artifactoryUrl, buildName, buildNumber);
return createBuildInfoIdentifier(artifactoryUrl, buildName, buildNumber, platformUrl, buildInfo.getStarted());
}

private String generateUrl(String artifactoryUrl, Run build, String buildName) {
return artifactoryUrl + "/webapp/builds/" + Util.rawEncode(buildName) + "/"
+ Util.rawEncode(BuildUniqueIdentifierHelper.getBuildNumber(build));
return ArtifactoryBuildInfoClient.createBuildInfoUrl(artifactoryUrl, buildName, BuildUniqueIdentifierHelper.getBuildNumber(build));
}

/**
Expand Down
Loading