-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathrepo.go
118 lines (101 loc) · 2.81 KB
/
repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package repo
import (
"context"
"encoding/json"
"errors"
"net/url"
"regexp"
"strings"
service "github.com/argoproj/argo-cd/v3/util/notification/argocd"
"github.com/argoproj/argo-cd/v3/util/notification/expression/shared"
"github.com/argoproj/notifications-engine/pkg/util/text"
giturls "github.com/chainguard-dev/git-urls"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
)
var gitSuffix = regexp.MustCompile(`\.git$`)
func getApplication(obj *unstructured.Unstructured) (*v1alpha1.Application, error) {
data, err := json.Marshal(obj)
if err != nil {
return nil, err
}
application := &v1alpha1.Application{}
err = json.Unmarshal(data, application)
if err != nil {
return nil, err
}
return application, nil
}
func getAppDetails(un *unstructured.Unstructured, argocdService service.Service) (*shared.AppDetail, error) {
app, err := getApplication(un)
if err != nil {
return nil, err
}
appDetail, err := argocdService.GetAppDetails(context.Background(), app)
if err != nil {
return nil, err
}
return appDetail, nil
}
func getCommitMetadata(commitSHA string, app *unstructured.Unstructured, argocdService service.Service) (*shared.CommitMetadata, error) {
repoURL, ok, err := unstructured.NestedString(app.Object, "spec", "source", "repoURL")
if err != nil {
return nil, err
}
if !ok {
panic(errors.New("failed to get application source repo URL"))
}
project, ok, err := unstructured.NestedString(app.Object, "spec", "project")
if err != nil {
return nil, err
}
if !ok {
panic(errors.New("failed to get application project"))
}
meta, err := argocdService.GetCommitMetadata(context.Background(), repoURL, commitSHA, project)
if err != nil {
return nil, err
}
return meta, nil
}
func FullNameByRepoURL(rawURL string) string {
parsed, err := giturls.Parse(rawURL)
if err != nil {
panic(err)
}
path := gitSuffix.ReplaceAllString(parsed.Path, "")
if pathParts := text.SplitRemoveEmpty(path, "/"); len(pathParts) >= 2 {
return strings.Join(pathParts[:2], "/")
}
return path
}
func repoURLToHTTPS(rawURL string) string {
parsed, err := giturls.Parse(rawURL)
if err != nil {
panic(err)
}
parsed.Scheme = "https"
parsed.User = nil
return parsed.String()
}
func NewExprs(argocdService service.Service, app *unstructured.Unstructured) map[string]any {
return map[string]any{
"RepoURLToHTTPS": repoURLToHTTPS,
"FullNameByRepoURL": FullNameByRepoURL,
"QueryEscape": url.QueryEscape,
"GetCommitMetadata": func(commitSHA string) any {
meta, err := getCommitMetadata(commitSHA, app, argocdService)
if err != nil {
panic(err)
}
return *meta
},
"GetAppDetails": func() any {
appDetails, err := getAppDetails(app, argocdService)
if err != nil {
panic(err)
}
return *appDetails
},
}
}