package org.jenkinsci.plugins.ontrack;

import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;

/**
 * Allows to notify for a build.
 */
public class OntrackBuildNotifier extends AbstractOntrackNotifier {

    private final String project;
    private final String branch;
    // TODO Build expression

    @DataBoundConstructor
    public OntrackBuildNotifier(String project, String branch) {
        this.project = project;
        this.branch = branch;
    }

    @Override
    public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
        // TODO Expands the expressions into actual values
        // Logging of parameters
        // TODO Logs the build number to create
        listener.getLogger().format("Creating build on project %s for branch %s%n", project, branch);
        // FIXME Calling ontrack UI
        // OK
        return true;
    }

    @Extension
    public static final class OntrackBuildDescriptorImpl extends BuildStepDescriptor<Publisher> {

        public OntrackBuildDescriptorImpl() {
            super(OntrackBuildNotifier.class);
        }

        @Override
        public String getDisplayName() {
            return "ontrack Build creation";
        }

        @Override
        public boolean isApplicable(Class<? extends AbstractProject> jobType) {
            return true;
        }

    }
}

@@ -525,32 +525,35 @@
     
-// TODO Build expression
+private final String build;
 %0A%0A  
@@ -637,16 +637,30 @@
 g branch
+, String build
 ) %7B%0A    
@@ -713,24 +713,52 @@
 h = branch;%0A
+        this.build = build;%0A
     %7D%0A%0A    @
@@ -1002,56 +1002,8 @@
 ers%0A
-        // TODO Logs the build number to create%0A
     
@@ -1050,16 +1050,19 @@
 g build 
+%25s 
 on proje
@@ -1084,16 +1084,23 @@
 h %25s%25n%22,
+ build,
  project

build parameter