From 5b5c5df18fd6323fc39d6327677c05e7539790bf Mon Sep 17 00:00:00 2001 From: Patrick Hermann Date: Sun, 19 Jan 2025 16:05:38 +0000 Subject: [PATCH] commit to feature/add-homerun-survey --- cmd/push.go | 42 ++++++++++++++++++++++++++++----------- profiles/homerun.yaml | 46 +++++++++++++++++++++++++------------------ surveys/surveys.go | 15 +++++++------- 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/cmd/push.go b/cmd/push.go index bfc866a..1b91bf3 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -172,6 +172,9 @@ var pushCmd = &cobra.Command{ values["authors"] = authorNames values["authorAddresses"] = authorAddresses values["allUsecases"] = demo.Usecases + values["messageTemplates"] = demo.MessageTemplates + + fmt.Println(demo.MessageTemplates["INFO"]["run"]) fmt.Println("USECASE", demo.Usecases["gitlab"][1]) @@ -187,7 +190,6 @@ var pushCmd = &cobra.Command{ return input + " Hello from push" }, "random": func(input []string) string { - //severities := []string{"INFO", "WARNING", "CRITICAL", "ERROR"} rand.Seed(time.Now().UnixNano()) return input[rand.Intn(len(input))] }, @@ -198,23 +200,39 @@ var pushCmd = &cobra.Command{ "getValueFromStringMap": func(key string, keyValues map[string]string) string { return keyValues[key] }, + "textBlock": func(severity string, verb string, template map[string]map[string]string) string { + return template[severity][verb] + }, "randomUsecase": func(system string, usecases map[string][]string) string { - //severities := []string{"INFO", "WARNING", "CRITICAL", "ERROR"} rand.Seed(time.Now().UnixNano()) systemUsecases := usecases[system] - return systemUsecases[rand.Intn(len(systemUsecases))] + + allUsecaseNames := []string{} + + // LOOP OVER USECASES + for _, usecase := range systemUsecases { + usecasesplit := strings.Split(usecase, ":") + allUsecaseNames = append(allUsecaseNames, usecasesplit[0]) + } + + return allUsecaseNames[rand.Intn(len(allUsecaseNames))] }, - // "randomFromSlice": func(inputSlice []string) string { - // // Check if the slice is empty - // if len(inputSlice) == 0 { - // return "" // Return empty string if the slice is empty - // } + "getUsecaseVerb": func(usecaseName string, system string, usecases map[string][]string) string { + rand.Seed(time.Now().UnixNano()) + + verb := "Verb not found for usecase: " + usecaseName + systemUsecases := usecases[system] - // rand.Seed(uint64(time.Now().UnixNano())) - // randomIndex := rand.Intn(len(inputSlice)) + // LOOP OVER USECASES + for _, usecase := range systemUsecases { + usecasesplit := strings.Split(usecase, ":") + if usecasesplit[0] == usecaseName { + verb = usecasesplit[1] + } + } - // return inputSlice[randomIndex] - // }, + return verb + }, } fmt.Println(demo.BodyTemplate) diff --git a/profiles/homerun.yaml b/profiles/homerun.yaml index bfc5507..2058275 100644 --- a/profiles/homerun.yaml +++ b/profiles/homerun.yaml @@ -11,8 +11,8 @@ preSurvey: name: severity options: - "INFO" - - "WARNING" - "ERROR" + - "SUCCESS" # surveys: # - tests/{{ .system }}-survey.yaml @@ -22,38 +22,46 @@ aliases: - "github-survey:git" bodyTemplate: | + {{ $object := randomUsecase .system .allUsecases }} + {{ $verb := getUsecaseVerb $object .system .allUsecases }} + {{ $author := random .authors }} + {{ $assignee := random .authors }} { "System": "{{ .system }}", - {{ $object := randomUsecase .system .allUsecases }} - "Title": "{{ $object }}", + "Title": "{{ $object }} {{ $verb }}", + "Message": "{{ $object }} {{ textBlock .severity $verb .messageTemplates }}", "Severity": "{{ .severity }}" - {{ $author := random .authors }} "Author": "{{ $author }}", "Tags": "{{ random .authors }}", "Timestamp": "{{ timestamp }}", - {{ $assignee := random .authors }} "AssigneeName": "{{ $assignee }}", "AssigneeAddress": "{{ getValueFromStringMap $assignee .authorAddresses }}", } +messageTemplates: + INFO: + run: "is running" + create: "was created" + ERROR: + run: "did not run successfully" + create: "was not created successfully" + SUCCESS: + run: "did run successfully" + create: "was created successfully" + + usecases: gitlab: - - "PullRequest" - - "PipelineRun" - - "Issue" + - "PullRequest:create" + - "PipelineRun:run" + - "Issue:create" github: - - "PullRequest" - - "WorflowRun" - - "Issue" + - "MergeRequest:create" + - "WorflowRun:run" + - "Issue:create" ansible: - - "PlaybookRun" - - "CollecionBuild" - -events: - PullRequest: - title: "PullRequest {{ .objectTitle }} was created on {{ .system }}" - message: PullRequest {{ .objectTitle }} was created on {{ .system }} by {{ .author }} - + - "PlaybookRun:run" + - "CollecionBuild:run" authors: - "mark:mark@blink182.com" diff --git a/surveys/surveys.go b/surveys/surveys.go index 7f0917a..cbbf551 100644 --- a/surveys/surveys.go +++ b/surveys/surveys.go @@ -122,13 +122,14 @@ func ReadYAML(filename string, out interface{}) error { // HomerunDemo REPRESENTS THE STRUCTURE OF THE YAML FILE type HomerunDemo struct { - Surveys []string `yaml:"surveys"` - Templates []string `yaml:"templates"` - Values []string `yaml:"values"` - Aliases []string `yaml:"aliases"` - BodyTemplate string `yaml:"bodyTemplate"` - Authors []string `yaml:"authors"` - Usecases map[string][]string `yaml:"usecases"` + Surveys []string `yaml:"surveys"` + Templates []string `yaml:"templates"` + Values []string `yaml:"values"` + Aliases []string `yaml:"aliases"` + BodyTemplate string `yaml:"bodyTemplate"` + Authors []string `yaml:"authors"` + Usecases map[string][]string `yaml:"usecases"` + MessageTemplates map[string]map[string]string `yaml:"messageTemplates"` } // BUILD THE SURVEY FUNCTION WITH THE NEW RANDOM SETUP