Skip to content

Commit

Permalink
Merge pull request #29 from LinuxSuRen/release-auto
Browse files Browse the repository at this point in the history
Support set the pre-release action against an alpha version
  • Loading branch information
LinuxSuRen authored Nov 8, 2021
2 parents c50287d + 0d09121 commit a8c752f
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 224 deletions.
17 changes: 17 additions & 0 deletions api/v1alpha1/git_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package v1alpha1

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGitProvider(t *testing.T) {
assert.Equal(t, ProviderUnknown, GetDefaultProvider(&Repository{Address: "https://baidu.com/x/b"}))
assert.Equal(t, ProviderGitHub, GetDefaultProvider(&Repository{Address: "https://github.com/x/b"}))
assert.Equal(t, ProviderGitlab, GetDefaultProvider(&Repository{Address: "https://gitlab.com/x/b"}))
assert.Equal(t, ProviderGitea, GetDefaultProvider(&Repository{Address: "https://gitea.com/x/b"}))
assert.Equal(t, ProviderGitee, GetDefaultProvider(&Repository{Address: "https://gitee.com/x/b"}))
assert.Equal(t, ProviderBitbucket, GetDefaultProvider(&Repository{Address: "https://bitbucket.org/x/b"}))
assert.Equal(t, ProviderGitHub, GetDefaultProvider(&Repository{Provider: ProviderGitHub}))
assert.Equal(t, Provider(""), GetDefaultProvider(&Repository{Address: ""}))
}
50 changes: 42 additions & 8 deletions api/v1alpha1/releaser_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -50,8 +51,8 @@ const (
)

