Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
fix: missing scheme from gitea client (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso authored Nov 30, 2023
1 parent 1b808db commit e12d000
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions pkg/validators/gitea/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package gitea
import (
"context"
"fmt"
"net/url"

"code.gitea.io/sdk/gitea"
"github.com/open-component-model/mpas-product-controller/api/v1alpha1"
deliveryv1alpha1 "github.com/open-component-model/git-controller/apis/delivery/v1alpha1"
gitv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

deliveryv1alpha1 "github.com/open-component-model/git-controller/apis/delivery/v1alpha1"
gitv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"

"github.com/open-component-model/mpas-product-controller/api/v1alpha1"
"github.com/open-component-model/mpas-product-controller/pkg/validators"
)

Expand Down Expand Up @@ -88,7 +88,12 @@ func (v *Validator) IsMergedOrClosed(ctx context.Context, repository gitv1alpha1

domain := defaultDomain
if repository.Spec.Domain != "" {
domain = repository.Spec.Domain
u, err := v.parseDomainURL(repository.Spec.Domain, repository.Spec.Insecure)
if err != nil {
return false, fmt.Errorf("failed to parse repository url %s: %w", repository.Spec.Domain, err)
}

domain = u
}

client, err := gitea.NewClient(domain, gitea.SetToken(string(token)))
Expand All @@ -115,7 +120,12 @@ func (v *Validator) createCheckRunStatus(ctx context.Context, repository gitv1al

domain := defaultDomain
if repository.Spec.Domain != "" {
domain = repository.Spec.Domain
u, err := v.parseDomainURL(repository.Spec.Domain, repository.Spec.Insecure)
if err != nil {
return fmt.Errorf("failed to parse repository url %s: %w", repository.Spec.Domain, err)
}

domain = u
}

client, err := gitea.NewClient(domain, gitea.SetToken(string(token)))
Expand Down Expand Up @@ -160,3 +170,19 @@ func (v *Validator) retrieveAccessToken(ctx context.Context, obj gitv1alpha1.Rep

return token, nil
}

func (v *Validator) parseDomainURL(domain string, insecure bool) (string, error) {
u, err := url.Parse(domain)
if err != nil {
return "", fmt.Errorf("failed to parse url: %w", err)
}

if u.Scheme == "" {
u.Scheme = "https"
if insecure {
u.Scheme = "http"
}
}

return u.String(), nil
}

0 comments on commit e12d000

Please sign in to comment.