Search in sources :

Example 1 with ArtifactoryClient

use of net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient in project ontrack by nemerosa.

the class ArtifactoryPromotionSyncServiceImpl method sync.

private void sync(Branch branch, JobRunListener listener) {
    // Gets the sync properties
    Property<ArtifactoryPromotionSyncProperty> syncProperty = propertyService.getProperty(branch, ArtifactoryPromotionSyncPropertyType.class);
    if (syncProperty.isEmpty()) {
        throw new IllegalStateException(String.format("Cannot find sync. property on branch %d", branch.id()));
    }
    String buildName = syncProperty.getValue().getBuildName();
    String buildNameFilter = syncProperty.getValue().getBuildNameFilter();
    ArtifactoryConfiguration configuration = syncProperty.getValue().getConfiguration();
    String log = String.format("Sync branch %s/%s with Artifactory build %s (%s)", branch.getProject().getName(), branch.getName(), buildName, buildNameFilter);
    logger.info("[artifactory-sync] {}", log);
    listener.message(log);
    // Build name filter
    Pattern buildNamePattern = Pattern.compile(replace(replace(buildNameFilter, ".", "\\."), "*", ".*"));
    // Gets an Artifactory client
    ArtifactoryClient client = artifactoryClientFactory.getClient(configuration);
    // Gets all the build numbers for the specified build name
    List<String> buildNumbers = client.getBuildNumbers(buildName).stream().filter(name -> buildNamePattern.matcher(name).matches()).collect(Collectors.toList());
    // Synchronises the promotion levels for each build
    for (String buildNumber : buildNumbers) {
        syncBuild(branch, buildName, buildNumber, client, listener);
    }
}
Also used : ArtifactoryConfProperties(net.nemerosa.ontrack.extension.artifactory.ArtifactoryConfProperties) ArtifactoryConfiguration(net.nemerosa.ontrack.extension.artifactory.configuration.ArtifactoryConfiguration) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ArtifactoryClientFactory(net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClientFactory) ArtifactoryClient(net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient) Service(org.springframework.stereotype.Service) JobOrchestratorSupplier(net.nemerosa.ontrack.job.orchestrator.JobOrchestratorSupplier) JsonNode(com.fasterxml.jackson.databind.JsonNode) StringUtils.replace(org.apache.commons.lang3.StringUtils.replace) ArtifactoryConfigurationService(net.nemerosa.ontrack.extension.artifactory.configuration.ArtifactoryConfigurationService) net.nemerosa.ontrack.job(net.nemerosa.ontrack.job) AbstractBranchJob(net.nemerosa.ontrack.model.support.AbstractBranchJob) Logger(org.slf4j.Logger) ConfigurationServiceListener(net.nemerosa.ontrack.model.support.ConfigurationServiceListener) Collectors(java.util.stream.Collectors) ArtifactoryPromotionSyncProperty(net.nemerosa.ontrack.extension.artifactory.property.ArtifactoryPromotionSyncProperty) SecurityService(net.nemerosa.ontrack.model.security.SecurityService) List(java.util.List) Stream(java.util.stream.Stream) ArtifactoryStatus(net.nemerosa.ontrack.extension.artifactory.model.ArtifactoryStatus) net.nemerosa.ontrack.model.structure(net.nemerosa.ontrack.model.structure) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ArtifactoryPromotionSyncPropertyType(net.nemerosa.ontrack.extension.artifactory.property.ArtifactoryPromotionSyncPropertyType) Pattern(java.util.regex.Pattern) ArtifactoryConfiguration(net.nemerosa.ontrack.extension.artifactory.configuration.ArtifactoryConfiguration) ArtifactoryPromotionSyncProperty(net.nemerosa.ontrack.extension.artifactory.property.ArtifactoryPromotionSyncProperty) ArtifactoryClient(net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient)

Example 2 with ArtifactoryClient

use of net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient in project ontrack by nemerosa.

the class ArtifactoryConfigurationServiceImpl method validate.

@Override
protected ConnectionResult validate(ArtifactoryConfiguration configuration) {
    try {
        ArtifactoryClient client = artifactoryClientFactory.getClient(configuration);
        // Gets the basic info
        client.getBuildNames();
        // OK
        return ConnectionResult.ok();
    } catch (Exception ex) {
        return ConnectionResult.error(ex.getMessage());
    }
}
Also used : ArtifactoryClient(net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient)

