diff --git a/internal/pkg/plugin/githubactions/golang/workflow.go b/internal/pkg/plugin/githubactions/golang/workflow.go index 4d564f6e6..e1cfe6e49 100644 --- a/internal/pkg/plugin/githubactions/golang/workflow.go +++ b/internal/pkg/plugin/githubactions/golang/workflow.go @@ -2,7 +2,7 @@ package golang import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller/github" - githubCommon "github.com/devstream-io/devstream/pkg/util/github" + githubCommon "github.com/devstream-io/devstream/pkg/util/scm/github" ) var workflows = []*githubCommon.Workflow{ diff --git a/internal/pkg/plugin/githubactions/nodejs/workflow.go b/internal/pkg/plugin/githubactions/nodejs/workflow.go index 173a840a3..49d211c47 100644 --- a/internal/pkg/plugin/githubactions/nodejs/workflow.go +++ b/internal/pkg/plugin/githubactions/nodejs/workflow.go @@ -2,7 +2,7 @@ package nodejs import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller/github" - githubCommon "github.com/devstream-io/devstream/pkg/util/github" + githubCommon "github.com/devstream-io/devstream/pkg/util/scm/github" ) var workflows = []*githubCommon.Workflow{ diff --git a/internal/pkg/plugin/githubactions/python/workflow.go b/internal/pkg/plugin/githubactions/python/workflow.go index f60c5165a..aad6a076b 100644 --- a/internal/pkg/plugin/githubactions/python/workflow.go +++ b/internal/pkg/plugin/githubactions/python/workflow.go @@ -2,7 +2,7 @@ package python import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller/github" - githubCommon "github.com/devstream-io/devstream/pkg/util/github" + githubCommon "github.com/devstream-io/devstream/pkg/util/scm/github" ) var workflows = []*githubCommon.Workflow{ diff --git a/internal/pkg/plugin/gitlabci/golang/golang.go b/internal/pkg/plugin/gitlabci/golang/golang.go index 0b5abd2ea..2eed44a0e 100644 --- a/internal/pkg/plugin/gitlabci/golang/golang.go +++ b/internal/pkg/plugin/gitlabci/golang/golang.go @@ -3,7 +3,7 @@ package golang import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller" "github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci" - "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) func setCIContent(options plugininstaller.RawOptions) (plugininstaller.RawOptions, error) { diff --git a/internal/pkg/plugin/gitlabci/java/create.go b/internal/pkg/plugin/gitlabci/java/create.go index f29397824..ca44923dc 100644 --- a/internal/pkg/plugin/gitlabci/java/create.go +++ b/internal/pkg/plugin/gitlabci/java/create.go @@ -5,8 +5,8 @@ import ( "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func Create(options map[string]interface{}) (map[string]interface{}, error) { diff --git a/internal/pkg/plugin/gitlabci/java/delete.go b/internal/pkg/plugin/gitlabci/java/delete.go index f22033552..aba4c42d3 100644 --- a/internal/pkg/plugin/gitlabci/java/delete.go +++ b/internal/pkg/plugin/gitlabci/java/delete.go @@ -5,8 +5,8 @@ import ( "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func Delete(options map[string]interface{}) (bool, error) { diff --git a/internal/pkg/plugin/gitlabci/java/options.go b/internal/pkg/plugin/gitlabci/java/options.go index aaecbbc35..89556051d 100644 --- a/internal/pkg/plugin/gitlabci/java/options.go +++ b/internal/pkg/plugin/gitlabci/java/options.go @@ -8,8 +8,8 @@ import ( "github.com/spf13/viper" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) type RegistryType string diff --git a/internal/pkg/plugin/gitlabci/java/update.go b/internal/pkg/plugin/gitlabci/java/update.go index fe13d9d71..32fea9d32 100644 --- a/internal/pkg/plugin/gitlabci/java/update.go +++ b/internal/pkg/plugin/gitlabci/java/update.go @@ -5,8 +5,8 @@ import ( "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func Update(options map[string]interface{}) (map[string]interface{}, error) { diff --git a/internal/pkg/plugin/jiragithub/create.go b/internal/pkg/plugin/jiragithub/create.go index 9562ab46b..92b610426 100644 --- a/internal/pkg/plugin/jiragithub/create.go +++ b/internal/pkg/plugin/jiragithub/create.go @@ -5,9 +5,9 @@ import ( "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) // Create sets up jira-github-integ workflows. diff --git a/internal/pkg/plugin/jiragithub/delete.go b/internal/pkg/plugin/jiragithub/delete.go index 62c524766..8042d5d08 100644 --- a/internal/pkg/plugin/jiragithub/delete.go +++ b/internal/pkg/plugin/jiragithub/delete.go @@ -5,9 +5,9 @@ import ( "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) // Delete remove jira-github-integ workflows. diff --git a/internal/pkg/plugin/jiragithub/helper.go b/internal/pkg/plugin/jiragithub/helper.go index 9e780a832..8a3c89efc 100644 --- a/internal/pkg/plugin/jiragithub/helper.go +++ b/internal/pkg/plugin/jiragithub/helper.go @@ -3,7 +3,7 @@ package jiragithub import ( "github.com/spf13/viper" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) func setRepoSecrets(gitHubClient *github.Client) error { diff --git a/internal/pkg/plugin/jiragithub/read.go b/internal/pkg/plugin/jiragithub/read.go index 459763433..767ff203f 100644 --- a/internal/pkg/plugin/jiragithub/read.go +++ b/internal/pkg/plugin/jiragithub/read.go @@ -5,9 +5,9 @@ import ( "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) // Read get jira-github-integ workflows. diff --git a/internal/pkg/plugin/jiragithub/render.go b/internal/pkg/plugin/jiragithub/render.go index 019e57073..754d83dc4 100644 --- a/internal/pkg/plugin/jiragithub/render.go +++ b/internal/pkg/plugin/jiragithub/render.go @@ -3,7 +3,7 @@ package jiragithub import ( "github.com/mitchellh/mapstructure" - github "github.com/devstream-io/devstream/pkg/util/github" + github "github.com/devstream-io/devstream/pkg/util/scm/github" "github.com/devstream-io/devstream/pkg/util/template" ) diff --git a/internal/pkg/plugin/jiragithub/update.go b/internal/pkg/plugin/jiragithub/update.go index 0daec22b4..5aa29044d 100644 --- a/internal/pkg/plugin/jiragithub/update.go +++ b/internal/pkg/plugin/jiragithub/update.go @@ -5,9 +5,9 @@ import ( "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) // Update remove and set up jira-github-integ workflows. diff --git a/internal/pkg/plugin/jiragithub/workflow.go b/internal/pkg/plugin/jiragithub/workflow.go index 15b5aaa0d..a937a8725 100644 --- a/internal/pkg/plugin/jiragithub/workflow.go +++ b/internal/pkg/plugin/jiragithub/workflow.go @@ -1,7 +1,7 @@ package jiragithub import ( - github "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) const ( diff --git a/internal/pkg/plugin/trellogithub/github.go b/internal/pkg/plugin/trellogithub/github.go index 7863d1bf2..784d4012e 100644 --- a/internal/pkg/plugin/trellogithub/github.go +++ b/internal/pkg/plugin/trellogithub/github.go @@ -5,8 +5,8 @@ import ( "github.com/spf13/viper" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) // VerifyWorkflows get the workflows with names "wf1.yml", "wf2.yml", then: diff --git a/internal/pkg/plugin/trellogithub/read.go b/internal/pkg/plugin/trellogithub/read.go index 5b0fb7c6f..0bfdc658a 100644 --- a/internal/pkg/plugin/trellogithub/read.go +++ b/internal/pkg/plugin/trellogithub/read.go @@ -1,8 +1,8 @@ package trellogithub import ( - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) func Read(options map[string]interface{}) (map[string]interface{}, error) { diff --git a/internal/pkg/plugin/trellogithub/trellogithub.go b/internal/pkg/plugin/trellogithub/trellogithub.go index 5861a2cdf..f54e72a20 100644 --- a/internal/pkg/plugin/trellogithub/trellogithub.go +++ b/internal/pkg/plugin/trellogithub/trellogithub.go @@ -5,13 +5,12 @@ import ( "fmt" mapset "github.com/deckarep/golang-set/v2" - "github.com/mitchellh/mapstructure" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" "github.com/devstream-io/devstream/pkg/util/mapz" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) type TrelloGithub struct { diff --git a/internal/pkg/plugin/trellogithub/workflow.go b/internal/pkg/plugin/trellogithub/workflow.go index 0980e332d..571ddbcf6 100644 --- a/internal/pkg/plugin/trellogithub/workflow.go +++ b/internal/pkg/plugin/trellogithub/workflow.go @@ -2,7 +2,7 @@ package trellogithub import ( "github.com/devstream-io/devstream/internal/pkg/plugin/trellogithub/trello" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) const ( diff --git a/internal/pkg/plugininstaller/ci/installer.go b/internal/pkg/plugininstaller/ci/installer.go index 82bbcd0be..b38b0d7f6 100644 --- a/internal/pkg/plugininstaller/ci/installer.go +++ b/internal/pkg/plugininstaller/ci/installer.go @@ -3,8 +3,10 @@ package ci import ( "errors" + "github.com/devstream-io/devstream/pkg/util/scm" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/internal/pkg/plugininstaller" - "github.com/devstream-io/devstream/pkg/util/git" ) func PushCIFiles(options plugininstaller.RawOptions) error { @@ -18,7 +20,7 @@ func PushCIFiles(options plugininstaller.RawOptions) error { return err } //3. init git client - gitClient, err := opts.ProjectRepo.NewClient() + gitClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo()) if err != nil { return err } @@ -45,7 +47,7 @@ func DeleteCIFiles(options plugininstaller.RawOptions) error { return errors.New("can't get valid ci files, please check your config") } //2. init git client - gitClient, err := opts.ProjectRepo.NewClient() + gitClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo()) if err != nil { return err } diff --git a/internal/pkg/plugininstaller/ci/option.go b/internal/pkg/plugininstaller/ci/option.go index e6066291a..e31ed1ff7 100644 --- a/internal/pkg/plugininstaller/ci/option.go +++ b/internal/pkg/plugininstaller/ci/option.go @@ -11,7 +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/git" + "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" ) diff --git a/internal/pkg/plugininstaller/ci/state.go b/internal/pkg/plugininstaller/ci/state.go index edfce9a07..df9a40214 100644 --- a/internal/pkg/plugininstaller/ci/state.go +++ b/internal/pkg/plugininstaller/ci/state.go @@ -3,6 +3,7 @@ package ci import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller" "github.com/devstream-io/devstream/internal/pkg/statemanager" + "github.com/devstream-io/devstream/pkg/util/scm" ) func GetCIFileStatus(options plugininstaller.RawOptions) (statemanager.ResourceState, error) { @@ -11,7 +12,7 @@ func GetCIFileStatus(options plugininstaller.RawOptions) (statemanager.ResourceS return nil, err } fileLocation := getCIFilePath(opts.CIConfig.Type) - client, err := opts.ProjectRepo.NewClient() + client, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo()) if err != nil { return nil, err } diff --git a/internal/pkg/plugininstaller/common/repo.go b/internal/pkg/plugininstaller/common/repo.go index 317b56588..6b720e9f5 100644 --- a/internal/pkg/plugininstaller/common/repo.go +++ b/internal/pkg/plugininstaller/common/repo.go @@ -7,16 +7,14 @@ import ( "strings" "github.com/devstream-io/devstream/pkg/util/file" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" - "github.com/devstream-io/devstream/pkg/util/gitlab" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) const ( defaultMainBranch = "main" - transitBranch = "init-with-devstream" - defaultCommitMsg = "init with devstream" ) // Repo is the repo info of github or gitlab @@ -44,18 +42,6 @@ func (d *Repo) BuildRepoRenderConfig() map[string]interface{} { return renderConfigMap } -// CreateGithubClient build github client connection info -func (d *Repo) NewClient() (git.ClientOperation, error) { - repoInfo := d.BuildRepoInfo() - switch d.RepoType { - case "github": - return github.NewClient(repoInfo) - case "gitlab": - return gitlab.NewClient(repoInfo) - } - return nil, fmt.Errorf("scaffolding not support repo destination: %s", d.RepoType) -} - // CreateAndRenderLocalRepo will download repo from source repo and render it locally func (d *Repo) CreateAndRenderLocalRepo(appName string, vars map[string]interface{}) (git.GitFileContentMap, error) { //TODO(steinliber) support gtlab later @@ -76,28 +62,6 @@ func (d *Repo) CreateAndRenderLocalRepo(appName string, vars map[string]interfac ) } -// This Func will push repo to remote base on repoType -func (d *Repo) CreateAndPush(gitMap git.GitFileContentMap) error { - client, err := d.NewClient() - if err != nil { - return err - } - commitInfo := &git.CommitInfo{ - CommitMsg: defaultCommitMsg, - CommitBranch: transitBranch, - GitFileMap: gitMap, - } - return git.PushInitRepo(client, commitInfo) -} - -func (d *Repo) Delete() error { - client, err := d.NewClient() - if err != nil { - return err - } - return client.DeleteRepo() -} - func (d *Repo) BuildRepoInfo() *git.RepoInfo { branch := d.Branch if branch == "" { @@ -111,31 +75,22 @@ func (d *Repo) BuildRepoInfo() *git.RepoInfo { NeedAuth: true, Branch: branch, BaseURL: d.BaseURL, + Type: d.RepoType, } } -func (d *Repo) getRepoDownloadURL() string { - repoInfo := d.BuildRepoInfo() - latestCodeZipfileDownloadURL := fmt.Sprintf( - github.DefaultLatestCodeZipfileDownloadUrlFormat, repoInfo.GetRepoOwner(), repoInfo.Repo, repoInfo.Branch, - ) - log.Debugf("LatestCodeZipfileDownloadUrl: %s.", latestCodeZipfileDownloadURL) - return latestCodeZipfileDownloadURL -} - +// GetRepoToken get repo token from env func (d *Repo) GetRepoToken() string { - var envKey string switch d.RepoType { case "github": - envKey = "GITHUB_TOKEN" + os.Getenv(github.TokenEnvKey) case "gitlab": - envKey = "GITLAB_TOKEN" - default: - return "" + os.Getenv(gitlab.TokenEnvKey) } - return os.Getenv(envKey) + return "" } +// BuildURL return url build from repo struct func (d *Repo) BuildURL() string { repoInfo := d.BuildRepoInfo() switch d.RepoType { @@ -154,33 +109,7 @@ func (d *Repo) BuildURL() string { } } -func (d *Repo) AddWebHook(webhookInfo *git.WebhookConfig) error { - client, err := d.NewClient() - if err != nil { - return err - } - return client.AddWebhook(webhookInfo) -} - -func (d *Repo) DeleteWebhook(webhookInfo *git.WebhookConfig) error { - client, err := d.NewClient() - if err != nil { - return err - } - return client.DeleteWebhook(webhookInfo) -} - -func (d *Repo) setDefaultBranch() { - if d.Branch == "" { - switch d.RepoType { - case "github": - d.Branch = "main" - case "gitlab": - d.Branch = "master" - } - } -} - +// NewRepoFromURL build repo struct from scm url func NewRepoFromURL(repoURL, branch string) (*Repo, error) { repo := &Repo{ Branch: branch, @@ -207,6 +136,29 @@ func NewRepoFromURL(repoURL, branch string) (*Repo, error) { } repo.Owner = pathPart[0] repo.Repo = pathPart[1] - repo.setDefaultBranch() + repo.Branch = repo.getBranch() return repo, nil } + +func (d *Repo) getBranch() string { + branch := d.Branch + if branch != "" { + return branch + } + switch d.RepoType { + case "github": + branch = "main" + case "gitlab": + branch = "master" + } + return branch +} + +func (d *Repo) getRepoDownloadURL() string { + repoInfo := d.BuildRepoInfo() + latestCodeZipfileDownloadURL := fmt.Sprintf( + github.DefaultLatestCodeZipfileDownloadUrlFormat, repoInfo.GetRepoOwner(), repoInfo.Repo, repoInfo.Branch, + ) + log.Debugf("LatestCodeZipfileDownloadUrl: %s.", latestCodeZipfileDownloadURL) + return latestCodeZipfileDownloadURL +} diff --git a/internal/pkg/plugininstaller/github/option.go b/internal/pkg/plugininstaller/github/option.go index 089cfb704..41f0af520 100644 --- a/internal/pkg/plugininstaller/github/option.go +++ b/internal/pkg/plugininstaller/github/option.go @@ -3,13 +3,12 @@ package github import ( "fmt" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/template" - "github.com/mitchellh/mapstructure" "github.com/devstream-io/devstream/internal/pkg/plugininstaller" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" + "github.com/devstream-io/devstream/pkg/util/template" ) const ( diff --git a/internal/pkg/plugininstaller/github/workflow.go b/internal/pkg/plugininstaller/github/workflow.go index 222aa4109..d8e777a07 100644 --- a/internal/pkg/plugininstaller/github/workflow.go +++ b/internal/pkg/plugininstaller/github/workflow.go @@ -2,7 +2,7 @@ package github import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) func BuildWorkFlowsWrapper(workflows []*github.Workflow) plugininstaller.MutableOperation { diff --git a/internal/pkg/plugininstaller/jenkins/installer.go b/internal/pkg/plugininstaller/jenkins/installer.go index 9113022a1..b178f9483 100644 --- a/internal/pkg/plugininstaller/jenkins/installer.go +++ b/internal/pkg/plugininstaller/jenkins/installer.go @@ -3,6 +3,7 @@ package jenkins import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm" ) func CreateOrUpdateJob(options plugininstaller.RawOptions) error { @@ -23,7 +24,11 @@ func CreateOrUpdateJob(options plugininstaller.RawOptions) error { return err } // 3. create repo webhook - return opts.ProjectRepo.AddWebHook(opts.buildWebhookInfo()) + scmClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo()) + if err != nil { + return err + } + return scmClient.AddWebhook(opts.buildWebhookInfo()) } func DeleteJob(options plugininstaller.RawOptions) error { @@ -41,7 +46,11 @@ func DeleteJob(options plugininstaller.RawOptions) error { return err } // delete repo webhook - return opts.ProjectRepo.DeleteWebhook(opts.buildWebhookInfo()) + scmClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo()) + if err != nil { + return err + } + return scmClient.DeleteWebhook(opts.buildWebhookInfo()) } func PreInstall(plugins []string, cascTemplate string) plugininstaller.BaseOperation { diff --git a/internal/pkg/plugininstaller/jenkins/option.go b/internal/pkg/plugininstaller/jenkins/option.go index 01ce9253b..2c35f9755 100644 --- a/internal/pkg/plugininstaller/jenkins/option.go +++ b/internal/pkg/plugininstaller/jenkins/option.go @@ -11,9 +11,9 @@ import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller" "github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci" "github.com/devstream-io/devstream/internal/pkg/plugininstaller/common" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/jenkins" "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" ) diff --git a/internal/pkg/plugininstaller/reposcaffolding/installer.go b/internal/pkg/plugininstaller/reposcaffolding/installer.go index 3c435e007..c18e9e0af 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/installer.go +++ b/internal/pkg/plugininstaller/reposcaffolding/installer.go @@ -2,6 +2,8 @@ package reposcaffolding import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller" + "github.com/devstream-io/devstream/pkg/util/scm" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) // InstallRepo will install repo by opts config @@ -20,7 +22,15 @@ func InstallRepo(options plugininstaller.RawOptions) error { } // 2. Push local repo to remote - return opts.DestinationRepo.CreateAndPush(gitMap) + client, err := scm.NewClient(opts.DestinationRepo.BuildRepoInfo()) + if err != nil { + return err + } + return scm.PushInitRepo(client, &git.CommitInfo{ + CommitMsg: scm.DefaultCommitMsg, + CommitBranch: scm.TransitBranch, + GitFileMap: gitMap, + }) } // DeleteRepo will delete repo by options @@ -31,5 +41,9 @@ func DeleteRepo(options plugininstaller.RawOptions) error { return err } - return opts.DestinationRepo.Delete() + client, err := scm.NewClient(opts.DestinationRepo.BuildRepoInfo()) + if err != nil { + return err + } + return client.DeleteRepo() } diff --git a/internal/pkg/plugininstaller/reposcaffolding/state.go b/internal/pkg/plugininstaller/reposcaffolding/state.go index 3ed0d3f9a..78987d949 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/state.go +++ b/internal/pkg/plugininstaller/reposcaffolding/state.go @@ -6,9 +6,9 @@ import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller" "github.com/devstream-io/devstream/internal/pkg/plugininstaller/common" "github.com/devstream-io/devstream/internal/pkg/statemanager" - "github.com/devstream-io/devstream/pkg/util/github" - "github.com/devstream-io/devstream/pkg/util/gitlab" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/github" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) func GetDynamicState(options plugininstaller.RawOptions) (statemanager.ResourceState, error) { diff --git a/internal/pkg/upgrade/upgrade.go b/internal/pkg/upgrade/upgrade.go index e71809d32..51220f1f6 100644 --- a/internal/pkg/upgrade/upgrade.go +++ b/internal/pkg/upgrade/upgrade.go @@ -10,9 +10,9 @@ import ( "github.com/tcnksm/go-input" "github.com/devstream-io/devstream/internal/pkg/version" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) const ( diff --git a/pkg/util/git/repo.go b/pkg/util/git/repo.go deleted file mode 100644 index 59a7955ef..000000000 --- a/pkg/util/git/repo.go +++ /dev/null @@ -1,38 +0,0 @@ -package git - -import ( - "github.com/devstream-io/devstream/pkg/util/log" -) - -type ClientOperation interface { - InitRepo() error - DeleteRepo() error - PushLocalFileToRepo(commitInfo *CommitInfo, checkUpdate bool) (bool, error) - GetLocationInfo(path string) ([]*RepoFileStatus, error) - DeleteFiles(commitInfo *CommitInfo) error - AddWebhook(webhookConfig *WebhookConfig) error - DeleteWebhook(webhookConfig *WebhookConfig) error -} - -func PushInitRepo(client ClientOperation, commitInfo *CommitInfo) error { - // 1. init repo - if err := client.InitRepo(); err != nil { - return err - } - - // if encounter rollout error, delete repo - var needRollBack bool - defer func() { - if !needRollBack { - return - } - // need to clean the repo created when reterr != nil - if err := client.DeleteRepo(); err != nil { - log.Errorf("failed to delete the repo: %s.", err) - } - }() - - // 2. push local path to repo - needRollBack, err := client.PushLocalFileToRepo(commitInfo, false) - return err -} diff --git a/pkg/util/scm/client.go b/pkg/util/scm/client.go new file mode 100644 index 000000000..15496df95 --- /dev/null +++ b/pkg/util/scm/client.go @@ -0,0 +1,58 @@ +package scm + +import ( + "fmt" + + "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" +) + +const ( + DefaultCommitMsg = "init with devstream" + TransitBranch = "init-with-devstream" +) + +func NewClient(repoInfo *git.RepoInfo) (ClientOperation, error) { + switch repoInfo.Type { + case "github": + return github.NewClient(repoInfo) + case "gitlab": + return gitlab.NewClient(repoInfo) + } + return nil, fmt.Errorf("scaffolding not support repo destination: %s", repoInfo.Type) +} + +type ClientOperation interface { + InitRepo() error + DeleteRepo() error + PushLocalFileToRepo(commitInfo *git.CommitInfo, checkUpdate bool) (bool, error) + GetLocationInfo(path string) ([]*git.RepoFileStatus, error) + DeleteFiles(commitInfo *git.CommitInfo) error + AddWebhook(webhookConfig *git.WebhookConfig) error + DeleteWebhook(webhookConfig *git.WebhookConfig) error +} + +func PushInitRepo(client ClientOperation, commitInfo *git.CommitInfo) error { + // 1. init repo + if err := client.InitRepo(); err != nil { + return err + } + + // if encounter rollout error, delete repo + var needRollBack bool + defer func() { + if !needRollBack { + return + } + // need to clean the repo created when reterr != nil + if err := client.DeleteRepo(); err != nil { + log.Errorf("failed to delete the repo: %s.", err) + } + }() + + // 2. push local path to repo + needRollBack, err := client.PushLocalFileToRepo(commitInfo, false) + return err +} diff --git a/pkg/util/git/repo_test.go b/pkg/util/scm/client_test.go similarity index 89% rename from pkg/util/git/repo_test.go rename to pkg/util/scm/client_test.go index b63c552df..0dc42e850 100644 --- a/pkg/util/git/repo_test.go +++ b/pkg/util/scm/client_test.go @@ -1,4 +1,4 @@ -package git_test +package scm_test import ( "errors" @@ -6,7 +6,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) type mockRepoStruct struct { @@ -65,7 +66,7 @@ var _ = Describe("PushInitRepo func", func() { } }) It("should return err", func() { - err = git.PushInitRepo(mockRepo, commitInfo) + err = scm.PushInitRepo(mockRepo, commitInfo) Expect(err).Error().Should(HaveOccurred()) Expect(err.Error()).Should(Equal("init error")) }) @@ -80,7 +81,7 @@ var _ = Describe("PushInitRepo func", func() { } }) It("should return err", func() { - err = git.PushInitRepo(mockRepo, commitInfo) + err = scm.PushInitRepo(mockRepo, commitInfo) Expect(err).Error().Should(HaveOccurred()) Expect(err.Error()).Should(Equal("push error")) Expect(mockRepo.deleteFuncIsRun).Should(BeFalse()) @@ -95,7 +96,7 @@ var _ = Describe("PushInitRepo func", func() { } }) It("should run DeleteRepo method", func() { - err = git.PushInitRepo(mockRepo, commitInfo) + err = scm.PushInitRepo(mockRepo, commitInfo) Expect(err).Error().Should(HaveOccurred()) Expect(err.Error()).Should(Equal("push error")) Expect(mockRepo.deleteFuncIsRun).Should(BeTrue()) diff --git a/pkg/util/git/commit.go b/pkg/util/scm/git/commit.go similarity index 100% rename from pkg/util/git/commit.go rename to pkg/util/scm/git/commit.go diff --git a/pkg/util/git/commit_test.go b/pkg/util/scm/git/commit_test.go similarity index 98% rename from pkg/util/git/commit_test.go rename to pkg/util/scm/git/commit_test.go index 395876499..571202d40 100644 --- a/pkg/util/git/commit_test.go +++ b/pkg/util/scm/git/commit_test.go @@ -7,7 +7,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) var _ = Describe("CommitInfo struct", func() { diff --git a/pkg/util/git/repo_suite_test.go b/pkg/util/scm/git/git_suite_test.go similarity index 100% rename from pkg/util/git/repo_suite_test.go rename to pkg/util/scm/git/git_suite_test.go diff --git a/pkg/util/git/repoinfo.go b/pkg/util/scm/git/repoinfo.go similarity index 97% rename from pkg/util/git/repoinfo.go rename to pkg/util/scm/git/repoinfo.go index cc96f8bd3..177059419 100644 --- a/pkg/util/git/repoinfo.go +++ b/pkg/util/scm/git/repoinfo.go @@ -9,6 +9,7 @@ type RepoInfo struct { Branch string Owner string Org string + Type string // used for gitlab Visibility string diff --git a/pkg/util/git/repoinfo_test.go b/pkg/util/scm/git/repoinfo_test.go similarity index 95% rename from pkg/util/git/repoinfo_test.go rename to pkg/util/scm/git/repoinfo_test.go index 00152784f..2186fe842 100644 --- a/pkg/util/git/repoinfo_test.go +++ b/pkg/util/scm/git/repoinfo_test.go @@ -6,7 +6,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) var _ = Describe("RepoInfo struct", func() { diff --git a/pkg/util/git/state.go b/pkg/util/scm/git/state.go similarity index 100% rename from pkg/util/git/state.go rename to pkg/util/scm/git/state.go diff --git a/pkg/util/git/state_test.go b/pkg/util/scm/git/state_test.go similarity index 94% rename from pkg/util/git/state_test.go rename to pkg/util/scm/git/state_test.go index 6ef2838fb..a5f0d0f19 100644 --- a/pkg/util/git/state_test.go +++ b/pkg/util/scm/git/state_test.go @@ -4,7 +4,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) var _ = Describe("RepoFileStatus struct", func() { diff --git a/pkg/util/git/webhook.go b/pkg/util/scm/git/webhook.go similarity index 100% rename from pkg/util/git/webhook.go rename to pkg/util/scm/git/webhook.go diff --git a/pkg/util/github/branch.go b/pkg/util/scm/github/branch.go similarity index 96% rename from pkg/util/github/branch.go rename to pkg/util/scm/github/branch.go index 9ffe49595..b63a758f1 100644 --- a/pkg/util/github/branch.go +++ b/pkg/util/scm/github/branch.go @@ -5,8 +5,8 @@ import ( "github.com/google/go-github/v42/github" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func (c *Client) NewBranch(newBranch string) (*github.Reference, error) { diff --git a/pkg/util/github/branch_test.go b/pkg/util/scm/github/branch_test.go similarity index 97% rename from pkg/util/github/branch_test.go rename to pkg/util/scm/github/branch_test.go index 556e81941..0cda76d95 100644 --- a/pkg/util/github/branch_test.go +++ b/pkg/util/scm/github/branch_test.go @@ -5,7 +5,7 @@ import ( "net/http" "testing" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) type newBranchTest struct { diff --git a/pkg/util/github/commit.go b/pkg/util/scm/github/commit.go similarity index 96% rename from pkg/util/github/commit.go rename to pkg/util/scm/github/commit.go index c2cbda77b..24e2f28ad 100644 --- a/pkg/util/github/commit.go +++ b/pkg/util/scm/github/commit.go @@ -4,9 +4,10 @@ import ( "errors" "fmt" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/google/go-github/v42/github" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" ) diff --git a/pkg/util/github/commit_test.go b/pkg/util/scm/github/commit_test.go similarity index 96% rename from pkg/util/github/commit_test.go rename to pkg/util/scm/github/commit_test.go index 17ae0ced5..9dc3bf759 100644 --- a/pkg/util/github/commit_test.go +++ b/pkg/util/scm/github/commit_test.go @@ -8,7 +8,7 @@ import ( "github.com/google/go-github/v42/github" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) type commitTest struct { diff --git a/pkg/util/github/download.go b/pkg/util/scm/github/download.go similarity index 100% rename from pkg/util/github/download.go rename to pkg/util/scm/github/download.go diff --git a/pkg/util/github/download_test.go b/pkg/util/scm/github/download_test.go similarity index 98% rename from pkg/util/github/download_test.go rename to pkg/util/scm/github/download_test.go index ce1b3a38b..107d94761 100644 --- a/pkg/util/github/download_test.go +++ b/pkg/util/scm/github/download_test.go @@ -10,8 +10,8 @@ import ( . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) var _ = Describe("DownloadAsset", func() { diff --git a/pkg/util/github/file.go b/pkg/util/scm/github/file.go similarity index 98% rename from pkg/util/github/file.go rename to pkg/util/scm/github/file.go index 1858adbcd..73a6b2f69 100644 --- a/pkg/util/github/file.go +++ b/pkg/util/scm/github/file.go @@ -6,8 +6,8 @@ import ( "github.com/google/go-github/v42/github" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func (c *Client) CreateFile(content []byte, filePath, targetBranch string) error { diff --git a/pkg/util/github/file_test.go b/pkg/util/scm/github/file_test.go similarity index 95% rename from pkg/util/github/file_test.go rename to pkg/util/scm/github/file_test.go index 6f5bc07d8..3de20d63d 100644 --- a/pkg/util/github/file_test.go +++ b/pkg/util/scm/github/file_test.go @@ -8,9 +8,9 @@ import ( . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) var _ = Describe("Github files methods", func() { diff --git a/pkg/util/github/github.go b/pkg/util/scm/github/github.go similarity index 80% rename from pkg/util/github/github.go rename to pkg/util/scm/github/github.go index 94f9989b2..394bfc9a4 100644 --- a/pkg/util/github/github.go +++ b/pkg/util/scm/github/github.go @@ -8,18 +8,17 @@ import ( "github.com/google/go-github/v42/github" "golang.org/x/oauth2" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) const ( - defaultWorkPath = ".github-workpath" - defaultCommitAuthor = "devstream" - defaultCommitAuthorEmail = "devstream@merico.dev" - // https://github.com/merico-dev/dtm-scaffolding-golang/archive/refs/heads/main.zip -> 302 -> - // https://codeload.github.com/merico-dev/dtm-scaffolding-golang/zip/refs/heads/main + defaultWorkPath = ".github-workpath" + defaultCommitAuthor = "devstream" + defaultCommitAuthorEmail = "devstream@merico.dev" + repoPlaceHolderFileName = ".placeholder" DefaultLatestCodeZipfileDownloadUrlFormat = "https://codeload.github.com/%s/%s/zip/refs/heads/%s" - // defaultLatestCodeZipfileName = "main-latest.zip" + TokenEnvKey = "GITHUB_TOKEN" ) var client *Client @@ -66,7 +65,7 @@ func NewClient(option *git.RepoInfo) (*Client, error) { // Don't use `token := viper.GetString("github_token")` here, // it will fail without calling `viper.BindEnv("github_token")` first. // os.Getenv() function is more clear and reasonable here. - token := os.Getenv("GITHUB_TOKEN") + token := os.Getenv(TokenEnvKey) if token == "" { // github_token works well as GITHUB_TOKEN. token = os.Getenv("github_token") diff --git a/pkg/util/github/github_suite_test.go b/pkg/util/scm/github/github_suite_test.go similarity index 85% rename from pkg/util/github/github_suite_test.go rename to pkg/util/scm/github/github_suite_test.go index b9a396e66..76fc0c24b 100644 --- a/pkg/util/github/github_suite_test.go +++ b/pkg/util/scm/github/github_suite_test.go @@ -11,9 +11,9 @@ import ( githubCommon "github.com/google/go-github/v42/github" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" - util_github "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" + util_github "github.com/devstream-io/devstream/pkg/util/scm/github" ) var ( diff --git a/pkg/util/github/github_test.go b/pkg/util/scm/github/github_test.go similarity index 98% rename from pkg/util/github/github_test.go rename to pkg/util/scm/github/github_test.go index 452739603..906224a97 100644 --- a/pkg/util/github/github_test.go +++ b/pkg/util/scm/github/github_test.go @@ -11,7 +11,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) const ( diff --git a/pkg/util/github/pullrequest.go b/pkg/util/scm/github/pullrequest.go similarity index 96% rename from pkg/util/github/pullrequest.go rename to pkg/util/scm/github/pullrequest.go index c8b34418f..7dca2b0bd 100644 --- a/pkg/util/github/pullrequest.go +++ b/pkg/util/scm/github/pullrequest.go @@ -3,10 +3,10 @@ package github import ( "fmt" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/log" - "github.com/google/go-github/v42/github" + + "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) type MergeMethod string diff --git a/pkg/util/github/pullrequest_test.go b/pkg/util/scm/github/pullrequest_test.go similarity index 97% rename from pkg/util/github/pullrequest_test.go rename to pkg/util/scm/github/pullrequest_test.go index 457fae8fb..126cee6fb 100644 --- a/pkg/util/github/pullrequest_test.go +++ b/pkg/util/scm/github/pullrequest_test.go @@ -8,8 +8,8 @@ import ( . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) var _ = Describe("NewPullRequest", func() { diff --git a/pkg/util/github/release.go b/pkg/util/scm/github/release.go similarity index 100% rename from pkg/util/github/release.go rename to pkg/util/scm/github/release.go diff --git a/pkg/util/github/release_test.go b/pkg/util/scm/github/release_test.go similarity index 96% rename from pkg/util/github/release_test.go rename to pkg/util/scm/github/release_test.go index f2eaefc5a..20d1fa6ec 100644 --- a/pkg/util/github/release_test.go +++ b/pkg/util/scm/github/release_test.go @@ -7,7 +7,7 @@ import ( "gotest.tools/assert/cmp" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) type releaseTest struct { diff --git a/pkg/util/github/repo.go b/pkg/util/scm/github/repo.go similarity index 87% rename from pkg/util/github/repo.go rename to pkg/util/scm/github/repo.go index e5ddee21f..31e2f96bd 100644 --- a/pkg/util/github/repo.go +++ b/pkg/util/scm/github/repo.go @@ -7,8 +7,8 @@ import ( "github.com/google/go-github/v42/github" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func (c *Client) CreateRepo(org, defaultBranch string) error { @@ -91,6 +91,16 @@ func (c *Client) PushLocalFileToRepo(commitInfo *git.CommitInfo, checkChange boo log.Debugf("Failed to merge commits: %s.", err) return true, err } + // 4. delete placeholder file + if err = c.DeleteFiles(&git.CommitInfo{ + CommitMsg: "delete placeholder file", + CommitBranch: c.Branch, + GitFileMap: git.GitFileContentMap{ + repoPlaceHolderFileName: []byte{}, + }, + }); err != nil { + log.Debugf("github delete init file failed: %s", err) + } return false, nil } @@ -103,9 +113,9 @@ func (c *Client) InitRepo() error { } log.Infof("The repo %s has been created.", c.Repo) - // upload a placeholder file to make repo not empty - if err := c.CreateFile([]byte(" "), ".placeholder", c.Branch); err != nil { - log.Debugf("Failed to add the first file: %s.", err) + // upload a placeholder file to make repo not empty + if err := c.CreateFile([]byte(" "), repoPlaceHolderFileName, c.Branch); err != nil { + log.Debugf("Failed to add the first file: %s. ", err) return err } log.Debugf("Added the .placeholder file.") diff --git a/pkg/util/github/repo_test.go b/pkg/util/scm/github/repo_test.go similarity index 87% rename from pkg/util/github/repo_test.go rename to pkg/util/scm/github/repo_test.go index dbcdd98bb..0d25ae260 100644 --- a/pkg/util/github/repo_test.go +++ b/pkg/util/scm/github/repo_test.go @@ -7,19 +7,19 @@ import ( "strconv" "strings" - "github.com/google/go-github/v42/github" + githubCommon "github.com/google/go-github/v42/github" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" - "github.com/devstream-io/devstream/pkg/util/git" - gh "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) var _ = Describe("Repo", func() { var ( s *ghttp.Server - rightClient, wrongClient *gh.Client + rightClient, wrongClient *github.Client owner, repoName, org = "o", "r", "or" commitInfo *git.CommitInfo ) @@ -40,9 +40,9 @@ var _ = Describe("Repo", func() { } BeforeEach(func() { s = ghttp.NewServer() - rightClient, _ = gh.NewClientWithOption(rightOpt, s.URL()) + rightClient, _ = github.NewClientWithOption(rightOpt, s.URL()) Expect(rightClient).NotTo(Equal(nil)) - wrongClient, _ = gh.NewClientWithOption(wrongOpt, s.URL()) + wrongClient, _ = github.NewClientWithOption(wrongOpt, s.URL()) Expect(wrongClient).NotTo(Equal(nil)) commitInfo = &git.CommitInfo{ CommitMsg: "test", @@ -109,18 +109,18 @@ var _ = Describe("Repo", func() { r, err := rightClient.DescribeRepo() Expect(err).NotTo(Succeed()) Expect(err.Error()).To(ContainSubstring(strconv.Itoa(http.StatusInternalServerError))) - var wantR *github.Repository + var wantR *githubCommon.Repository Expect(r).To(Equal(wantR)) }) It("GetRepoDescription with no error and status 200", func() { u := fmt.Sprintf("/repos/%v/%v", org, repoName) s.Reset() - s.RouteToHandler("GET", gh.BaseURLPath+u, func(w http.ResponseWriter, r *http.Request) { + s.RouteToHandler("GET", github.BaseURLPath+u, func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ``) }) r, err := rightClient.DescribeRepo() Expect(err).To(Succeed()) - var wantR *github.Repository = &github.Repository{} + var wantR *githubCommon.Repository = &githubCommon.Repository{} Expect(r).To(Equal(wantR)) }) }) @@ -164,7 +164,7 @@ var _ = Describe("Repo", func() { Expect(err.Error()).To(ContainSubstring(strconv.Itoa(http.StatusInternalServerError))) }) It("CreateFile with status 500", func() { - u := gh.BaseURLPath + fmt.Sprintf("/orgs/%v/repos", org) + u := github.BaseURLPath + fmt.Sprintf("/orgs/%v/repos", org) s.Reset() s.SetAllowUnhandledRequests(true) s.SetUnhandledRequestStatusCode(http.StatusInternalServerError) @@ -177,8 +177,8 @@ var _ = Describe("Repo", func() { Expect(err.Error()).To(ContainSubstring(strconv.Itoa(http.StatusInternalServerError))) }) It("CreateFile with status 200", func() { - u := gh.BaseURLPath + fmt.Sprintf("/orgs/%v/repos", org) - u2 := gh.BaseURLPath + fmt.Sprintf("/repos/%s/%s/contents/%s", org, repoName, ".placeholder") + u := github.BaseURLPath + fmt.Sprintf("/orgs/%v/repos", org) + u2 := github.BaseURLPath + fmt.Sprintf("/repos/%s/%s/contents/%s", org, repoName, ".placeholder") s.Reset() s.RouteToHandler("POST", u, func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{}`) @@ -201,7 +201,7 @@ var _ = Describe("Repo", func() { s.Reset() s.SetUnhandledRequestStatusCode(http.StatusInternalServerError) s.SetAllowUnhandledRequests(true) - s.RouteToHandler("GET", gh.BaseURLPath+u, func(w http.ResponseWriter, r *http.Request) { + s.RouteToHandler("GET", github.BaseURLPath+u, func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ``) }) err := rightClient.ProtectBranch(mainBranch) diff --git a/pkg/util/github/secrets.go b/pkg/util/scm/github/secrets.go similarity index 100% rename from pkg/util/github/secrets.go rename to pkg/util/scm/github/secrets.go diff --git a/pkg/util/github/secrets_test.go b/pkg/util/scm/github/secrets_test.go similarity index 97% rename from pkg/util/github/secrets_test.go rename to pkg/util/scm/github/secrets_test.go index 08ef14442..53de7da0e 100644 --- a/pkg/util/github/secrets_test.go +++ b/pkg/util/scm/github/secrets_test.go @@ -7,8 +7,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) var _ = Describe("Secrets", func() { diff --git a/pkg/util/github/workflow.go b/pkg/util/scm/github/workflow.go similarity index 100% rename from pkg/util/github/workflow.go rename to pkg/util/scm/github/workflow.go diff --git a/pkg/util/github/workflow_helper.go b/pkg/util/scm/github/workflow_helper.go similarity index 100% rename from pkg/util/github/workflow_helper.go rename to pkg/util/scm/github/workflow_helper.go diff --git a/pkg/util/github/workflow_helper_test.go b/pkg/util/scm/github/workflow_helper_test.go similarity index 95% rename from pkg/util/github/workflow_helper_test.go rename to pkg/util/scm/github/workflow_helper_test.go index 27184d329..e3d2e679f 100644 --- a/pkg/util/github/workflow_helper_test.go +++ b/pkg/util/scm/github/workflow_helper_test.go @@ -4,12 +4,11 @@ import ( "fmt" "net/http" - // "github.com/devstream-io/devstream/pkg/util/github" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) var _ = Describe("WorkflowHelper", func() { diff --git a/pkg/util/github/workflow_test.go b/pkg/util/scm/github/workflow_test.go similarity index 98% rename from pkg/util/github/workflow_test.go rename to pkg/util/scm/github/workflow_test.go index d8250ba42..f1a2fb5b3 100644 --- a/pkg/util/github/workflow_test.go +++ b/pkg/util/scm/github/workflow_test.go @@ -9,8 +9,8 @@ import ( . "github.com/onsi/gomega" "github.com/onsi/gomega/ghttp" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/github" ) var _ = Describe("Workflow", func() { diff --git a/pkg/util/gitlab/commit.go b/pkg/util/scm/gitlab/commit.go similarity index 97% rename from pkg/util/gitlab/commit.go rename to pkg/util/scm/gitlab/commit.go index 806a77c02..ed3722285 100644 --- a/pkg/util/gitlab/commit.go +++ b/pkg/util/scm/gitlab/commit.go @@ -5,7 +5,7 @@ import ( "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func (c *Client) PushLocalFileToRepo(commitInfo *git.CommitInfo, checkUpdate bool) (bool, error) { diff --git a/pkg/util/gitlab/commit_test.go b/pkg/util/scm/gitlab/commit_test.go similarity index 96% rename from pkg/util/gitlab/commit_test.go rename to pkg/util/scm/gitlab/commit_test.go index a3fd9639e..8935ca631 100644 --- a/pkg/util/gitlab/commit_test.go +++ b/pkg/util/scm/gitlab/commit_test.go @@ -10,8 +10,8 @@ import ( gitlabCommon "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) var _ = Describe("commit method", func() { diff --git a/pkg/util/gitlab/file.go b/pkg/util/scm/gitlab/file.go similarity index 97% rename from pkg/util/gitlab/file.go rename to pkg/util/scm/gitlab/file.go index 87a5e1b3e..6e84398a3 100644 --- a/pkg/util/gitlab/file.go +++ b/pkg/util/scm/gitlab/file.go @@ -6,8 +6,8 @@ import ( "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func (c *Client) DeleteFiles(commitInfo *git.CommitInfo) error { diff --git a/pkg/util/gitlab/file_test.go b/pkg/util/scm/gitlab/file_test.go similarity index 96% rename from pkg/util/gitlab/file_test.go rename to pkg/util/scm/gitlab/file_test.go index 41d60b039..c202363c2 100644 --- a/pkg/util/gitlab/file_test.go +++ b/pkg/util/scm/gitlab/file_test.go @@ -9,8 +9,8 @@ import ( "github.com/onsi/gomega/ghttp" gitlabCommon "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) var _ = Describe("CreateCommitInfo method", func() { diff --git a/pkg/util/gitlab/gitlab.go b/pkg/util/scm/gitlab/gitlab.go similarity index 83% rename from pkg/util/gitlab/gitlab.go rename to pkg/util/scm/gitlab/gitlab.go index 17adeed65..78280055c 100644 --- a/pkg/util/gitlab/gitlab.go +++ b/pkg/util/scm/gitlab/gitlab.go @@ -6,11 +6,12 @@ import ( "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) const ( DefaultGitlabHost = "https://gitlab.com" + TokenEnvKey = "GITLAB_TOKEN" ) type Client struct { @@ -19,7 +20,7 @@ type Client struct { } func NewClient(options *git.RepoInfo) (*Client, error) { - token := os.Getenv("GITLAB_TOKEN") + token := os.Getenv(TokenEnvKey) if token == "" { return nil, errors.New("failed to read GITLAB_TOKEN from environment variable") } diff --git a/pkg/util/gitlab/gitlab_suite_test.go b/pkg/util/scm/gitlab/gitlab_suite_test.go similarity index 100% rename from pkg/util/gitlab/gitlab_suite_test.go rename to pkg/util/scm/gitlab/gitlab_suite_test.go diff --git a/pkg/util/gitlab/gitlab_test.go b/pkg/util/scm/gitlab/gitlab_test.go similarity index 93% rename from pkg/util/gitlab/gitlab_test.go rename to pkg/util/scm/gitlab/gitlab_test.go index 2b6d4836b..5fecaa392 100644 --- a/pkg/util/gitlab/gitlab_test.go +++ b/pkg/util/scm/gitlab/gitlab_test.go @@ -7,8 +7,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) var _ = Describe("NewClient func", func() { diff --git a/pkg/util/gitlab/repo.go b/pkg/util/scm/gitlab/repo.go similarity index 98% rename from pkg/util/gitlab/repo.go rename to pkg/util/scm/gitlab/repo.go index e061b1d68..c42d482fe 100644 --- a/pkg/util/gitlab/repo.go +++ b/pkg/util/scm/gitlab/repo.go @@ -5,8 +5,8 @@ import ( "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" "github.com/devstream-io/devstream/pkg/util/log" + "github.com/devstream-io/devstream/pkg/util/scm/git" ) func (c *Client) InitRepo() error { diff --git a/pkg/util/gitlab/repo_test.go b/pkg/util/scm/gitlab/repo_test.go similarity index 96% rename from pkg/util/gitlab/repo_test.go rename to pkg/util/scm/gitlab/repo_test.go index ceed9330a..edd2671a2 100644 --- a/pkg/util/gitlab/repo_test.go +++ b/pkg/util/scm/gitlab/repo_test.go @@ -9,8 +9,8 @@ import ( "github.com/onsi/gomega/ghttp" gitlabCommon "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) var _ = Describe("repo methods", func() { diff --git a/pkg/util/gitlab/template.go b/pkg/util/scm/gitlab/template.go similarity index 100% rename from pkg/util/gitlab/template.go rename to pkg/util/scm/gitlab/template.go diff --git a/pkg/util/gitlab/template_test.go b/pkg/util/scm/gitlab/template_test.go similarity index 94% rename from pkg/util/gitlab/template_test.go rename to pkg/util/scm/gitlab/template_test.go index 3e3bd0fe4..935618579 100644 --- a/pkg/util/gitlab/template_test.go +++ b/pkg/util/scm/gitlab/template_test.go @@ -9,8 +9,8 @@ import ( "github.com/onsi/gomega/ghttp" gitlabCommon "github.com/xanzy/go-gitlab" - "github.com/devstream-io/devstream/pkg/util/git" - "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/scm/git" + "github.com/devstream-io/devstream/pkg/util/scm/gitlab" ) var _ = Describe("repo methods", func() { diff --git a/pkg/util/scm/scm_suite_test.go b/pkg/util/scm/scm_suite_test.go new file mode 100644 index 000000000..690788779 --- /dev/null +++ b/pkg/util/scm/scm_suite_test.go @@ -0,0 +1,13 @@ +package scm_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestPlanmanager(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Util scm Suite") +}