Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/pkg/plugin/gitlabcedocker/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ var _ = Describe("Options", func() {

Expect(OptsBuild).To(Equal(OptsExpect))
})

})
})
18 changes: 13 additions & 5 deletions internal/pkg/plugininstaller/ci/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/common"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/scm/git"
"github.com/devstream-io/devstream/pkg/util/template"
"github.com/devstream-io/devstream/pkg/util/types"
Expand Down Expand Up @@ -42,14 +43,17 @@ func (opt *Options) buildGitMap() (gitMap git.GitFileContentMap, err error) {
ciConfig := opt.CIConfig
switch {
case ciConfig.LocalPath != "":
gitMap, err = ciConfig.getFromLocation(opt.ProjectRepo.Repo)
gitMap, err = ciConfig.getFromLocal(opt.ProjectRepo.Repo)
case ciConfig.RemoteURL != "":
gitMap, err = ciConfig.getFromURL(opt.ProjectRepo.Repo)
case ciConfig.Content != "":
gitMap, err = ciConfig.getFromContent(opt.ProjectRepo.Repo)
}
if len(gitMap) == 0 {
return nil, errors.New("can't get valid Jenkinsfile, please check your config")
if err != nil {
log.Warnf("ci get file failed: %+v", err)
}
return nil, errors.New("can't get valid ci file, please check your config")
}
return gitMap, err
}
Expand All @@ -65,7 +69,7 @@ func (c *CIConfig) getFromURL(appName string) (git.GitFileContentMap, error) {
return gitFileMap, nil
}

