Skip to content

Commit

Permalink
Merge pull request #988 from steinliber/feat-pkg-generic-repo
Browse files Browse the repository at this point in the history
refactor: github and gitlab in pkg/util
  • Loading branch information
aFlyBird0 authored Aug 15, 2022
2 parents 04cfcef + 351a0fc commit e325ef7
Show file tree
Hide file tree
Showing 74 changed files with 1,379 additions and 1,091 deletions.
13 changes: 10 additions & 3 deletions internal/pkg/plugin/gitlabci/generic/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand Down Expand Up @@ -42,11 +42,18 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
}

// commit file
client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return nil, err
}
if err = client.CommitSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName, ciFileContentBytes.String()); err != nil {
_, err = client.PushLocalFileToRepo(&git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: ciFileContentBytes.Bytes(),
},
})
if err != nil {
return nil, err
}

Expand Down
11 changes: 8 additions & 3 deletions internal/pkg/plugin/gitlabci/generic/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -22,12 +22,17 @@ func Delete(options map[string]interface{}) (bool, error) {
return false, fmt.Errorf("opts are illegal")
}

client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return false, err
}
commitInfo := &git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{ciFileName: []byte("")},
}

if err = client.DeleteSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName); err != nil {
if err = client.DeleteFiles(commitInfo); err != nil {
return false, err
}

Expand Down
22 changes: 22 additions & 0 deletions internal/pkg/plugin/gitlabci/generic/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package generic

import (
"errors"
"strings"

"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/gitlab"
)

// Options is the struct for configurations of the gitlabci-generic plugin.
type Options struct {
PathWithNamespace string `validate:"required"`
Expand All @@ -8,3 +16,17 @@ type Options struct {
BaseURL string `validate:"omitempty,url"`
TemplateVariables map[string]interface{}
}

func (opts *Options) newGitlabClient() (*gitlab.Client, error) {
pathSplit := strings.Split(opts.PathWithNamespace, "/")
if len(pathSplit) != 2 {
return nil, errors.New("gitlabci generic not valid PathWithNamespace params")
}
repoInfo := &git.RepoInfo{
Owner: pathSplit[0],
Repo: pathSplit[1],
Branch: opts.Branch,
BaseURL: opts.BaseURL,
}
return gitlab.NewClient(repoInfo)
}
5 changes: 2 additions & 3 deletions internal/pkg/plugin/gitlabci/generic/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -22,12 +21,12 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) {
return nil, fmt.Errorf("opts are illegal")
}

client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return nil, err
}

exists, err := client.FileExists(opts.PathWithNamespace, opts.Branch, ciFileName)
exists, err := client.FileExists(ciFileName)
if err != nil {
return nil, err
}
Expand Down
13 changes: 10 additions & 3 deletions internal/pkg/plugin/gitlabci/generic/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand Down Expand Up @@ -42,12 +42,19 @@ func Update(options map[string]interface{}) (map[string]interface{}, error) {
}

// update file
client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return nil, err
}
commitInfo := &git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: ciFileContentBytes.Bytes(),
},
}
// the only difference between create and update
if err = client.UpdateSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName, ciFileContentBytes.String()); err != nil {
if err = client.UpdateFiles(commitInfo); err != nil {
return nil, err
}

Expand Down
13 changes: 10 additions & 3 deletions internal/pkg/plugin/gitlabci/golang/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -26,7 +26,7 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
return nil, fmt.Errorf("opts are illegal")
}

client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return nil, err
}
Expand All @@ -36,7 +36,14 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
return nil, err
}

if err = client.CommitSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName, ciFileContent); err != nil {
_, err = client.PushLocalFileToRepo(&git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: []byte(ciFileContent),
},
})
if err != nil {
return nil, err
}

Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/plugin/gitlabci/golang/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -24,12 +24,16 @@ func Delete(options map[string]interface{}) (bool, error) {
}
return false, fmt.Errorf("opts are illegal")
}
client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return false, err
}

if err = client.DeleteSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName); err != nil {
commitInfo := &git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{ciFileName: []byte("")},
}
if err = client.DeleteFiles(commitInfo); err != nil {
return false, err
}

