5
5
6
6
"github.com/devstream-io/devstream/pkg/util/log"
7
7
"github.com/devstream-io/devstream/pkg/util/scm"
8
+ "github.com/devstream-io/devstream/pkg/util/scm/git"
8
9
)
9
10
10
11
const (
@@ -23,41 +24,49 @@ type app struct {
23
24
RepoTemplate * repoTemplate `yaml:"repoTemplate" mapstructure:"repoTemplate"`
24
25
CIRawConfigs []pipelineRaw `yaml:"ci" mapstructure:"ci"`
25
26
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:"-"`
26
33
}
27
34
28
35
func (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
34
39
}
35
40
36
- // get ci/cd pipelineTemplates
41
+ // 2. get ci/cd pipelineTemplates
37
42
appVars := a .Spec .merge (vars )
38
43
tools , err := a .generateCICDTools (templateMap , appVars )
39
44
if err != nil {
40
45
return nil , fmt .Errorf ("app[%s] get pipeline tools failed: %w" , a .Name , err )
41
46
}
47
+
48
+ // 3. generate app repo and template repo from scmInfo
49
+ repoScaffoldingTool := a .generateRepoTemplateTool ()
42
50
if repoScaffoldingTool != nil {
43
51
tools = append (tools , repoScaffoldingTool )
44
52
}
45
-
46
53
log .Debugf ("Have got %d tools from app %s." , len (tools ), a .Name )
47
-
48
54
return tools , nil
49
55
}
50
56
51
- // getAppPipelineTool generate ci/cd tools from app config
57
+ // generateAppPipelineTool generate ci/cd tools from app config
52
58
func (a * app ) generateCICDTools (templateMap map [string ]string , appVars map [string ]any ) (Tools , error ) {
53
59
allPipelineRaw := append (a .CIRawConfigs , a .CDRawConfigs ... )
54
60
var tools Tools
61
+ // pipelineGlobalVars is used to pass variable from ci/cd pipelines
62
+ pipelineGlobalVars := a .newPipelineGlobalOptionFromApp ()
55
63
for _ , p := range allPipelineRaw {
56
64
t , err := p .getPipelineTemplate (templateMap , appVars )
57
65
if err != nil {
58
66
return nil , err
59
67
}
60
- pipelineTool , err := t .generatePipelineTool (a )
68
+ t .updatePipelineVars (pipelineGlobalVars )
69
+ pipelineTool , err := t .generatePipelineTool (pipelineGlobalVars )
61
70
if err != nil {
62
71
return nil , err
63
72
}
@@ -67,41 +76,48 @@ func (a *app) generateCICDTools(templateMap map[string]string, appVars map[strin
67
76
return tools , nil
68
77
}
69
78
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 )
80
83
// 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
- }
86
84
if a .RepoTemplate .Vars == nil {
87
- a . RepoTemplate . Vars = make (RawOptions )
85
+ templateVars = make (RawOptions )
88
86
}
89
87
return newTool (
90
88
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 ,
94
92
},
95
- ), nil
93
+ )
96
94
}
97
- return nil , nil
95
+ return nil
98
96
}
99
97
100
98
// 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 == "" {
103
104
a .Repo .Name = a .Name
104
105
}
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
105
121
}
106
122
107
123
// since all plugin depends on code is deployed, get dependsOn for repoTemplate
@@ -113,3 +129,13 @@ func (a *app) getRepoTemplateDependants() []string {
113
129
}
114
130
return dependsOn
115
131
}
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