Skip to content

Commit

Permalink
fix(dop): Synchronize pipeline yml file when running pipeline to solv…
Browse files Browse the repository at this point in the history
…e eventbox data loss problem (#4216)

* fix: Synchronize yml file when running pipeline to solve eventbox data loss problem

* polish the code
  • Loading branch information
littlejiancc authored Mar 2, 2022
1 parent 26b4d99 commit 60ff17f
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions modules/dop/providers/projectpipeline/service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"

cmspb "github.com/erda-project/erda-proto-go/core/pipeline/cms/pb"
Expand Down Expand Up @@ -1052,15 +1053,36 @@ func (p *ProjectPipelineService) autoRunPipeline(identityInfo apistructs.Identit
if err != nil {
return nil, err
}
orgStr := createV2.Labels[apistructs.LabelOrgID]
orgID, err := strconv.ParseUint(orgStr, 10, 64)
if err != nil {
return nil, err
}

// If source type is erda,should sync pipelineYml file
pipelineYml := source.PipelineYml
if source.SourceType == deftype.ErdaProjectPipelineType.String() {
pipelineYml, err = p.fetchRemotePipeline(source, orgStr, identityInfo.UserID)
if err != nil {
logrus.Errorf("failed to fetchRemotePipeline, err: %s", err.Error())
return nil, err
}
if pipelineYml != source.PipelineYml {
_, err = p.PipelineSource.Update(context.Background(), &spb.PipelineSourceUpdateRequest{
PipelineYml: pipelineYml,
PipelineSourceID: source.ID,
})
if err != nil {
logrus.Errorf("failed to update pipelien source, err: %s", err.Error())
return nil, err
}
}

}

// update user gittar token
var worker = limit_sync_group.NewWorker(3)
worker.AddFunc(func(locker *limit_sync_group.Locker, i ...interface{}) error {
orgStr := createV2.Labels[apistructs.LabelOrgID]
orgID, err := strconv.ParseUint(orgStr, 10, 64)
if err != nil {
return apierrors.ErrRunProjectPipeline.InternalError(fmt.Errorf("not find orgID"))
}
// update CmsNsConfigs
if err = p.UpdateCmsNsConfigs(identityInfo.UserID, orgID); err != nil {
return apierrors.ErrRunProjectPipeline.InternalError(err)
Expand All @@ -1073,7 +1095,7 @@ func (p *ProjectPipelineService) autoRunPipeline(identityInfo apistructs.Identit
PipelineYmlName: filepath.Join(source.Path, source.Name),
AppID: appID,
Branch: createV2.Labels[apistructs.LabelBranch],
PipelineYmlContent: source.PipelineYml,
PipelineYmlContent: pipelineYml,
UserID: identityInfo.UserID,
})
if err != nil {
Expand All @@ -1093,7 +1115,7 @@ func (p *ProjectPipelineService) autoRunPipeline(identityInfo apistructs.Identit
if err != nil {
return nil, apierrors.ErrRunProjectPipeline.InternalError(err)
}
totalActionNum, err := countActionNumByPipelineYml(source.PipelineYml)
totalActionNum, err := countActionNumByPipelineYml(pipelineYml)
if err != nil {
return nil, apierrors.ErrRunProjectPipeline.InternalError(err)
}
Expand Down Expand Up @@ -1171,8 +1193,8 @@ func (p *ProjectPipelineService) ListApp(ctx context.Context, params *pb.ListApp
}

for _, v := range statics.GetPipelineDefinitionStatistics() {
appName := getAppNameByRemote(v.Remote)
if v2, ok := appNamePipelineNumMap[appName]; ok {
remoteName := getNameByRemote(v.Remote)
if v2, ok := appNamePipelineNumMap[remoteName.AppName]; ok {
v2.FailedNum = int(v.FailedNum)
v2.RunningNum = int(v.RunningNum)
v2.TotalNum = int(v.TotalNum)
Expand Down Expand Up @@ -1304,10 +1326,26 @@ func (p *ProjectPipelineService) makeLocationByAppID(appID uint64) (string, erro
}, cicdPipelineType), nil
}

func getAppNameByRemote(remote string) string {
type RemoteName struct {
OrgName string
ProjectName string
AppName string
}

func getNameByRemote(remote string) RemoteName {
splits := strings.Split(remote, string(filepath.Separator))
if len(splits) != 3 {
return ""
return RemoteName{}
}
return RemoteName{
OrgName: splits[0],
ProjectName: splits[1],
AppName: splits[2],
}
return splits[2]
}

func (p *ProjectPipelineService) fetchRemotePipeline(source *spb.PipelineSource, orgID, userID string) (string, error) {
remoteName := getNameByRemote(source.Remote)
searchINode := remoteName.ProjectName + "/" + remoteName.AppName + "/blob/" + source.Ref + "/" + filepath.Join(source.Path, source.Name)
return p.bundle.GetGittarBlobNode("/wb/"+searchINode, orgID, userID)
}

0 comments on commit 60ff17f

Please sign in to comment.