Skip to content

Commit a702125

Browse files
authored
Merge pull request #1255 from merico-dev/feat-github-actions-language
feat: pipeline support language default config
2 parents 431676b + 81ba7e4 commit a702125

7 files changed

Lines changed: 118 additions & 26 deletions

File tree

internal/pkg/plugin/installer/ci/cifile/server/githubci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (g *GitHubWorkflow) FilterCIFilesFunc() file.DirFIleFilterFunc {
2828
if isDir {
2929
return false
3030
}
31-
return strings.Contains(filePath, "workflows")
31+
return strings.Contains(filePath, "yml") || strings.Contains(filePath, "yaml")
3232
}
3333
}
3434

internal/pkg/plugin/installer/ci/step/general.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010
)
1111

1212
type test struct {
13-
Enable *bool `mapstructure:"enable"`
14-
Command string `mapstructure:"command"`
15-
ContainerName string `mapstructure:"containerName"`
16-
CoverageCommand string `mapstructure:"coverageCommand"`
17-
CoverageStatusCommand string `mapstructure:"CoverageStatusCommand"`
13+
Enable *bool `mapstructure:"enable"`
14+
Command []string `mapstructure:"command"`
15+
ContainerName string `mapstructure:"containerName"`
16+
CoverageCommand string `mapstructure:"coverageCommand"`
17+
CoverageStatusCommand string `mapstructure:"CoverageStatusCommand"`
1818
}
1919

2020
type GeneralStepConfig struct {
21-
Language *Language `mapstructure:"language"`
22-
Test *test `mapstructure:"test"`
21+
Language language `mapstructure:"language"`
22+
Test test `mapstructure:"test"`
2323
}
2424

2525
// GetJenkinsPlugins return jenkins plugins info
@@ -37,12 +37,12 @@ func (g *GeneralStepConfig) ConfigSCM(client scm.ClientOperation) error {
3737
}
3838