Expand Down
22 changes: 22 additions & 0 deletions internal/pkg/plugin/gitlabci/golang/gitlabci.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package golang

import (
"errors"
"strings"

"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/gitlab"
)

const (
ciFileName string = ".gitlab-ci.yml"
commitMessage string = "managed by DevStream"
Expand All @@ -17,3 +25,17 @@ func buildState(opts *Options) map[string]interface{} {
"branch": opts.Branch,
}
}

func (opts *Options) newGitlabClient() (*gitlab.Client, error) {
pathSplit := strings.Split(opts.PathWithNamespace, "/")
if len(pathSplit) != 2 {
return nil, errors.New("gitlabci generic not valid PathWithNamespace params")
}
repoInfo := &git.RepoInfo{
Owner: pathSplit[0],
Repo: pathSplit[1],
Branch: opts.Branch,
BaseURL: opts.BaseURL,
}
return gitlab.NewClient(repoInfo)
}
5 changes: 2 additions & 3 deletions internal/pkg/plugin/gitlabci/golang/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -23,12 +22,12 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) {
}
return nil, fmt.Errorf("opts are illegal")
}
client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return nil, err
}

exists, err := client.FileExists(opts.PathWithNamespace, opts.Branch, ciFileName)
exists, err := client.FileExists(ciFileName)
if err != nil {
return nil, err
}
Expand Down
13 changes: 10 additions & 3 deletions internal/pkg/plugin/gitlabci/golang/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -26,7 +26,7 @@ func Update(options map[string]interface{}) (map[string]interface{}, error) {
return nil, fmt.Errorf("opts are illegal")
}

client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return nil, err
}
Expand All @@ -35,9 +35,16 @@ func Update(options map[string]interface{}) (map[string]interface{}, error) {
if err != nil {
return nil, err
}
commitInfo := &git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: []byte(ciFileContent),
},
}

// the only difference between "Create" and "Update"
if err = client.UpdateSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName, ciFileContent); err != nil {
if err = client.UpdateFiles(commitInfo); err != nil {
return nil, err
}

Expand Down
14 changes: 10 additions & 4 deletions internal/pkg/plugin/gitlabci/java/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand Down Expand Up @@ -33,12 +33,18 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
return nil, err
}

client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return nil, err
}

if err = client.CommitSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName, content); err != nil {
_, err = client.PushLocalFileToRepo(&git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: []byte(content),
},
})
if err != nil {
return nil, err
}

Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/plugin/gitlabci/java/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/gitlab"
"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -22,12 +22,16 @@ func Delete(options map[string]interface{}) (bool, error) {
return false, fmt.Errorf("opts are illegal")
}

client, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL))
client, err := opts.newGitlabClient()
if err != nil {
return false, err
}

if err = client.DeleteSingleFile(opts.PathWithNamespace, opts.Branch, commitMessage, ciFileName); err != nil {
commitInfo := &git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{ciFileName: []byte("")},
}
if err = client.DeleteFiles(commitInfo); err != nil {
return false, err
}
return false, nil
Expand Down
19 changes: 19 additions & 0 deletions internal/pkg/plugin/gitlabci/java/options.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package java

import (
"errors"
"fmt"
"html/template"
"strings"

"github.com/spf13/viper"

"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/gitlab"
)

type RegistryType string
Expand Down Expand Up @@ -148,3 +153,17 @@ func (d *Deploy) setup(opts *Options) {
}

}

func (opts *Options) newGitlabClient() (*gitlab.Client, error) {
pathSplit := strings.Split(opts.PathWithNamespace, "/")
if len(pathSplit) != 2 {
return nil, errors.New("gitlabci generic not valid PathWithNamespace params")
}
repoInfo := &git.RepoInfo{
Owner: pathSplit[0],
Repo: pathSplit[1],
Branch: opts.Branch,
BaseURL: opts.BaseURL,
}
return gitlab.NewClient(repoInfo)
}
Loading

0 comments on commit e325ef7

Please sign in to comment.