diff --git a/.idea/runConfigurations/reqman_issues_gitlab_sync.xml b/.idea/runConfigurations/reqman_issues_gitlab_sync.xml index 61f5a0eff284d034b923306e6a8c74aa1326641e..9a3c9e92e3fed838a685f461d63258c4220f0f9a 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 "-X main.version=1.0.15 -X main.build=20220616123315"" /> - <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 b7465d515bb1fd6aec6a5cb06783cbf8ef7530b7..c944df9a2ae5ff4247e0af2d79162e1a1011fed1 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 0c7d2b15f85d1752467dc7be504b6f8313cbed5e..3b1bc5489f91ac5dd1e6924f44d0f2d5dfa236ae 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 84ef8bc49710418fff8cd1ed9e7b2b4e31549dd5..b6cc83a1e92d571dc0ef58a25d0d2d4993cf3bf0 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 4b6c55a4fe22786b54cd43c2d05f8b71efda10a7..bef18c31dd09faca39acab6c9f0709fa7637c723 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 bdb6410f7e4f9d0c967a1a9e78f6bc21688963fd..fd82bcdd475b8a3eaddc2ba8553dda0fbe32e6d7 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 afd3e06a8ed52bdc892797047e2f785be7eec723..9d8ad9c4b4fcf80a3f6163362667463fa410b1a8 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 da8ad3f875f64e5d0581ded4777d36048ad14c02..355fa63aed6ac5c0749bba2df5c82d320ff43e48 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 1d1a262e0a410aca17d47cf318e693d7fd76c533..2515db4148884dfe4fcb66de08a077bfa80c24d4 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 801e45b5719c53765a6fbf5b4879bb8c35bc0fc9..137a6428c3c27125fc5297fad0e5600fd730ca9f 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 381ac023ef76238d62026cd38d576bc4bf314dcb..2063525eda1b089016561c68d5e13413bfdb308e 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 b2b46a5a534894ed4e05effd9e71afa61c3b05af..d4a323f70469383e8e421c8c193d875ed42b33bd 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 3db66345360a897382acc2540385eb2740615c63..45616e186c5380cd704f6fbdbead75bdaddb31c6 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 7414611b1cf62fdc563e3f57618a94d811becc1d..e4de0d202c624302f9b84e661ba25cb60534db68 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 8b55d6c780604e95ef95e3d3397982deea28bd8e..14a2048c4232ad02136b9708fb6694917dc84364 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