// IsValid checks if this is valid
func (p *Phase) IsValid() bool {
switch *p {
func (p Phase) IsValid() bool {
switch p {
case PhaseDraft, PhaseReady, PhaseDone:
return true
default:
Expand All @@ -61,13 +62,42 @@ func (p *Phase) IsValid() bool {

// Repository represents a git repository
type Repository struct {
Name string `json:"name"`
// +optional
Name string `json:"name"`
// +optional
Provider Provider `json:"provider,omitempty"`
Address string `json:"address"`
Branch string `json:"branch,omitempty"`
Version string `json:"version,omitempty"`
Message string `json:"message,omitempty"`
Action Action `json:"action,omitempty"`
// +optional
Branch string `json:"branch,omitempty"`
// +optional
Version string `json:"version,omitempty"`
// +optional
Message string `json:"message,omitempty"`
// +optional
Action Action `json:"action,omitempty"`
}

// GetDefaultProvider returns the default git provider
func GetDefaultProvider(r *Repository) Provider {
if r.Provider != "" {
return r.Provider
}

address := r.Address
if strings.HasPrefix(address, "https://github.com/") {
return ProviderGitHub
} else if strings.HasPrefix(address, "https://gitlab.com/") {
return ProviderGitlab
} else if strings.HasPrefix(address, "https://bitbucket.org/") {
return ProviderBitbucket
} else if strings.HasPrefix(address, "https://gitee.com/") {
return ProviderGitee
} else if strings.HasPrefix(address, "https://gitea.com/") {
return ProviderGitea
} else if address != "" {
return ProviderUnknown
}
return ""
}

// GitOps indicates to integrate with GitOps
Expand All @@ -93,6 +123,7 @@ const (
type Action string

const (
ActionAuto Action = "auto"
ActionTag Action = "tag"
ActionPreRelease Action = "pre-release"
ActionRelease Action = "release"
Expand All @@ -114,7 +145,7 @@ type Condition struct {
Message string `json:"message"`
}

// ConditionType is the type of a condition
// ConditionType is the type of condition
type ConditionType string

const (
Expand All @@ -132,6 +163,9 @@ const (

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.spec.phase`,description="The phase of a Releaser"
// +kubebuilder:printcolumn:name="Version",type=string,JSONPath=`.spec.version`,description="The version of a Releaser"
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`,description="The age of a Releaser"

// Releaser is the Schema for the releasers API
type Releaser struct {
Expand Down
13 changes: 13 additions & 0 deletions api/v1alpha1/releaser_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1alpha1

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestPhase(t *testing.T) {
assert.False(t, Phase("unknown").IsValid())
assert.True(t, Phase("draft").IsValid())
assert.True(t, Phase("ready").IsValid())
assert.True(t, Phase("done").IsValid())
}
22 changes: 8 additions & 14 deletions api/v1alpha1/releaser_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"strings"
)

// log is for logging in this package.
Expand Down Expand Up @@ -56,17 +55,7 @@ func (r *Releaser) Default() {
for i, _ := range r.Spec.Repositories {
repo := &r.Spec.Repositories[i]
if repo.Provider == "" {
if strings.HasPrefix(repo.Address, "https://github.com/") {
repo.Provider = ProviderGitHub
} else if strings.HasPrefix(repo.Address, "https://gitlab.com/") {
repo.Provider = ProviderGitlab
} else if strings.HasPrefix(repo.Address, "https://bitbucket.org/") {
repo.Provider = ProviderBitbucket
} else if strings.HasPrefix(repo.Address, "https://gitee.com/") {
repo.Provider = ProviderGitee
} else {
repo.Provider = ProviderUnknown
}
repo.Provider = GetDefaultProvider(repo)
}
if repo.Action == "" {
repo.Action = ActionTag
Expand All @@ -79,8 +68,13 @@ func (r *Releaser) Default() {
}
}

if r.Spec.GitOps != nil && r.Spec.GitOps.Repository.Branch == "" {
r.Spec.GitOps.Repository.Branch = defaultBranchName
if r.Spec.GitOps != nil {
if r.Spec.GitOps.Repository.Branch == "" {
r.Spec.GitOps.Repository.Branch = defaultBranchName
}
if r.Spec.GitOps.Repository.Provider == "" {
r.Spec.GitOps.Repository.Provider = GetDefaultProvider(&r.Spec.GitOps.Repository)
}
}

if r.Spec.Secret.Namespace == "" {
Expand Down
167 changes: 0 additions & 167 deletions config/crd/bases/devops.kubesphere.io_releasers.yaml

This file was deleted.

42 changes: 31 additions & 11 deletions controllers/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,15 @@ func release(repo devopsv1alpha1.Repository, secret *v1.Secret, user string) (er

token := string(secret.Data[v1.BasicAuthPasswordKey])
server := string(secret.Data["server"])
var orgAndRepo string
switch repo.Provider {
case devopsv1alpha1.ProviderGitHub:
orgAndRepo = strings.ReplaceAll(repo.Address, "https://github.com/", "")
case devopsv1alpha1.ProviderGitlab:
orgAndRepo = strings.ReplaceAll(repo.Address, "https://gitlab.com/", "")
orgAndRepo = strings.ReplaceAll(orgAndRepo, ".git", "")
case devopsv1alpha1.ProviderGitea:
orgAndRepo = strings.ReplaceAll(repo.Address, server, "")
}
orgAndRepo := getOrgAndRepo(repo, server)

provider := internal_scm.GetGitProvider(string(repo.Provider), server, orgAndRepo, token)
if provider == nil {
return
}

switch repo.Action {
action := getAction(repo)
switch action {
case devopsv1alpha1.ActionPreRelease:
err = provider.Release(repo.Version, repo.Branch, false, true)
case devopsv1alpha1.ActionRelease:
Expand All @@ -108,6 +100,34 @@ func release(repo devopsv1alpha1.Repository, secret *v1.Secret, user string) (er
return
}

func getOrgAndRepo(repo devopsv1alpha1.Repository, server string) (orgAndRepo string) {
provider := devopsv1alpha1.GetDefaultProvider(&repo)
address := repo.Address
address = strings.ReplaceAll(address, ".git", "")

switch provider {
case devopsv1alpha1.ProviderGitHub:
orgAndRepo = strings.ReplaceAll(address, "https://github.com/", "")
case devopsv1alpha1.ProviderGitlab:
orgAndRepo = strings.ReplaceAll(address, "https://gitlab.com/", "")
case devopsv1alpha1.ProviderGitee:
orgAndRepo = strings.ReplaceAll(address, "https://gitee.com/", "")
case devopsv1alpha1.ProviderBitbucket:
orgAndRepo = strings.ReplaceAll(address, "https://bitbucket.org/", "")
case devopsv1alpha1.ProviderGitea:
orgAndRepo = strings.ReplaceAll(address, server, "")
}
return
}

func getAction(repo devopsv1alpha1.Repository) (action devopsv1alpha1.Action) {
action = repo.Action
if action == devopsv1alpha1.ActionAuto && isPreRelease(repo.Version) {
action = devopsv1alpha1.ActionPreRelease
}
return
}

func getAuth(secret *v1.Secret) (auth transport.AuthMethod) {
if secret == nil {
return
Expand Down
Loading

0 comments on commit a8c752f

Please sign in to comment.