Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: some refactors and fix some typo #1162

Merged
merged 1 commit into from
Oct 11, 2022
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
2 changes: 1 addition & 1 deletion internal/pkg/plugin/gitlabci/java/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
if err != nil {
return nil, err
}
_, err = client.PushLocalFileToRepo(&git.CommitInfo{
_, err = client.PushLocalFilesToRepo(&git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
Expand Down
41 changes: 30 additions & 11 deletions internal/pkg/plugininstaller/ci/ci.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package ci

import "github.com/devstream-io/devstream/pkg/util/file"

type ciRepoType string

const (
defaultBranch = "feat-repo-ci-update"
defaultCommitMsg = "update ci config"
ciJenkinsConfigLocation = "Jenkinsfile"
ciGitHubWorkConfigLocation = ".github/workflows"
ciGitLabConfigLocation = ".gitlab-ci.yml"
ciJenkinsType ciRepoType = "jenkins"
ciGitLabType ciRepoType = "gitlab"
ciGitHubType ciRepoType = "github"
deleteCommitMsg = "delete ci files"
)
type CI interface {
// Type return ci type
Type() ciRepoType
// CIFilePath returns the file path of ci config file
// gitlab and jenkins is just a file, so we can just use filename
// but GitHub use directory, we should process this situation
// for GitHub: return ".github/workflows" or ".github/workflows/subFilename"
// for gitlab, jenkins: will ignore subFilename param
CIFilePath(subFilename ...string) string
// filterCIFilesFunc returns a filter function to select ci config file
filterCIFilesFunc() file.DirFIleFilterFunc
// getGitNameFunc returns a function to transform file path to git name of ci config file
getGitNameFunc() file.DirFileNameFunc
}
steinliber marked this conversation as resolved.
Show resolved Hide resolved

func NewCI(ciType ciRepoType) CI {
// there are no validation for ciType
// because we have already validated it by `validate` flag in CIConfig.Type
switch ciType {
case ciGitLabType:
return &GitLabCI{}
case ciGitHubType:
return &GitHubCI{}
case ciJenkinsType:
return &JenkinsCI{}
}
return nil
}
45 changes: 45 additions & 0 deletions internal/pkg/plugininstaller/ci/githubci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ci

import (
"path/filepath"
"strings"

"github.com/devstream-io/devstream/pkg/util/file"
)

const (
ciGitHubType ciRepoType = "github"
ciGitHubWorkConfigLocation string = ".github/workflows"
)

type GitHubCI struct {
}

func (g *GitHubCI) Type() ciRepoType {
return ciGitHubType
}

func (g *GitHubCI) CIFilePath(subFilename ...string) string {
// if subFilename is empty, return dir(.github/workflows)
if len(subFilename) == 0 {
return ciGitHubWorkConfigLocation
}
// else return dir + subFilename
return filepath.Join(ciGitHubWorkConfigLocation, filepath.Base(subFilename[0]))
}

func (g *GitHubCI) filterCIFilesFunc() file.DirFIleFilterFunc {
return func(filePath string, isDir bool) bool {
// not process dir
if isDir {
return false
}
return strings.Contains(filePath, "workflows")
}
}

func (g *GitHubCI) getGitNameFunc() file.DirFileNameFunc {
return func(filePath, walkDir string) string {
return g.CIFilePath(filePath)
}
}
39 changes: 39 additions & 0 deletions internal/pkg/plugininstaller/ci/gitlabci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ci

import (
"path/filepath"

"github.com/devstream-io/devstream/pkg/util/file"
)

const (
ciGitLabType ciRepoType = "gitlab"
ciGitLabConfigLocation string = ".gitlab-ci.yml"
)

type GitLabCI struct {
}

func (g *GitLabCI) Type() ciRepoType {
return ciGitLabType
}

func (g *GitLabCI) CIFilePath(_ ...string) string {
return ciGitLabConfigLocation
}

func (g *GitLabCI) filterCIFilesFunc() file.DirFIleFilterFunc {
return func(filePath string, isDir bool) bool {
// not process dir
if isDir {
return false
}
return filepath.Base(filePath) == g.CIFilePath()
}
}

func (g *GitLabCI) getGitNameFunc() file.DirFileNameFunc {
return func(filePath, walkDir string) string {
return g.CIFilePath()
}
}
13 changes: 10 additions & 3 deletions internal/pkg/plugininstaller/ci/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
)

const (
defaultBranch = "feat-repo-ci-update"
defaultCommitMsg = "update ci config"
deleteCommitMsg = "delete ci files"
)

func PushCIFiles(options plugininstaller.RawOptions) error {
opts, err := NewOptions(options)
if err != nil {
Expand All @@ -25,7 +31,7 @@ func PushCIFiles(options plugininstaller.RawOptions) error {
return err
}
//4. push ci files to git repo
_, err = gitClient.PushLocalFileToRepo(&git.CommitInfo{
_, err = gitClient.PushLocalFilesToRepo(&git.CommitInfo{
CommitMsg: defaultCommitMsg,
CommitBranch: defaultBranch,
GitFileMap: gitMap,
Expand Down Expand Up @@ -53,8 +59,9 @@ func DeleteCIFiles(options plugininstaller.RawOptions) error {
}
//3. delete ci files from git repo
commitInfo := &git.CommitInfo{
GitFileMap: gitMap,
CommitMsg: deleteCommitMsg,
CommitMsg: deleteCommitMsg,
CommitBranch: defaultBranch,
GitFileMap: gitMap,
}
return gitClient.DeleteFiles(commitInfo)
}
35 changes: 35 additions & 0 deletions internal/pkg/plugininstaller/ci/jenkinsci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ci

import "github.com/devstream-io/devstream/pkg/util/file"

const (
ciJenkinsType ciRepoType = "jenkins"
ciJenkinsConfigLocation string = "Jenkinsfile"
)

type JenkinsCI struct {
}

func (j *JenkinsCI) Type() ciRepoType {
return ciJenkinsType
}

func (j *JenkinsCI) CIFilePath(_ ...string) string {
return ciJenkinsConfigLocation
}

func (j *JenkinsCI) filterCIFilesFunc() file.DirFIleFilterFunc {
return func(filePath string, isDir bool) bool {
// not process dir
if isDir {
return false
}
return filePath == ciJenkinsConfigLocation
}
}

func (j *JenkinsCI) getGitNameFunc() file.DirFileNameFunc {
return func(filePath, walkDir string) string {
return j.CIFilePath()
}
}
19 changes: 11 additions & 8 deletions internal/pkg/plugininstaller/ci/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func NewOptions(options plugininstaller.RawOptions) (*Options, error) {
return &opts, nil
}

func (c *CIConfig) CIClient() (ciClient CI) {
return NewCI(c.Type)
}

// getCIFile will generate ci files by config
func (opt *Options) buildGitMap() (gitMap git.GitFileContentMap, err error) {
ciConfig := opt.CIConfig
Expand All @@ -64,7 +68,7 @@ func (c *CIConfig) getFromURL(appName string) (git.GitFileContentMap, error) {
if err != nil {
return nil, err
}
fileName := getGitNameFunc(c.Type)("", path.Base(c.RemoteURL))
fileName := c.CIClient().getGitNameFunc()("", path.Base(c.RemoteURL))
gitFileMap[fileName] = []byte(content)
return gitFileMap, nil
}
Expand All @@ -75,18 +79,17 @@ func (c *CIConfig) getFromLocal(appName string) (git.GitFileContentMap, error) {
if err != nil {
return nil, err
}

ciClient := c.CIClient()
// process dir
if info.IsDir() {
return file.WalkDir(
c.LocalPath, filterCIFilesFunc(c.Type),
getGitNameFunc(c.Type), processCIFilesFunc(appName, c.Vars),
c.LocalPath, ciClient.filterCIFilesFunc(),
ciClient.getGitNameFunc(), processCIFilesFunc(appName, c.Vars),
)
}
// process file
gitFilePath := getCIFilePath(c.Type)
if c.Type == ciGitHubType {
gitFilePath = filepath.Join(gitFilePath, filepath.Base(c.LocalPath))
}
gitFilePath := ciClient.CIFilePath(filepath.Base(c.LocalPath))
content, err := template.New().FromLocalFile(c.LocalPath).SetDefaultRender(appName, c.Vars).Render()
if err != nil {
return nil, err
Expand All @@ -101,7 +104,7 @@ func (c *CIConfig) getFromContent(appName string) (git.GitFileContentMap, error)
if err != nil {
return nil, err
}
gitFileMap[getCIFilePath(c.Type)] = []byte(content)
gitFileMap[c.CIClient().CIFilePath(appName)] = []byte(content)
return gitFileMap, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/plugininstaller/ci/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func GetCIFileStatus(options plugininstaller.RawOptions) (statemanager.ResourceS
if err != nil {
return nil, err
}
fileLocation := getCIFilePath(opts.CIConfig.Type)
fileLocation := opts.CIConfig.CIClient().CIFilePath()
client, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo())
if err != nil {
return nil, err
Expand Down
41 changes: 0 additions & 41 deletions internal/pkg/plugininstaller/ci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,11 @@ package ci

import (
"os"
"path/filepath"
"strings"

"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/template"
)

// gitlab and jenkins is just a file, so we can just use filename
// github use directory, we shoud process this situation
func getCIFilePath(ciType ciRepoType) string {
switch ciType {
case ciGitLabType:
return ciGitLabConfigLocation
case ciGitHubType:
return ciGitHubWorkConfigLocation
case ciJenkinsType:
return ciJenkinsConfigLocation
}
return ""
}

func filterCIFilesFunc(ciType ciRepoType) file.DirFIleFilterFunc {
ciFileName := getCIFilePath(ciType)
return func(filePath string, isDir bool) bool {
// not process dir
if isDir {
return false
}
if ciType == ciGitHubType {
return strings.Contains(filePath, "workflows")
}
return filepath.Base(filePath) == ciFileName
}
}

func processCIFilesFunc(templateName string, vars map[string]interface{}) file.DirFileProcessFunc {
return func(filePath string) ([]byte, error) {
if len(vars) == 0 {
Expand All @@ -49,14 +19,3 @@ func processCIFilesFunc(templateName string, vars map[string]interface{}) file.D
return []byte(renderContent), nil
}
}

func getGitNameFunc(ciType ciRepoType) file.DirFileNameFunc {
ciFilePath := getCIFilePath(ciType)
return func(filePath, walkDir string) string {
fileBaseName := filepath.Base(filePath)
if ciType == ciGitHubType {
return filepath.Join(ciFilePath, fileBaseName)
}
return ciFilePath
}
}
8 changes: 4 additions & 4 deletions internal/pkg/plugininstaller/ci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("ci walkDir methods", func() {
})
It("should return false", func() {
for _, tt := range testCases {
Expect(filterCIFilesFunc(tt.ciType)(tt.filePath, tt.isDir)).Should(BeFalse())
Expect(NewCI(tt.ciType).filterCIFilesFunc()(tt.filePath, tt.isDir)).Should(BeFalse())
}
})
})
Expand All @@ -44,7 +44,7 @@ var _ = Describe("ci walkDir methods", func() {
})
It("should return true", func() {
for _, tt := range testCases {
Expect(filterCIFilesFunc(tt.ciType)(tt.filePath, tt.isDir)).Should(BeTrue())
Expect(NewCI(tt.ciType).filterCIFilesFunc()(tt.filePath, tt.isDir)).Should(BeTrue())
}
})
})
Expand Down Expand Up @@ -89,7 +89,7 @@ var _ = Describe("ci walkDir methods", func() {
testCaseData = &testCase{"workflows/pr.yaml", ciRepoType("github"), false}
})
It("should return github workflows path", func() {
result := getGitNameFunc(testCaseData.ciType)(testCaseData.filePath, "workflows")
result := NewCI(testCaseData.ciType).getGitNameFunc()(testCaseData.filePath, "workflows")
Expect(result).Should(Equal(".github/workflows/pr.yaml"))
})
})
Expand All @@ -98,7 +98,7 @@ var _ = Describe("ci walkDir methods", func() {
testCaseData = &testCase{"work/Jenkinsfile", ciRepoType("jenkins"), false}
})
It("should return github workflows path", func() {
result := getGitNameFunc(testCaseData.ciType)(testCaseData.filePath, "")
result := NewCI(testCaseData.ciType).getGitNameFunc()(testCaseData.filePath, "")
Expect(result).Should(Equal("Jenkinsfile"))
})
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/file/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func WalkDir(
srcPath string, filterFunc DirFIleFilterFunc, fileNameFunc DirFileNameFunc, processFunc DirFileProcessFunc,
) (map[string][]byte, error) {
contentMap := make(map[string][]byte)
// 1. create temp dir for destination
if err := filepath.Walk(srcPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.Debugf("Walk error: %s.", err)
Expand All @@ -33,7 +32,7 @@ func WalkDir(
return nil
}

// if file endswith tpl, render this file, else copy this file directly
// if file ends-with tpl, render this file, else copy this file directly
dstFileName := fileNameFunc(path, srcPath)
content, err := processFunc(path)
if err != nil {
Expand Down
Loading