Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit 2820fdb

Browse files
author
Alexander Matyushentsev
authored
feat: add sync.GetInfoItem function (#363)
Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
1 parent a20725a commit 2820fdb

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

docs/functions.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ String related functions.
2020

2121
Executes function built-in Golang [strings.ReplaceAll](https://pkg.go.dev/strings#ReplaceAll) function.
2222

23+
### **sync**
24+
25+
<hr>
26+
**`sync.GetInfoItem(app map, name string) string`**
27+
28+
Returns the `info` item value by given name stored in the Argo CD App sync operation.
29+
2330
### **repo**
2431
Functions that provide additional information about Application source repository.
2532
<hr>

expr/expr.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package expr
33
import (
44
"github.com/argoproj-labs/argocd-notifications/expr/repo"
55
"github.com/argoproj-labs/argocd-notifications/expr/strings"
6+
"github.com/argoproj-labs/argocd-notifications/expr/sync"
67
"github.com/argoproj-labs/argocd-notifications/expr/time"
78
"github.com/argoproj-labs/argocd-notifications/shared/argocd"
89
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -14,6 +15,7 @@ func init() {
1415
helpers = make(map[string]interface{})
1516
register("time", time.NewExprs())
1617
register("strings", strings.NewExprs())
18+
register("sync", sync.NewExprs())
1719
}
1820

1921
func register(namespace string, entry map[string]interface{}) {

expr/sync/sync.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sync
2+
3+
import (
4+
"fmt"
5+
6+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
7+
)
8+
9+
func NewExprs() map[string]interface{} {
10+
return map[string]interface{}{
11+
"GetInfoItem": func(app map[string]interface{}, name string) string {
12+
res, err := getInfoItem(app, name)
13+
if err != nil {
14+
panic(err)
15+
}
16+
return res
17+
},
18+
}
19+
}
20+
21+
func getInfoItem(app map[string]interface{}, name string) (string, error) {
22+
un := unstructured.Unstructured{Object: app}
23+
operation, ok, _ := unstructured.NestedMap(app, "operation")
24+
if !ok {
25+
operation, ok, _ = unstructured.NestedMap(app, "status", "operationState", "operation")
26+
}
27+
if !ok {
28+
return "", fmt.Errorf("application '%s' has no operation", un.GetName())
29+
}
30+
31+
infoItems, ok := operation["info"].([]interface{})
32+
if !ok {
33+
return "", fmt.Errorf("application '%s' has no info items", un.GetName())
34+
}
35+
for _, infoItem := range infoItems {
36+
item, ok := infoItem.(map[string]interface{})
37+
if !ok {
38+
continue
39+
}
40+
if item["name"] == name {
41+
res, ok := item["value"].(string)
42+
if !ok {
43+
return "", fmt.Errorf("application '%s' has invalid value of info item '%s'", un.GetName(), name)
44+
}
45+
return res, nil
46+
}
47+
}
48+
return "", fmt.Errorf("application '%s' has no info item with name '%s'", un.GetName(), name)
49+
}

expr/sync/sync_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sync
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
. "github.com/argoproj-labs/argocd-notifications/testing"
9+
)
10+
11+
var (
12+
infoItems = []interface{}{
13+
map[string]interface{}{
14+
"name": "name1",
15+
"value": "val1",
16+
},
17+
}
18+
)
19+
20+
func TestGetInfoItem_RequestedOperation(t *testing.T) {
21+
app := NewApp("test")
22+
app.Object["operation"] = map[string]interface{}{
23+
"info": infoItems,
24+
}
25+
26+
val, err := getInfoItem(app.Object, "name1")
27+
assert.NoError(t, err)
28+
assert.Equal(t, "val1", val)
29+
}
30+
31+
func TestGetInfoItem_CompletedOperation(t *testing.T) {
32+
app := NewApp("test")
33+
app.Object["status"] = map[string]interface{}{
34+
"operationState": map[string]interface{}{
35+
"operation": map[string]interface{}{
36+
"info": infoItems,
37+
},
38+
},
39+
}
40+
41+
val, err := getInfoItem(app.Object, "name1")
42+
assert.NoError(t, err)
43+
assert.Equal(t, "val1", val)
44+
}
45+
46+
func TestGetInfoItem_NoOperation(t *testing.T) {
47+
app := NewApp("test")
48+
_, err := getInfoItem(app.Object, "name1")
49+
assert.Error(t, err)
50+
}

0 commit comments

Comments
 (0)