Search in sources :

Example 1 with SCMTriggerItem

use of jenkins.triggers.SCMTriggerItem in project gogs-webhook-plugin by jenkinsci.

the class GogsPayloadProcessor method triggerJobs.

public GogsResults triggerJobs(String jobName, String deliveryID) {
    SecurityContext saveCtx = ACL.impersonate(ACL.SYSTEM);
    GogsResults result = new GogsResults();
    try {
        BuildableItem project = GogsUtils.find(jobName, BuildableItem.class);
        if (project != null) {
            GogsTrigger gTrigger = null;
            Cause cause = new GogsCause(deliveryID);
            if (project instanceof ParameterizedJobMixIn.ParameterizedJob) {
                ParameterizedJobMixIn.ParameterizedJob pJob = (ParameterizedJobMixIn.ParameterizedJob) project;
                for (Trigger trigger : pJob.getTriggers().values()) {
                    if (trigger instanceof GogsTrigger) {
                        gTrigger = (GogsTrigger) trigger;
                        break;
                    }
                }
            }
            if (gTrigger != null) {
                SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(project);
                GogsPayload gogsPayload = new GogsPayload(this.payload);
                if (item != null) {
                    item.scheduleBuild2(0, gogsPayload);
                }
            } else {
                project.scheduleBuild(0, cause);
            }
            result.setMessage(String.format("Job '%s' is executed", jobName));
        } else {
            String msg = String.format("Job '%s' is not defined in Jenkins", jobName);
            result.setStatus(404, msg);
            LOGGER.warning(msg);
        }
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        LOGGER.severe(sw.toString());
    } finally {
        SecurityContextHolder.setContext(saveCtx);
    }
    return result;
}
Also used : ParameterizedJobMixIn(jenkins.model.ParameterizedJobMixIn) Trigger(hudson.triggers.Trigger) StringWriter(java.io.StringWriter) BuildableItem(hudson.model.BuildableItem) Cause(hudson.model.Cause) SecurityContext(org.acegisecurity.context.SecurityContext) SCMTriggerItem(jenkins.triggers.SCMTriggerItem) PrintWriter(java.io.PrintWriter)

Example 2 with SCMTriggerItem

use of jenkins.triggers.SCMTriggerItem in project sonar-scanner-jenkins by SonarSource.

the class BuilderUtils method getModuleRoot.

public static FilePath getModuleRoot(Run<?, ?> run, FilePath workspace) {
    FilePath moduleRoot = null;
    if (run instanceof AbstractBuild) {
        AbstractBuild<?, ?> build = (AbstractBuild<?, ?>) run;
        moduleRoot = build.getModuleRoot();
    } else {
        // otherwise get the first module of the first SCM
        Object parent = run.getParent();
        if (parent instanceof SCMTriggerItem) {
            SCMTriggerItem scmTrigger = (SCMTriggerItem) parent;
            Collection<? extends SCM> scms = scmTrigger.getSCMs();
            if (!scms.isEmpty()) {
                SCM scm = scms.iterator().next();
                FilePath[] moduleRoots = scm.getModuleRoots(workspace, null);
                moduleRoot = moduleRoots != null && moduleRoots.length > 0 ? moduleRoots[0] : null;
            }
        }
        if (moduleRoot == null) {
            moduleRoot = workspace;
        }
    }
    return moduleRoot;
}
Also used : FilePath(hudson.FilePath) AbstractBuild(hudson.model.AbstractBuild) SCM(hudson.scm.SCM) SCMTriggerItem(jenkins.triggers.SCMTriggerItem)

Aggregations

SCMTriggerItem (jenkins.triggers.SCMTriggerItem)2 FilePath (hudson.FilePath)1 AbstractBuild (hudson.model.AbstractBuild)1 BuildableItem (hudson.model.BuildableItem)1 Cause (hudson.model.Cause)1 SCM (hudson.scm.SCM)1 Trigger (hudson.triggers.Trigger)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ParameterizedJobMixIn (jenkins.model.ParameterizedJobMixIn)1 SecurityContext (org.acegisecurity.context.SecurityContext)1