Skip to content
Snippets Groups Projects
Verified Commit 9575ffb6 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

chore: commit save point

parent f7d38658
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,7 @@ type Configuration struct { ...@@ -31,7 +31,7 @@ type Configuration struct {
Gitlab struct { Gitlab struct {
Token string `yaml:"Token"` Token string `yaml:"Token"`
} `yaml:"GitLab"` } `yaml:"Gitlab"`
Overview struct { Overview struct {
Template struct { Template struct {
......
package main package main
import ( import (
"errors"
"fmt"
"net/url" "net/url"
"path" "path"
"gopkg.in/yaml.v3"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/xanzy/go-gitlab" "github.com/xanzy/go-gitlab"
) )
...@@ -168,6 +172,83 @@ func syncFilesWithGitlab() error { ...@@ -168,6 +172,83 @@ func syncFilesWithGitlab() error {
} }
func nodesEqual(l, r *yaml.Node) bool {
if l.Kind == yaml.ScalarNode && r.Kind == yaml.ScalarNode {
return l.Value == r.Value
}
panic("equals on non-scalars not implemented!")
}
func recursiveRemove(nodes *yaml.Node) error {
if nodes.Kind == yaml.DocumentNode {
recursiveRemove(nodes.Content[0])
}
if nodes.Kind != yaml.MappingNode {
return nil
}
var found int
for i := 0; i < len(nodes.Content); i += 2 {
fmt.Println(nodes.Content[i].Value)
if nodes.Content[i].Value == "Issues" {
found = i
break
}
if err := recursiveRemove(nodes.Content[i+1]); err != nil {
return errors.New("at key " + nodes.Content[i].Value + ": " + err.Error())
}
}
if found > 0 {
copy(nodes.Content[found:], nodes.Content[found+2:]) // Shift a[i+1:] left one index.
nodes.Content[len(nodes.Content)-1] = nil // Erase last element (write zero value).
nodes.Content[len(nodes.Content)-2] = nil // Erase last element (write zero value).
nodes.Content = nodes.Content[:len(nodes.Content)-2]
}
return nil
}
func recursiveMerge(from, into *yaml.Node) error {
if from.Kind != into.Kind {
return errors.New("cannot merge nodes of different kinds")
}
switch from.Kind {
case yaml.MappingNode:
for i := 0; i < len(from.Content); i += 2 {
found := false
for j := 0; j < len(into.Content); j += 2 {
if nodesEqual(from.Content[i], into.Content[j]) {
found = true
if err := recursiveMerge(from.Content[i+1], into.Content[j+1]); err != nil {
return errors.New("at key " + from.Content[i].Value + ": " + err.Error())
}
break
}
}
if !found {
into.Content = append(into.Content, from.Content[i:i+2]...)
}
}
case yaml.SequenceNode:
into.Content = append(into.Content, from.Content...)
case yaml.DocumentNode:
recursiveMerge(from.Content[0], into.Content[0])
default:
return errors.New("can only merge mapping and sequence nodes")
}
return nil
}
func syncIssuesWithGitlab(pageData map[string]*requirement) { func syncIssuesWithGitlab(pageData map[string]*requirement) {
for _, pageData := range pageData { for _, pageData := range pageData {
...@@ -185,6 +266,15 @@ func syncIssuesWithGitlab(pageData map[string]*requirement) { ...@@ -185,6 +266,15 @@ func syncIssuesWithGitlab(pageData map[string]*requirement) {
} }
pageData.Issues[k].GitlabRemote = issue pageData.Issues[k].GitlabRemote = issue
pageData.Issues[k].GitlabIntern.ID = issue.IID pageData.Issues[k].GitlabIntern.ID = issue.IID
var change yaml.Node
bytes, err := yaml.Marshal(&pageData)
if err != nil {
fmt.Println(err)
}
yaml.Unmarshal(bytes, &change)
recursiveRemove(pageData.OriginNode)
recursiveMerge(&change, pageData.OriginNode)
} }
} }
......
...@@ -23,7 +23,7 @@ type GitlabInternalIssueStruct struct { ...@@ -23,7 +23,7 @@ type GitlabInternalIssueStruct struct {
type Issue struct { type Issue struct {
GitlabIntern *GitlabInternalIssueStruct `yaml:"Gitlab"` GitlabIntern *GitlabInternalIssueStruct `yaml:"Gitlab"`
GitlabRemote *gitlab.Issue GitlabRemote *gitlab.Issue `yaml:"-"`
} }
func loadIssuesFromGitlab(context *gitlabContext, issueID int) (*gitlab.Issue, error) { func loadIssuesFromGitlab(context *gitlabContext, issueID int) (*gitlab.Issue, error) {
......
...@@ -17,16 +17,16 @@ import ( ...@@ -17,16 +17,16 @@ import (
) )
type requirement struct { type requirement struct {
ToDos []task ToDos []task `yaml:"-"`
Absolute string Absolute string `yaml:"-"`
File string File string `yaml:"-"`
Items []Item `yaml:"Items"` Items []Item `yaml:"Items"`
Privacy []Privacy `yaml:"Privacy"` Privacy []Privacy `yaml:"Privacy"`
Issues []Issue `yaml:"Issues"` Issues []Issue `yaml:"Issues"`
// remember the node structure of yaml // remember the node structure of yaml
OriginNode *yaml.Node OriginNode *yaml.Node `yaml:"-"`
OriginText string OriginText string `yaml:"-"`
ID string `yaml:"ID"` ID string `yaml:"ID"`
References []string `yaml:"References"` References []string `yaml:"References"`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment