From 1f2d08189332af8736db8c3c0e22c4a2ba3aad03 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Fri, 20 Oct 2023 16:49:33 +0200 Subject: [PATCH] feat: add insecure flag to force http on the domain (#73) * feat: add insecure flag to force http on the domain * adding release docs --- apis/mpas/v1alpha1/repository_types.go | 7 ++++ .../bases/mpas.ocm.software_repositories.yaml | 3 ++ docs/release_notes/v0.9.0.md | 7 ++++ pkg/providers/gitea/gitea.go | 36 ++++++++++++------- pkg/version/release.go | 2 +- 5 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 docs/release_notes/v0.9.0.md diff --git a/apis/mpas/v1alpha1/repository_types.go b/apis/mpas/v1alpha1/repository_types.go index a88d8c4..0d02203 100644 --- a/apis/mpas/v1alpha1/repository_types.go +++ b/apis/mpas/v1alpha1/repository_types.go @@ -53,6 +53,9 @@ type RepositorySpec struct { //+optional //+kubebuilder:validation:Pattern="^\\w+(\\.|:[0-9]).*$" Domain string `json:"domain,omitempty"` + // Insecure should be defined if `domain` is not HTTPS. + //+optional + Insecure bool `json:"insecure,omitempty"` //+optional Maintainers []string `json:"maintainers,omitempty"` //+optional @@ -105,6 +108,10 @@ func (in Repository) GetRepositoryURL() string { return fmt.Sprintf("%s:%s/%s", in.Spec.Domain, in.Spec.Owner, in.GetName()) } + if in.Spec.Insecure { + return fmt.Sprintf("http://%s/%s/%s", in.Spec.Domain, in.Spec.Owner, in.GetName()) + } + return fmt.Sprintf("https://%s/%s/%s", in.Spec.Domain, in.Spec.Owner, in.GetName()) } diff --git a/config/crd/bases/mpas.ocm.software_repositories.yaml b/config/crd/bases/mpas.ocm.software_repositories.yaml index 4d8447b..663feac 100644 --- a/config/crd/bases/mpas.ocm.software_repositories.yaml +++ b/config/crd/bases/mpas.ocm.software_repositories.yaml @@ -83,6 +83,9 @@ spec: - adopt - fail type: string + insecure: + description: Insecure should be defined if `domain` is not HTTPS. + type: boolean interval: type: string isOrganization: diff --git a/docs/release_notes/v0.9.0.md b/docs/release_notes/v0.9.0.md new file mode 100644 index 0000000..4b9b34d --- /dev/null +++ b/docs/release_notes/v0.9.0.md @@ -0,0 +1,7 @@ +# Release 0.9.0 + +- feat: add insecure flag to force http on the domain (#73) +- fix: allowing domain to be added including a scheme (#72) +- Remove blackduck PR scans (#71) +- add blackduck scans (#70) +- New PR Template (#66) diff --git a/pkg/providers/gitea/gitea.go b/pkg/providers/gitea/gitea.go index 696bde0..d9b4461 100644 --- a/pkg/providers/gitea/gitea.go +++ b/pkg/providers/gitea/gitea.go @@ -10,6 +10,7 @@ import ( "encoding/base64" "errors" "fmt" + "net/url" "code.gitea.io/sdk/gitea" deliveryv1alpha1 "github.com/open-component-model/git-controller/apis/delivery/v1alpha1" @@ -22,9 +23,8 @@ import ( ) const ( - tokenKey = "password" - providerType = "gitea" - defaultDomain = "gitea.com" + tokenKey = "password" + providerType = "gitea" ) // Client gitea. @@ -65,9 +65,9 @@ func (c *Client) CreateRepository(ctx context.Context, obj mpasv1alpha1.Reposito return fmt.Errorf("token '%s' not found in secret", tokenKey) } - domain := defaultDomain - if obj.Spec.Domain != "" { - domain = obj.Spec.Domain + domain, err := c.getDomain(obj) + if err != nil { + return fmt.Errorf("failed to generate domain url: %w", err) } client, err := gitea.NewClient(domain, gitea.SetToken(string(token))) @@ -167,9 +167,9 @@ func (c *Client) CreatePullRequest(ctx context.Context, branch string, sync deli return -1, fmt.Errorf("token '%s' not found in secret", tokenKey) } - domain := defaultDomain - if repository.Spec.Domain != "" { - domain = repository.Spec.Domain + domain, err := c.getDomain(repository) + if err != nil { + return -1, fmt.Errorf("failed to generate domain url: %w", err) } gclient, err := gitea.NewClient(domain, gitea.SetToken(string(token))) @@ -237,9 +237,9 @@ func (c *Client) CreateBranchProtection(ctx context.Context, repository mpasv1al logger.Info("got secret") - domain := defaultDomain - if repository.Spec.Domain != "" { - domain = repository.Spec.Domain + domain, err := c.getDomain(repository) + if err != nil { + return fmt.Errorf("failed to generate domain url: %w", err) } logger.Info("default domain set", "domain", domain) @@ -267,3 +267,15 @@ func (c *Client) CreateBranchProtection(ctx context.Context, repository mpasv1al return nil } + +func (c *Client) getDomain(obj mpasv1alpha1.Repository) (string, error) { + u, err := url.Parse(obj.GetRepositoryURL()) + if err != nil { + return "", fmt.Errorf("failed to parse repository url: %w", err) + } + + // construct the domain including the scheme and host but without the path + // gitea requires a host and a scheme + domain := fmt.Sprintf("%s://%s", u.Scheme, u.Host) + return domain, nil +} diff --git a/pkg/version/release.go b/pkg/version/release.go index 468dddd..b27ae63 100644 --- a/pkg/version/release.go +++ b/pkg/version/release.go @@ -5,7 +5,7 @@ package version // ReleaseVersion is the version number in semver format "vX.Y.Z", prefixed with "v". -var ReleaseVersion = "v0.8.0" +var ReleaseVersion = "v0.9.0" // ReleaseCandidate is the release candidate ID in format "rc.X", which will be appended to the release version. var ReleaseCandidate = "rc.1"