All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.takari.orchestra.plugins.nexus.NexusWebhookResource Maven / Gradle / Ivy

package io.takari.orchestra.plugins.nexus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.siesta.Resource;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Named
@Path("/api/v1/nexus-webhook")
public class NexusWebhookResource implements Resource {

    private static final Logger log = LoggerFactory.getLogger(NexusWebhookResource.class);

    private final Configuration cfg;
    private final ProcessExecutor processExecutor;

    @Inject
    public NexusWebhookResource(Configuration cfg, ProcessExecutor processExecutor) {
        this.cfg = cfg;
        this.processExecutor = processExecutor;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void process(final NexusWebhookRequest req) {
        String repoFilter = cfg.getRepositoryFilter();
        if (repoFilter != null) {
            repoFilter = repoFilter.trim();
            if (!req.getRepositoryName().matches(repoFilter)) {
                log.info("process ['{}'] -> not matched with the repository name filter, ignoring", req);
                return;
            }
        }

        String artifactFilter = cfg.getArtifactFilter();
        if (artifactFilter != null) {
            artifactFilter = artifactFilter.trim();
            if (!req.getPath().matches(artifactFilter)) {
                log.info("process ['{}'] -> not matched with the artifact path filter, ignoring", req);
                return;
            }
        }

        // TODO configuration? scripted?
        final String version = extractVersion(artifactFilter, req.getPath());
        if (version == null) {
            log.info("process ['{}'] -> can't extract the artifact's version", req);
            return;
        }

        // TODO configuration
        processExecutor.execute("defaultAnsible", req.getUserId(), req.getPath(), version);
        log.info("process ['{}'] -> started...", req);
    }

    // TODO public for testing, make private after the DI
    public static String extractVersion(String artifactFilter, String path) {
        // TODO cache?
        Pattern p = Pattern.compile(artifactFilter);
        Matcher m = p.matcher(path);
        if (m.matches()) {
            if (m.groupCount() != 1) {
                return null;
            }

            return m.group(1);
        }

        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy