Skip to content

Commit

Permalink
refactor: some refactors and fix some typo
Browse files Browse the repository at this point in the history
Signed-off-by: Bird <aflybird0@gmail.com>
  • Loading branch information
aFlyBird0 committed Oct 10, 2022
1 parent 5f9bd07 commit f173d5d
Show file tree
Hide file tree
Showing 25 changed files with 209 additions and 105 deletions.
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
52 changes: 43 additions & 9 deletions internal/pkg/plugininstaller/ci/ci.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
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"
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
}

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
}
40 changes: 40 additions & 0 deletions internal/pkg/plugininstaller/ci/githubci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ci

import (
"path/filepath"
"strings"

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

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)
}
}
34 changes: 34 additions & 0 deletions internal/pkg/plugininstaller/ci/gitlabci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ci

import (
"path/filepath"

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

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()
}
}
7 changes: 4 additions & 3 deletions internal/pkg/plugininstaller/ci/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 +53,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)
}
30 changes: 30 additions & 0 deletions internal/pkg/plugininstaller/ci/jenkinsci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ci

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

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
5 changes: 3 additions & 2 deletions pkg/util/scm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewClient(repoInfo *git.RepoInfo) (ClientOperation, error) {
type ClientOperation interface {
InitRepo() error
DeleteRepo() error
PushLocalFileToRepo(commitInfo *git.CommitInfo, checkUpdate bool) (bool, error)
PushLocalFilesToRepo(commitInfo *git.CommitInfo, checkUpdate bool) (bool, error)
GetLocationInfo(path string) ([]*git.RepoFileStatus, error)
DeleteFiles(commitInfo *git.CommitInfo) error
AddWebhook(webhookConfig *git.WebhookConfig) error
Expand All @@ -54,7 +54,8 @@ func PushInitRepo(client ClientOperation, commitInfo *git.CommitInfo) error {
}()

// 2. push local path to repo
needRollBack, err := client.PushLocalFileToRepo(commitInfo, false)
var err error
needRollBack, err = client.PushLocalFilesToRepo(commitInfo, false)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/scm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (m *mockRepoStruct) InitRepo() error {
}
return nil
}
func (m *mockRepoStruct) PushLocalFileToRepo(commitInfo *git.CommitInfo, checkUpdate bool) (bool, error) {
func (m *mockRepoStruct) PushLocalFilesToRepo(commitInfo *git.CommitInfo, checkUpdate bool) (bool, error) {
if m.pushRaiseError {
return m.needRollBack, errors.New("push error")
}
Expand Down
Loading

0 comments on commit f173d5d

Please sign in to comment.