From cee6aa80da2dea0b457250778df103acd5e48e86 Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Tue, 21 Jun 2022 12:20:22 +0200
Subject: [PATCH] chore: commit save point

---
 .../reqman_issues_gitlab_sync.xml             |  2 +-
 application/source/changelog.go               |  6 +-
 application/source/commandline.go             | 29 +++---
 application/source/config.go                  |  2 +-
 application/source/errors.go                  |  2 +
 application/source/files.go                   | 35 +++++++
 application/source/gitlab.go                  | 97 ++++++++++++++++++-
 application/source/issues.go                  | 58 +----------
 application/source/items.go                   |  6 +-
 application/source/overview.go                | 12 +--
 application/source/privacy.go                 |  6 +-
 application/source/requirements.go            | 16 +--
 application/source/tasks.go                   |  6 +-
 application/source/util.go                    |  2 +-
 development/examples/example1/req1/1/test1.md |  2 +
 15 files changed, 182 insertions(+), 99 deletions(-)

diff --git a/.idea/runConfigurations/reqman_issues_gitlab_sync.xml b/.idea/runConfigurations/reqman_issues_gitlab_sync.xml
index 61f5a0e..9a3c9e9 100644
--- a/.idea/runConfigurations/reqman_issues_gitlab_sync.xml
+++ b/.idea/runConfigurations/reqman_issues_gitlab_sync.xml
@@ -3,7 +3,7 @@
     <module name="requirements-manager" />
     <working_directory value="$PROJECT_DIR$/application/source" />
     <go_parameters value="-ldflags &quot;-X main.version=1.0.15 -X main.build=20220616123315&quot;" />
-    <parameters value="gitlab sync -p $PROJECT_DIR$/development/examples/example1" />
+    <parameters value="gitlab sync -p $PROJECT_DIR$/development/examples/example1/req1/1/test1.md" />
     <kind value="DIRECTORY" />
     <package value="gitlab.schukai.com/oss/utilities/requirements-manager" />
     <directory value="$PROJECT_DIR$/application/source" />