func (c *CIConfig) getFromLocation(appName string) (git.GitFileContentMap, error) {
func (c *CIConfig) getFromLocal(appName string) (git.GitFileContentMap, error) {
gitFileMap := make(git.GitFileContentMap)
info, err := os.Stat(c.LocalPath)
if err != nil {
Expand All @@ -80,7 +84,7 @@ func (c *CIConfig) getFromLocation(appName string) (git.GitFileContentMap, error
}
// process file
gitFilePath := getCIFilePath(c.Type)
if c.Type == ciGitHubWorkConfigLocation {
if c.Type == ciGitHubType {
gitFilePath = filepath.Join(gitFilePath, filepath.Base(c.LocalPath))
}
content, err := template.New().FromLocalFile(c.LocalPath).SetDefaultRender(appName, c.Vars).Render()
Expand All @@ -91,8 +95,12 @@ func (c *CIConfig) getFromLocation(appName string) (git.GitFileContentMap, error
return gitFileMap, nil
}

func (c *CIConfig) getFromContent(content string) (git.GitFileContentMap, error) {
func (c *CIConfig) getFromContent(appName string) (git.GitFileContentMap, error) {
gitFileMap := make(git.GitFileContentMap)
content, err := template.New().FromContent(c.Content).SetDefaultRender(appName, c.Vars).Render()
if err != nil {
return nil, err
}
gitFileMap[getCIFilePath(c.Type)] = []byte(content)
return gitFileMap, nil
}
Expand Down
219 changes: 219 additions & 0 deletions internal/pkg/plugininstaller/ci/option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package ci

import (
"fmt"
"net/http"
"os"
"path"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"

"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/common"
)

var _ = Describe("Options struct", func() {
Context("NewOptions method", func() {
var (
rawOptions plugininstaller.RawOptions
)
When("options is valid", func() {
BeforeEach(func() {
rawOptions = plugininstaller.RawOptions{
"ci": map[string]interface{}{
"type": "gitlab",
"content": "test",
},
}
})
It("should not raise error", func() {
_, err := NewOptions(rawOptions)
Expect(err).Error().ShouldNot(HaveOccurred())
})
})
})

Context("buildGitMap method", func() {
var (
opts *Options
repo *common.Repo
)
BeforeEach(func() {
opts = &Options{}
repo = &common.Repo{
Owner: "test",
Repo: "test_repo",
Branch: "test_branch",
RepoType: "gitlab",
}
opts.ProjectRepo = repo
})
When("all ci config is empty", func() {
BeforeEach(func() {
opts.CIConfig = &CIConfig{}
})
It("should raise error", func() {
_, err := opts.buildGitMap()
Expect(err).Error().Should(HaveOccurred())
})
})
When("LocalPath field is setted with github ci files", func() {
var (
localDir, localFile string
testContent []byte
)
BeforeEach(func() {
tempDir := GinkgoT().TempDir()
localDir = fmt.Sprintf("%s/%s", tempDir, ciGitHubWorkConfigLocation)
err := os.MkdirAll(localDir, os.ModePerm)
Expect(err).Error().ShouldNot(HaveOccurred())
tempFile, err := os.CreateTemp(localDir, "testFile")
Expect(err).Error().ShouldNot(HaveOccurred())
localFile = tempFile.Name()
testContent = []byte("_test")
err = os.WriteFile(localFile, testContent, 0755)
Expect(err).Error().ShouldNot(HaveOccurred())
})
When("LocalPath is directory", func() {
BeforeEach(func() {
opts.CIConfig = &CIConfig{
Type: ciGitHubType,
LocalPath: localDir,
}
})
It("should get all files content", func() {
gitMap, err := opts.buildGitMap()
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(gitMap).ShouldNot(BeEmpty())
expectedKey := fmt.Sprintf("%s/%s", ciGitHubWorkConfigLocation, path.Base(localFile))
v, ok := gitMap[expectedKey]
Expect(ok).Should(BeTrue())
Expect(v).Should(Equal(testContent))
})
})
When("localPath is file", func() {
BeforeEach(func() {
opts.CIConfig = &CIConfig{
Type: ciGitHubType,
LocalPath: localFile,
}
})
It("should get file content", func() {
gitMap, err := opts.buildGitMap()
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(gitMap).ShouldNot(BeEmpty())
expectedKey := fmt.Sprintf("%s/%s", ciGitHubWorkConfigLocation, path.Base(localFile))
v, ok := gitMap[expectedKey]
Expect(ok).Should(BeTrue())
Expect(v).Should(Equal(testContent))
})
})
})
When("Content field is setted", func() {
var (
testContent string
)
BeforeEach(func() {
testContent = "testJenkins"
opts.CIConfig = &CIConfig{
Type: ciJenkinsType,
Content: testContent,
}
})
It("should return gitmap", func() {
gitMap, err := opts.buildGitMap()
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(gitMap)).Should(Equal(1))
v, ok := gitMap[ciJenkinsConfigLocation]
Expect(ok).Should(BeTrue())
Expect(v).Should(Equal([]byte(testContent)))
})
})

When("RemoteURL field is setted", func() {
var (
templateVal string
s *ghttp.Server
)
BeforeEach(func() {
s = ghttp.NewServer()
testContent := "testGitlabCI [[ .App ]]"
templateVal = "template variable"
s.RouteToHandler("GET", "/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, testContent)
})
opts.CIConfig = &CIConfig{
Type: ciGitLabType,
RemoteURL: s.URL(),
Vars: map[string]interface{}{
"App": templateVal,
},
}
})
AfterEach(func() {
s.Close()
})
It("should get gitmap", func() {
gitMap, err := opts.buildGitMap()
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(gitMap)).Should(Equal(1))
v, ok := gitMap[ciGitLabConfigLocation]
Expect(ok).Should(BeTrue())
Expect(string(v)).Should(Equal(fmt.Sprintf("testGitlabCI %s", templateVal)))
})
})
})

Context("FillDefaultValue method", func() {
var (
defaultOpts, opts *Options
)
BeforeEach(func() {
opts = &Options{}
defaultCIConfig := &CIConfig{
Type: ciGitHubType,
RemoteURL: "http://www.test.com",
}
defaultRepo := &common.Repo{
Owner: "test",
Repo: "test_repo",
Branch: "test_branch",
RepoType: "gitlab",
}
defaultOpts = &Options{
CIConfig: defaultCIConfig,
ProjectRepo: defaultRepo,
}
})
When("ci config and repo are all empty", func() {
It("should set default value", func() {
opts.FillDefaultValue(defaultOpts)
Expect(opts.CIConfig).ShouldNot(BeNil())
Expect(opts.ProjectRepo).ShouldNot(BeNil())
Expect(opts.CIConfig.RemoteURL).Should(Equal("http://www.test.com"))
Expect(opts.ProjectRepo.Repo).Should(Equal("test_repo"))
})
})
When("ci config and repo has some value", func() {
BeforeEach(func() {
opts.CIConfig = &CIConfig{
RemoteURL: "http://exist.com",
}
opts.ProjectRepo = &common.Repo{
Branch: "new_branch",
}
})
It("should update empty value", func() {
opts.FillDefaultValue(defaultOpts)
Expect(opts.CIConfig).ShouldNot(BeNil())
Expect(opts.ProjectRepo).ShouldNot(BeNil())
Expect(opts.CIConfig.RemoteURL).Should(Equal("http://exist.com"))
Expect(opts.CIConfig.Type).Should(Equal(ciGitHubType))
Expect(opts.ProjectRepo.Branch).Should(Equal("new_branch"))
Expect(opts.ProjectRepo.Repo).Should(Equal("test_repo"))
})
})
})
})
45 changes: 45 additions & 0 deletions internal/pkg/plugininstaller/ci/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
. "github.com/onsi/gomega"

"github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/common"
)

var _ = Describe("Validate func", func() {
Expand Down Expand Up @@ -60,6 +61,23 @@ var _ = Describe("Validate func", func() {
_, err = ci.Validate(repoCiConflictOption)
Expect(err).Should(HaveOccurred())

ciTypeNotExistOptions := map[string]any{
"ci": map[string]any{
"localPath": "workflows/Jenkinsfile",
"type": "gg",
},
"projectRepo": map[string]any{
"baseURL": "http://127.0.0.1:30020",
"branch": "main",
"org": "",
"owner": "test_user",
"repo": "test",
"repoType": "gitlab",
},
}
_, err = ci.Validate(ciTypeNotExistOptions)
Expect(err).Should(HaveOccurred())

})
})
When("options is valid", func() {
Expand Down Expand Up @@ -99,3 +117,30 @@ var _ = Describe("Validate func", func() {
})
})
})

var _ = Describe("SetDefaultConfig func", func() {
var defaultOpts *ci.Options
BeforeEach(func() {
defaultCIConfig := &ci.CIConfig{
Type: "github",
RemoteURL: "http://www.test.com",
}
defaultRepo := &common.Repo{
Owner: "test",
Repo: "test_repo",
Branch: "test_branch",
RepoType: "gitlab",
}
defaultOpts = &ci.Options{
CIConfig: defaultCIConfig,
ProjectRepo: defaultRepo,
}
})
It("should work normal", func() {
defaultFunc := ci.SetDefaultConfig(defaultOpts)
rawOptions := map[string]interface{}{}
opts, err := defaultFunc(rawOptions)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(opts)).Should(Equal(2))
})
})
Loading