package org.tkirill.teamcity.bisectPlugin;

import jetbrains.buildServer.controllers.BaseFormXmlController;
import jetbrains.buildServer.serverSide.SBuildServer;
import jetbrains.buildServer.web.openapi.WebControllerManager;
import org.jdom.Element;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BisectController extends BaseFormXmlController {
    public BisectController(SBuildServer server, WebControllerManager manager) {
        super(server);
        manager.registerController("/bisect/run.html", this);
    }

    @Override
    protected ModelAndView doGet(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response, Element xmlResponse) {
        response.setStatus(200);
//        String buildId = request.getParameter("buildId");
//        if (buildId != null) {
//            if (storage.getValue("bisect-plugin-build-" + buildId) != null) {
//                response.setStatus(400);
//                return;
//            }
//            storage.putValue("bisect-plugin-build-" + buildId, "started");
//        }
    }
}

@@ -97,24 +97,131 @@
 Controller;%0A
+import jetbrains.buildServer.serverSide.CustomDataStorage;%0Aimport jetbrains.buildServer.serverSide.SBuild;%0A
 import jetbr
@@ -350,16 +350,58 @@
 lement;%0A
+import org.jetbrains.annotations.NotNull;%0A
 import o
@@ -822,16 +822,25 @@
 w doGet(
+@NotNull 
 HttpServ
@@ -850,32 +850,41 @@
 Request request,
+ @NotNull
  HttpServletResp
@@ -968,16 +968,25 @@
  doPost(
+@NotNull 
 HttpServ
@@ -1004,16 +1004,25 @@
 request,
+ @NotNull
  HttpSer
@@ -1043,16 +1043,25 @@
 esponse,
+ @NotNull
  Element
@@ -1068,32 +1068,422 @@
  xmlResponse) %7B%0A
+        long buildId = Long.parseLong(request.getParameter(%22buildId%22));%0A        SBuild build = myServer.findBuildInstanceById(buildId);%0A        if (build == null) %7B%0A            response.setStatus(404);%0A            return;%0A        %7D%0A%0A        CustomDataStorage storage = build.getBuildType().getCustomDataStorage(%22bisectPlugin%22);%0A        storage.putValue(String.valueOf(buildId), %22running%22);%0A
         response

Store the flag designating that bisect is running.