diff --git a/application/source/changelog.go b/application/source/changelog.go
index b7465d5..c944df9 100644
--- a/application/source/changelog.go
+++ b/application/source/changelog.go
@@ -8,7 +8,7 @@ import (
 
 type logs []string
 
-func buildChangelog(config *Configuration, pageMap map[string]*requirement) (error, string, bool) {
+func buildChangelog(pageMap map[string]*requirement) (error, string, bool) {
 
 	changelog := make(map[string]logs)
 	datemap := make(map[string]string)
@@ -56,14 +56,14 @@ func buildChangelog(config *Configuration, pageMap map[string]*requirement) (err
 
 }
 
-func printChangelog(config *Configuration) error {
+func printChangelog() error {
 
 	err, pageData := collectStructureFromFiles(config.Path)
 	if err != nil {
 		return err
 	}
 
-	err, table, _ := buildChangelog(config, pageData)
+	err, table, _ := buildChangelog(pageData)
 	fmt.Println(table)
 	return err
 }
diff --git a/application/source/commandline.go b/application/source/commandline.go
index 0c7d2b1..3b1bc54 100644
--- a/application/source/commandline.go
+++ b/application/source/commandline.go
@@ -9,16 +9,21 @@ import (
 	"github.com/jessevdk/go-flags"
 )
 
+var (
+	config    *Configuration
+	arguments *commandLineOptions
+)
+
 func executeCommand() {
 
-	arguments := new(commandLineOptions)
+	arguments = new(commandLineOptions)
 	p := flags.NewParser(arguments, flags.Default)
 	_, err := p.Parse()
 	if err != nil {
 		os.Exit(-1)
 	}
 
-	config := NewConfiguration(arguments.Config)
+	config = NewConfiguration(arguments.Config)
 
 	if arguments.Path != "" {
 		config.Path = arguments.Path
@@ -42,7 +47,7 @@ func executeCommand() {
 		subcommand := activeCommand.Active
 		switch subcommand.Name {
 		case "print":
-			err := printChangelog(config)
+			err := printChangelog()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
@@ -51,7 +56,7 @@ func executeCommand() {
 		subcommand := activeCommand.Active
 		switch subcommand.Name {
 		case "print":
-			err := printPrivacy(config)
+			err := printPrivacy()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
@@ -77,7 +82,7 @@ func executeCommand() {
 				config.Overview.Template.Latex = arguments.Overview.Print.LatexTemplatePath
 			}
 
-			err := PrintOverview(config, arguments)
+			err := PrintOverview()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
@@ -87,13 +92,13 @@ func executeCommand() {
 		subcommand := activeCommand.Active
 		switch subcommand.Name {
 		case "report":
-			err := printRequirementReport(config, arguments)
+			err := printRequirementReport()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
 		case "add":
 			config.Requirement.Template.Add = defaultNewRequirementTemplate
-			err := addRequirement(config, arguments)
+			err := addRequirement()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
@@ -101,7 +106,7 @@ func executeCommand() {
 			if len(arguments.Requirements.Print.Columns) > 0 {
 				config.Requirement.Table.Columns = validateColumns(arguments.Requirements.Print.Columns)
 			}
-			err := printRequirements(config)
+			err := printRequirements()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
@@ -111,7 +116,7 @@ func executeCommand() {
 		subcommand := activeCommand.Active
 		switch subcommand.Name {
 		case "print":
-			err := printTaskTable(config)
+			err := printTaskTable()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
@@ -126,7 +131,7 @@ func executeCommand() {
 				config.Gitlab.Token = arguments.Issues.Print.GitlabToken
 			}
 
-			err := printIssueTable(config, arguments)
+			err := printIssueTable()
 			if err != nil {
 				printErrorAndExit(2, err.Error())
 			}
@@ -135,7 +140,7 @@ func executeCommand() {
 			subcommand := activeCommand.Active
 			switch subcommand.Name {
 			case "print":
-				err := printItemTable(config)
+				err := printItemTable()
 				if err != nil {
 					printErrorAndExit(2, err.Error())
 				}
@@ -152,7 +157,7 @@ func executeCommand() {
 				config.Gitlab.Token = arguments.Gitlab.GitlabToken
 			}
 
-			syncTasksWithGitlab(config)
+			syncFilesWithGitlab()
 		}
 	}
 
diff --git a/application/source/config.go b/application/source/config.go
index 84ef8bc..b6cc83a 100644
--- a/application/source/config.go
+++ b/application/source/config.go
@@ -31,7 +31,7 @@ type Configuration struct {
 
 	Gitlab struct {
 		Token string `yaml:"Token"`
-	} `yaml:"Gitlab"`
+	} `yaml:"GitLab"`
 
 	Overview struct {
 		Template struct {
diff --git a/application/source/errors.go b/application/source/errors.go
index 4b6c55a..bef18c3 100644
--- a/application/source/errors.go
+++ b/application/source/errors.go
@@ -4,4 +4,6 @@ import "errors"
 
 var (
 	noGitRepositoryError = errors.New("no git repository found")
+	notFoundError        = errors.New("not found")
+	notPermittedError    = errors.New("not permitted")
 )
diff --git a/application/source/files.go b/application/source/files.go
index bdb6410..fd82bcd 100644
--- a/application/source/files.go
+++ b/application/source/files.go
@@ -1,10 +1,13 @@
 package main
 
 import (
+	"bytes"
 	"io/ioutil"
 	"os"
 	"path"
 	"path/filepath"
+
+	"gopkg.in/yaml.v3"
 )
 
 //
@@ -63,3 +66,35 @@ func collectStructureFromFiles(directory string) (error, map[string]*requirement
 
 	return err, pageMap
 }
+
+func rewriteFiles(pageData map[string]*requirement) error {
+
+	for filename, pd := range pageData {
+
+		buf := new(bytes.Buffer)
+
+		var encoder = yaml.NewEncoder(buf)
+		err := encoder.Encode(pd.OriginNode)
+		if err != nil {
+			return err
+		}
+		encoder.Close()
+
+		text := "---\n" + buf.String() + "\n...\n"
+		text += pd.OriginText
+
+		info, err := os.Stat(filename)
+		if err != nil {
+			return err
+		}
+		mode := info.Mode()
+
+		err = os.WriteFile(filename, []byte(text), mode)
+		if err != nil {
+			return err
+		}
+
+	}
+
+	return nil
+}
diff --git a/application/source/gitlab.go b/application/source/gitlab.go
index afd3e06..9d8ad9c 100644
--- a/application/source/gitlab.go
+++ b/application/source/gitlab.go
@@ -22,10 +22,10 @@ type gitlabContext struct {
 	project *gitlab.Project
 }
 
-func getGitlabContext(config *Configuration) *gitlabContext {
+func getGitlabContext() *gitlabContext {
 
 	context := &gitlabContext{}
-	workingPath, err := getGitDirectory(config)
+	workingPath, err := getGitDirectory()
 	if err != nil {
 		printErrorAndExit(2, "Failed to get git directory", err.Error())
 	}
@@ -116,9 +116,9 @@ func searchProject(context *gitlabContext) {
 
 }
 
-func enrichIssuesWithGitlab(pageMap map[string]*requirement, config *Configuration) error {
+func enrichIssuesWithGitlab(pageMap map[string]*requirement) error {
 
-	context := getGitlabContext(config)
+	context := getGitlabContext()
 	searchProject(context)
 
 	for _, requirement := range pageMap {
@@ -145,6 +145,95 @@ func enrichIssuesWithGitlab(pageMap map[string]*requirement, config *Configurati
 	return nil
 }
 
+func syncFilesWithGitlab() error {
+
+	err, pageData := collectStructureFromFiles(config.Path)
+	if err != nil {
+		return err
+	}
+
+	issuesList := []Issue{}
+
+	for _, pageData := range pageData {
+		for _, info := range pageData.Issues {
+			issuesList = append(issuesList, info)
+		}
+	}
+
+	context := getGitlabContext()
+	searchProject(context)
+
+	syncIssuesWithGitlab(pageData)
+	return rewriteFiles(pageData)
+
+}
+
+func syncIssuesWithGitlab(pageData map[string]*requirement) {
+
+	for _, pageData := range pageData {
+		for _, info := range pageData.Issues {
+			if info.GitlabRemote != nil {
+				continue
+			}
+			if info.GitlabIntern == nil {
+				continue
+			}
+			if info.GitlabIntern.ID == 0 {
+				issue, err := createIssue(info)
+				if err != nil {
+					printErrorAndExit(2, "Failed to create issue %s", err.Error())
+				}
+				info.GitlabRemote = issue
+			}
+
+		}
+	}
+}
+
+func createIssue(issue Issue) (*gitlab.Issue, error) {
+	context := getGitlabContext()
+	searchProject(context)
+
+	//title := *string(issue.GitlabIntern.Title)
+
+	createionOptions := gitlab.CreateIssueOptions{
+		Title: gitlab.String(issue.GitlabIntern.Title),
+	}
+
+	ptr := &createionOptions
+
+	gitlabIssue, respose, err := context.client.Issues.CreateIssue(context.project.ID, ptr)
+
+	if respose.StatusCode == 401 {
+		return gitlabIssue, err
+	}
+
+	if respose.StatusCode > 299 {
+		return gitlabIssue, err
+	}
+
+	return gitlabIssue, err
+}
+
+//
+//	context := getGitlabContext(config)
+//	searchProject(context)
+//
+//	options := &gitlab.CreateIssueOptions{
+//		Title:  gitlab.String(issue.Title),
+//		Status: gitlab.String(issue.Status),
+//		Labels: gitlab.StringSlice(issue.Labels),
+//	}
+//
+//	i, _, err := context.client.Issues.CreateIssue(context.project.ID, options)
+//	if err != nil {
+//		return nil, err
+//	}
+//
+//	return i, nil
+//
+//}
+//
 //
 //func getProject(config *Configuration) *gitlab.Project {
 //
diff --git a/application/source/issues.go b/application/source/issues.go
index da8ad3f..355fa63 100644
--- a/application/source/issues.go
+++ b/application/source/issues.go
@@ -3,12 +3,9 @@ package main
 import (
 	"bytes"
 	"fmt"
-	"os"
 	"strings"
 	"time"
 
-	"gopkg.in/yaml.v3"
-
 	"github.com/olekukonko/tablewriter"
 	"github.com/xanzy/go-gitlab"
 )
@@ -43,7 +40,7 @@ func loadIssuesFromGitlab(context *gitlabContext, issueID int) (*gitlab.Issue, e
 	//err, pageData := collectStructureFromFiles(config.Path)
 }
 
-func buildIssueOverviewTable(config *Configuration, pageMap map[string]*requirement, extended bool) (error, string, bool) {
+func buildIssueOverviewTable(pageMap map[string]*requirement, extended bool) (error, string, bool) {
 
 	buf := new(bytes.Buffer)
 	has := false
@@ -141,65 +138,18 @@ func buildIssueOverviewTable(config *Configuration, pageMap map[string]*requirem
 	return nil, buf.String(), has
 }
 
-func printIssueTable(config *Configuration, arguments *commandLineOptions) error {
+func printIssueTable() error {
 	err, pageMap := collectStructureFromFiles(config.Path)
 
 	if arguments.Issues.Print.Remote {
-		enrichIssuesWithGitlab(pageMap, config)
+		enrichIssuesWithGitlab(pageMap)
 		if err != nil {
 			return err
 		}
 	}
 
-	err, table, _ := buildIssueOverviewTable(config, pageMap, true)
+	err, table, _ := buildIssueOverviewTable(pageMap, true)
 	fmt.Println(table)
 
 	return nil
 }
-
-func syncTasksWithGitlab(config *Configuration) error {
-
-	err, pageData := collectStructureFromFiles(config.Path)
-	if err != nil {
-		return err
-	}
-
-	issuesList := []Issue{}
-
-	for _, pageData := range pageData {
-		for _, info := range pageData.Issues {
-			issuesList = append(issuesList, info)
-		}
-	}
-
-	context := getGitlabContext(config)
-	searchProject(context)
-
-	for filename, pd := range pageData {
-
-		buf := new(bytes.Buffer)
-		var encoder = yaml.NewEncoder(buf)
-		err := encoder.Encode(pd.OriginNode)
-		if err != nil {
-			return err
-		}
-		encoder.Close()
-
-		text := "---\n" + buf.String() + "\n...\n"
-		text += pd.OriginText
-
-		info, err := os.Stat(filename)
-		if err != nil {
-			return err
-		}
-		mode := info.Mode()
-
-		err = os.WriteFile(filename, []byte(text), mode)
-		if err != nil {
-			return err
-		}
-
-	}
-
-	return nil
-}
diff --git a/application/source/items.go b/application/source/items.go
index 1d1a262..2515db4 100644
--- a/application/source/items.go
+++ b/application/source/items.go
@@ -19,7 +19,7 @@ type Item struct {
 	ProvidedBy    string    `yaml:"Provided by"`
 }
 
-func buildItemOverviewTable(config *Configuration, pageMap map[string]*requirement, extended bool) (error, string, bool) {
+func buildItemOverviewTable(pageMap map[string]*requirement, extended bool) (error, string, bool) {
 
 	buf := new(bytes.Buffer)
 	has := false
@@ -114,13 +114,13 @@ func buildItemOverviewTable(config *Configuration, pageMap map[string]*requireme
 	return nil, buf.String(), has
 }
 
-func printItemTable(config *Configuration) error {
+func printItemTable() error {
 	err, pageMap := collectStructureFromFiles(config.Path)
 	if err != nil {
 		return err
 	}
 
-	err, table, _ := buildItemOverviewTable(config, pageMap, true)
+	err, table, _ := buildItemOverviewTable(pageMap, true)
 	fmt.Println(table)
 
 	return nil
diff --git a/application/source/overview.go b/application/source/overview.go
index 801e45b..137a642 100644
--- a/application/source/overview.go
+++ b/application/source/overview.go
@@ -30,34 +30,34 @@ type Dataset struct {
 	CreatedFormat   string
 }
 
-func PrintOverview(config *Configuration, arguments *commandLineOptions) error {
+func PrintOverview() error {
 
 	err, pageData := collectStructureFromFiles(config.Path)
 	if err != nil {
 		return err
 	}
 
-	err, meta, hasMeta := buildRequirements(config, pageData)
+	err, meta, hasMeta := buildRequirements(pageData)
 	if err != nil {
 		return err
 	}
 
-	err, changelog, hasChangelog := buildChangelog(config, pageData)
+	err, changelog, hasChangelog := buildChangelog(pageData)
 	if err != nil {
 		return err
 	}
 
-	err, privacy, hasPrivacy := buildPrivacy(config, pageData)
+	err, privacy, hasPrivacy := buildPrivacy(pageData)
 	if err != nil {
 		return err
 	}
 
-	err, tasks, hasTasks := buildTasksTable(config, pageData, false)
+	err, tasks, hasTasks := buildTasksTable(pageData, false)
 	if err != nil {
 		return err
 	}
 
-	err, items, hasItems := buildItemOverviewTable(config, pageData, false)
+	err, items, hasItems := buildItemOverviewTable(pageData, false)
 	if err != nil {
 		return err
 	}
diff --git a/application/source/privacy.go b/application/source/privacy.go
index 381ac02..2063525 100644
--- a/application/source/privacy.go
+++ b/application/source/privacy.go
@@ -19,7 +19,7 @@ type Privacy struct {
 	TOM                 string `yaml:"TOM"`
 }
 
-func buildPrivacy(config *Configuration, pageMap map[string]*requirement) (error, string, bool) {
+func buildPrivacy(pageMap map[string]*requirement) (error, string, bool) {
 
 	hasPrivacy := false
 	text := ""
@@ -82,14 +82,14 @@ func buildPrivacy(config *Configuration, pageMap map[string]*requirement) (error
 
 }
 
-func printPrivacy(config *Configuration) error {
+func printPrivacy() error {
 
 	err, pageData := collectStructureFromFiles(config.Path)
 	if err != nil {
 		return err
 	}
 
-	err, data, _ := buildPrivacy(config, pageData)
+	err, data, _ := buildPrivacy(pageData)
 	fmt.Println(data)
 	return err
 }
diff --git a/application/source/requirements.go b/application/source/requirements.go
index b2b46a5..d4a323f 100644
--- a/application/source/requirements.go
+++ b/application/source/requirements.go
@@ -48,7 +48,7 @@ type requirement struct {
 	LastUpdate time.Time     `yaml:"Last Update"`
 }
 
-func addRequirement(config *Configuration, arguments *commandLineOptions) error {
+func addRequirement() error {
 
 	if arguments.Requirements.Add.ID == "" {
 		printErrorAndExit(2, "the id must not be empty")
@@ -72,7 +72,7 @@ func addRequirement(config *Configuration, arguments *commandLineOptions) error
 	return nil
 }
 
-func buildRequirements(config *Configuration, pageMap map[string]*requirement) (error, string, bool) {
+func buildRequirements(pageMap map[string]*requirement) (error, string, bool) {
 
 	hasRequirements := false
 
@@ -157,13 +157,13 @@ func buildRequirements(config *Configuration, pageMap map[string]*requirement) (
 
 }
 
-func printRequirements(config *Configuration) error {
+func printRequirements() error {
 	err, pageData := collectStructureFromFiles(config.Path)
 	if err != nil {
 		return err
 	}
 
-	err, table, _ := buildRequirements(config, pageData)
+	err, table, _ := buildRequirements(pageData)
 	fmt.Println(table)
 	return err
 }
@@ -185,10 +185,10 @@ func integrateGitlabTimeSpent(pageMap map[string]*requirement) {
 
 }
 
-func buildTimeReport(config *Configuration, arguments *commandLineOptions, pageMap map[string]*requirement) (error, string, string, bool) {
+func buildTimeReport(pageMap map[string]*requirement) (error, string, string, bool) {
 
 	if arguments.Requirements.Report.Remote {
-		err := enrichIssuesWithGitlab(pageMap, config)
+		err := enrichIssuesWithGitlab(pageMap)
 		if err != nil {
 			return err, "", "", false
 		}
@@ -408,14 +408,14 @@ func buildTable2(pageMap map[string]*requirement) string {
 	return buf.String()
 }
 
-func printRequirementReport(config *Configuration, arguments *commandLineOptions) error {
+func printRequirementReport() error {
 
 	err, pageData := collectStructureFromFiles(config.Path)
 	if err != nil {
 		return err
 	}
 
-	err, table1, table2, _ := buildTimeReport(config, arguments, pageData)
+	err, table1, table2, _ := buildTimeReport(pageData)
 	if err != nil {
 		return err
 	}
diff --git a/application/source/tasks.go b/application/source/tasks.go
index 3db6634..45616e1 100644
--- a/application/source/tasks.go
+++ b/application/source/tasks.go
@@ -72,7 +72,7 @@ func findTask(content []byte) []task {
 	return toDos
 }
 
-func buildTasksTable(config *Configuration, pageMap map[string]*requirement, extended bool) (error, string, bool) {
+func buildTasksTable(pageMap map[string]*requirement, extended bool) (error, string, bool) {
 
 	buf := new(bytes.Buffer)
 
@@ -130,13 +130,13 @@ func buildTasksTable(config *Configuration, pageMap map[string]*requirement, ext
 	return nil, buf.String(), has
 }
 
-func printTaskTable(config *Configuration) error {
+func printTaskTable() error {
 	err, pageData := collectStructureFromFiles(config.Path)
 	if err != nil {
 		return err
 	}
 
-	err, table, _ := buildTasksTable(config, pageData, true)
+	err, table, _ := buildTasksTable(pageData, true)
 	fmt.Println(table)
 
 	return nil
diff --git a/application/source/util.go b/application/source/util.go
index 7414611..e4de0d2 100644
--- a/application/source/util.go
+++ b/application/source/util.go
@@ -137,7 +137,7 @@ func translateHeaders(columns []string) []string {
 
 }
 
-func getGitDirectory(config *Configuration) (string, error) {
+func getGitDirectory() (string, error) {
 
 	path := config.Path
 
diff --git a/development/examples/example1/req1/1/test1.md b/development/examples/example1/req1/1/test1.md
index 8b55d6c..14a2048 100644
--- a/development/examples/example1/req1/1/test1.md
+++ b/development/examples/example1/req1/1/test1.md
@@ -107,6 +107,8 @@ Privacy:
 
 
 
+
+
 ### {{ .Title }} - {{ .ID }}
 
 #### Subheading
-- 
GitLab