/
integrations_parser.go
99 lines (88 loc) · 3.28 KB
/
integrations_parser.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
package helpers
import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/utils/log"
"io"
"os"
"time"
)
type ProjectIntegration struct {
Id int `json:"id"`
MasterIntegrationId int `json:"masterIntegrationId"`
Name string `json:"name"`
MasterIntegrationType string `json:"masterIntegrationType"`
ProjectId int `json:"projectId"`
MasterIntegrationName string `json:"masterIntegrationName"`
ProviderId int `json:"providerId"`
Environments interface{} `json:"environments"`
IsInternal bool `json:"isInternal"`
CreatedByUserName string `json:"createdByUserName"`
UpdatedByUserName string `json:"updatedByUserName"`
FormJSONValues []FormJSONValues `json:"formJSONValues"`
CreatedBy int `json:"createdBy"`
UpdatedBy int `json:"updatedBy"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (p ProjectIntegration) GetIntegrationAsEnvironmentVariable() map[string]string {
envVars := GetStructAsEnvironmentVariables(p)
for _, formJsonValue := range p.FormJSONValues {
valueKey := ConvertFirstRuneToLowerCase(formJsonValue.Label)
envVars[valueKey] = formJsonValue.Value
}
return envVars
}
type FormJSONValues struct {
Label string `json:"label"`
Value string `json:"value"`
}
type ParseIntegrationsOptions struct {
PathToIntegrationsFile string
}
type IntegrationsParser interface {
Parse(*ParseIntegrationsOptions) error
GetIntegrations() map[string]ProjectIntegration
GetSimplifiedIntegrations() map[string]map[string]interface{}
}
type IntegrationsParserImpl struct {
integrations []ProjectIntegration
}
func NewIntegrationParser() IntegrationsParser {
return &IntegrationsParserImpl{
integrations: []ProjectIntegration{},
}
}
func (i *IntegrationsParserImpl) Parse(options *ParseIntegrationsOptions) error {
log.Info("Parsing project integrations")
jsonFile, err := os.Open(options.PathToIntegrationsFile)
if err != nil {
return err
}
defer jsonFile.Close()
fileContent, err := io.ReadAll(jsonFile)
if err != nil {
return err
}
return json.Unmarshal(fileContent, &i.integrations)
}
func (i *IntegrationsParserImpl) GetIntegrations() map[string]ProjectIntegration {
integrationsMap := make(map[string]ProjectIntegration)
for _, integration := range i.integrations {
integrationsMap[integration.Name] = integration
}
return integrationsMap
}
func (i *IntegrationsParserImpl) GetSimplifiedIntegrations() map[string]map[string]interface{} {
integrationsMap := make(map[string]map[string]interface{})
for _, integration := range i.integrations {
integrationsMap[integration.Name] = make(map[string]interface{})
integrationsMap[integration.Name]["id"] = integration.Id
integrationsMap[integration.Name]["name"] = integration.Name
integrationsMap[integration.Name]["masterName"] = integration.MasterIntegrationName
integrationsMap[integration.Name]["displayName"] = integration.MasterIntegrationName
for _, value := range integration.FormJSONValues {
integrationsMap[integration.Name][value.Label] = value.Value
}
}
return integrationsMap
}