55
66 "github.com/devstream-io/devstream/pkg/util/log"
77 "github.com/devstream-io/devstream/pkg/util/scm"
8+ "github.com/devstream-io/devstream/pkg/util/scm/git"
89)
910
1011const (
@@ -23,41 +24,49 @@ type app struct {
2324 RepoTemplate * repoTemplate `yaml:"repoTemplate" mapstructure:"repoTemplate"`
2425 CIRawConfigs []pipelineRaw `yaml:"ci" mapstructure:"ci"`
2526 CDRawConfigs []pipelineRaw `yaml:"cd" mapstructure:"cd"`
27+
28+ // these two variables is used internal for convince
29+ // repoInfo is generated from Repo field with setDefault method
30+ repoInfo * git.RepoInfo `yaml:"-" mapstructure:"-"`
31+ // repoTemplateInfo is generated from RepoTemplate field with setDefault method
32+ repoTemplateInfo * git.RepoInfo `yaml:"-" mapstructure:"-"`
2633}
2734
2835func (a * app ) getTools (vars map [string ]any , templateMap map [string ]string ) (Tools , error ) {
29- // generate app repo and template repo from scmInfo
30- a .setDefault ()
31- repoScaffoldingTool , err := a .getRepoTemplateTool ()
32- if err != nil {
33- return nil , fmt .Errorf ("app[%s] can't get valid repo config: %w" , a .Name , err )
36+ // 1. set app default field repoInfo and repoTemplateInfo
37+ if err := a .setDefault (); err != nil {
38+ return nil , err
3439 }
3540
36- // get ci/cd pipelineTemplates
41+ // 2. get ci/cd pipelineTemplates
3742 appVars := a .Spec .merge (vars )
3843 tools , err := a .generateCICDTools (templateMap , appVars )
3944 if err != nil {
4045 return nil , fmt .Errorf ("app[%s] get pipeline tools failed: %w" , a .Name , err )
4146 }
47+
48+ // 3. generate app repo and template repo from scmInfo
49+ repoScaffoldingTool := a .generateRepoTemplateTool ()
4250 if repoScaffoldingTool != nil {
4351 tools = append (tools , repoScaffoldingTool )
4452 }
45-
4653 log .Debugf ("Have got %d tools from app %s." , len (tools ), a .Name )
47-
4854 return tools , nil
4955}
5056
51- // getAppPipelineTool generate ci/cd tools from app config
57+ // generateAppPipelineTool generate ci/cd tools from app config
5258func (a * app ) generateCICDTools (templateMap map [string ]string , appVars map [string ]any ) (Tools , error ) {
5359 allPipelineRaw := append (a .CIRawConfigs , a .CDRawConfigs ... )
5460 var tools Tools
61+ // pipelineGlobalVars is used to pass variable from ci/cd pipelines
62+ pipelineGlobalVars := a .newPipelineGlobalOptionFromApp ()
5563 for _ , p := range allPipelineRaw {
5664 t , err := p .getPipelineTemplate (templateMap , appVars )
5765 if err != nil {
5866 return nil , err
5967 }
60- pipelineTool , err := t .generatePipelineTool (a )
68+ t .updatePipelineVars (pipelineGlobalVars )
69+ pipelineTool , err := t .generatePipelineTool (pipelineGlobalVars )
6170 if err != nil {
6271 return nil , err
6372 }
@@ -67,41 +76,48 @@ func (a *app) generateCICDTools(templateMap map[string]string, appVars map[strin
6776 return tools , nil
6877}
6978
70- // getRepoTemplateTool will use repo-scaffolding plugin for app
71- func (a * app ) getRepoTemplateTool () (* Tool , error ) {
72- if a .Repo == nil {
73- return nil , fmt .Errorf ("app.repo field can't be empty" )
74- }
75- appRepo , err := a .Repo .BuildRepoInfo ()
76- if err != nil {
77- return nil , fmt .Errorf ("configmanager[app] parse repo failed: %w" , err )
78- }
79- if a .RepoTemplate != nil {
79+ // generateRepoTemplateTool will use repo-scaffolding plugin for app
80+ func (a * app ) generateRepoTemplateTool () * Tool {
81+ if a .repoTemplateInfo != nil {
82+ templateVars := make (RawOptions )
8083 // templateRepo doesn't need auth info
81- templateRepo , err := a .RepoTemplate .BuildRepoInfo ()
82- templateRepo .NeedAuth = false
83- if err != nil {
84- return nil , fmt .Errorf ("configmanager[app] parse repoTemplate failed: %w" , err )
85- }
8684 if a .RepoTemplate .Vars == nil {
87- a . RepoTemplate . Vars = make (RawOptions )
85+ templateVars = make (RawOptions )
8886 }
8987 return newTool (
9088 repoScaffoldingPluginName , a .Name , RawOptions {
91- "destinationRepo" : RawOptions (appRepo .Encode ()),
92- "sourceRepo" : RawOptions (templateRepo .Encode ()),
93- "vars" : a . RepoTemplate . Vars ,
89+ "destinationRepo" : RawOptions (a . repoInfo .Encode ()),
90+ "sourceRepo" : RawOptions (a . repoTemplateInfo .Encode ()),
91+ "vars" : templateVars ,
9492 },
95- ), nil
93+ )
9694 }
97- return nil , nil
95+ return nil
9896}
9997
10098// setDefault will set repoName to appName if repo.name field is empty
101- func (a * app ) setDefault () {
102- if a .Repo != nil && a .Repo .Name == "" {
99+ func (a * app ) setDefault () error {
100+ if a .Repo == nil {
101+ return fmt .Errorf ("configmanager[app] is invalid, repo field must be configured" )
102+ }
103+ if a .Repo .Name == "" {
103104 a .Repo .Name = a .Name
104105 }
106+ appRepo , err := a .Repo .BuildRepoInfo ()
107+ if err != nil {
108+ return fmt .Errorf ("configmanager[app] parse repo failed: %w" , err )
109+ }
110+ a .repoInfo = appRepo
111+ if a .RepoTemplate != nil {
112+ // templateRepo doesn't need auth info
113+ templateRepo , err := a .RepoTemplate .BuildRepoInfo ()
114+ if err != nil {
115+ return fmt .Errorf ("configmanager[app] parse repoTemplate failed: %w" , err )
116+ }
117+ templateRepo .NeedAuth = false
118+ a .repoTemplateInfo = templateRepo
119+ }
120+ return nil
105121}
106122
107123// since all plugin depends on code is deployed, get dependsOn for repoTemplate
@@ -113,3 +129,13 @@ func (a *app) getRepoTemplateDependants() []string {
113129 }
114130 return dependsOn
115131}
132+
133+ // newPipelineGlobalOptionFromApp generate pipeline options used for pipeline option configuration
134+ func (a * app ) newPipelineGlobalOptionFromApp () * pipelineGlobalOption {
135+ return & pipelineGlobalOption {
136+ RepoInfo : a .repoInfo ,
137+ AppSpec : a .Spec ,
138+ Scm : a .Repo ,
139+ AppName : a .Name ,
140+ }
141+ }
0 commit comments