Skip to content

Commit

Permalink
feat: ci support update when ci file change
Browse files Browse the repository at this point in the history
Signed-off-by: Meng JiaFeng <jiafeng.meng@merico.dev>
  • Loading branch information
steinliber committed Oct 11, 2022
1 parent e28bbdf commit 9465bd9
Show file tree
Hide file tree
Showing 29 changed files with 409 additions and 363 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.PushLocalFilesToRepo(&git.CommitInfo{
_, err = client.PushFiles(&git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/plugin/gitlabci/java/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) {
return nil, err
}

exists, err := client.FileExists(ciFileName)
pathInfo, err := client.GetPathInfo(ciFileName)
if err != nil {
return nil, err
}

if !exists {
if len(pathInfo) == 0 {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/plugin/gitlabci/java/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Update(options map[string]interface{}) (map[string]interface{}, error) {
}

// the only difference between "Create" and "Update"
if err = client.UpdateFiles(commitInfo); err != nil {
if _, err = client.PushFiles(commitInfo, true); err != nil {
return nil, err
}

Expand Down
18 changes: 9 additions & 9 deletions internal/pkg/plugininstaller/ci/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

const (
defaultBranch = "feat-repo-ci-update"
defaultCommitMsg = "update ci config"
deleteCommitMsg = "delete ci files"
createCommitMsg = "update ci config"
deleteCommitMsg = "delete ci files"
// this variable is only used for github to fork a branch and create pr
defaultBranch = "feat-repo-ci-update"
)

func PushCIFiles(options plugininstaller.RawOptions) error {
Expand All @@ -31,10 +32,10 @@ func PushCIFiles(options plugininstaller.RawOptions) error {
return err
}
//4. push ci files to git repo
_, err = gitClient.PushLocalFilesToRepo(&git.CommitInfo{
CommitMsg: defaultCommitMsg,
CommitBranch: defaultBranch,
_, err = gitClient.PushFiles(&git.CommitInfo{
CommitMsg: createCommitMsg,
GitFileMap: gitMap,
CommitBranch: defaultBranch,
}, true)
return err
}
Expand All @@ -59,9 +60,8 @@ func DeleteCIFiles(options plugininstaller.RawOptions) error {
}
//3. delete ci files from git repo
commitInfo := &git.CommitInfo{
CommitMsg: deleteCommitMsg,
CommitBranch: defaultBranch,
GitFileMap: gitMap,
CommitMsg: deleteCommitMsg,
GitFileMap: gitMap,
}
return gitClient.DeleteFiles(commitInfo)
}
13 changes: 7 additions & 6 deletions internal/pkg/plugininstaller/ci/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci/server"
"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"
Expand All @@ -18,7 +19,7 @@ import (
)

type CIConfig struct {
Type ciRepoType `validate:"oneof=jenkins github gitlab" mapstructure:"type"`
Type server.CIServerType `validate:"oneof=jenkins github gitlab" mapstructure:"type"`
LocalPath string `mapstructure:"localPath"`
RemoteURL string `mapstructure:"remoteURL"`
Content string `mapstructure:"content"`
Expand All @@ -38,8 +39,8 @@ func NewOptions(options plugininstaller.RawOptions) (*Options, error) {
return &opts, nil
}

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

// getCIFile will generate ci files by config
Expand Down Expand Up @@ -68,7 +69,7 @@ func (c *CIConfig) getFromURL(appName string) (git.GitFileContentMap, error) {
if err != nil {
return nil, err
}
fileName := c.CIClient().getGitNameFunc()("", path.Base(c.RemoteURL))
fileName := c.CIClient().GetGitNameFunc()("", path.Base(c.RemoteURL))
gitFileMap[fileName] = []byte(content)
return gitFileMap, nil
}
Expand All @@ -84,8 +85,8 @@ func (c *CIConfig) getFromLocal(appName string) (git.GitFileContentMap, error) {
// process dir
if info.IsDir() {
return file.WalkDir(
c.LocalPath, ciClient.filterCIFilesFunc(),
ciClient.getGitNameFunc(), processCIFilesFunc(appName, c.Vars),
c.LocalPath, ciClient.FilterCIFilesFunc(),
ciClient.GetGitNameFunc(), processCIFilesFunc(appName, c.Vars),
)
}
// process file
Expand Down
23 changes: 12 additions & 11 deletions internal/pkg/plugininstaller/ci/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/onsi/gomega/ghttp"

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

Expand Down Expand Up @@ -66,7 +67,7 @@ var _ = Describe("Options struct", func() {
)
BeforeEach(func() {
tempDir := GinkgoT().TempDir()
localDir = fmt.Sprintf("%s/%s", tempDir, ciGitHubWorkConfigLocation)
localDir = fmt.Sprintf("%s/%s", tempDir, ".github/workflows")
err := os.MkdirAll(localDir, os.ModePerm)
Expect(err).Error().ShouldNot(HaveOccurred())
tempFile, err := os.CreateTemp(localDir, "testFile")
Expand All @@ -79,15 +80,15 @@ var _ = Describe("Options struct", func() {
When("LocalPath is directory", func() {
BeforeEach(func() {
opts.CIConfig = &CIConfig{
Type: ciGitHubType,
Type: "github",
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))
expectedKey := fmt.Sprintf("%s/%s", ".github/workflows", path.Base(localFile))
v, ok := gitMap[expectedKey]
Expect(ok).Should(BeTrue())
Expect(v).Should(Equal(testContent))
Expand All @@ -96,15 +97,15 @@ var _ = Describe("Options struct", func() {
When("localPath is file", func() {
BeforeEach(func() {
opts.CIConfig = &CIConfig{
Type: ciGitHubType,
Type: "github",
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))
expectedKey := fmt.Sprintf("%s/%s", ".github/workflows", path.Base(localFile))
v, ok := gitMap[expectedKey]
Expect(ok).Should(BeTrue())
Expect(v).Should(Equal(testContent))
Expand All @@ -118,15 +119,15 @@ var _ = Describe("Options struct", func() {
BeforeEach(func() {
testContent = "testJenkins"
opts.CIConfig = &CIConfig{
Type: ciJenkinsType,
Type: "jenkins",
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]
v, ok := gitMap["Jenkinsfile"]
Expect(ok).Should(BeTrue())
Expect(v).Should(Equal([]byte(testContent)))
})
Expand All @@ -145,7 +146,7 @@ var _ = Describe("Options struct", func() {
fmt.Fprint(w, testContent)
})
opts.CIConfig = &CIConfig{
Type: ciGitLabType,
Type: "gitlab",
RemoteURL: s.URL(),
Vars: map[string]interface{}{
"App": templateVal,
Expand All @@ -159,7 +160,7 @@ var _ = Describe("Options struct", func() {
gitMap, err := opts.buildGitMap()
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(gitMap)).Should(Equal(1))
v, ok := gitMap[ciGitLabConfigLocation]
v, ok := gitMap[".gitlab-ci.yml"]
Expect(ok).Should(BeTrue())
Expect(string(v)).Should(Equal(fmt.Sprintf("testGitlabCI %s", templateVal)))
})
Expand All @@ -173,7 +174,7 @@ var _ = Describe("Options struct", func() {
BeforeEach(func() {
opts = &Options{}
defaultCIConfig := &CIConfig{
Type: ciGitHubType,
Type: "github",
RemoteURL: "http://www.test.com",
}
defaultRepo := &common.Repo{
Expand Down Expand Up @@ -210,7 +211,7 @@ var _ = Describe("Options struct", func() {
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.CIConfig.Type).Should(Equal(server.CIServerType("github")))
Expect(opts.ProjectRepo.Branch).Should(Equal("new_branch"))
Expect(opts.ProjectRepo.Repo).Should(Equal("test_repo"))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ci
package server

import (
"path/filepath"
Expand All @@ -8,14 +8,14 @@ import (
)

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

type GitHubCI struct {
}

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

Expand All @@ -28,7 +28,7 @@ func (g *GitHubCI) CIFilePath(subFilename ...string) string {
return filepath.Join(ciGitHubWorkConfigLocation, filepath.Base(subFilename[0]))
}

func (g *GitHubCI) filterCIFilesFunc() file.DirFIleFilterFunc {
func (g *GitHubCI) FilterCIFilesFunc() file.DirFIleFilterFunc {
return func(filePath string, isDir bool) bool {
// not process dir
if isDir {
Expand All @@ -38,7 +38,7 @@ func (g *GitHubCI) filterCIFilesFunc() file.DirFIleFilterFunc {
}
}

func (g *GitHubCI) getGitNameFunc() file.DirFileNameFunc {
func (g *GitHubCI) GetGitNameFunc() file.DirFileNameFunc {
return func(filePath, walkDir string) string {
return g.CIFilePath(filePath)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ci
package server

import (
"path/filepath"
Expand All @@ -7,22 +7,22 @@ import (
)

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

type GitLabCI struct {
}

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

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

func (g *GitLabCI) filterCIFilesFunc() file.DirFIleFilterFunc {
func (g *GitLabCI) FilterCIFilesFunc() file.DirFIleFilterFunc {
return func(filePath string, isDir bool) bool {
// not process dir
if isDir {
Expand All @@ -32,7 +32,7 @@ func (g *GitLabCI) filterCIFilesFunc() file.DirFIleFilterFunc {
}
}

func (g *GitLabCI) getGitNameFunc() file.DirFileNameFunc {
func (g *GitLabCI) GetGitNameFunc() file.DirFileNameFunc {
return func(filePath, walkDir string) string {
return g.CIFilePath()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package ci
package server

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

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

type JenkinsCI struct {
}

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

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

func (j *JenkinsCI) filterCIFilesFunc() file.DirFIleFilterFunc {
func (j *JenkinsCI) FilterCIFilesFunc() file.DirFIleFilterFunc {
return func(filePath string, isDir bool) bool {
// not process dir
if isDir {
Expand All @@ -28,7 +28,7 @@ func (j *JenkinsCI) filterCIFilesFunc() file.DirFIleFilterFunc {
}
}

func (j *JenkinsCI) getGitNameFunc() file.DirFileNameFunc {
func (j *JenkinsCI) GetGitNameFunc() file.DirFileNameFunc {
return func(filePath, walkDir string) string {
return j.CIFilePath()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package ci
package server

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

type ciRepoType string
type CIServerType string

type CI interface {
type CIServerOptions interface {
// Type return ci type
Type() ciRepoType
Type() CIServerType
// 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
FilterCIFilesFunc() file.DirFIleFilterFunc
// getGitNameFunc returns a function to transform file path to git name of ci config file
getGitNameFunc() file.DirFileNameFunc
GetGitNameFunc() file.DirFileNameFunc
}

func NewCI(ciType ciRepoType) CI {
func NewCIServer(ciType CIServerType) CIServerOptions {
// there are no validation for ciType
// because we have already validated it by `validate` flag in CIConfig.Type
switch ciType {
Expand Down
13 changes: 13 additions & 0 deletions internal/pkg/plugininstaller/ci/server/server_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package server_test

import (
"testing"

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

func TestCommon(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "PluginInstaller Ci Server Suite")
}
Loading

0 comments on commit 9465bd9

Please sign in to comment.