Skip to content

Commit

Permalink
Merge branch 'master' into gitlab-pull-push-checker
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwlde committed Oct 4, 2023
2 parents 5a12453 + 8fbe884 commit a2be269
Show file tree
Hide file tree
Showing 20 changed files with 413 additions and 327 deletions.
2 changes: 1 addition & 1 deletion api/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func New(client *kommons.Client, kubernetes kubernetes.Interface, db *gorm.DB, c
}

func (ctx *Context) IsDebug() bool {
return ctx.Canary.IsDebug()
return ctx.Canary.IsDebug() || ctx.IsTrace()
}

func (ctx *Context) IsTrace() bool {
Expand Down
4 changes: 2 additions & 2 deletions api/v1/canary_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type CanarySpec struct {
MongoDB []MongoDBCheck `yaml:"mongodb,omitempty" json:"mongodb,omitempty"`
CloudWatch []CloudWatchCheck `yaml:"cloudwatch,omitempty" json:"cloudwatch,omitempty"`
GitHub []GitHubCheck `yaml:"github,omitempty" json:"github,omitempty"`
GitLab []GitLabCheck `yaml:"gitlab,omitempty" json:"gitlab,omitempty"`
GitProtocol []GitProtocolCheck `yaml:"gitProtocol,omitempty" json:"gitProtocol,omitempty"`
Kubernetes []KubernetesCheck `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty"`
Folder []FolderCheck `yaml:"folder,omitempty" json:"folder,omitempty"`
Exec []ExecCheck `yaml:"exec,omitempty" json:"exec,omitempty"`
Expand Down Expand Up @@ -162,7 +162,7 @@ func (spec CanarySpec) GetAllChecks() []external.Check {
for _, check := range spec.GitHub {
checks = append(checks, check)
}
for _, check := range spec.GitLab {
for _, check := range spec.GitProtocol {
checks = append(checks, check)
}
for _, check := range spec.Kubernetes {
Expand Down
15 changes: 8 additions & 7 deletions api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,20 +779,21 @@ func (c GitHubCheck) GetEndpoint() string {
return strings.ReplaceAll(c.Query, " ", "-")
}

type GitLabCheck struct {
type GitProtocolCheck struct {
Description `yaml:",inline" json:",inline"`
Templatable `yaml:",inline" json:",inline"`
ConnectionName string `yaml:"connection,omitempty" json:"connection,omitempty"`
Repository string `yaml:"repository" json:"repository"`
Username string `yaml:"username" json:"username"`
Password types.EnvVar `yaml:"password,omitempty" json:"password,omitempty"`
FileName string `yaml:"filename,omitempty" json:"filename,omitempty"`
Username types.EnvVar `yaml:"username" json:"username"`
Password types.EnvVar `yaml:"password" json:"password"`
}

func (c GitLabCheck) GetType() string {
return "gitProtocol"
func (c GitProtocolCheck) GetType() string {
return "gitprotocol"
}

func (c GitLabCheck) GetEndpoint() string {
func (c GitProtocolCheck) GetEndpoint() string {
return strings.ReplaceAll(c.Repository, "/", "-")
}

Expand Down Expand Up @@ -1370,7 +1371,7 @@ var AllChecks = []external.Check{
ExecCheck{},
FolderCheck{},
GitHubCheck{},
GitLabCheck{},
GitProtocolCheck{},
HelmCheck{},
HTTPCheck{},
ICMPCheck{},
Expand Down
15 changes: 8 additions & 7 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion checks/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var All = []Checker{
&ExecChecker{},
&FolderChecker{},
&GitHubChecker{},
&GitLabChecker{},
&GitProtocolChecker{},
&HTTPChecker{},
&IcmpChecker{},
&JmeterChecker{},
Expand Down
44 changes: 28 additions & 16 deletions checks/gitlab.go → checks/git_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ import (
)

const (
TestFileName = "test.txt"
DefaultFileName = "test.txt"
)

type GitLabChecker struct{}
type GitProtocolChecker struct{}

func (c *GitLabChecker) Type() string {
return "gitlab"
func (c *GitProtocolChecker) Type() string {
return "gitprotocol"
}

func (c *GitLabChecker) Run(ctx *context.Context) pkg.Results {
func (c *GitProtocolChecker) Run(ctx *context.Context) pkg.Results {
var results pkg.Results
for _, conf := range ctx.Canary.Spec.GitLab {
for _, conf := range ctx.Canary.Spec.GitProtocol {
results = append(results, c.Check(ctx, conf)...)
}
return results
}

func pushChanges(repoURL, username, password string) error {
func pushChanges(repoURL, username, password, filename string) error {
dir, err := os.MkdirTemp("", "repo-clone-")
if err != nil {
return fmt.Errorf("failed to create temp directory: %v", err)
Expand All @@ -53,7 +53,7 @@ func pushChanges(repoURL, username, password string) error {
return fmt.Errorf("failed to get worktree: %v", err)
}

filePath := path.Join(dir, TestFileName)
filePath := path.Join(dir, filename)
currentTime := time.Now().Format("2006-01-02-04-05")

// Check if the file exists and if not we will create it
Expand All @@ -70,11 +70,11 @@ func pushChanges(repoURL, username, password string) error {
return fmt.Errorf("failed to update file: %v", err)
}

if _, err := w.Add(TestFileName); err != nil {
if _, err := w.Add(filename); err != nil {
return fmt.Errorf("failed to add changes to staging area: %v", err)
}

commitMsg := fmt.Sprintf("Updated %s with time %s", TestFileName, currentTime)
commitMsg := fmt.Sprintf("Updated %s with time %s", filename, currentTime)
if _, err := w.Commit(commitMsg, &git.CommitOptions{
Author: &object.Signature{
Name: username,
Expand All @@ -94,25 +94,37 @@ func pushChanges(repoURL, username, password string) error {
return nil
}

func (c *GitLabChecker) Check(ctx *context.Context, extConfig external.Check) pkg.Results {
check := extConfig.(v1.GitLabCheck)
func (c *GitProtocolChecker) Check(ctx *context.Context, extConfig external.Check) pkg.Results {
check := extConfig.(v1.GitProtocolCheck)
result := pkg.Success(check, ctx.Canary)
var results pkg.Results
results = append(results, result)

// Fetching GitLab Token
filename := check.FileName

if len(filename) == 0 {
filename = DefaultFileName
}

// Fetching Username
username, err := ctx.GetEnvValueFromCache(check.Username)
if err != nil {
return results.Failf("error fetching git user from env cache: %v", err)
}

// Fetching Password
password, err := ctx.GetEnvValueFromCache(check.Password)
if err != nil {
return results.Failf("error fetching gitlab token from env cache: %v", err)
return results.Failf("error fetching git password from env cache: %v", err)
}

// Push Changes
if err := pushChanges(check.Repository, check.Username, password); err != nil {
if err := pushChanges(check.Repository, username, password, filename); err != nil {
return results.Failf("error pushing changes: %v", err)
}

details := map[string]string{
"msg": "GitLab pull/push command succeeded.",
"msg": "Git pull/push command succeeded.",
}
result.AddDetails(details)

Expand Down
Loading

0 comments on commit a2be269

Please sign in to comment.