package cn.reghao.autodop.dmaster.app.controller;

import cn.reghao.jdkutil.result.WebBody;
import cn.reghao.jdkutil.serializer.JsonConverter;
import cn.reghao.autodop.dmaster.app.service.BuildDeployDispatcher;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@RestController
@Api(tags = "Webhook 接口")
@RequestMapping("/api/hook")
public class WebhookController {
    private BuildDeployDispatcher buildDeployDispatcher;

    public WebhookController(BuildDeployDispatcher buildDeployDispatcher) {
        this.buildDeployDispatcher = buildDeployDispatcher;
    }

    @ApiOperation(value = "通过 webhook 自动部署应用")
    @PostMapping("/hook")
    public String hook(@RequestBody String body) {
        JsonObject jsonObject = JsonConverter.jsonToJsonElement(body).getAsJsonObject();
        String repo = jsonObject.get("repository").getAsJsonObject().get("url").getAsString();
        String ref = jsonObject.get("ref").getAsString();
        String branch = ref.substring(ref.lastIndexOf("/")+1);

        return WebBody.success("buildDeployResult");
    }
}
