func AddTask(w http.ResponseWriter, r *http.Request) {
	project := context.Get(r, "project").(db.Project)
	user := context.Get(r, "user").(*db.User)
	var taskObj db.Task
	if !helpers.Bind(w, r, &taskObj) {
		return
	}
	newTask, err := AddTaskToPool(helpers.Store(r), taskObj, &user.ID, project.ID)
	if err != nil {
		util.LogErrorWithFields(err, log.Fields{"error": "Cannot write new event to database"})
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	helpers.WriteJSON(w, http.StatusCreated, newTask)
}
AddTask inserts a task into the database and returns a header or returns error
package tasks
import (
	"github.com/ansible-semaphore/semaphore/api/helpers"
	"github.com/ansible-semaphore/semaphore/db"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"
	log "github.com/Sirupsen/logrus"
	"github.com/ansible-semaphore/semaphore/util"
	"github.com/gorilla/context"
)
func getNextBuildVersion(startVersion string, currentVersion string) string {
	re := regexp.MustCompile(`^(.*[^\d])?(\d+)([^\d].*)?$`)
	m := re.FindStringSubmatch(startVersion)
	if m == nil {
		return startVersion
	}
	var prefix, suffix, body string
	switch len(m) - 1 {
	case 3:
		prefix = m[1]
		body = m[2]
		suffix = m[3]
	case 2:
		if _, err := strconv.Atoi(m[1]); err == nil {
			body = m[1]
			suffix = m[2]
		} else {
			prefix = m[1]
			body = m[2]
		}
	case 1:
		body = m[1]
	default:
		return startVersion
	}
	if !strings.HasPrefix(currentVersion, prefix) ||
		!strings.HasSuffix(currentVersion, suffix) {
		return startVersion
	}
	curr, err := strconv.Atoi(currentVersion[len(prefix) : len(currentVersion)-len(suffix)])
	if err != nil {
		return startVersion
	}
	start, err := strconv.Atoi(body)
	if err != nil {
		panic(err)
	}
	var newVer int
	if start > curr {
		newVer = start
	} else {
		newVer = curr + 1
	}
	return prefix + strconv.Itoa(newVer) + suffix
}
func AddTaskToPool(d db.Store, taskObj db.Task, userID *int, projectID int) (newTask db.Task, err error) {
	taskObj.Created = time.Now()
	taskObj.Status = taskWaitingStatus
	taskObj.UserID = userID
	taskObj.ProjectID = projectID
	tpl, err := d.GetTemplate(projectID, taskObj.TemplateID)
	if err != nil {
		return
	}
	err = taskObj.ValidateNewTask(tpl)
	if err != nil {
		return
	}
	if tpl.Type == db.TemplateBuild {  
		var builds []db.TaskWithTpl
		builds, err = d.GetTemplateTasks(tpl, db.RetrieveQueryParams{Count: 1})
		if err != nil {
			return
		}
		if len(builds) == 0 {
			taskObj.Version = tpl.StartVersion
		} else {
			v := getNextBuildVersion(*tpl.StartVersion, *builds[0].Version)
			taskObj.Version = &v
		}
	}
	newTask, err = d.CreateTask(taskObj)
	if err != nil {
		return
	}
	pool.register <- &task{
		store:     d,
		task:      newTask,
		projectID: projectID,
	}
	objType := db.EventTask
	desc := "Task ID " + strconv.Itoa(newTask.ID) + " queued for running"
	_, err = d.CreateEvent(db.Event{
		UserID:      userID,
		ProjectID:   &projectID,
		ObjectType:  &objType,
		ObjectID:    &newTask.ID,
		Description: &desc,
	})
	return
}