package com.app.controller.platform;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 功能描述：读取Git的信息
 * 需要在pom.xml增加依赖
 * pl.project13.maven
 * git-commit-id-plugin
 *
 * @Author 梁展鹏
 */
@Api(value = "git info from git-commit-id-plugin")
@Slf4j
@RestController
@RequestMapping("/platform/git-commit")
public class GitCommitController {

    @ApiOperation(value = "get commit info")
    @GetMapping("/info")
    public String showGitCommitInfo() {
        // git.properties
        ResourceBundle resourceBundle = ResourceBundle.getBundle("git", ObjectUtil.defaultIfNull(null, Locale.getDefault()));
        Enumeration<String> keysEnumeration = resourceBundle.getKeys();

        Map<String, String> map = new TreeMap<>();
        while (keysEnumeration.hasMoreElements()) {
            String key = keysEnumeration.nextElement();
            map.put(key, resourceBundle.getString(key));
        }

        return JSON.toJSONString(map, SerializerFeature.PrettyFormat);
    }

    @ApiOperation(value = "get commit id")
    @GetMapping("/id")
    public String showGitCommitId() {
        // git.properties
        ResourceBundle resourceBundle = ResourceBundle.getBundle("git", ObjectUtil.defaultIfNull(null, Locale.getDefault()));
        return resourceBundle.getString("git.commit.id");
    }

}
