Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: gitlab-generic and gitlab-golang use installer #1024

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/pkg/plugin/cigeneric/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ func Delete(options map[string]interface{}) (bool, error) {
return false, err
}
return true, nil

}
61 changes: 16 additions & 45 deletions internal/pkg/plugin/gitlabci/generic/create.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,29 @@
package generic

import (
"fmt"

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/internal/pkg/plugin/gitlabci"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/template"
)

func Create(options map[string]interface{}) (map[string]interface{}, error) {
var opts Options
if err := mapstructure.Decode(options, &opts); err != nil {
return nil, err
}

if errs := validate(&opts); len(errs) != 0 {
for _, e := range errs {
log.Errorf("Options error: %s.", e)
}
return nil, fmt.Errorf("opts are illegal")
}

// download template
ciTemplateString, err := download(opts.TemplateURL)
if err != nil {
return nil, err
}

// render template
ciFileContent, err := template.New().
FromContent(ciTemplateString).
SetDefaultRender("ci", opts.TemplateVariables).Render()

if err != nil {
return nil, fmt.Errorf("execute template error: %s", err)
operator := &plugininstaller.Operator{
PreExecuteOperations: plugininstaller.PreExecuteOperations{
ci.SetDefaultConfig(gitlabci.DefaultCIOptions),
ci.Validate,
},
ExecuteOperations: plugininstaller.ExecuteOperations{
ci.PushCIFiles,
},
GetStateOperation: ci.GetCIFileStatus,
}

// commit file
client, err := opts.newGitlabClient()
// Execute all Operations in Operator
status, err := operator.Execute(plugininstaller.RawOptions(options))
if err != nil {
return nil, err
}
_, err = client.PushLocalFileToRepo(&git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: []byte(ciFileContent),
},
}, false)
if err != nil {
return nil, err
}

return buildState(&opts), nil
log.Debugf("Return map: %v", status)
return status, nil
}
40 changes: 13 additions & 27 deletions internal/pkg/plugin/gitlabci/generic/delete.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
package generic

import (
"fmt"

"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/internal/pkg/plugin/gitlabci"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci"
)

func Delete(options map[string]interface{}) (bool, error) {
var opts Options
if err := mapstructure.Decode(options, &opts); err != nil {
return false, err
}

if errs := validate(&opts); len(errs) != 0 {
for _, e := range errs {
log.Errorf("Options error: %s.", e)
}
return false, fmt.Errorf("opts are illegal")
operator := &plugininstaller.Operator{
PreExecuteOperations: plugininstaller.PreExecuteOperations{
ci.SetDefaultConfig(gitlabci.DefaultCIOptions),
ci.Validate,
},
ExecuteOperations: plugininstaller.ExecuteOperations{
ci.DeleteCIFiles,
},
}

client, err := opts.newGitlabClient()
// Execute all Operations in Operator
_, err := operator.Execute(plugininstaller.RawOptions(options))
if err != nil {
return false, err
}
commitInfo := &git.CommitInfo{
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{ciFileName: []byte("")},
}

if err = client.DeleteFiles(commitInfo); err != nil {
return false, err
}

return true, nil
}
38 changes: 0 additions & 38 deletions internal/pkg/plugin/gitlabci/generic/generic.go
Original file line number Diff line number Diff line change
@@ -1,39 +1 @@
package generic

import (
"fmt"
"io"
"net/http"
)

const (
ciFileName string = ".gitlab-ci.yml"
commitMessage string = "managed by DevStream"
)

func buildState(opts *Options) map[string]interface{} {
return map[string]interface{}{
"pathWithNamespace": opts.PathWithNamespace,
"branch": opts.Branch,
"templateURL": opts.TemplateURL,
"templateVariables": opts.TemplateVariables,
}
}

func download(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to download template: %s %s", url, resp.Status)
}

resBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(resBytes), nil
}
32 changes: 0 additions & 32 deletions internal/pkg/plugin/gitlabci/generic/options.go

This file was deleted.

37 changes: 12 additions & 25 deletions internal/pkg/plugin/gitlabci/generic/read.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
package generic

import (
"fmt"

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/plugin/gitlabci"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci"
"github.com/devstream-io/devstream/pkg/util/log"
)

func Read(options map[string]interface{}) (map[string]interface{}, error) {
var opts Options
if err := mapstructure.Decode(options, &opts); err != nil {
return nil, err
operator := &plugininstaller.Operator{
PreExecuteOperations: plugininstaller.PreExecuteOperations{
ci.SetDefaultConfig(gitlabci.DefaultCIOptions),
ci.Validate,
},
GetStateOperation: ci.GetCIFileStatus,
}

if errs := validate(&opts); len(errs) != 0 {
for _, e := range errs {
log.Errorf("Options error: %s.", e)
}
return nil, fmt.Errorf("opts are illegal")
}

client, err := opts.newGitlabClient()
status, err := operator.Execute(plugininstaller.RawOptions(options))
if err != nil {
return nil, err
}

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

if !exists {
return nil, nil
}

return buildState(&opts), nil
log.Debugf("Return map: %v", status)
return status, nil
}
56 changes: 1 addition & 55 deletions internal/pkg/plugin/gitlabci/generic/update.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,5 @@
package generic

import (
"fmt"

"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/template"
)

func Update(options map[string]interface{}) (map[string]interface{}, error) {
var opts Options
if err := mapstructure.Decode(options, &opts); err != nil {
return nil, err
}

if errs := validate(&opts); len(errs) != 0 {
for _, e := range errs {
log.Errorf("Options error: %s.", e)
}
return nil, fmt.Errorf("opts are illegal")
}

// download template
ciTemplateString, err := download(opts.TemplateURL)
if err != nil {
return nil, err
}

// render template
ciFileContent, err := template.New().
FromContent(ciTemplateString).
SetDefaultRender("ci", opts.TemplateVariables).Render()

if err != nil {
return nil, fmt.Errorf("execute template error: %s", err)
}

// update file
client, err := opts.newGitlabClient()
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.UpdateFiles(commitInfo); err != nil {
return nil, err
}

return buildState(&opts), nil
return Create(options)
}
10 changes: 0 additions & 10 deletions internal/pkg/plugin/gitlabci/generic/validate.go

This file was deleted.

15 changes: 15 additions & 0 deletions internal/pkg/plugin/gitlabci/gilabci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gitlabci

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

var DefaultCIOptions = &ci.Options{
CIConfig: &ci.CIConfig{
Type: "gitlab",
},
ProjectRepo: &common.Repo{
RepoType: "gitlab",
},
}
Loading