2021-12-28 20:42:51 +08:00
|
|
|
|
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)
|
2021-12-29 19:40:57 +08:00
|
|
|
|
destinationApi, err := modifiedApiList.toDestinationConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-12-28 20:42:51 +08:00
|
|
|
|
|
|
|
|
|
// 4.2 service
|
2021-12-29 19:40:57 +08:00
|
|
|
|
modifiedServiceList := getModifiedService(cfg.Service, modifiedFileList)
|
|
|
|
|
destinationService, err := modifiedServiceList.toDestinationConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-12-28 20:42:51 +08:00
|
|
|
|
|
|
|
|
|
// 5. 组装所有ci信息并输出
|
2021-12-29 19:40:57 +08:00
|
|
|
|
return &drone.Config{
|
|
|
|
|
Data: destinationApi + "\n\n" + destinationService,
|
|
|
|
|
Kind: "pipeline",
|
|
|
|
|
}, nil
|
2021-12-28 20:42:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getGitClient(req *config.Request) git.Client {
|
|
|
|
|
return git.New(req.Repo.Namespace, req.Repo.Name, req.Build.After, req.Repo.Config)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 19:40:57 +08:00
|
|
|
|
func getModifiedApi(api ApiList, modifiedFileList []string) ApiList {
|
|
|
|
|
ret := make(ApiList, 0)
|
2021-12-28 20:42:51 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
2021-12-29 19:40:57 +08:00
|
|
|
|
|
|
|
|
|
func getModifiedService(service ServiceList, modifiedFileList []string) ServiceList {
|
|
|
|
|
ret := make(ServiceList, 0)
|
|
|
|
|
tempIndex := NewSet()
|
|
|
|
|
for i, s := range service {
|
|
|
|
|
for _, file := range modifiedFileList {
|
|
|
|
|
if strings.HasSuffix(file, s.Root) {
|
|
|
|
|
tempIndex.Add(i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, i := range tempIndex.ToSlice() {
|
|
|
|
|
ret = append(ret, service[i])
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|