3939
func (g *GeneralStepConfig) SetDefault() {
40-
if g.Language != nil && g.Language.Name == "" {
40+
if g.Language.Name == "" {
4141
return
4242
}
4343
generalDefaultOpts := g.Language.getGeneralDefaultOption()
4444
if g.Test.Enable != types.Bool(false) {
45-
if err := mergo.Merge(g.Test, generalDefaultOpts); err != nil {
45+
if err := mergo.Merge(&g.Test, generalDefaultOpts.testOption); err != nil {
4646
log.Warnf("step general merge default config failed: %+v", err)
4747
}
4848
}

internal/pkg/plugin/installer/ci/step/general_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
package step_test
1+
package step
22

33
import (
44
. "github.com/onsi/ginkgo/v2"
55
. "github.com/onsi/gomega"
66

7-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/step"
87
"github.com/devstream-io/devstream/pkg/util/jenkins"
98
"github.com/devstream-io/devstream/pkg/util/scm"
109
)
1110

1211
var _ = Describe("GeneralStepConfig struct", func() {
1312
var (
14-
c *step.GeneralStepConfig
13+
c *GeneralStepConfig
1514
mockClient *jenkins.MockClient
1615
)
1716
BeforeEach(func() {
18-
c = &step.GeneralStepConfig{
19-
Language: &step.Language{
20-
Name: "test",
21-
},
22-
}
17+
c = &GeneralStepConfig{}
2318
})
2419
Context("GetJenkinsPlugins method", func() {
2520
It("should return empty plugins", func() {
@@ -41,4 +36,29 @@ var _ = Describe("GeneralStepConfig struct", func() {
4136
Expect(err).Error().ShouldNot(HaveOccurred())
4237
})
4338
})
39+
Context("SetDefault method", func() {
40+
When("language is empty", func() {
41+
BeforeEach(func() {
42+
c.Language = language{}
43+
})
44+
It("should return GeneralStepConfig", func() {
45+
c.SetDefault()
46+
Expect(c.Test.Enable).Should(BeNil())
47+
})
48+
})
49+
When("language is valid", func() {
50+
BeforeEach(func() {
51+
c.Language = language{
52+
Name: "go",
53+
}
54+
})
55+
It("should set test options", func() {
56+
goTestOption, exist := languageDefaultOptionMap["go"]
57+
Expect(exist).Should(BeTrue())
58+
Expect(goTestOption).ShouldNot(BeNil())
59+
c.SetDefault()
60+
Expect(c.Test).Should(Equal(*goTestOption.testOption))
61+
})
62+
})
63+
})
4464
})

internal/pkg/plugin/installer/ci/step/language.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"github.com/devstream-io/devstream/pkg/util/types"
77
)
88

9-
type Language struct {
10-
Name string `mapstructure:"name"`
11-
Version string `mapstructure:"version"`
9+
type language struct {
10+
Name string `mapstructure:"name"`
11+
Version string `mapstructure:"version"`
12+
FrameWork string `mapstructure:"frameWork"`
1213
}
1314

1415
type generalDefaultOption struct {
@@ -18,15 +19,15 @@ type generalDefaultOption struct {
1819
var languageDefaultOptionMap = map[string]*generalDefaultOption{
1920
"java": {
2021
testOption: &test{
21-
Command: "mvn -B test",
22+
Command: []string{"mvn -B test"},
2223
ContainerName: "maven:3.8.1-jdk-8",
2324
Enable: types.Bool(true),
2425
},
2526
},
26-
"golang": {
27+
"go": {
2728
testOption: &test{
2829
Enable: types.Bool(true),
29-
Command: "go test ./...",
30+
Command: []string{"go test ./..."},
3031
CoverageCommand: "go tool cover -func=coverage.out >> coverage.cov",
3132
CoverageStatusCommand: `cat coverage.cov
3233
body=$(cat coverage.cov)
@@ -36,9 +37,29 @@ body="${body//$'\r'/'%0D'}"
3637
echo ::set-output name=body::$body`,
3738
},
3839
},
40+
"python": {
41+
testOption: &test{
42+
Command: []string{
43+
"python -m pip install --upgrade pip",
44+
"pip install -r requirements.txt",
45+
"python3 -m unittest",
46+
},
47+
Enable: types.Bool(true),
48+
},
49+
},
50+
"nodejs": {
51+
testOption: &test{
52+
Command: []string{
53+
"npm ci",
54+
"npm run build --if-present",
55+
"npm test",
56+
},
57+
Enable: types.Bool(true),
58+
},
59+
},
3960
}
4061

41-
func (l *Language) getGeneralDefaultOption() *generalDefaultOption {
62+
func (l *language) getGeneralDefaultOption() *generalDefaultOption {
4263
lang := strings.TrimSpace(strings.ToLower(l.Name))
4364
return languageDefaultOptionMap[lang]
4465
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package step
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("Language struct", func() {
9+
var (
10+
l *language
11+
)
12+
When("lanuage is go", func() {
13+
BeforeEach(func() {
14+
l = &language{
15+
Name: "go",
16+
}
17+
})
18+
It("should return go default config", func() {
19+
defaultConfig := l.getGeneralDefaultOption()
20+
Expect(defaultConfig).ShouldNot(BeNil())
21+
Expect(defaultConfig.testOption.Command).Should(Equal([]string{"go test ./..."}))
22+
})
23+
})
24+
When("lanuage is java", func() {
25+
BeforeEach(func() {
26+
l = &language{
27+
Name: "java",
28+
}
29+
})
30+
It("should return java default config", func() {
31+
defaultConfig := l.getGeneralDefaultOption()
32+
Expect(defaultConfig).ShouldNot(BeNil())
33+
Expect(defaultConfig.testOption.Command).Should(Equal([]string{"mvn -B test"}))
34+
})
35+
})
36+
When("language is not exist", func() {
37+
BeforeEach(func() {
38+
l = &language{
39+
Name: "not_exist",
40+
}
41+
})
42+
It("should return go default config", func() {
43+
defaultConfig := l.getGeneralDefaultOption()
44+
Expect(defaultConfig).Should(BeNil())
45+
})
46+
})
47+
})

internal/pkg/show/config/plugins/github-actions.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ tools:
1515
pipeline:
1616
# workFlowfilePath is the location of workflows, it can be remote or local
1717
configLocation: https://github.com/devstream-io/devstream/main/staging/dtm-github-action-example/general
18+
general:
19+
language:
20+
name: LANGUAGE # go/java/nodejs/python...
21+
version: VERSION # language version
1822
imageRepo:
1923
# image repo URL for pulling/pushing
2024
url: http://harbor.example.com:80

pkg/util/template/render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Render(name, templateStr string, variable any, funcMaps ...template.FuncMap
1717

1818
t, err := t.Parse(templateStr)
1919
if err != nil {
20-
log.Debugf("Template parse file failed: %s.", err)
20+
log.Warnf("Template parse file failed: %s.", err)
2121
return "", err
2222
}
2323

0 commit comments

Comments
 (0)