Example 3 with ArtifactoryClient

use of net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient in project ontrack by nemerosa.

the class ArtifactoryPromotionSyncServiceImplTest method setup.

@Before
public void setup() {
    structureService = mock(StructureService.class);
    propertyService = mock(PropertyService.class);
    ArtifactoryClientFactory artifactoryClientFactory = mock(ArtifactoryClientFactory.class);
    ArtifactoryConfigurationService configurationService = mock(ArtifactoryConfigurationService.class);
    ArtifactoryConfProperties artifactoryConfProperties = new ArtifactoryConfProperties();
    SecurityService securityService = mock(SecurityService.class);
    doAnswer(invocation -> {
        Supplier run = (Supplier) invocation.getArguments()[0];
        return run.get();
    }).when(securityService).asAdmin(any(Supplier.class));
    service = new ArtifactoryPromotionSyncServiceImpl(structureService, propertyService, artifactoryClientFactory, configurationService, artifactoryConfProperties, securityService);
    // Fake Artifactory client
    artifactoryClient = mock(ArtifactoryClient.class);
    when(artifactoryClientFactory.getClient(any())).thenReturn(artifactoryClient);
    // Branch to sync
    project = Project.of(new NameDescription("P", "Project")).withId(ID.of(1));
    branch = Branch.of(project, new NameDescription("B", "Branch")).withId(ID.of(10));
    // Existing build
    build = Build.of(branch, new NameDescription("1.0.0", "Build 1.0.0"), Signature.of("test")).withId(ID.of(100));
    when(structureService.findBuildByName("P", "B", "1.0.0")).thenReturn(Optional.of(build));
    // Existing promotions
    when(artifactoryClient.getStatuses(any())).thenReturn(Collections.singletonList(new ArtifactoryStatus("COPPER", "x", Time.now())));
    // Existing promotion level
    promotionLevel = PromotionLevel.of(branch, new NameDescription("COPPER", "Copper level")).withId(ID.of(100));
    when(structureService.findPromotionLevelByName("P", "B", "COPPER")).thenReturn(Optional.of(promotionLevel));
}
Also used : ArtifactoryConfProperties(net.nemerosa.ontrack.extension.artifactory.ArtifactoryConfProperties) ArtifactoryClientFactory(net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClientFactory) ArtifactoryConfigurationService(net.nemerosa.ontrack.extension.artifactory.configuration.ArtifactoryConfigurationService) SecurityService(net.nemerosa.ontrack.model.security.SecurityService) Supplier(java.util.function.Supplier) ArtifactoryStatus(net.nemerosa.ontrack.extension.artifactory.model.ArtifactoryStatus) ArtifactoryClient(net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient) Before(org.junit.Before)

Aggregations

ArtifactoryClient (net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient)3 ArtifactoryConfProperties (net.nemerosa.ontrack.extension.artifactory.ArtifactoryConfProperties)2 ArtifactoryClientFactory (net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClientFactory)2 ArtifactoryConfigurationService (net.nemerosa.ontrack.extension.artifactory.configuration.ArtifactoryConfigurationService)2 ArtifactoryStatus (net.nemerosa.ontrack.extension.artifactory.model.ArtifactoryStatus)2 SecurityService (net.nemerosa.ontrack.model.security.SecurityService)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 List (java.util.List)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ArtifactoryConfiguration (net.nemerosa.ontrack.extension.artifactory.configuration.ArtifactoryConfiguration)1 ArtifactoryPromotionSyncProperty (net.nemerosa.ontrack.extension.artifactory.property.ArtifactoryPromotionSyncProperty)1 ArtifactoryPromotionSyncPropertyType (net.nemerosa.ontrack.extension.artifactory.property.ArtifactoryPromotionSyncPropertyType)1 net.nemerosa.ontrack.job (net.nemerosa.ontrack.job)1 JobOrchestratorSupplier (net.nemerosa.ontrack.job.orchestrator.JobOrchestratorSupplier)1 net.nemerosa.ontrack.model.structure (net.nemerosa.ontrack.model.structure)1 AbstractBranchJob (net.nemerosa.ontrack.model.support.AbstractBranchJob)1