update
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-12-28 20:42:51 +08:00
parent 465f6981ed
commit de700cfdef
10 changed files with 312 additions and 42 deletions
+80
View File
@@ -0,0 +1,80 @@
package config_handler
import (
"context"
"encoding/json"
"strings"
"git.icechen.cn/pkg/drone_plugin/git"
"github.com/drone/drone-go/drone"
"github.com/drone/drone-go/plugin/config"
"github.com/sirupsen/logrus"
)
// New returns a new conversion plugin.
func New() config.Plugin {
return &plugin{}
}
type plugin struct{}
func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config, error) {
resp, err := json.Marshal(req)
if err != nil {
logrus.Error(err)
return nil, err
}
logrus.Infof("%s", string(resp))
// 1. 获取原始配置文件信息
cfg, err := getRealConfig(getGitClient(req))
if err != nil {
logrus.Error(err)
return nil, err
}
// 2. 非 monorepo 的直接返回
if cfg.Type != TypeMonorepo {
// 返回 nil 按照 204 处理
return nil, nil
}
// 3. 获取提交文件树
modifiedFileList, err := getGitClient(req).GetCommitModifiedFileList()
if err != nil {
logrus.Error(err)
return nil, err
}
// 4. 根据文件树以及原始配置文件的信息,组装需要构建的服务的ci信息
// 4.1 api
modifiedApiList := getModifiedApi(cfg.Api, modifiedFileList)
_ = modifiedApiList
// 4.2 service
// 5. 组装所有ci信息并输出
return nil, nil
}
func getGitClient(req *config.Request) git.Client {
return git.New(req.Repo.Namespace, req.Repo.Name, req.Build.After, req.Repo.Config)
}
func getModifiedApi(api []Api, modifiedFileList []string) []Api {
ret := make([]Api, 0)
tempIndex := NewSet()
for i, a := range api {
for _, file := range modifiedFileList {
if strings.HasSuffix(file, a.Root) {
tempIndex.Add(i)
}
}
}
for _, i := range tempIndex.ToSlice() {
ret = append(ret, api[i])
}
return ret
}
+27
View File
@@ -0,0 +1,27 @@
package config_handler
import "git.icechen.cn/pkg/drone_plugin/go_handler"
type ApiHandler interface {
ToDestinationConfig() (string, error)
}
func (al ApiList) toDestinationConfig() (string, error) {
return "", nil
}
func (a Api) toDestinationConfig() (string, error) {
var handler ApiHandler
switch a.Type {
case go_handler.TypeGolang:
handler = go_handler.GoApiHandler{}
}
return handler.ToDestinationConfig()
}
func (sl ServiceList) toDestinationConfig() {
}
func (s Service) toDestinationConfig() {
}
+46
View File
@@ -0,0 +1,46 @@
package config_handler
import (
"git.icechen.cn/pkg/drone_plugin/git"
"gopkg.in/yaml.v3"
)
const TypeMonorepo = "monorepo" // 单库类型
type Config struct {
Kind string `json:"kind" yaml:"kind"`
Type string `json:"type" yaml:"type"`
Name string `json:"name" yaml:"name"`
Api ApiList `json:"api" yaml:"api"`
Service ServiceList `json:"service" yaml:"service"`
}
type (
ApiList []Api
Api struct {
Name string `json:"name" yaml:"name"`
Root string `json:"root" yaml:"root"`
Type string `json:"type" yaml:"type"`
}
)
type (
ServiceList []Service
Service struct {
Name string `json:"name" yaml:"name"`
Root string `json:"root" yaml:"root"`
Type string `json:"type" yaml:"type"`
}
)
// getRealConfig 获取原始配置文件内容
func getRealConfig(client git.Client) (Config, error) {
var c Config
realConfigContent, err := client.GetRealConfig()
if err != nil {
return c, err
}
err = yaml.Unmarshal([]byte(realConfigContent), &c)
return c, err
}
+21
View File
@@ -0,0 +1,21 @@
package config_handler
type Set map[int]struct{}
func NewSet() Set {
return make(Set)
}
func (s *Set) Add(data ...int) {
for _, d := range data {
(*s)[d] = struct{}{}
}
}
func (s Set) ToSlice() []int {
ret := make([]int, 0, len(s))
for k := range s {
ret = append(ret, k)
}
return ret
}