grim/youtrack-import

Parents 608238fe4050
Children c649ef444625
Replace all \r\n with just \n and clean up the regexs in the creole code
--- a/bitbucket/converter.go Sun Jan 19 14:03:22 2020 -0600
+++ b/bitbucket/converter.go Sun Jan 19 14:31:08 2020 -0600
@@ -2,6 +2,7 @@
import (
"fmt"
+ "regexp"
"strings"
"hg.sr.ht/~grim/youtrack-import/youtrack"
@@ -33,8 +34,22 @@
"critical": "Critical",
"blocker": "Show-stopper",
}
+
+ newlineRegex = regexp.MustCompile(`\r\n`)
)
+func cleanupMessage(m map[string]*youtrack.User, nIssues int, projectID, repository, message string) string {
+ output := message
+
+ // replace \r\n with \n
+ output = newlineRegex.ReplaceAllString(output, "\n")
+
+ output = replaceKeywords(m, nIssues, projectID, repository, output)
+ output = replaceCreole(m, output)
+
+ return output
+}
+
func (a *Archive) convertIssue(nIssues int, projectID, repository string, bb Issue, userMap map[string]*youtrack.User) *youtrack.Issue {
reporter := "admin"
if user, found := userMap[bb.Reporter.AccountID]; found {
@@ -49,7 +64,7 @@
yt := &youtrack.Issue{
Number: bb.ID,
Summary: bb.Title,
- Description: replaceKeywords(userMap, nIssues, projectID, repository, bb.Content),
+ Description: cleanupMessage(userMap, nIssues, projectID, repository, bb.Content),
Created: bb.CreatedOn,
Updated: bb.UpdatedOn,
Reporter: reporter,
@@ -134,7 +149,7 @@
ytComment := &youtrack.Comment{
Author: author,
- Text: replaceKeywords(userMap, nIssues, projectID, repository, comment.Content),
+ Text: cleanupMessage(userMap, nIssues, projectID, repository, comment.Content),
Created: comment.CreatedOn,
Updated: comment.UpdatedOn,
Markdown: true,
--- a/bitbucket/creole.go Sun Jan 19 14:03:22 2020 -0600
+++ b/bitbucket/creole.go Sun Jan 19 14:31:08 2020 -0600
@@ -1,54 +1,22 @@
package bitbucket
import (
- "fmt"
"regexp"
- "strings"
"hg.sr.ht/~grim/youtrack-import/youtrack"
)
var (
creoleLinkRegex = regexp.MustCompile(`\[\[(.+?)\|(.+?)\]\]`)
- creoleCodeRegex = regexp.MustCompile("```" + `\r\n#!((.*?)\r\n)`)
+ creoleCodeRegex = regexp.MustCompile("```" + `\n#!((.*?)\n)`)
)
func replaceCreoleLinks(message string) string {
- output := message
-
- matches := creoleLinkRegex.FindAllStringSubmatch(message, -1)
- for _, match := range matches {
- old := match[0]
- uri := match[1]
- text := match[2]
-
- replacement := fmt.Sprintf("[%s](%s)", text, uri)
- output = strings.Replace(output, old, replacement, 1)
- }
-
- return output
+ return creoleLinkRegex.ReplaceAllString(message, "[$2]($1)")
}
func replaceCreoleCode(message string) string {
- output := message
- matched := false
-
- matches := creoleCodeRegex.FindAllStringSubmatch(message, -1)
- for _, match := range matches {
- old := match[0]
- lang := match[2]
-
- matched = true
-
- replacement := fmt.Sprintf("```%s\n", lang)
- output = strings.Replace(output, old, replacement, 1)
- }
-
- if matched {
- fmt.Printf("output: %q\n", output)
- }
-
- return output
+ return creoleCodeRegex.ReplaceAllString(message, "```$2\n")
}
func replaceCreole(m map[string]*youtrack.User, message string) string {
--- a/bitbucket/keywords.go Sun Jan 19 14:03:22 2020 -0600
+++ b/bitbucket/keywords.go Sun Jan 19 14:31:08 2020 -0600
@@ -107,7 +107,5 @@
output = replaceChangeSets(repository, output)
output = replaceCommitHashes(repository, output)
- output = replaceCreole(m, output)
-
return output
}