123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package service
- import (
- "context"
- "strconv"
- "strings"
- "go-common/app/admin/ep/saga/conf"
- "go-common/app/admin/ep/saga/model"
- "go-common/library/log"
- "github.com/xanzy/go-gitlab"
- )
- // collectprojectproc cron func
- func (s *Service) collectprojectproc() {
- /*defer func() {
- if x := recover(); x != nil {
- log.Error("collectprojectproc panic(%v)", errors.WithStack(fmt.Errorf("%v", x)))
- go s.collectprojectproc()
- log.Info("collectprojectproc recover")
- }
- }()*/
- var err error
- if err = s.CollectProject(context.TODO()); err != nil {
- log.Error("s.CollectProject err (%+v)", err)
- }
- }
- // CollectProject collect project information
- func (s *Service) CollectProject(c context.Context) (err error) {
- var (
- projects []*gitlab.Project
- total = 0
- page = 1
- )
- log.Info("Collect Project start")
- for page <= 1000 {
- if projects, err = s.gitlab.ListProjects(page); err != nil {
- return
- }
- num := len(projects)
- if num <= 0 {
- break
- }
- total = total + num
- for _, p := range projects {
- if err = s.insertDB(p); err != nil {
- return
- }
- }
- page = page + 1
- }
- log.Info("Collect Project end, find %d projects", total)
- return
- }
- // insertDB
- func (s *Service) insertDB(project *gitlab.Project) (err error) {
- var (
- b bool
- parseFail bool
- projectInfo = &model.ProjectInfo{
- ProjectID: project.ID,
- Name: project.Name,
- Description: project.Description,
- WebURL: project.WebURL,
- Repo: project.SSHURLToRepo,
- DefaultBranch: project.DefaultBranch,
- //Owner: project.Owner.Name,
- SpaceName: project.Namespace.Name,
- SpaceKind: project.Namespace.Kind,
- Saga: false,
- Runner: false,
- Department: "",
- Business: "",
- Language: "",
- }
- )
- if project.Namespace.Kind == "user" {
- return
- }
- if b, err = s.dao.HasProjectInfo(project.ID); err != nil {
- return
- }
- if len(project.Description) > 6 {
- projectInfo.Department, projectInfo.Business, projectInfo.Language, parseFail = parseDes(project.Description)
- }
- if parseFail {
- projectInfo.Department, projectInfo.Business = parseGroup(project.Namespace.Name)
- }
- if b {
- /*if err = s.update(project.ID, projectInfo); err != nil {
- return
- }*/
- if err = s.dao.UpdateProjectInfo(project.ID, projectInfo); err != nil {
- log.Warn("UpdateProjectInfo ProjectID(%d), Description: (%s)", projectInfo.ProjectID, projectInfo.Description)
- if strings.Contains(err.Error(), "Incorrect string value") {
- projectInfo.Description = strconv.QuoteToASCII(projectInfo.Description)
- }
- if err = s.dao.UpdateProjectInfo(project.ID, projectInfo); err != nil {
- return
- }
- }
- } else {
- if err = s.dao.AddProjectInfo(projectInfo); err != nil {
- log.Warn("AddProjectInfo ProjectID(%d), Description: (%s)", projectInfo.ProjectID, projectInfo.Description)
- if strings.Contains(err.Error(), "Incorrect string value") {
- projectInfo.Description = strconv.QuoteToASCII(projectInfo.Description)
- }
- if err = s.dao.AddProjectInfo(projectInfo); err != nil {
- return
- }
- }
- }
- return
- }
- // update database
- /*func (s *Service) update(projectID int, projectSrc *model.ProjectInfo) (err error) {
- var (
- projectDes *model.ProjectInfo
- )
- if projectDes, err = s.dao.ProjectInfoByID(projectID); err != nil {
- return
- }
- if *projectSrc == *projectDes {
- return
- }
- s.dao.UpdateProjectInfo(projectID, projectSrc)
- return
- }*/
- // parseDes get info from project description
- func parseDes(s string) (department, business, language string, parseFail bool) {
- //[主站 android java]
- ids := strings.Index(s, "[")
- idx := strings.LastIndex(s, "]")
- if ids == -1 || idx == -1 {
- parseFail = true
- return
- }
- str := s[ids+1 : idx]
- fields := strings.Fields(str)
- if len(fields) < 3 {
- parseFail = true
- return
- }
- department = fields[0]
- business = fields[1]
- language = fields[2]
- for _, de := range conf.Conf.Property.DeInfo {
- if department == de.Label {
- department = de.Value
- }
- }
- for _, bu := range conf.Conf.Property.BuInfo {
- if business == bu.Label {
- business = bu.Value
- }
- }
- return
- }
- func parseGroup(s string) (department, business string) {
- group := strings.Fields(conf.Conf.Property.Group.Name)
- de := strings.Fields(conf.Conf.Property.Group.Department)
- bu := strings.Fields(conf.Conf.Property.Group.Business)
- for i := 0; i < len(group); i++ {
- if s == group[i] {
- department, business = de[i], bu[i]
- }
- }
- return
- }
|