Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #63 from bancodobrasil/feat/rules-json
Browse files Browse the repository at this point in the history
feat: rules json support
  • Loading branch information
eduardaguterres authored Nov 7, 2022
2 parents 6b1925a + b8d726a commit d32dd4f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 119 deletions.
196 changes: 87 additions & 109 deletions services/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package services

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -176,58 +174,24 @@ func (gs *gitlabService) Save(rulesheet *dtos.Rulesheet, commitMessage string) e
}
actions = append(actions, commitAction)

rulesBuffer := bytes.NewBufferString("")
// RULES
// Rules
if rulesheet.Rules == nil {
empty := make(map[string]interface{}, 0)
rulesheet.Rules = &empty
}

rules := make([]string, 0)

for k := range *rulesheet.Rules {
// fmt.Printf("RULE k: %s\n", k)
rules = append(rules, k)
}

sort.Strings(rules)

for _, ruleName := range rules {
// fmt.Printf("RULE: %s\n", ruleName)
switch rule := ((*rulesheet.Rules)[ruleName]).(type) {
case string:
fmt.Fprintf(rulesBuffer, "%s = %s\n", ruleName, rule)
case []interface{}:
for _, entry := range rule {
err := printRule(entry, rulesBuffer, ruleName, true)
if err != nil {
log.Errorf("Failed marshal rule: %v", err)
return err
}
}
case interface{}:
err := printRule(rule, rulesBuffer, ruleName, false)
if err != nil {
log.Errorf("Failed marshal rule: %v", err)
return err
}
default:
fmt.Fprintf(rulesBuffer, "DEFAULT %s = %s\n", ruleName, reflect.TypeOf(rule))
}
content, err = json.MarshalIndent(rulesheet.Rules, "", " ")
if err != nil {
log.Errorf("Failed to marshal parameters: %v", err)
return err
}

// fmt.Printf("RULES: %s\n", rulesBuffer.String())

commitAction, err = createOrUpdateGitlabFileCommitAction(git, proj, cfg.GitlabDefaultBranch, "rules.featws", rulesBuffer.String())
commitAction, err = createOrUpdateGitlabFileCommitAction(git, proj, cfg.GitlabDefaultBranch, "rules.json", string(content))
if err != nil {
log.Errorf("Failed to commit rules: %v", err)
log.Errorf("Failed to commit parameters: %v", err)
return err
}
actions = append(actions, commitAction)

// commitActionData, _ := json.Marshal(commitAction)
// fmt.Println(string(commitActionData))

_, _, err = git.Commits.CreateCommit(proj.ID, &gitlab.CreateCommitOptions{
Branch: &cfg.GitlabDefaultBranch,
CommitMessage: gitlab.String(commitMessage),
Expand All @@ -241,39 +205,39 @@ func (gs *gitlabService) Save(rulesheet *dtos.Rulesheet, commitMessage string) e
return err
}

func printRule(rule interface{}, rulesBuffer *bytes.Buffer, ruleName string, isSliceItem bool) error {
ruleNameTag := "[%s]"
if isSliceItem {
ruleNameTag = "[" + ruleNameTag + "]"
}
// func printRule(rule interface{}, rulesBuffer *bytes.Buffer, ruleName string, isSliceItem bool) error {
// ruleNameTag := "[%s]"
// if isSliceItem {
// ruleNameTag = "[" + ruleNameTag + "]"
// }

switch r := rule.(type) {
case *dtos.Rule:
value, err := json.Marshal(r.Value)
if err != nil {
log.Errorf("Failed marshal rule value: %v", err)
return err
}
fmt.Fprintf(rulesBuffer, ruleNameTag+"\ncondition = %s\nvalue = %s\ntype = object\n\n", ruleName, r.Condition, string(value))
case map[string]interface{}:
fmt.Fprintf(rulesBuffer, ruleNameTag+"\n", ruleName)
keys := make([]string, 0)

for k := range r {
// fmt.Printf("RULE k: %s\n", k)
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := r[k]
fmt.Fprintf(rulesBuffer, "%s = %s\n", k, v)
}
fmt.Fprintf(rulesBuffer, "\n")
default:
fmt.Fprintf(rulesBuffer, "DEFAULT ENTRY %s = %s\n", ruleName, reflect.TypeOf(rule))
}
return nil
}
// switch r := rule.(type) {
// case *dtos.Rule:
// value, err := json.Marshal(r.Value)
// if err != nil {
// log.Errorf("Failed marshal rule value: %v", err)
// return err
// }
// fmt.Fprintf(rulesBuffer, ruleNameTag+"\ncondition = %s\nvalue = %s\ntype = object\n\n", ruleName, r.Condition, string(value))
// case map[string]interface{}:
// fmt.Fprintf(rulesBuffer, ruleNameTag+"\n", ruleName)
// keys := make([]string, 0)

// for k := range r {
// // fmt.Printf("RULE k: %s\n", k)
// keys = append(keys, k)
// }
// sort.Strings(keys)
// for _, k := range keys {
// v := r[k]
// fmt.Fprintf(rulesBuffer, "%s = %s\n", k, v)
// }
// fmt.Fprintf(rulesBuffer, "\n")
// default:
// fmt.Fprintf(rulesBuffer, "DEFAULT ENTRY %s = %s\n", ruleName, reflect.TypeOf(rule))
// }
// return nil
// }

func createOrUpdateGitlabFileCommitAction(git *gitlab.Client, proj *gitlab.Project, ref string, filename string, content string) (*gitlab.CommitActionOptions, error) {
action, err := defineCreateOrUpdateGitlabFileAction(git, proj, ref, filename)
Expand Down Expand Up @@ -347,56 +311,70 @@ func (gs *gitlabService) Fill(rulesheet *dtos.Rulesheet) (err error) {
return
}

bRules, err := gitlabLoadString(git, proj, gs.cfg.GitlabDefaultBranch, "rules.featws")
bRulesJSON, err := gitlabLoadString(git, proj, gs.cfg.GitlabDefaultBranch, "rules.json")
if err != nil {
log.Errorf("Failed to fetch parameters: %v", err)
log.Errorf("Failed to check rules JSON: %v", err)
return
}

rulesFile, err := featws.Load(bRules)
if err != nil {
log.Errorf("Failed to load featws file: %v", err)
return
}
if string(bRulesJSON) != "" {
err = gitlabLoadJSON(git, proj, gs.cfg.GitlabDefaultBranch, "rules.json", &rulesheet.Rules)
if err != nil {
log.Errorf("Failed to fetch parameters: %v", err)
return
}
} else {
bRules, err := gitlabLoadString(git, proj, gs.cfg.GitlabDefaultBranch, "rules.featws")
if err != nil {
log.Errorf("Failed to fetch parameters: %v", err)
return err
}

rules := make(map[string]interface{})
rulesFile, err := featws.Load(bRules)
if err != nil {
log.Errorf("Failed to load featws file: %v", err)
return err
}

s := rulesFile.Section("")
rules := make(map[string]interface{})

for _, k := range s.Keys() {
rules[k.Name()] = k.Value()
}
s := rulesFile.Section("")

for _, sname := range rulesFile.SectionStrings() {
if sname == featws.DefaultSection {
continue
for _, k := range s.Keys() {
rules[k.Name()] = k.Value()
}
if sname[:1] == "[" {
continue
}
sec := make(map[string]interface{})
for _, k := range rulesFile.Section(sname).Keys() {
sec[k.Name()] = k.Value()

for _, sname := range rulesFile.SectionStrings() {
if sname == featws.DefaultSection {
continue
}
if sname[:1] == "[" {
continue
}
sec := make(map[string]interface{})
for _, k := range rulesFile.Section(sname).Keys() {
sec[k.Name()] = k.Value()
}
rules[sname] = sec
}
rules[sname] = sec
}

for _, aname := range rulesFile.ArrayStrings() {
a := rulesFile.Array(aname)
for _, aname := range rulesFile.ArrayStrings() {
a := rulesFile.Array(aname)

arr := make([]map[string]interface{}, 0)
arr := make([]map[string]interface{}, 0)

for _, s := range a.Sections() {
sec := make(map[string]interface{})
for _, k := range s.Keys() {
sec[k.Name()] = k.Value()
for _, s := range a.Sections() {
sec := make(map[string]interface{})
for _, k := range s.Keys() {
sec[k.Name()] = k.Value()
}
arr = append(arr, sec)
}
arr = append(arr, sec)
rules[aname[1:len(aname)-1]] = arr
}
rules[aname[1:len(aname)-1]] = arr
}

rulesheet.Rules = &rules
rulesheet.Rules = &rules
}

return
}
Expand Down
20 changes: 10 additions & 10 deletions services/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestSaveTestFilesCreation(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "", rulesFeatws)
assert.Equal(t, "{}", rulesFeatws)

w.Write(data)
return
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestSaveTestFilesCreationWithFeatures(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "", rulesFeatws)
assert.Equal(t, "{}", rulesFeatws)

w.Write(data)
return
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestSaveTestFilesCreationWithParameters(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[\n {\n \"name\": \"test1\"\n },\n {\n \"name\": \"test2\"\n },\n {\n \"name\": \"test3\"\n }\n]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "", rulesFeatws)
assert.Equal(t, "{}", rulesFeatws)

w.Write(data)
return
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestSaveTestFilesCreationWithRuleInterface(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "[[tags]]\ncondition = test\nvalue = {\"nomeAplicativo\":\"testAplicativo\",\"textoUrlDesvio\":\"testURLdesvio\",\"textoUrlPadrao\":\"testURLpadrao\"}\ntype = object\n\n", rulesFeatws)
assert.Equal(t, "{\n \"tags\": [\n {\n \"condition\": \"test\",\n \"value\": {\n \"nomeAplicativo\": \"testAplicativo\",\n \"textoUrlDesvio\": \"testURLdesvio\",\n \"textoUrlPadrao\": \"testURLpadrao\"\n },\n \"type\": \"testType\"\n }\n ]\n}", rulesFeatws)

w.Write(data)
return
Expand All @@ -380,8 +380,8 @@ func TestSaveTestFilesCreationWithRuleString(t *testing.T) {
dto := SetupRulesheet()

mappedRules := map[string]interface{}{
"rule1": "true",
"rule2": "\"test\"",
"rule1": true,
"rule2": "test",
}
dto.Rules = &mappedRules

Expand Down Expand Up @@ -412,7 +412,7 @@ func TestSaveTestFilesCreationWithRuleString(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "rule1 = true\nrule2 = \"test\"\n", rulesFeatws)
assert.Equal(t, "{\n \"rule1\": true,\n \"rule2\": \"test\"\n}", rulesFeatws)

w.Write(data)
return
Expand Down Expand Up @@ -470,7 +470,7 @@ func TestSaveTestFilesCreationWithStringRule(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "test1 = test1\ntest2 = test2\ntest3 = test3\n", rulesFeatws)
assert.Equal(t, "{\n \"test1\": \"test1\",\n \"test2\": \"test2\",\n \"test3\": \"test3\"\n}", rulesFeatws)

w.Write(data)
return
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestSaveTestFilesCreationWithDefaultRule(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "[[tags]]\ncondition = test\nvalue = {\"NomeAplicativo\":\"testAplicativo\",\"TextoURLDesvio\":\"testURLdesvio\",\"TextoURLPadrao\":\"testURLpadrao\"}\ntype = object\n\n", rulesFeatws)
assert.Equal(t, "{\n \"tags\": [\n {\n \"condition\": \"test\",\n \"value\": {\n \"NomeAplicativo\": \"testAplicativo\",\n \"TextoURLDesvio\": \"testURLdesvio\",\n \"TextoURLPadrao\": \"testURLpadrao\"\n },\n \"type\": \"testType\"\n }\n ]\n}", rulesFeatws)

w.Write(data)
return
Expand Down Expand Up @@ -609,7 +609,7 @@ func TestSaveTestFilesUpdate(t *testing.T) {
parameters := c["actions"].([]interface{})[3].(map[string]interface{})["content"].(string)
assert.Equal(t, "[]", parameters)
rulesFeatws := c["actions"].([]interface{})[4].(map[string]interface{})["content"].(string)
assert.Equal(t, "", rulesFeatws)
assert.Equal(t, "{}", rulesFeatws)

w.Write(data)
return
Expand Down

0 comments on commit d32dd4f

Please sign in to comment.