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

NCL-6600 Add API for using latest version #840

Merged
merged 3 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void checkDependencies()
+ "org.hamcrest:hamcrest-all:1.3 jar test \n"
+ "org.jacoco:jacoco-maven-plugin:0.8[.\\d+]+\\s+ maven-plugin \n"
+ "org.jboss.byteman:byteman-bmunit:4[.\\d+]+\\s+ jar test \n"
+ "org.jboss.da:reports-model:2.1.0.Api-1 jar compile \n"
+ "org.jboss.da:reports-model:2.1.0.Api-[.\\d+]+\\s+ jar compile \n"
+ "org.jdom:jdom:1.1.3 jar compile \n"
+ "org.projectlombok:lombok:1.[.\\d+]+\\s+ jar provided \n"
+ "org.projectlombok:lombok-maven-plugin:1.[.\\d+]+\\s+ maven-plugin \n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,36 @@
*/
package org.commonjava.maven.ext.common.json;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.goots.hiderdoclet.doclet.JavadocExclude;
import org.jboss.da.lookup.model.MavenLookupResult;
import org.jboss.da.model.rest.GAV;
import org.jboss.da.reports.model.response.LookupReport;

@Setter
@Getter
@EqualsAndHashCode(callSuper = true)
@JavadocExclude
public class ExtendedMavenLookupResult
extends MavenLookupResult
@EqualsAndHashCode(exclude = "projectVersionRef")
@NoArgsConstructor
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class DependencyAnalyserResult
{
@NonNull
@JsonUnwrapped
private GAV gav;

@JsonIgnore
private ProjectVersionRef projectVersionRef;

public ExtendedMavenLookupResult(@NonNull GAV gav, String bestMatchVersion )
{
super( gav, bestMatchVersion );
}
private String latestVersion;

@Override
public String toString ()
{
return "PVR : " + projectVersionRef + " ; BestMatch : " + getBestMatchVersion();
}
private String bestMatchVersion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
import kong.unirest.jackson.JacksonObjectMapper;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef;
import org.commonjava.maven.ext.common.json.ExtendedMavenLookupResult;
import org.commonjava.maven.ext.common.json.DependencyAnalyserResult;
import org.commonjava.maven.ext.common.json.PME;
import org.goots.hiderdoclet.doclet.JavadocExclude;
import org.jboss.da.lookup.model.MavenLookupResult;
import org.jboss.da.model.rest.GAV;

import java.io.File;
Expand All @@ -45,7 +44,8 @@ public class JSONUtils
private static final String GROUP_ID = "groupId";
private static final String ARTIFACT_ID = "artifactId";
private static final String VERSION = "version";
private static final String BEST_MATCH = "bestMatchVersion";
private static final String BEST_MATCH_VERSION = "bestMatchVersion";
private static final String LATEST_VERSION = "latestVersion";

private static final ObjectMapper MAPPER = new ObjectMapper();

Expand Down Expand Up @@ -114,32 +114,36 @@ public void serialize( ProjectVersionRef value, JsonGenerator gen, SerializerPro
}

@JavadocExclude
public static class MavenLookupResultDeserializer extends JsonDeserializer<MavenLookupResult>
public static class MavenResultDeserializer extends JsonDeserializer<DependencyAnalyserResult>
{
@Override
public ExtendedMavenLookupResult deserialize( JsonParser p, DeserializationContext ctxt)
public DependencyAnalyserResult deserialize( JsonParser p, DeserializationContext ctxt)
throws IOException
{
JsonNode node = p.getCodec().readTree( p);
final String groupId = node.get(GROUP_ID).asText();
final String artifactId = node.get( ARTIFACT_ID ).asText();
final String version = node.get( VERSION ).asText();

ExtendedMavenLookupResult result;
DependencyAnalyserResult result = new DependencyAnalyserResult();
result.setGav( new GAV( groupId, artifactId, version ) );

String bestMatch = null;
if ( node.has( BEST_MATCH ) && !node.get( BEST_MATCH ).getNodeType().equals( JsonNodeType.NULL ) )
if ( node.has( BEST_MATCH_VERSION ) && !node.get( BEST_MATCH_VERSION ).getNodeType().equals( JsonNodeType.NULL ) )
{
bestMatch = node.get( BEST_MATCH ).asText();
result.setBestMatchVersion( node.get( BEST_MATCH_VERSION ).asText() );
}
if ( node.has( LATEST_VERSION ) && !node.get( LATEST_VERSION ).getNodeType().equals( JsonNodeType.NULL ) )
{
result.setLatestVersion( node.get( LATEST_VERSION ).asText() );
}

result = new ExtendedMavenLookupResult( new GAV( groupId, artifactId, version ), bestMatch );
result.setProjectVersionRef( new SimpleProjectVersionRef( groupId, artifactId, version ) );

return result;
}
}


@JavadocExclude
public static class InternalObjectMapper extends JacksonObjectMapper
{
Expand All @@ -154,7 +158,7 @@ public InternalObjectMapper ( ObjectMapper mapper)
SimpleModule module = new SimpleModule();
module.addDeserializer( ProjectVersionRef.class, new ProjectVersionRefDeserializer());
module.addSerializer(ProjectVersionRef.class, new ProjectVersionRefSerializer());
module.addDeserializer( MavenLookupResult.class, new MavenLookupResultDeserializer() );
module.addDeserializer( DependencyAnalyserResult.class, new MavenResultDeserializer() );
mapper.registerModule( module );

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import org.commonjava.maven.ext.common.util.PropertyResolver;
import org.commonjava.maven.ext.core.ManipulationSession;
import org.commonjava.maven.ext.core.impl.Version;
import org.commonjava.maven.ext.core.state.RESTState;
import org.commonjava.maven.ext.core.state.VersioningState;
import org.commonjava.maven.ext.io.rest.Translator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -158,7 +156,7 @@ public void overrideProjectVersion (ProjectVersionRef gav) throws ManipulationEx
source.add( gav );
source.add( getGAV() );

Map<ProjectVersionRef, String> restResult = getRESTAPI().translateVersions( source );
Map<ProjectVersionRef, String> restResult = getRESTAPI().lookupVersions( source );
String targetBuild = restResult.get( gav );
String thisMapping = restResult.get( getGAV() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void populateBOMVersions( ) throws RestException
// Call the REST to populate the result.
logger.debug( "Passing {} BOM GAVs following into the REST client api {} ", restParam.size(), restParam );
logger.info( "Calling REST client for BOMs..." );
Map<ProjectVersionRef, String> restResult = state.getVersionTranslator().translateVersions( restParam );
Map<ProjectVersionRef, String> restResult = state.getVersionTranslator().lookupVersions( restParam );
logger.debug( "REST Client returned for BOMs {} ", restResult );

final ListIterator<ProjectVersionRef> emptyIterator = Collections.<ProjectVersionRef>emptyList().listIterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.commonjava.maven.ext.core.state.PluginState;
import org.commonjava.maven.ext.core.state.RESTState;
import org.commonjava.maven.ext.core.state.VersioningState;
import org.commonjava.maven.ext.core.util.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,15 +84,15 @@ private void collect( final List<Project> projects )
return;
}

final ArrayList<ProjectVersionRef> newProjectKeys = new ArrayList<>();
final List<ProjectVersionRef> restLookupProjectVersionParamList = new ArrayList<>();
final String override = vs.getOverride();

for ( final Project project : projects )
{
if ( isEmpty( override ) )
{
// Strip SNAPSHOT and handle OSGi and alternate suffixes from the version for matching.
newProjectKeys.add( new SimpleProjectVersionRef(
restLookupProjectVersionParamList.add( new SimpleProjectVersionRef(
project.getKey().asProjectRef(), handlePotentialSnapshotVersion(
vs, Version.getOsgiVersion(VersionCalculator.handleAlternate( vs, project.getVersion() ) ) ) ) );
}
Expand All @@ -102,37 +101,49 @@ else if ( project.isExecutionRoot() )
// We want to manually override the version ; therefore ignore what is in the project and calculate potential
// matches for that instead.
Project p = projects.get( 0 );
newProjectKeys.add( new SimpleProjectVersionRef( p.getGroupId(), p.getArtifactId(), override ) );
restLookupProjectVersionParamList.add( new SimpleProjectVersionRef( p.getGroupId(), p.getArtifactId(), override ) );
}
}

final Set<ProjectVersionRef> restParamSet = new HashSet<>( newProjectKeys );
final List<ProjectVersionRef> restLookupVersionsParamList = new ArrayList<>();
final Set<ArtifactRef> localDeps = establishAllDependencies( session, projects, null );

// Ok we now have a defined list of top level project plus a unique list of all possible dependencies.
// Need to send that to the rest interface to get a translation.
for ( ArtifactRef p : localDeps )
{
restParamSet.add( p.asProjectVersionRef() );
restLookupVersionsParamList.add( p.asProjectVersionRef() );
}
final List<ProjectVersionRef> restParam = new ArrayList<>( restParamSet );

// Call the REST to populate the result.
logger.debug ("Passing {} GAVs into the REST client api {} ", restParam.size(), restParam);
Map<ProjectVersionRef, String> restResult = state.getVersionTranslator().translateVersions( restParam );
logger.info ("REST Client returned {} ", restResult);
logger.debug ("Passing {} GAVs into the REST client api {} ", restLookupVersionsParamList.size(), restLookupVersionsParamList);
Map<ProjectVersionRef, String> vRestResult = state.getVersionTranslator().lookupVersions( restLookupVersionsParamList );
logger.info ("REST Client returned: {} ", vRestResult);

// TODO: ### parseVersions pulls out project refs from prior rest call.
vs.setRESTMetadata (parseVersions(session, projects, state, newProjectKeys, restResult));
logger.debug ("Passing {} Project GAVs into the REST client api {} ", restLookupProjectVersionParamList.size(), restLookupProjectVersionParamList);
Map<ProjectVersionRef, String> pvResultResult = state.getVersionTranslator().lookupProjectVersions( restLookupProjectVersionParamList );
logger.info ("REST Client returned for project versions: {} ", pvResultResult);
Map<ProjectRef, Set<String>> versionStates = new HashMap<>();
pvResultResult.forEach( ( key, value ) -> {
Set<String> versions = versionStates.computeIfAbsent( key.asProjectRef(), k -> new HashSet<>() );
versions.add( value );
} );
vs.setRESTMetadata( versionStates );
Comment on lines +124 to +131
Copy link
Member

Choose a reason for hiding this comment

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

I am trying to understand the difference in the contents of pvResultResult and versionStates. It seems that they have virtually identical keys (ProjectVersionRef vs. ProjectRef), but the value is String vs. Set<String>. Is the value of versionStates always a singleton because there's only ever one key so there's only ever one value added? I feel like there is something obfuscated about this code.

In any case, we may be able to simplify with something like vs.setRESTMetadata( Stream.of( pvResultResult ).collect( Collectors.toMap() ) ).


final Map<ArtifactRef, String> overrides = new HashMap<>();

// Convert the loaded remote ProjectVersionRefs to the original ArtifactRefs
for (ArtifactRef a : localDeps )
{
if (restResult != null && restResult.containsKey( a.asProjectVersionRef() ))
ProjectVersionRef pvr = a.asProjectVersionRef();
if (vRestResult.containsKey( pvr ))
{
overrides.put( a, vRestResult.get( pvr));
}
// If a dependency is a project version also check those results as well
if (pvResultResult.containsKey( pvr ))
{
overrides.put( a, restResult.get( a.asProjectVersionRef()));
overrides.put( a, pvResultResult.get( pvr ));
}
}
logger.debug( "Setting REST Overrides {} ", overrides );
Expand All @@ -141,87 +152,6 @@ else if ( project.isExecutionRoot() )
ps.setRemoteRESTOverrides( overrides );
}

/**
* Parse the rest result for the project GAs and store them in versioning state for use
* there by incremental suffix calculation.
*/
private Map<ProjectRef, Set<String>> parseVersions( ManipulationSession session, List<Project> projects, RESTState state,
List<ProjectVersionRef> newProjectKeys, Map<ProjectVersionRef, String> restResult )
throws ManipulationException
{
Map<ProjectRef, Set<String>> versionStates = new HashMap<>();
for ( final ProjectVersionRef p : newProjectKeys )
{
if ( restResult.containsKey( p ) )
{
// Found part of the current project to store in Versioning State
Set<String> versions = versionStates.computeIfAbsent( p.asProjectRef(), k -> new HashSet<>() );
versions.add( restResult.get( p ) );
}
}
logger.debug ("Added the following ProjectRef:Version from REST call into VersionState {}", versionStates);

// We know we have ProjectVersionRef(s) of the current project(s). We need to establish potential
// blacklist by calling
// GET /listings/blacklist/ga?groupid=GROUP_ID&artifactid=ARTIFACT_ID
// passing in the groupId and artifactId.

// From the results we then need to establish whether the community version occurs in the blacklist
// causing a total abort and whether any redhat versions occur in the blacklist. If they do, that will
// affect the incremental potential options. The simplest option is simply to add those results to versionStates
// list. This will cause the incremental build number to be set to greater than those.

List<ProjectVersionRef> blacklist;

for ( Project p : projects )
{
if ( p.isExecutionRoot() )
{
logger.debug ("Calling REST client for blacklist with {}...", p.getKey().asProjectRef());
blacklist = state.getVersionTranslator().findBlacklisted( p.getKey().asProjectRef() );

if ( blacklist.size() > 0)
{
String suffix = PropertiesUtils.getSuffix( session );
String bVersion = blacklist.get( 0 ).getVersionString();
String pVersion = p.getVersion();
logger.debug( "REST Client returned for blacklist {} ", blacklist );

if ( isEmpty( suffix ) )
{
logger.warn( "No version suffix found ; unable to verify community blacklisting." );
}
else if ( blacklist.size() == 1 && !bVersion.contains( suffix ) )
{
if ( pVersion.contains( suffix ) )
{
pVersion = pVersion.substring( 0, pVersion.indexOf( suffix ) - 1 );
}
if ( pVersion.equals( bVersion ) )
{
throw new ManipulationException( "Community artifact '{}' has been blacklisted. Unable to build project version {}",
blacklist.get( 0 ), p.getVersion() );
}
}

// Found part of the current project to store in Versioning State
Set<String> versions = versionStates.computeIfAbsent( p.getKey().asProjectRef(), k -> new HashSet<>() );
for ( ProjectVersionRef b : blacklist )
{
versions.add( b.getVersionString() );
}

}
// else no blacklisted artifacts so just continue

break;
}
}


return versionStates;
}

/**
* No-op in this case - any changes, if configured, would happen in Versioning or Dependency Manipulators.
*/
Expand Down
18 changes: 0 additions & 18 deletions integration-test/src/it/rest-blacklist/invoker.properties

This file was deleted.

Loading