Search in sources :

Example 1 with ArtifactoryStatus

use of net.nemerosa.ontrack.extension.artifactory.model.ArtifactoryStatus in project ontrack by nemerosa.

the class ArtifactoryPromotionSyncServiceImpl method syncBuild.

protected void syncBuild(Branch branch, String artifactoryBuildName, String buildName, ArtifactoryClient client, JobRunListener listener) {
    // Looks for the build
    Optional<Build> buildOpt = structureService.findBuildByName(branch.getProject().getName(), branch.getName(), buildName);
    if (buildOpt.isPresent()) {
        // Log
        String log = String.format("Sync branch %s/%s for Artifactory build %s", branch.getProject().getName(), branch.getName(), buildName);
        logger.debug("[artifactory-sync] {}", log);
        listener.message(log);
        // Gets the build information from Artifactory
        JsonNode buildInfo = client.getBuildInfo(artifactoryBuildName, buildName);
        // Gets the list of statuses
        List<ArtifactoryStatus> statuses = client.getStatuses(buildInfo);
        // For all statuses
        for (ArtifactoryStatus artifactoryStatus : statuses) {
            String statusName = artifactoryStatus.getName();
            // Looks for an existing promotion level with the same name on the branch
            Optional<PromotionLevel> promotionLevelOpt = structureService.findPromotionLevelByName(branch.getProject().getName(), branch.getName(), statusName);
            if (promotionLevelOpt.isPresent()) {
                // Looks for an existing promotion run for the build
                Optional<PromotionRun> runOpt = structureService.getLastPromotionRunForBuildAndPromotionLevel(buildOpt.get(), promotionLevelOpt.get());
                if (!runOpt.isPresent()) {
                    // No existing promotion, we can promote safely
                    logger.info("[artifactory-sync] Promote {}/{}/{} to {}", branch.getProject().getName(), branch.getName(), buildName, statusName);
                    // Actual promotion
                    structureService.newPromotionRun(PromotionRun.of(buildOpt.get(), promotionLevelOpt.get(), Signature.of(artifactoryStatus.getUser()).withTime(artifactoryStatus.getTimestamp()), "Promoted from Artifactory"));
                }
            }
        }
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArtifactoryStatus(net.nemerosa.ontrack.extension.artifactory.model.ArtifactoryStatus)

Example 2 with ArtifactoryStatus

use of net.nemerosa.ontrack.extension.artifactory.model.ArtifactoryStatus 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

ArtifactoryStatus (net.nemerosa.ontrack.extension.artifactory.model.ArtifactoryStatus)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Supplier (java.util.function.Supplier)1 ArtifactoryConfProperties (net.nemerosa.ontrack.extension.artifactory.ArtifactoryConfProperties)1 ArtifactoryClient (net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClient)1 ArtifactoryClientFactory (net.nemerosa.ontrack.extension.artifactory.client.ArtifactoryClientFactory)1 ArtifactoryConfigurationService (net.nemerosa.ontrack.extension.artifactory.configuration.ArtifactoryConfigurationService)1 SecurityService (net.nemerosa.ontrack.model.security.SecurityService)1 Before (org.junit.Before)1