Skip to content

Commit

Permalink
chore: add gitops.link
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Sep 30, 2024
1 parent 705a790 commit a81a7c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions query/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package query

import (
"fmt"
"net/url"
"path/filepath"
"strings"

"gopkg.in/yaml.v3"

Expand All @@ -28,6 +30,7 @@ func (t *Kustomize) AsMap() map[string]any {
}

type Git struct {
Link string `json:"link"`
File string `json:"file"`
Dir string `json:"dir"`
URL string `json:"url"`
Expand All @@ -37,6 +40,7 @@ type Git struct {
func (t *Git) AsMap() map[string]any {
return map[string]any{
"file": t.File,
"link": t.Link,
"dir": t.Dir,
"url": t.URL,
"branch": t.Branch,
Expand Down Expand Up @@ -95,9 +99,23 @@ func GetGitOpsSource(ctx context.Context, id uuid.UUID) (GitOpsSource, error) {
source.Git.Dir = filepath.Dir(source.Git.File)
}

if strings.Contains(source.Git.URL, "github.com") {
source.Git.Link = fmt.Sprintf("https://%s/tree/%s/%s", stripScheme(source.Git.URL), source.Git.Branch, source.Git.File)
}

return source, nil
}

func stripScheme(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return uri
}
u.Scheme = ""
u.User = nil
return strings.TrimPrefix(strings.ReplaceAll(u.String(), ".git", ""), "//")
}

func gitopsSourceCELFunction() func(ctx context.Context) cel.EnvOption {
return func(ctx context.Context) cel.EnvOption {
return cel.Function("gitops.source",
Expand Down
1 change: 1 addition & 0 deletions tests/config_gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var _ = ginkgo.Describe("Config Gitops Source", ginkgo.Ordered, func() {
Expect(source.Git.File).To(Equal("aws-demo/spec/namespaces/flux/namespace.yaml"))
Expect(source.Git.Dir).To(Equal("aws-demo/spec/namespaces/flux"))
Expect(source.Git.URL).To(Equal("ssh://[email protected]/flanksource/sandbox.git"))
Expect(source.Git.Link).To(Equal("https://github.com/flanksource/sandbox/tree/main/aws-demo/spec/namespaces/flux/namespace.yaml"))
Expect(source.Git.Branch).To(Equal("main"))
})

Expand Down

0 comments on commit a81a7c3

Please sign in to comment.